You appeared to make a valid effort, thus I've made the necessary corrections - although do not pass it in as is, you must review and edit where necessary.
In the PersonApp class you did not instantiate an object. This neglect made the constructor and Person class's methods virtually useless. I suggest you review your
object creation and
object use sections in your textbook.
Objects and classes are the heart and soul of Java, you must learn these concepts through and through.
Best of luck! And if you're still not clear, let me know.
** also, after a test run or two, your age calculation is not accurate, but I'll leave that to you.
/*
person
==============
-name: String
-DOB : Integer
-icNo: String
-Gender: String
===============================
+ findAge():int
+ setName(name:String):void
+ setDOB(DOS:Integer):void
+ seticNo(icNo:String):void
+ setGender(gender:String):void
+ getName():String
+ getDOB():integer
+ geticNo():String
+ getGender():String
*/
public class Person{
private String name;
private String DOB;
private String icNo;
private String Gender;
// four param constructor
public Person(String na, String date, String ic, String sex){
name = na;
DOB = date;
icNo = ic;
Gender = sex;
}
// setters
public void setName(String na){
name = na;
}
public void setDOB(String date){
DOB = date;
}
public void seticNo(String ic){
icNo = ic;
}
public void setGender(String sex){
Gender = sex;
}
// getters
public String getName(){
return name;
}
public String getDOB(){
return DOB;
}
public String geticNo(){
return icNo;
}
public String getGender(){
return Gender;
}
// age calculator
public int findAge() {
String b = icNo.substring(1,3);
int c = Integer.parseInt(b);
c = 1900 + c;
int age = 2008 - c ;
return age;
}
}
import java.util.Scanner;
public class PersonApp {
public static void main(String args[]){
String name, dob, gender, icNo;
Scanner input = new Scanner(System.in);
System.out.print("Enter your name :");
name = input.nextLine ();
System.out.print("Enter your date of birth (dd/mm/yyyy) :");
dob = input.nextLine ();
System.out.print("Enter your gender :");
gender = input.nextLine ();
System.out.print("Enter your IC number :");
icNo = input.nextLine ();
// *** instantiate an object! ***
// *** this is why we created the constructor ***
Person p = new Person(name, dob, icNo, gender);
// *** now we can use it!!
System.out.println(p.getName() + " is " + p.findAge() + " years old. ");
System.out.println("Gender is " + p.getGender() + " and IC number is " + p.geticNo());
}
}