9 Replies - 450 Views - Last Post: 30 November 2012 - 05:36 AM Rate Topic: -----

#1 applemad20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-November 12

Task To Create A Person Class Then Make person objects

Posted 26 November 2012 - 10:07 AM

Ive created a Person class, The Class will hold the following information:
  • forename
  • surname
  • age
  • height (in metres)
  • gender


These variables will be stroed using Private variables. Here Is My Code For the Person class so far:


public class Person
{
	private String foreName;
	private String surName;
	private int age;
	private double height;
	private String gender;
	


	
	public Person(String foreName, String surName, int age, double height, String gender)
	{
		this.foreName = foreName;
		this.surName = surName;
		this.age = age;
		this.height = height;
		this.gender = gender;
		
		
	}
	
	public String toString()
	{
		return "Person[foreName = " + foreName + "]";
	}
	



Now i need to add appropriate get and set methods for each variable, How would i go about on doing this?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Task To Create A Person Class Then Make person objects

#2 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

Re: Task To Create A Person Class Then Make person objects

Posted 26 November 2012 - 10:14 AM

well i guess you don't understand what get and set methods actually to do. Well basically the idea is very simple. With a set method you set a value to a private variable and with get you retrieve its value.

for example in your code if you were to write the get and set methods for the private variable foreName

public String getForeName(){
return foreName;
}

public void setForeName(String foreName){
this.foreName = foreName;}



Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2555
  • View blog
  • Posts: 10,655
  • Joined: 20-September 08

Re: Task To Create A Person Class Then Make person objects

Posted 26 November 2012 - 10:24 AM

Surname and forename are one word each btw, so don't need to be in camel case
Was This Post Helpful? 0
  • +
  • -

#4 applemad20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-November 12

Re: Task To Create A Person Class Then Make person objects

Posted 26 November 2012 - 12:15 PM

I need to write a format mathod which will print out the variable data using the format method.

The data must be printed in a table tabbed way like this:

Sam Jones 56 2.4 Male
Sally
Was This Post Helpful? 0
  • +
  • -

#5 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: Task To Create A Person Class Then Make person objects

Posted 26 November 2012 - 12:27 PM

You would handle that in your overriden toString method:
You would simply print out the object's attributes in the correct formatting so for example:

public String toString() {
System.out.println(foreName + " " + surName + " " + age + " " + height + " " + gender;
}



Remember to parse the integer variable age (not done in solution above, I am sure you can figure it out :) )
Was This Post Helpful? 0
  • +
  • -

#6 applemad20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-November 12

Re: Task To Create A Person Class Then Make person objects

Posted 26 November 2012 - 12:47 PM

i Have to write a format()method for which constructs a string containing formatting information such that when the string is printed the data values are neatly aligned in columns and thus suitable for screen display similar to the following.

Joe Williams 67 7.2 male
Sally Roberts 45 2.3 female
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Task To Create A Person Class Then Make person objects

Posted 26 November 2012 - 12:56 PM

Use printf() using %Ns format where N as a value like %20s for the String and %5.2f for the double

if it is for the toString() method use String.format("%10s %10s %2d %5.2f %8s", ...

This post has been edited by pbl: 26 November 2012 - 01:04 PM

Was This Post Helpful? 0
  • +
  • -

#8 applemad20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-November 12

Re: Task To Create A Person Class Then Make person objects

Posted 29 November 2012 - 03:17 AM

With the String.format("%10s %10s %2d %5.2f %8s",.....,.....,.....,.....,.....);

It does the columbs does anyone know how i can get them to centre align?

This is the output i get when i run the program:

Posted Image

This is the code i have for my method:

public String format()
	{
    return String.format("%10s %10s %10d %10.2f %10s", getForeName() , getSurName() , getAge() , getHeight() , getGender());
	}


Thanks
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Task To Create A Person Class Then Make person objects

Posted 29 November 2012 - 05:14 PM

"%-10s" would left aligned

Center aligned would be a bit more complicated, you will to format() it on a String using a simple %s then to prefix and append a " " until the desired length is obtained
Was This Post Helpful? 0
  • +
  • -

#10 applemad20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-November 12

Re: Task To Create A Person Class Then Make person objects

Posted 30 November 2012 - 05:36 AM

SOLVED NOW Thanks......... :clap:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1