Assume you work in a tutoring center, which operates Mon-Wed. The center keeps track of how many students come to the center and which tutors they see, similar to the table below:
tutor Mon Tue Wed Amy 25 3 0 John 14 5 12 Nick 33 22 10 Maria 0 20 5Write a Java program that does the following:
(1) The program shall read the number of tutors from the command line
c:\> java uin5679hw4 4 into variable numOfTutors. E.g.:
executes the program “uin5679hw4” with “4” as the parameter for the number of tutors.
(2) The program shall read the data as integers from a file named “tutors.txt” into a two-dimensional array
(3) The program shall print the following named tutorData[numOfTutors][3]. (Please note that in the example above, the numbers in bold are in the file and then stored in the array and not the headings of rows/columns). The file has numOfTutors total lines, each line contains 3 integers (number of students per day for a tutor), separated by spaces.
output (underlined
“Total number of students using the center: = values computed from program):
M: value, Tu: value, W: value. Total: value
Tutor1: . (sum of students for each day, sum of all day sums)
value, Tutor2: value, …. Total: value
Max total students per day: . (sum of students for each tutor, sum of all tutor sums)
value
Max students per tutor: . (max of all day sums)
value
Minimum number of students in all slots: . (max of all tutor sums)
value
(4) The program shall use int[] .” (min of all elements of array)
arrays
(5) The program shall use the following to store results for above computations: sumPerDay[3], sumPerTutor[numOfTutors].
methods
• readData() to read the data from the file in (2). The method takes an 2-D int[][] array as parameter (the tutorData) and returns nothing. :
• max1dArray() to calculate the maximum value of a 1-D array, e.g. sumPerDay[]. This method shall take an int[] array as parameter, and return an int as the result.
• min1dArray() to calculate the min value of a 1-D array, e.g. a row of tutorData. This method shall take an int[] array as parameter, and return an int as the result.
Now for my code (note I didn't attempt to put in the output yet as I can't even compile the program.)
import java.io.*;
import java.util.*;
class TutorCenter {
public static void main(String[] args) throws IOException {
int numOfTutors = Integer.parseInt(args[0]);
Scanner dataFile = new Scanner(new FileReader ("tutors.txt"));
int[][] tutorData = new int[numOfTutors][3];
for (int i = 0; i < numOfTutors; i++) {
for(int j = 0; j < 3; j++)
tutorData[i][j] = dataFile.nextInt();
}
for (int j = 0; j < tutorData[numOfTutors].length; j++) {
int alldaysums = 0;
for (int i = 0; i < tutorData[3].length; i++) {
alldaysums = alldaysums + tutorData[j][i];
}
}
int[] sumPerDay = new int[3];
for (int i = 0; i < tutorData[numOfTutors].length; i++) {
sumPerDay[3] = sumPerDay[3] + tutorData[numOfTutors][i];
}
int[] sumPerTutor = new int[numOfTutors];
for (int j = 0; j < tutorData[3].length; j++) {
sumPerTutor[numOfTutors] = sumPerTutor[numOfTutors] + tutorData[3][j];
}
}
int max1dArrayDay(int[] sumPerDay) {
int max = sumPerDay[3];
for (int i = 1; i < sumPerDay.length; i++) {
if (sumPerDay[3] > max) {
max = sumPerDay[i];
}
}
return max;
}
int max1dArrayTutor(int[] sumPerTutor) {
int max = sumPerTutor[numOfTutors];
for (int j = 1; j < sumPerTutor.length; j++) {
if (sumPerTutor[numOfTutors] > max) {
max = sumPerTutor[j];
}
}
return max;
}
int min1dArray(int[][] tutorData) {
int min = tutorData[numOfTutors][3];
for(int i = 1; i < tutorData.length; i++){
if(tutorData[i][i] < min){
min = tutorData[i][i];
}
}
return min;
}
}
Any form of help will be greatly appreciated, Thanks!
Edited by Roger, 27 November 2010 - 07:11 PM.
added code block


Sign In
Create Account

Back to top









