Log in

View Full Version : Java assignment: check please


Jess
June 23rd, 2013, 11:39 AM
StudentClient.java
public class StudentClient
{
public static void main( String [] args )
{
Student student1 = new Student("Bob", 15);
Student student2 = new Student("Jan", 13);
System.out.println("Name: " + student1.getName());
System.out.println("Age: " + student1.getAge());
System.out.println("Type of Student: " + student1.typeOfStudent());

System.out.println("\n" + student2.fullString());
System.out.println("Type of Student: " + student2.typeOfStudent());

student1.setName("Ted");
student1.setAge(35);

System.out.println("\n" + student1.fullString());
System.out.println("Type of Student: " + student1.typeOfStudent());
} //ends main
} //ends program





Student.java
/* Jessica Chen
* Saturday 6/22/13
* Java 1.7.0_21 and NetBeans 7.3
* Determines type of student based on age
*/

public class Student
{
private String name;
private int age;

public Student(String newName, int newAge){
setName(newName);
setAge(newAge);
}

//Mutator method to set name
public void setName(String newName){
name = newName;
}

//Accessor method to get name
public String getName(){
return name;
}

//Mutator method to check validity of data and set age
public void setAge(int newAge) {
if (newAge > 0){
age = newAge;
} else {
System.out.println("Age cannot be negative.");
}
}

//Accessor method to get age
public int getAge() {
return age;
}

//method to determine school level
public String typeOfStudent (int age){
if (age >= 0 && age <= 4){
return "preschool";
}
else if (age == 5){
return "kindergarten";
}
else if (age >= 6 && age <= 10){
return "elementary school";
}
else if (age >= 11 && age <= 13){
return "middle school";
}
else if (age >=14 && age <= 17){
return "high school";
}
else if (age >= 18){
return "college";
}
else{
return "invalid";
}
}

//method to return name and age
public String fullString() {
return "Name: " + name + "\nAge: " + age;
}

} //end Student class

There's something not right about the Student.java code, but I don't know what. The StudentClient.java file is not supposed to be changed. What needs to be fixed?

ethanf93
June 23rd, 2013, 11:55 AM
I had to compile the programs to see the error, you might want to post that message in the future. The error is on the line

public String typeOfStudent (int age){

Compare that with the call:
student1.typeOfStudent()
Do you see the error?

Jess
June 23rd, 2013, 12:10 PM
Yep

Error is

method typeOfStudent in class Student cannot be applied to given types;
required: int
found: no arguments
reason: actual and formal arguments lists differ in length

ethanf93
June 23rd, 2013, 12:18 PM
To rephrase that error message, what it's telling you is that the arguments are wrong. It expected a method with no arguments when you made the call:
student1.typeOfStudent()
but it found a method that required arguments:
public String typeOfStudent (int age){

Consider: Do you need int age in that method's definition?

Jess
June 23rd, 2013, 12:34 PM
I got it now; should be public String typeOfStudent (){

thanks for the help