Dont know where to srart!
Hi just wondering if someone could point me in the right direction. This isnt h/w im on summer break and tryin to get ahead befour next year but dont even know how to approach this question! The thing is i can do all the question up to the chapter befour this but am just gettin stuck. Many thanks sam
The question is: Modify the Student class 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 averages are included in the description of the student. Modify the driver class main method to exercise the new Student methods.
1. I can see i need to first include the 3 test scores, but how do i do this so i can later identify them by test 1,2,3?
2. Provide a constructor that sets all instance values based on parameter values. - i dont understand what this means?
public class Student
{
private String firstName, lastName;
private Address homeAddress, schoolAddress;
//-----------------------------------------------------------------
// Constructor: Sets up this student with the specified values.
//-----------------------------------------------------------------
public Student (String first, String last, Address home,
Address school)
{
firstName = first;
lastName = last;
homeAddress = home;
schoolAddress = school;
}
//-----------------------------------------------------------------
// 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;
return result;
}
}
public 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;
}
}
Re: Dont know where to srart!
Re: Dont know where to srart!
For handling the test scores, you will probably want to use an array. If you don't know about arrays, learn here: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
An array is basically a collection of data. Each piece of data resides within a certain index, and one simply has to request the index in order to retrieve the data that resides in the index.