View Single Post
  #4 (permalink)  
Old 01-12-2008, 10:54 AM
CaptainMorgan's Avatar
CaptainMorgan CaptainMorgan is offline
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 839
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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.

Code:
/* 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; } }
Code:
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()); } }
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)

Last edited by CaptainMorgan : 01-12-2008 at 10:57 AM.
Reply With Quote