I had an assignment that goes like this:
-----------------------------------------------------------------
Write a class named Person containing the following instance variables with suitable types:
name, personal number, address, age
The following methods must be included in the class:
the constructor Person that initiates all instance variables
changeName that changes the name using a parameter
changeAddress that changes the address using a parameter
incrementAge that adds one to the current age
getName that returns the name
getPerNo that returns the personal number
getAge that returns the age
getAddress that returns the address
toString that returns a nicely formatted string containing all the data contained in a Person object. The method is called by using the objects (or reference) name.
Write a test program that tests all of the methods in the class Person. The tests must follow each other in this order:
Create two Person objects; person1 and person2. The user must type in the starting values for all the data for each object.
Print out all data for both objects.
Allow the user to change the name and address of person1.
Increment the age of person2.
-----------------------------------------------------------------------------------------------------
I wrote some code that works, but i'm not sure if it's the right way. What can i improve in it?
-----------------------------------------------------------------------------------------------------
[CODE] package javaLab_4; import java.util.Scanner; public class Person { public String name; public String adress; public int age; public long personalNumber; //Objects constructor with parameters: public Person(String personName, String personAdress, int personAge, long personPerNum){ Scanner userInput = new Scanner(System.in); System.out.println("\n>Type the name: "); personName = userInput.nextLine(); name = personName; //name = userInput.nextLine(); System.out.println(">Type the adress: "); personAdress = userInput.nextLine(); adress = personAdress; //or adress = userInput.nextLine(); System.out.println(">Type in the age: "); personAge = userInput.nextInt(); age = personAge; //or age = userInput.nextInt(); System.out.println(">Type in the personal number: "); personPerNum = userInput.nextLong(); personalNumber = personPerNum; //or personalNumber = userInput.nextLong(); }//End of Person Constructor. public void changeName(String newName){ Scanner userInput = new Scanner(System.in); System.out.println("\n>Enter a new name: "); newName = userInput.nextLine(); name = newName; }//End of changeName method. public void changeAdress(String newAdress){ Scanner userInput = new Scanner(System.in); System.out.println("\n>Enter a new adress: "); newAdress = userInput.nextLine(); adress = newAdress; }//End of changeAdress method. public void incrementAge(){ age++; }//End of incrementAge method. public String getName(){ return "Name: "+name; }//End of getName method. public String getPerNo(){ return "Personal number: "+personalNumber; }//End of getperNo method. public String getAge(){ return "Age: "+age; }//End of getAge method. public String getAdress(){ return "Adress: "+adress; }//End of getAdress method. public String toString(){ return "\n>Credentials:\n--------------------------" + "\n-Name: "+name+"\n-Adress: "+ adress+"\n-Age: "+age+"\n-Personal number: "+personalNumber+"\n"; } } [/CODE]
Test:
---------------------------------------------------------------------------------------------------------------------------------------[CODE] package javaLab_4; public class PersonTest { public static void main(String[] args) { Person person1 = new Person(null, null, 0, 0); Person person2 = new Person(null, null, 0, 0); System.out.println(person1.toString()); System.out.println(person2.toString()); person1.changeName(null); person1.changeAdress(null); person2.incrementAge(); //Person1: System.out.println(person1.getName()+"\n"+person1.getPerNo()+"\n"+person1.getAdress()+"\n-----------------"); //Person2: System.out.println(person2.getName()+"\n"+person2.getPerNo()+"\n"+person2.getAge()); } } [/CODE]
My thoughts are:
-In the test class, i had to use null pointers to fill out the parameters, can it be done differently.
-When using the queries, i had to put them in System.out.print statements to see them. Why can't i just invoke the method on it's own and still see it on the screen?
PS: i'm using Eclipse Helios to compile and run all my java classes. If i try in command (cmd) i get an error (Symbol not found).