Welcome to Dream.In.Code
Become a Java Expert!

Join 150,118 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,965 people online right now. Registration is fast and FREE... Join Now!




Person Class

 
Reply to this topicStart new topic

Person Class, Two Classes

vbstar
23 Jun, 2008 - 05:00 PM
Post #1

New D.I.C Head
*

Joined: 6 Jun, 2008
Posts: 9

Here is what I have to do;

I need to know how to write two classes one a user defined class called Person with private instance variables defined as;

private int age;
private String firstName;
private string lastName;

the class needs a constructor;

public Person(String_firstName, String_lastName, int_age)

that assigns values to the private instance variables. The class should also include public methods;

public void printName() - prints the persons name
public void printAge()
public void printAgeGroup()

The second class with the method, "public static void main(String args[])". At least two objects based on the person class should be created in the "main" method. The name, age, and age group should be displayed for each object.



Here is what I have so far but it is not working can anyone help me with this?

public class Person
{
private int age;
private String firstName;
private String lastName;
private String nameString;
private String ageString;

public Person(String firstName, String lastName, int age)
{
this.age = age;
this.firstName = firstName;
this.lastName = lastName;
}

public void printName()
{
System.out.println(nameString);
}

public void printAge()
{
System.out.println(ageString);
}

public void printAgeGroup()
{
System.out.println(nameString);
System.out.println(ageString);
}


}

and this but it prints null to the screen instead of the names and ages

public class TestPerson
{
public static void main(String[] args)
{
Person person1 = new Person("John", "Smith", 32);
Person person2 = new Person("Jane", "Doe", 25);
person1.printName();
person1.printAge();
person1.printAgeGroup();

person2.printName();
person2.printAge();
person2.printAgeGroup();

}
}

User is offlineProfile CardPM
+Quote Post

pbl
RE: Person Class
23 Jun, 2008 - 05:51 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Please post your code like this: code.gif

you never assign a value to nameString

2 alternatives:

you assign it in the constructor

CODE

public Person(String firstName, String lastName, int age)
{
this.age = age;
this.firstName = firstName;
this.lastName = lastName;
nameString = firstName + " " + lastName;
}


or your forget your nameString variable and build it on request

CODE

public void printName()
{
System.out.println(firstName + " " + lastName);
}


As far as ageString is concerned you never assign a value to it


User is offlineProfile CardPM
+Quote Post

vbstar
RE: Person Class
24 Jun, 2008 - 07:27 PM
Post #3

New D.I.C Head
*

Joined: 6 Jun, 2008
Posts: 9


Thank You very much for your help



QUOTE(vbstar @ 23 Jun, 2008 - 06:00 PM) *

Here is what I have to do;

I need to know how to write two classes one a user defined class called Person with private instance variables defined as;

private int age;
private String firstName;
private string lastName;

the class needs a constructor;

public Person(String_firstName, String_lastName, int_age)

that assigns values to the private instance variables. The class should also include public methods;

public void printName() - prints the persons name
public void printAge()
public void printAgeGroup()

The second class with the method, "public static void main(String args[])". At least two objects based on the person class should be created in the "main" method. The name, age, and age group should be displayed for each object.



Here is what I have so far but it is not working can anyone help me with this?

public class Person
{
private int age;
private String firstName;
private String lastName;
private String nameString;
private String ageString;

public Person(String firstName, String lastName, int age)
{
this.age = age;
this.firstName = firstName;
this.lastName = lastName;
}

public void printName()
{
System.out.println(nameString);
}

public void printAge()
{
System.out.println(ageString);
}

public void printAgeGroup()
{
System.out.println(nameString);
System.out.println(ageString);
}


}

and this but it prints null to the screen instead of the names and ages

public class TestPerson
{
public static void main(String[] args)
{
Person person1 = new Person("John", "Smith", 32);
Person person2 = new Person("Jane", "Doe", 25);
person1.printName();
person1.printAge();
person1.printAgeGroup();

person2.printName();
person2.printAge();
person2.printAgeGroup();

}
}


User is offlineProfile CardPM
+Quote Post

pbl
RE: Person Class
24 Jun, 2008 - 07:36 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(vbstar @ 24 Jun, 2008 - 08:27 PM) *

Thank You very much for your help


No problem.. happy coding

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:17AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month