I have a computer programming assignment that in my class that I am stuck on. I understand how to do get this outcome using arrays. However, we now have to accomplish the same task using objects and I am highly confused.
This is due this Wednesday, so any help would be GREATLY appreciated!
Here is my code from the array version that I already completed when we didn't have to make objects:
import java.io.*; // Import all classes in java.io package. Save typing.
public class GradeAverage {
public static void main (String[] args) throws IOException {
// This is how we set things up to read lines of text from the user. BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int initialNumStudents = 0; int maxStudents = 100;
// the arrays to save student data and averages String[] lastName = new String[maxStudents];//stores students' first names String[] firstName = new String[maxStudents];//stores students' last names int[] exam1 = new int[maxStudents];//stores students' first exam score int[] exam2 = new int[maxStudents];//stores students' second exam score int[] exam3 = new int[maxStudents];//stores students' third exam score int[] examAvg = new int[maxStudents];//store the average exam score for each student int k = 0;//array index for arrays receiving user input int i = 0;//array index for computing exam averages int averageAll = 0;//allows to get data for all students for total average
for(k= 0; k < maxStudents; k++) {
System.out.print("What is the student's last name?");//allows user to enter last name String line = in.readLine(); if ( line.equals( ".")) break;//allows user to exit loop lastName[k] = line;
System.out.print("What is the student's first name?");//allows user to enter first name firstName[k] = in.readLine();
System.out.print("What is the student's first exam grade?");//allows user to enter 1st exam exam1[k] = Integer.parseInt(in.readLine());
System.out.print("What is the student's second exam grade? ");//allows user to enter 2nd exam exam2[k] = Integer.parseInt(in.readLine());
System.out.print("What is the student's third exam grade? ");//allows user to enter 3rd exam exam3[k] = Integer.parseInt(in.readLine());
examAvg[k] = Math.round ((exam1[k] + exam2[k] + exam3[k])/3);//formula to average each students //individual average
}
//The following block of code calculates the average exam score for ALL student data. initialNumStudents = k;
for (i = 0; i <= (initialNumStudents - 1);i++) {
averageAll = averageAll + examAvg; }
averageAll = Math.round(averageAll /initialNumStudents);//method to average all scores
//This block of code prints the results into a readable table
System.out.println ("The average exam grade for all entered students is " + averageAll +".");
} // end main
} // end GradeAverage
Here is the new assignment description:
COMP 110
Second Java Program: Objects
due date: Wed, Nov. 15 by 4:00 pm
OVERVIEW
This Java program will do something similar to the one you just wrote, but we will do it with objects. You will write a class to represent a student and his/her information, and instantiate an object of that class for each student that you need to save information for.
Your program that will read in from the keyboard a collection of information and grades for students and then find the class average on each exam. It will also compute the overall average of all grades across all students.
The program text will be a two (or more if you wish) Java classes (let's say class name CourseManager and class name Student) in two files (let's say CourseManager.java and Student.java). The CourseManager class will contain the required main method and can have any other methods you want to write to help well organize your code. The Student class will have the data fields needed to save a student's first and last name, a studentID, and grades for each of 3 exams.
In class Student you will write several methods as well. You will need accessors for the fields (getters and setters), and you will need methods to provide the summary information a user might want for a student -- getAverage is one for sure.
INPUT FORM
For simplicity we will assume that we have 3 exam grades per student, and that there are no missing grades. Let's assume we have some data like this:
Smith Bob 45.6 87.9 71.4 Anderson Cathy 78.7 91.3 89.2 Jones William 71.5 53.6 98.2
For now we will keep data input simple... in general, input processing can get rather complex. Let's have the program do this: prompt the user for each item, one per line. So for example, here is how the run would go for the above data:
You will be entering student names and grades. Please enter the requested information one item per line. Hit "return" after each line you type. When you have run out of students to enter, give the single character "." as the Last name.
Last name? Smith First name? Bob grade? 45.6 grade? 87.9 grade? 71.4 Last name? Anderson First name? Cathy grade? 78.7 grade? 91.3 grade? 89.2 Last name? Jones First name? William grade? 71.5 grade? 53.6 grade? 98.2 Last name? .
<etc... the program begins computing and printing results... >
The final line containing "." is the data flag that says all student data has been entered. The example program "FactQuoter.java" (see the class web page) shows the way to do this input in Java.
There will be an unknown (in advance) number of students, but we do know there will not be more than 100 students (meaning we can make out arrays of max size 100). We will need an array to store the student objects into as we create them.
When a new student last name is encountered, your program will create a new Student object and store it in an array at the next open slot. You will generate a unique student ID for this new Student object using a class variable (we will see this in class). Once you have the new Student object with unique ID, you will store the last name into it, and continue collecting more information for that student and storing it in the object (first name, 3 grades).
OUTPUT FORM
When you have received all the input, print out all the information in tabular form along with the same averages you produced in the last program. You will be using "System.out.print" and "System.out.println" to do this, which work very similar to the "Document.write" from JavaScript. However, remember that HTML is a thing of the past now... you will not be writing out any HTML in your output.
First print a useful header line that shows what information appears in the columns under it. Then, for each student, print out the student ID number in parentheses (NOT the array index, but the ID number stored in the Student object), the last name, a comma, a space, and the first name. On the same line print two tabs, then the 3 exam grades (a tab after each one); at the end of the line print the average grade. The next student will go on the next line. The last thing I want to see, after all students are shown, is a single line that gives the overall average for the class on all exams. To do this, you add up the averages stored in the average array, and divide by the number of students.
It will look something like this:
# Student name ex1 ex2 ex3 avg (1) Smith, Bob 45.6 87.9 71.4 68.3 (2) Anderson, Cathy 78.7 91.3 89.2 86.4 (3) Jones, William 71.5 53.6 98.2 74.4333
This class has 3 students. Overall exam average is 76.377
NOTE
You should be able to mostly use the output code you wrote in the previous program, with some changes that get the information from the Student objects you are using this time.
STRATEGY
Your program will have several sections to it:
(1) a section that reads in data from the user... creates an object for each new student and saves the info in it (2) a section that computes the requested averages using the information each student object can give you (2) a section that generates the output... as noted, you can reuse much of the code you wrote last time here for this section
HAND IN
Create your 2 java files in your UNC web space. Print them out, hand them in with the secret file names clearly indicated on them along with your ONYEN. Place them in the basket at the CS Dept. front desk by 4:00 pm on the due date.
FINAL THOUGHTS
Remember to use good programming style... use named constants where appropriate, choose meaningful variable names, have a beginning block comment as well as smaller comments throughout, design a clear algorithm, make it all indented and readable.