COP 2006 Assignment #4s Task Include comments and follow Code conventions on Can
ID: 3776299 • Letter: C
Question
COP 2006 Assignment #4s
Task
Include comments and follow Code conventions on Canvas; this counts up to 10% of the homework grade;
Assume you work in a teaching center, which operates Mon-Wed. The center keeps track of how many students come to the center each day, and which teachers they see.
Write a Java program that uses the 2D array teacherData below (the array is hardcoded in your program):
int[][] teacherData = { // students per day (Mon Tue Wed) per teacher
{ 25, 3, 0 }, // Teacher1, Amy
{ 14, 5, 12 }, // Teacher2, John
{ 33, 22, 10 }, // Teacher3, Nick
{ 0, 20, 5 } }; // Teacher4, Maria
and produces the exact output that follows, if run in the command line: F
c:COP2006>java Hw4
Teacher1 met 25 (M) 3 (T) 0 (W) students.
Teacher2 met 14 (M) 5 (T) 12 (W) students.
Teacher3 met 33 (M) 22 (T) 10 (W) students.
Teacher4 met 0 (M) 20 (T) 5 (W) students.
Total number of students using the center: 149
M: 72, T: 50, W: 27
Teacher1: 28, Teacher2: 31, Teacher3: 65, Teacher4: 25.
Max students per day: 72.
Max Students per teacher: 65.
For more details, please see next page:
The program shall declare and instantiate two-dimensional int array teacherData[][] as given in page 1.
It shall also use variables numOfTeachers for the total number of rows, and numOfDays for the number of columns -which means, numOfTeachers is 4 and numOfDays is 3.
Instead of using 4 or 3, you should initialize these variables using the appropriate .length.
The program shall use the following int arrays to store results for sum computations:
sumPerDay[numOfDays], and
sumPerTeacher[numOfTeachers].
Example: sumPerTeacher contains (after sum is calculated based on teacherData): 28, 31, 65, 25 .
The program shall use the following methods:
printATeacherTotals() to print the values in the table for a specific teacher – see the output shown in page 1.
The method takes a 2-D int array as parameter (the teacherData) and an int x, and returns nothing (void).
This method prints the output for one teacher. You must use a for loop to call printATeacherData ()
The parameter x is the teacher number (index). For example, if you call the method with 0, it prints the output below for the first teacher:
Teacher1 met 25 (M) 3 (T) 0 (W) students
max1dArray() to calculate the maximum value of a 1-D array; program will call this e.g. with sumPerDay[]. This method shall take an int[] array as parameter, and return an int as the result.
Note: the displayed max is not of the teacher data, but the max of a sum per row (teacher) or per column (day).
The program shall print the output exactly as shown in page 1.
Extra Credit (+3%):
The program shall use a String array teacherNames[] to store the name of each teacher (see comments for teacherData in page 1) and use the array for all related output instead of Teacher1, Teacher2 etc. Your program must compile and run correctly to get any extra credit.
The output below gets the extra credit – it has names for each teacher:
Amy met 25 (M) 3 (T) 0 (W) students.
John met 14 (M) 5 (T) 12 (W) students.
Nick met 33 (M) 22 (T) 10 (W) students.
Maria met 0 (M) 20 (T) 5 (W) students.
Total number of students using the center: 149
M: 72, T: 50, W: 27
Amy: 28, John: 31, Nick: 65, Maria: 25.
Max students per day: 72.
Max Students per teacher: 65.
COP 2006 Assignment #4 Task Include comments and follow Code conventions on Canvas; this counts up to 10% of the homework grade; Assume you work in a tutoring center, which operates Mon-Wed. The center keeps track of how many students come to the center each day, and which tutors they see program that the 2D array tutorData b the array is hardcoded in your program) int40 tutorData ll students per day Mon Tue Wed per tutor 14, 5, 12 Y, Tutor2, John 133, 22, 10 Tutor3, Nick 20, 5 Tutor4, Maria. and prod tput that foll d lin c: COP 2006 java Hw4 Tutorl met 25 3 T) 0 (W) students 4 5 T) 12 W) stud Total number of students using the center: 149 M: 72, T 50, W: 27 28, T 2: 31. Th 3: 65, Th 4: 25 Max students per day: 72 Max Students per tutor: 65 1) The program shall declare and instantiate two-dimensional int array tutorDataDQ as given in page 1 shall al Of Th for the total of Days for th columns hich means, numOfTutors is 4 and numOfDays is 3 Instead of using 4 or 3, you should initialize these variables using the appropriate length 2) The program shall use the following int arrays to store results for sum computations umPerDayl OfDay and Of T Example: sumPerTutor contains (after sum is calculated b on tutorData: 28 31 3) The program shall use the following methods: Totals0 print the val the table f pecific tut tput sh AT pag The method takes a 2-D int array as parameter (the tutorData) and an int x, and returns nothing (void) This method prints the output for one tutor. You must use a for loop to call Data 0 or number (index). For example, if thod with 0 The p the tur call th prints th output below for the first tutor Tutor 1 met 25 3 T) 0 students maxldArray0 to calculate the maximum value of a l-D array, program will call this e.g. with sumPerDaylj This method shall take an intJ and return the resul he displayed of the tu data, b (tutor) or p (day) 4) The program shall print the output exactly as shown in page 1 Extra Cred +3% 5) The program shall use a String array tutorNames to store the name of each tutor (see comments for tutorData in page 1 and use the array for all related output instead of Tutorl, Tutor2 etc. Your program must compile and run correctly to get any extra credit. b gets the extr h tuto e output 25 3 (T 0 (W) students John 4 M) 5 (T) 12 W) stud et 0 MO 20 5 W) students Total number of students using the center: 149 M: 72, T: 50, W: 27 Amy: 28, John: 31, Nick: 65, Maria: 25 Max students per day: 72. Max Students per tutor: 65Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
class TeachingCenter
{
public static void main (String[] args) throws java.lang.Exception
{
int[][] teacherData = { // students per day (Mon Tue Wed) per teacher
{ 25, 3, 0 }, // Teacher1, Amy
{ 14, 5, 12 }, // Teacher2, John
{ 33, 22, 10 }, // Teacher3, Nick
{ 0, 20, 5 } }; // Teacher4, Maria
int numOfTeachers = 4;
int numOfDays = 3;
int sumPerDay[] = new int[3];
int sumPerTeacher[] = new int[4];
String[] teacherNames ={"Amy","John","Nick","Maria"};
int totalStudents = 0;
int i ,j;
for(i=0;i<4;i++)
{
System.out.println(teacherNames[i]+" met "+teacherData[i][0]+"(M) "+ teacherData[i][1]+"(T) "+ teacherData[i][2]+"(W) students.");
}
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
totalStudents =totalStudents + teacherData[i][j];
}
}
System.out.println("Total number of students using the center: " + totalStudents);
for(j=0;j<3;j++) //compute students per day
{
sumPerDay[j] = 0;
for(i=0;i<4;i++)
{
sumPerDay[j] = sumPerDay[j] + teacherData[i][j];
}
}
System.out.println("M:"+sumPerDay[0]+", T:"+sumPerDay[1]+", W:"+sumPerDay[2]);
int maxDay = 0;
for(i=0;i<3;i++)
{
if(maxDay <sumPerDay[i])
maxDay = sumPerDay[i];
}
for(i=0;i<4;i++) //compute sum per teacher
{
sumPerTeacher[i] = 0;
for(j=0;j<3;j++)
{
sumPerTeacher[i] = sumPerTeacher[i] + teacherData[i][j];
}
}
int maxTeacher = 0;
for(i=0;i<4;i++)
{
System.out.print(teacherNames[i]+":"+sumPerTeacher[i]+" ");
if(maxTeacher < sumPerTeacher[i])
{
maxTeacher = sumPerTeacher[i];
}
}
System.out.println(" Max students per day:"+maxDay);
System.out.println("Max students per teacher:"+maxTeacher);
}
}
output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.