Java - another assignment

Questions/Tutorials for any programming language not covered in other sections.
5 posts Page 1 of 1
Contributors
User avatar
Jessica
x Girl Power x
x Girl Power x
Posts: 35
Joined: Thu Feb 21, 2013 1:52 am

Java - another assignment
Jessica
Requirements:
  • 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.
Student.java
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
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


Results should look like this:
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
User avatar
lolxot
VIP - Donator
VIP - Donator
Posts: 155
Joined: Thu Apr 08, 2010 4:59 pm

Re: Java - another assignment
lolxot
Here you go:

StudentClient.java
Code: Select all
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(student1.getAge()));

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

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

            System.out.println("\n" + student1.fullString());
            System.out.println("Type of Student: " + student1.typeOfStudent(student1.getAge()));
        }
}
and Student.java
Code: Select all
public class Student {
   private String name;
        private int age;
       
        public Student(String newName, int newAge) {
            setName(newName);
            setAge(newAge);
        }
       
        public void setName(String newName) {
            name = newName;
        }
        
        public String getName(){
            return name;
        }
       
        public void setAge(int newAge) {
            if (newAge > 0) {
                age = newAge;
            } else {
                System.out.println("Age cannot be negative.");
            }
        }
       
        public int getAge() {
            return age;
        }
       
        public String typeOfStudent (int age) {
        	String returnMsg = "invalid";
        	if (age >= 0 && age <= 4) {
        		returnMsg = "Preschool";
        	} else if (age == 5) {
        		returnMsg = "Kindergarten";
        	} else if (age >= 6 && age <= 10) {
        		returnMsg = "Elementary School";
        	} else if (age >= 11 && age <= 13) {
        		returnMsg = "Middle School";
        	} else if (age >= 14 && age <= 17) {
        		returnMsg = "High School";
        	} else if (age >= 18) {
        		returnMsg = "College";
        	}
        	return returnMsg;
        }
       
        public String fullString() {
			return "Name: " + name + ", Age: " + age;
           
        }
               
}
User avatar
Jessica
x Girl Power x
x Girl Power x
Posts: 35
Joined: Thu Feb 21, 2013 1:52 am

Re: Java - another assignment
Jessica
Thanks, let me try that out

EDIT: StudentClient.java isn't suppose to be edited; you have to leave it alone. and I've never learned returnMsg, so I don't think I can use that ...:/

Here's more of the instructions:
The Student.java class should encapsulate the concept of a student, assuming that a student has the following attributes: a name and age. Include a constructor, the accessors and mutators, and methods fullString and typeOfStudent. You are to add the code to the Student.java class that already exists. Use the client class provided to you to test all the methods in your class (this will be the class that has the main method and runs…it is already written…DO NOT CHANGE IT).
User avatar
lolxot
VIP - Donator
VIP - Donator
Posts: 155
Joined: Thu Apr 08, 2010 4:59 pm

Re: Java - another assignment
lolxot
Here is the new Student.java code
Code: Select all
public class Student {
    private String name;
         private int age;
        
         public Student(String newName, int newAge) {
             setName(newName);
             setAge(newAge);
         }
        
         public void setName(String newName) {
             name = newName;
         }
        
         public String getName(){
             return name;
         }
        
         public void setAge(int newAge) {
             if (newAge > 0) {
                 age = newAge;
             } else {
                 System.out.println("Age cannot be negative.");
             }
         }
        
         public int getAge() {
             return age;
         }
        
         public String typeOfStudent() {
            String returnMsg = "invalid";
            if (age >= 0 && age <= 4) {
               returnMsg = "Preschool";
            } else if (age == 5) {
               returnMsg = "Kindergarten";
            } else if (age >= 6 && age <= 10) {
               returnMsg = "Elementary School";
            } else if (age >= 11 && age <= 13) {
               returnMsg = "Middle School";
            } else if (age >= 14 && age <= 17) {
               returnMsg = "High School";
            } else if (age >= 18) {
               returnMsg = "College";
            }
            return returnMsg;
         }
        
         public String fullString() {
          return "Name: " + name + ", Age: " + age;
         }   
 }
with this code you can keep your StudentClient.java class as ist was.
btw the "returnMsg" is just a string which is set to the value that should be returned as you can not give back a return statement in between an if-statement.
User avatar
Jessica
x Girl Power x
x Girl Power x
Posts: 35
Joined: Thu Feb 21, 2013 1:52 am

Re: Java - another assignment
Jessica
that solved it, thanks
5 posts Page 1 of 1
Return to “Misc”