The organizing committee for the HCC Celebrates Columbia\'s 50th Birthday, Inc.
ID: 3710657 • Letter: T
Question
The organizing committee for the HCC Celebrates Columbia's 50th Birthday, Inc. would like a report of the attendance for each of the seven days of the celebration. You have been asked to write a program that produces a report of the attendance for each day, the total attendance for the entire celebration, the average attendance for the entire celebration, the largest attendance and the day on which it occurred, and the least attendance along with the day on which it occurred.
Requirements:
The program will do the following:
1.Prompt the user to enter the attendance for each day.
2.Store each day's attendance in an array.
3.Print the attendance for each day.
4.Calculate the average attendance.
5.Print the average attendance. Show one decimal place.
6.Find the day with the largest attendance.
7.Print the largest attendance and the day on which it occurred.
8.Find the day with the least attendance.
9.Print the least attendance and the day on which it occurred.
Example Program Execution
In addition to a main method, you program wil have the following additional methods:
calculateTotal: Input is the attandance array. Returns the total attendance.
calculateAverage: Input is the attandance array. Returns the average attendance.
findLargest: Input is the attandance array. Returns the largest attendance.
findLeast: Input is the attandance array. Returns the least attendance.
printAttendance: Input is the attandance array. Prints the attendance for each day.
You may put all of the methods in a single class file.
Explanation / Answer
The required code is provided below, defined all the required methods exactly as needed. Go through it and let me know if you have any queries. Thanks.
// Attendance.java
import java.util.Scanner;
public class Attendance {
public static void main(String[] args) {
/**
* initializing Scanner to read input
*/
Scanner scanner = new Scanner(System.in);
int days = 7;
/**
* defining the array
*/
int attendance[] = new int[days];
/**
* prompting and getting input attendance values
*/
System.out.println("Please enter the attendance for:");
for (int i = 0; i < attendance.length; i++) {
System.out.printf("Day %d: ", (i + 1));
attendance[i] = scanner.nextInt();
}
System.out.println("Columbia 50th Birthday Celebration");
System.out.println("Attendance Report");
/**
* Displaying the stats
*/
printAttendance(attendance);
System.out.println(" Total Attendance: "+calculateTotal(attendance));
System.out.printf("Average Attendance: %.1f ",calculateAverage(attendance));
int greatest=findLargest(attendance);
System.out.print("Greatest Attendance: "+greatest+" on Day ");
/**
* finding the day(s) with highest attendance
*/
for(int i=0;i<attendance.length;i++){
if(attendance[i]==greatest){
System.out.print((i+1)+" ");
}
}
System.out.println();
int least=findLeast(attendance);
/**
* finding the day(s) with least attendance
*/
System.out.print("Least Attendance: "+least+" on Day ");
for(int i=0;i<attendance.length;i++){
if(attendance[i]==least){
System.out.print((i+1)+" ");
}
}
System.out.println();
}
/**
* method to calculate the total attendance
* @param attendance - input attendance array
* @return - sum total of all attendance values
*/
static int calculateTotal(int[] attendance) {
int total = 0;
for (int i = 0; i < attendance.length; i++) {
total += attendance[i];
}
return total;
}
/**
* method to calculate the average attendance
* @param attendance - input attendance array
* @return - average of all attendance values
*/
static double calculateAverage(int[] attendance) {
//finding total first
int total = calculateTotal(attendance);
double average = 0;
if (attendance.length > 0) {
//finding average
average = (double) total / attendance.length;
}
return average;
}
/**
* method to find the largest attendance
* @param attendance - input attendance array
* @return - the largest attendance value
*/
static int findLargest(int[] attendance) {
int largest = 0;
for (int i = 0; i < attendance.length; i++) {
if (i == 0) {
largest = attendance[i];
} else if (attendance[i] > largest) {
largest = attendance[i];
}
}
return largest;
}
/**
* method to find the least attendance
* @param attendance - input attendance array
* @return - the least attendance value
*/
static int findLeast(int[] attendance) {
int least = 0;
for (int i = 0; i < attendance.length; i++) {
if (i == 0) {
least = attendance[i];
} else if (attendance[i] < least) {
least = attendance[i];
}
}
return least;
}
/**
* method to print the days and corresponding attendance values
* @param attendance - input attendance array
*/
static void printAttendance(int[] attendance) {
for (int i = 0; i < attendance.length; i++) {
System.out.printf("Day %d: %d ", (i + 1), attendance[i]);
}
}
}
/*OUTPUT*/
Please enter the attendance for:
Day 1: 1507
Day 2: 1999
Day 3: 958
Day 4: 2001
Day 5: 1506
Day 6: 1800
Day 7: 1847
Columbia 50th Birthday Celebration
Attendance Report
Day 1: 1507
Day 2: 1999
Day 3: 958
Day 4: 2001
Day 5: 1506
Day 6: 1800
Day 7: 1847
Total Attendance: 11618
Average Attendance: 1659.7
Greatest Attendance: 2001 on Day 4
Least Attendance: 958 on Day 3