Java - another assignment
Posted: Sat Jun 22, 2013 8:54 pm
Requirements:
StudentClient.java (nothing is to be changed here)
As you can see I got the Student.java partially done but I'm completely stuck on the fullString and typeOfStudent parts (no idea what to put - I know I have to add in if/else statements for the ages and type of students [preschool, kindergarten etc etc] but I'm really confused >_<)
Thanks again for any help
- Include the proper instance variables (name and age).
- Include a constructor that allows the client to set beginning values for name and age.
- The constructor should take in two parameters (name and age) and should call mutator methods to validate new values for name and age.
- Include a getName method that returns the name of the student.
- Include a setName method that sets the value of the student’s name.
- Include a getAge method that returns the age of the student.
- Include a setAge method that sets the age if the value is greater than zero.
- Include a fullString method that returns the name and age of the student.
- Include a typeOfStudent method that returns the most likely level of schooling for the student by using a series of if statements.
- Preschool (age 0 – 4)
- Kindergarten (age 5)
- Elementary School (age 6-10)
- Middle School (age 11-13)
- High School (age 14-17)
- College (age 18 & up)
- Place a comment before each of the methods explaining how they work: getName, setName, getAge, setAge, fullString, typeOfStudent.
Code: Select all
/* 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;
}
public void setAge(int Age) {
if (newAge > 0){
age = newAge;
} else {
System.out.println("Age cannot be negative.");
}
}
//Accessor method to get age
public int getAge() {
return age;
}
public static String typeOfStudent (int age){
}
public String fullString() {
}
} //end Student class
StudentClient.java (nothing is to be changed here)
Code: Select all
Results should look like this:
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
Name: Bob
Age: 15
Type of Student: high school
Name: Jan
Age: 13
Type of Student: middle school
Name: Ted
Age: 35
Type of Student: college
As you can see I got the Student.java partially done but I'm completely stuck on the fullString and typeOfStudent parts (no idea what to put - I know I have to add in if/else statements for the ages and type of students [preschool, kindergarten etc etc] but I'm really confused >_<)
Thanks again for any help