Design and implement a class called Course that represents a course taken at a school. A Course object should keep track of up to five students, as represented by the modified Student class from the previous project (Note: you’ll have to copy the Student class from project 3 into this project, no importing!) The constructor of the Course class should accept only the name of the course. Provide a method called addStudent that accepts one Student parameter (the Course object should keep track of how many valid students have been added to the course). 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 (called TestCourse) with a main method that creates a course, adds several students (i.e. at least three), prints a roll, and prints the overall course test average.
My effort:
Address
public class Address {
/** Street address. */
private String streetAddress;
/** city. */
private String city;
/** State. */
private String state;
/** Postal code, any country. */
private String postalCode;
/**
* Constructor: Sets up this address with the specified data.
*
* @param street
* Holds new streetAddress
* @param town
* Holds new city
* @param st
* Holds new state
* @param code
* Holds new postalCode
*/
public Address(String street, String town, String st, String code) {
streetAddress = street;
city = town;
state = st;
postalCode = code;
}
/**
* Returns a description of this Address object.
*
* @return formatted value of streetAddress, city, state, zipCode
*/
public String toString() {
String result;
result = streetAddress + "\n";
result += city + ", " + state + " " + postalCode;
return result;
}
}
Student:
public class Student {
/** <p>First name of this student. </p>*/
private String firstName;
/** <p>Last name of this student. </p>*/
private String lastName;
/** <p>Home address of this student. </p>*/
private Address homeAddress;
/** <p>School address of this student.
* Can be shared by other students </p>*/
private Address schoolAddress;
/** <p>Test1 score. </p>*/
private int testScore1;
/** <p>Test2 score. </p>*/
private int testScore2;
/** <p>Test3 score. </p>*/
private int testScore3;
/** <p>Test number 1. </p>*/
private final int tnum1 = 1;
/** <p>Test number 2. </p>*/
private final int tnum2 = 2;
/** <p>Test number 3. </p>*/
private final int tnum3 = 3;
/**
* Constructor: Sets up this student with the specified values.
* @param first The first name of the student
* @param last The last name of the student
* @param home The home address of the student
* @param school The school address of the student
* @param test1 test1
* @param test2 test2
* @param test3 test3
*/
public Student(String first, String last, Address home,
Address school, int test1, int test2, int test3) {
firstName = first;
lastName = last;
homeAddress = home;
schoolAddress = school;
testScore1 = test1;
testScore2 = test2;
testScore3 = test3;
}
/**
* <p>A zero-parameter constructor that sets each
* test score to zero.</p>
*/
public Student() {
testScore1 = 0;
testScore2 = 0;
testScore3 = 0;
}
/**
* <p>Set test score according to the test number.</p>
* @param testNum test number
* @param score score
*/
public void setTestScore(int testNum, int score) {
switch(testNum) {
case tnum1:
testScore1 = score;
break;
case tnum2:
testScore2 = score;
break;
case tnum3:
testScore3 = score;
break;
default:
}
}
/**
* <p>Get test score.</p>
* @param testNum test number
* @return test score
*/
public int getTestScore(int testNum) {
switch(testNum) {
case tnum1:
return testScore1;
case tnum2:
return testScore2;
case tnum3:
return testScore3;
default:
}
return 0;
}
/**
* <p> Use average method to calculate the average score.</p>
* @return average
*/
public double average() {
return (testScore1 + testScore2 + testScore3) / tnum3;
}
/**
* Returns a string description of this Student object.
* @return formatted name and addresses of student
*/
public String toString() {
String result;
result = firstName + " " + lastName + "\n";
result += "Home Address:\n" + homeAddress + "\n";
result += "School Address:\n" + schoolAddress + "\n";
result += "Test1 score: " + testScore1 + "\n";
result += "Test2 score: " + testScore2 + "\n";
result += "Test3 score: " + testScore3 + "\n";
result += "Average score: " + average();
return result;
}
}
Course:
public class Course {
private String course;
private Student s1;
private Student s2;
private Student s3;
private Student s4;
private Student s5;
private int studentcount = 0;
public Course() {
course = "";
}
public Course(String name) {
course = name;
}
public Student addStudent(Student s1) {
if (studentcount == 0) {
s1 = new Student();
studentcount++;
return s1;
} else if (studentcount == 1) {
s2 = new Student();
studentcount++;
return s2;
} else if (studentcount == 2) {
s3 = new Student();
studentcount++;
return s3;
} else if (studentcount == 3) {
s4 = new Student();
studentcount++;
return s4;
} else if (studentcount == 4) {
s5 = new Student();
studentcount++;
return s5;
} else {
System.out.println("Sorry! The course is full. Please"
+ " try again next term.");
}
return null;
}
public double average() {
final int maxstudent = 5;
return (s1.average() + s2.average() + s3.average() +
s4.average() + s5.average()) / maxstudent;
}
public String roll() {
String result;
result = s1.toString() + "/n";
result += s2.toString() + "/n";
result += s3.toString() + "/n";
result += s4.toString() + "/n";
result += s5.toString() + "/n";
return result;
}
}
TestCourse:
import q3.Address;
import q3.Student;
public class TestCourse {
/**
* <p>This is the main method (entry point) that gets called by the JVM.</p>
*
* @param args command line arguments.
*/
public static void main(String[] args) {
// your code will go here!!!
Address school = new Address("3700 Willingdon Avenue", "Burnaby",
"BC", "V5G3H2");
Address home1 = new Address("9270 No.3 Road", "Richmond",
"BC", "V6Y2O9");
Address home2 = new Address("5660 No.4 Road", "Richmond",
"BC", "V6Y1N8");
Address home3 = new Address("4423 King Edward Avenue", "Vancouver",
"BC", "V7R4F9");
final int s1test1 = 76;
final int s1test2 = 89;
final int s1test3 = 84;
final int s2test1 = 60;
final int s2test2 = 55;
final int s2test3 = 72;
final int s3test1 = 89;
final int s3test2 = 95;
final int s3test3 = 82;
Course course1 = new Course("Geography");
Student student1 = new Student();
Student student2 = new Student();
Student student3 = new Student();
addStudent(student1);
addStudent(student2);
addStudent(student3);
System.out.println(course1.roll());
System.out.println();
System.out.println("Question four was called and ran sucessfully.");
}
};
When I run the program, there are errors. I know the problems probably are in addStudent and roll methods (TestCourse.class). How to fix it?
By the way, we need to throw exceptions in this program. Could you guys give me some hints or example code?
Many thanks.
Edison

New Topic/Question
Reply


MultiQuote




|