aspose file tools*
The moose likes Beginning Java and the fly likes Simple Person Program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simple Person Program" Watch "Simple Person Program" New topic
Author

Simple Person Program

Mark Adams
Greenhorn

Joined: Nov 23, 2007
Posts: 16
Please help, I am trying to write a simple program to ask for and display a persons first and last name and their age. This is all I have so far. I am sure that I am way off but just need some advice on where to go from here. Any help would be greatly appreciated.

Thanks,

Mark

// user-defined class Person

public class Person
{
// instance variables
private int age;
private String lastName;
private String firstName;

// defined constructor
public Person (String _firstName, String _lastName, int _age)
{


public int getAge() {
return age;
}

public void printAge(int _age) {
this.age = age;
}



// method to get first name
public String getFirstName()
{
return firstName; // show first name
} // end method getFirstName

// method to set first name
public void setFirstName(String _firstName)

{
this.firstName = firstName;
}

public String getLastName()
{
return lastName;
}

public void printName(String _lastName)
{
this.lastName = lastName;
}


}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42931
    
  68
Welcome to JavaRanch.

What do you mean by "ask" - having the user input the data on the command line? If so, you can use the java.io.Console class to read the input. If you're using an earlier JDK (which doesn't have this class) you'll need to use file I/O to read from System.in.
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1367
    
  18
It looks like a good start.

Let's take it one step at a time...


public class Person
{
// instance variables
private int age;
private String lastName;
private String firstName;

// defined constructor
public Person (String _firstName, String _lastName, int _age)
{


What code do you need to put inside the constructor so that it sets the variables? Give us your best suggestion, and we'll help you out if you get stuck.

You'll also need to think about a couple of other things:

Your program is going to ask for the name and age. You've only defined one constructor, and this constructor requires all the information up front.

If you are going to go down this path, you will need to save the values provided, and then create your person object with those values.

Then you won't need the "setter" methods that you have provided (unless the person changes their name).

If you want to keep the option open of creating a person even if you don't have all the information, then you will need to specify a second constructor that doesn't take any parameters.

Or... you could remove the constructor altogether (which would basically mean that a constructor is made for you. One which doesn't take any parameters).





The getAge() method will do what you need, but there are a couple of things to consider with printAge().
First, I would suggest a name that is more descriptive of what it does, such as setAge(), so as not to confuse your reader.
Second, your parameter is called _age. You will need to use the same name when setting the variable:






return firstName; is not going to show it. If you want to show it, you will have to print it out, for example using






Here you will run into the same problem as with the age. You need to set





As before, you aren't printing, you are setting, and you need to set the variable using the parameter that you are passing in.

Want to clean it up and we'll take it from there?

Is your program going to actually ask for the information, or can the name and age be specified from the command line when you run the program?
Mark Adams
Greenhorn

Joined: Nov 23, 2007
Posts: 16
Wow, you guys are great!!!

I don't have much programming experience however, I think I am starting to understand some of the concept. I get most confused with the formatting. I know what I want to do but I just can't seem to understand the format and how to use it. It's just a matter of time before some of the "I get it" kicks in,

How about
{
Firstname = firstName;
}
Mark Adams
Greenhorn

Joined: Nov 23, 2007
Posts: 16
public void setFirstname ( string name )
{
Firstname = name;
}
public string getFirstname()
{
return firstName;
}
Nina Niskanen
Greenhorn

Joined: Nov 03, 2007
Posts: 8
Originally posted by Mark Adams:
public void setFirstname ( string name )
{
Firstname = name;
}
public string getFirstname()
{
return firstName;
}


Hi Mark!

The problem with the code above is that you are setting the name to variable Firstname and returning it from a completely different variable firstName. In Java uppercase and lowercase letters matter.

Starting a name with an uppercase letter is also usually used only for classes.

If you change the variable you use in the setter to the same one that you use in the getter, you'll get on much better


<a href="http://faq.javaranch.com/java/HowToAskQuestionsOnJavaRanch" target="_blank" rel="nofollow">How To Ask Questions On Java Ranch</a>
Mark Adams
Greenhorn

Joined: Nov 23, 2007
Posts: 16
quote:
--------------------------------------------------------------------------------

public class Person
{
// instance variables
private int age;
private String lastName;
private String firstName;

// defined constructor
public Person (String _firstName, String _lastName, int _age)
{

--------------------------------------------------------------------------------
//constructor initializes first name

public _firstName ( String name )
{
_firstName = name; //initialize first name
}
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2516
    
  10

//constructor initializes first name

public _firstName ( String name )
{
_firstName = name; //initialize first name
}
This is not a constructor. It's a method.

You use an unusual style to name your variables and methods.
Have a look at Code Conventions for the Java Programming Language, and JavaBean conventions.

The code could look like:Regards, Jan
[ November 24, 2007: Message edited by: Jan Cumps ]

OCUP UML fundamental and ITIL foundation
youtube channel
Mark Adams
Greenhorn

Joined: Nov 23, 2007
Posts: 16
Thank you, I really think I just need some simple (smaller program)examples to follow to try and see everything that takes place.
It seems like it just goes from "Hello world!!!" to "What the #@!!".

Does this look like anything I can work with?
I think I will just try to start with having the program say
Hello, What is your first name?
Then
Hello!!
Please enter your first name.

But I cant even get this to work...

import java.util.Scanner;

public class Person
{

// main execute
public static void main( String args[] )
{
// create Scanner
Scanner input = new Scanner( System.in );

// setter method to create first name object


public void setFirstName ( string firstName )
{
this.firstName = firstName;
}


System.out.printf( "Hello!! %s\n\n",
firstName.getFirstName() );


// display welcome, get first name

System.out.println( "Welcome, Please enter your First name:" );

// read line
String theName = input.nextLine();

// set name
firstName.setFirstName( theName );
System.out.println();



} // end display


}
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1367
    
  18
Maybe you could break it down even more, and ignore the whole input thing. Instead of having your program ask for the name, just run it with three arguments:



and then grab these inside your main() method like this:



Then, once you have figured out how to create your person and print the name, you can start worrying about the input.
[ November 24, 2007: Message edited by: Katrina Owen ]
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1367
    
  18
Oh, and I'm completely biased - but you could always have a look at the assignments in the JavaRanch CattleDrive:
http://www.javaranch.com/java-college.jsp

They sort of bridge the gap between Hello World, and #@$%/%^
Mark Adams
Greenhorn

Joined: Nov 23, 2007
Posts: 16
Thank you, I will check it out.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple Person Program