Advertisement
Guest User

Untitled

a guest
Nov 26th, 2018
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.28 KB | None | 0 0
  1.  
  2. /*PP 7.2 Modify the Student class presented in this chapter as follows.
  3. Each student object should also contain the scores for three tests.
  4. Provide a constructor that sets all instance values based on parameter values.
  5. Overload the constructor such that each test score is assumed to be initially zero.
  6. Provide a method called setTestScore that accepts two parameters: the test number (1 through 3) and the score.
  7. Also provide a method called getTestScore that accepts the test number and returns the appropriate score.
  8. Provide a method called average that computes and returns the average test score for this student.
  9. Modify the toString method such that the test scores and average are included in the description of the student.
  10. Modify the driver class main method to exercise the new Student methods.
  11.  
  12. PP 7.3 Write a class called Course that represents a course taken at a school.
  13. Represent each student using the modified Student class from the previous programming project.
  14. Use an ArrayList in the Course to store the students taking that course.
  15. The constructor of the Course class should accept only the name of the course.
  16. Provide a method called addStudent that accepts one Student parameter.
  17. Provide a method called average that computes and returns the average of all students’ test score averages.
  18. Provide a method called roll that prints all students in the course.
  19. 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.
  20. */
  21.  
  22. public class StudentBody { // this is the driver class
  23.     // -----------------------------------------------------------------
  24.     // Creates some Address and Student objects and prints them.
  25.     // -----------------------------------------------------------------
  26.     public static void main(String[] args) {
  27.         Address school = new Address("800 Lancaster Ave.", "Villanova", "PA", 19085);
  28.         Address jHome = new Address("21 Jump Street", "Blacksburg", "VA", 24551);
  29.         Student john = new Student("John", "Smith", jHome, school, 10, 20, 30); // added scores to john
  30.  
  31.         Address mHome = new Address("123 Main Street", "Euclid", "OH", 44132);
  32.         Student marsha = new Student("Marsha", "Jones", mHome, school, 40, 50, 60); // added scores to marsha
  33.         //student 3
  34.         Address lHome = new Address("120 Bent Street", "Euclid", "Kansas", 4413233);
  35.         Student louis = new Student("Louis", "Johnson", lHome, school, 45, 55, 65);
  36.         //student 4
  37.         Address aHome = new Address("420 Down Street", "Fantasy", "CT", 06456);
  38.         Student ashley = new Student("Ashley", "Kujo", aHome, school, 40, 20, 90);
  39.         //student 5
  40.         Address sHome = new Address("123 Main Street", "Southbury", "CT", 064512);
  41.         Student sara = new Student("Sara", "Squeta", sHome, school, 100, 35, 74);
  42.        
  43.         Course computerScience = new Course("Computer Science");
  44.         computerScience.addStudent(john);
  45.         computerScience.addStudent(marsha);
  46.  
  47.         // 7.3 test
  48.         System.out.println(computerScience.roll());
  49.         System.out.println(
  50.                 "The overall test average of the 'Computer Science' course is: " + computerScience.getAverageScores());
  51.         System.out.println("--------------------------------------------------------------------------");
  52.        
  53.         computerScience.addStudent(ashley);
  54.         computerScience.addStudent(sara);
  55.         computerScience.addStudent(louis);
  56.        
  57.         System.out.println(computerScience.roll());
  58.         System.out.println(
  59.                 "The overall test average of the 'Computer Science' course is: " + computerScience.getAverageScores());
  60.         System.out.println("--------------------------------------------------------------------------");
  61.  
  62.         // tests of the modified driver class for problem 7.2
  63.         System.out.println(john);
  64.         System.out.println();
  65.         System.out.println(marsha);
  66.         john.setTestScore(3, 73);
  67.         System.out.println("--------------------------------------------------------------------------");
  68.         System.out.println("\nAfter setting the test score2 of john to 75, the information would be:\n" + john);
  69.         System.out.println("\nThe score of test3 of Marsha: " + marsha.getTestScore(3));
  70.         System.out.println("The score of test2 of John: " + john.getTestScore(2));
  71.  
  72.         System.out.println("--------------------------------------------------------------------------");
  73.         // 7.3 average test after changes from 7.2
  74.         System.out
  75.                 .println("The overall test average of the 'Computer Science' course after changes to john's grade is: "
  76.                         + computerScience.getAverageScores());
  77.     }
  78. }
  79.  
  80. class Course {
  81.     private String name, rollString;
  82.     private Student studentOne, studentTwo, studentThree, studentFour, studentFive;
  83.     private int studentCount;
  84.     private double averageCourse;
  85.    
  86.     public Course(String name) {
  87.         this.name = name;
  88.     }
  89.  
  90.     public Student addStudent(Student newStu) {
  91.         if (studentOne == null) {
  92.             studentOne = newStu;
  93.             studentCount = 1;
  94.         } else if (studentTwo == null) {
  95.             studentTwo = newStu;
  96.             studentCount = 2;
  97.         } else if (studentThree == null) {
  98.             studentThree = newStu;
  99.             studentCount = 3;
  100.         }
  101.         else if(studentFour == null) {
  102.             studentFour = newStu;
  103.             studentCount = 4;
  104.         }
  105.         else if(studentFive == null) {
  106.             studentFive = newStu;
  107.             studentCount = 5;
  108.         }
  109.  
  110.         return newStu;
  111.     }
  112.     public String getCourseName() {
  113.         return name;
  114.     }
  115.     public double getAverageScores() {
  116.         switch (studentCount) {
  117.         case 1:
  118.             averageCourse = studentOne.average();
  119.             break;
  120.         case 2:
  121.             averageCourse = (studentOne.average() + studentTwo.average()) / 2.0d;
  122.             break;
  123.         case 3:
  124.             averageCourse = (studentOne.average() + studentTwo.average() + studentThree.average()) / 3.0d;
  125.             break;
  126.         case 4:
  127.             averageCourse = (studentOne.average() + studentTwo.average() + studentThree.average() + studentFour.average()) / 4.0d;
  128.             break;
  129.         case 5:
  130.             averageCourse = (studentOne.average() + studentTwo.average() + studentThree.average() + studentFour.average() + studentFive.average()) / 5.0d;
  131.         default:
  132.             averageCourse = 0;
  133.         }
  134.         return averageCourse;
  135.     }
  136.  
  137.     public String roll() {
  138.             switch (studentCount) {
  139.             case 1:
  140.                 rollString = "Student(s) in class: \n" + studentOne.getName() + "\n";
  141.                 break;
  142.             case 2:
  143.                  rollString = "Student(s) in class: \n" + studentOne.getName() + "\n" + studentTwo.getName() + "\n";
  144.                 break;
  145.             case 3:
  146.                  rollString = "Student(s) in class: \n" + studentOne.getName() + "\n" + studentTwo.getName() + "\n" + studentThree.getName() + "\n";
  147.                 break;
  148.             case 4:
  149.                  rollString = "Student(s) in class: \n" + studentOne.getName() + "\n" + studentTwo.getName() + "\n" + studentThree.getName() + "\n" +studentFour.getName() + "\n";
  150.                 break;
  151.             case 5:
  152.                  rollString = "Student(s) in class: \n" + studentOne.getName() + "\n" +studentTwo.getName() + "\n" +studentThree.getName() + "\n" + studentFour.getName() + "\n" + studentFive.getName() + "\n";
  153.                 break;
  154.             default:
  155.                 rollString = "There are no students in the course.";
  156.                 break;
  157.     }
  158.             return rollString;
  159. }
  160. }
  161.  
  162. class Student {
  163.  
  164.     // variables
  165.     private String firstName, lastName;
  166.     private Address homeAddress, schoolAddress;
  167.     private int score1, score2, score3; // added three scores
  168.  
  169.     // added constructor that initializes variables
  170.     public Student() {
  171.         score1 = 0;
  172.         score2 = 0;
  173.         score3 = 0;
  174.     }
  175.     public String getName() {
  176.         return (firstName + " " + lastName);
  177.     }
  178.     // -----------------------------------------------------------------
  179.     // Constructor: Sets up this student with the specified values.
  180.     // -----------------------------------------------------------------
  181.     public Student(String first, String last, Address home, Address school, int test1, int test2, int test3) { // added
  182.                                                                                                                 // parameters
  183.         firstName = first;
  184.         lastName = last;
  185.         homeAddress = home;
  186.         schoolAddress = school;
  187.         score1 = test1;
  188.         score2 = test2;
  189.         score3 = test3;
  190.  
  191.     }
  192.  
  193.     // Provide a method called setTestScore that accepts two parameters: the test
  194.     // number (1 through 3) and the score.
  195.     public void setTestScore(int test, int score) {
  196.         if (test == 1)
  197.             score1 = score;
  198.         else if (test == 2)
  199.             score2 = score;
  200.         else if (test == 3)
  201.             score3 = score;
  202.         else {
  203.             System.out.println("Wrong input");
  204.         }
  205.  
  206.     }// end setTestScore
  207.  
  208.     // Also provide a method called getTestScore that accepts the test number and
  209.     // returns the appropriate score.
  210.     public int getTestScore(int test) {
  211.         int score = -1;// initialize
  212.  
  213.         if (test == 1)
  214.             score = score1;
  215.         else if (test == 2)
  216.             score = score2;
  217.         else if (test == 3)
  218.             score = score3;
  219.         else {
  220.             System.out.println("Wrong input");
  221.         }
  222.  
  223.         return score;
  224.     }// end getTestScore
  225.  
  226.     // Provide a method called average that computes and returns the average test
  227.     // score for this student.
  228.     public double average() {
  229.         double sum = score1 + score2 + score3;
  230.         return sum / 3;
  231.     }// end average method
  232.  
  233.     // -----------------------------------------------------------------
  234.     // Returns a string description of this Student object.
  235.     // -----------------------------------------------------------------
  236.     public String toString() {
  237.         String result;
  238.  
  239.         result = firstName + " " + lastName + "\n";
  240.         result += "Home Address:\n" + homeAddress + "\n";
  241.         result += "School Address:\n" + schoolAddress;
  242.         result += "Three test scores: \nTest1: " + getTestScore(1) + "\nTest2: " + getTestScore(2) + "\nTest3: "
  243.                 + getTestScore(3) + "\n"; // added test scores
  244.         result += "The average of the three test scores: " + average(); // added average
  245.  
  246.         return result;
  247.     }
  248. }
  249.  
  250. class Address {
  251.     private String streetAddress, city, state;
  252.     private long zipCode;
  253.  
  254.     // -----------------------------------------------------------------
  255.     // Constructor: Sets up this address with the specified data.
  256.     // -----------------------------------------------------------------
  257.     public Address(String street, String town, String st, long zip) {
  258.         streetAddress = street;
  259.         city = town;
  260.         state = st;
  261.         zipCode = zip;
  262.     }
  263.  
  264.     // -----------------------------------------------------------------
  265.     // Returns a description of this Address object.
  266.     // -----------------------------------------------------------------
  267.     public String toString() {
  268.         String result;
  269.  
  270.         result = streetAddress + "\n";
  271.         result += city + ", " + state + " " + zipCode;
  272.  
  273.         return result;
  274.     }
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement