Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*PP 7.2 Modify the Student class presented in this chapter as follows.
- Each student object should also contain the scores for three tests.
- Provide a constructor that sets all instance values based on parameter values.
- Overload the constructor such that each test score is assumed to be initially zero.
- Provide a method called setTestScore that accepts two parameters: the test number (1 through 3) and the score.
- Also provide a method called getTestScore that accepts the test number and returns the appropriate score.
- Provide a method called average that computes and returns the average test score for this student.
- Modify the toString method such that the test scores and average are included in the description of the student.
- Modify the driver class main method to exercise the new Student methods.
- PP 7.3 Write a class called Course that represents a course taken at a school.
- Represent each student using the modified Student class from the previous programming project.
- Use an ArrayList in the Course to store the students taking that course.
- The constructor of the Course class should accept only the name of the course.
- Provide a method called addStudent that accepts one Student parameter.
- Provide a method called average that computes and returns the average of all students’ test score averages.
- Provide a method called roll that prints all students in the course.
- Create a driver class with a main method that creates a course, adds several students, prints a roll, and prints the overall course test average.
- */
- public class StudentBody { // this is the driver class
- // -----------------------------------------------------------------
- // Creates some Address and Student objects and prints them.
- // -----------------------------------------------------------------
- public static void main(String[] args) {
- Address school = new Address("800 Lancaster Ave.", "Villanova", "PA", 19085);
- Address jHome = new Address("21 Jump Street", "Blacksburg", "VA", 24551);
- Student john = new Student("John", "Smith", jHome, school, 10, 20, 30); // added scores to john
- Address mHome = new Address("123 Main Street", "Euclid", "OH", 44132);
- Student marsha = new Student("Marsha", "Jones", mHome, school, 40, 50, 60); // added scores to marsha
- //student 3
- Address lHome = new Address("120 Bent Street", "Euclid", "Kansas", 4413233);
- Student louis = new Student("Louis", "Johnson", lHome, school, 45, 55, 65);
- //student 4
- Address aHome = new Address("420 Down Street", "Fantasy", "CT", 06456);
- Student ashley = new Student("Ashley", "Kujo", aHome, school, 40, 20, 90);
- //student 5
- Address sHome = new Address("123 Main Street", "Southbury", "CT", 064512);
- Student sara = new Student("Sara", "Squeta", sHome, school, 100, 35, 74);
- Course computerScience = new Course("Computer Science");
- computerScience.addStudent(john);
- computerScience.addStudent(marsha);
- // 7.3 test
- System.out.println(computerScience.roll());
- System.out.println(
- "The overall test average of the 'Computer Science' course is: " + computerScience.getAverageScores());
- System.out.println("--------------------------------------------------------------------------");
- computerScience.addStudent(ashley);
- computerScience.addStudent(sara);
- computerScience.addStudent(louis);
- System.out.println(computerScience.roll());
- System.out.println(
- "The overall test average of the 'Computer Science' course is: " + computerScience.getAverageScores());
- System.out.println("--------------------------------------------------------------------------");
- // tests of the modified driver class for problem 7.2
- System.out.println(john);
- System.out.println();
- System.out.println(marsha);
- john.setTestScore(3, 73);
- System.out.println("--------------------------------------------------------------------------");
- System.out.println("\nAfter setting the test score2 of john to 75, the information would be:\n" + john);
- System.out.println("\nThe score of test3 of Marsha: " + marsha.getTestScore(3));
- System.out.println("The score of test2 of John: " + john.getTestScore(2));
- System.out.println("--------------------------------------------------------------------------");
- // 7.3 average test after changes from 7.2
- System.out
- .println("The overall test average of the 'Computer Science' course after changes to john's grade is: "
- + computerScience.getAverageScores());
- }
- }
- class Course {
- private String name, rollString;
- private Student studentOne, studentTwo, studentThree, studentFour, studentFive;
- private int studentCount;
- private double averageCourse;
- public Course(String name) {
- this.name = name;
- }
- public Student addStudent(Student newStu) {
- if (studentOne == null) {
- studentOne = newStu;
- studentCount = 1;
- } else if (studentTwo == null) {
- studentTwo = newStu;
- studentCount = 2;
- } else if (studentThree == null) {
- studentThree = newStu;
- studentCount = 3;
- }
- else if(studentFour == null) {
- studentFour = newStu;
- studentCount = 4;
- }
- else if(studentFive == null) {
- studentFive = newStu;
- studentCount = 5;
- }
- return newStu;
- }
- public String getCourseName() {
- return name;
- }
- public double getAverageScores() {
- switch (studentCount) {
- case 1:
- averageCourse = studentOne.average();
- break;
- case 2:
- averageCourse = (studentOne.average() + studentTwo.average()) / 2.0d;
- break;
- case 3:
- averageCourse = (studentOne.average() + studentTwo.average() + studentThree.average()) / 3.0d;
- break;
- case 4:
- averageCourse = (studentOne.average() + studentTwo.average() + studentThree.average() + studentFour.average()) / 4.0d;
- break;
- case 5:
- averageCourse = (studentOne.average() + studentTwo.average() + studentThree.average() + studentFour.average() + studentFive.average()) / 5.0d;
- default:
- averageCourse = 0;
- }
- return averageCourse;
- }
- public String roll() {
- switch (studentCount) {
- case 1:
- rollString = "Student(s) in class: \n" + studentOne.getName() + "\n";
- break;
- case 2:
- rollString = "Student(s) in class: \n" + studentOne.getName() + "\n" + studentTwo.getName() + "\n";
- break;
- case 3:
- rollString = "Student(s) in class: \n" + studentOne.getName() + "\n" + studentTwo.getName() + "\n" + studentThree.getName() + "\n";
- break;
- case 4:
- rollString = "Student(s) in class: \n" + studentOne.getName() + "\n" + studentTwo.getName() + "\n" + studentThree.getName() + "\n" +studentFour.getName() + "\n";
- break;
- case 5:
- rollString = "Student(s) in class: \n" + studentOne.getName() + "\n" +studentTwo.getName() + "\n" +studentThree.getName() + "\n" + studentFour.getName() + "\n" + studentFive.getName() + "\n";
- break;
- default:
- rollString = "There are no students in the course.";
- break;
- }
- return rollString;
- }
- }
- class Student {
- // variables
- private String firstName, lastName;
- private Address homeAddress, schoolAddress;
- private int score1, score2, score3; // added three scores
- // added constructor that initializes variables
- public Student() {
- score1 = 0;
- score2 = 0;
- score3 = 0;
- }
- public String getName() {
- return (firstName + " " + lastName);
- }
- // -----------------------------------------------------------------
- // Constructor: Sets up this student with the specified values.
- // -----------------------------------------------------------------
- public Student(String first, String last, Address home, Address school, int test1, int test2, int test3) { // added
- // parameters
- firstName = first;
- lastName = last;
- homeAddress = home;
- schoolAddress = school;
- score1 = test1;
- score2 = test2;
- score3 = test3;
- }
- // Provide a method called setTestScore that accepts two parameters: the test
- // number (1 through 3) and the score.
- public void setTestScore(int test, int score) {
- if (test == 1)
- score1 = score;
- else if (test == 2)
- score2 = score;
- else if (test == 3)
- score3 = score;
- else {
- System.out.println("Wrong input");
- }
- }// end setTestScore
- // Also provide a method called getTestScore that accepts the test number and
- // returns the appropriate score.
- public int getTestScore(int test) {
- int score = -1;// initialize
- if (test == 1)
- score = score1;
- else if (test == 2)
- score = score2;
- else if (test == 3)
- score = score3;
- else {
- System.out.println("Wrong input");
- }
- return score;
- }// end getTestScore
- // Provide a method called average that computes and returns the average test
- // score for this student.
- public double average() {
- double sum = score1 + score2 + score3;
- return sum / 3;
- }// end average method
- // -----------------------------------------------------------------
- // Returns a string description of this Student object.
- // -----------------------------------------------------------------
- public String toString() {
- String result;
- result = firstName + " " + lastName + "\n";
- result += "Home Address:\n" + homeAddress + "\n";
- result += "School Address:\n" + schoolAddress;
- result += "Three test scores: \nTest1: " + getTestScore(1) + "\nTest2: " + getTestScore(2) + "\nTest3: "
- + getTestScore(3) + "\n"; // added test scores
- result += "The average of the three test scores: " + average(); // added average
- return result;
- }
- }
- class Address {
- private String streetAddress, city, state;
- private long zipCode;
- // -----------------------------------------------------------------
- // Constructor: Sets up this address with the specified data.
- // -----------------------------------------------------------------
- public Address(String street, String town, String st, long zip) {
- streetAddress = street;
- city = town;
- state = st;
- zipCode = zip;
- }
- // -----------------------------------------------------------------
- // Returns a description of this Address object.
- // -----------------------------------------------------------------
- public String toString() {
- String result;
- result = streetAddress + "\n";
- result += city + ", " + state + " " + zipCode;
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement