• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Simple Person Program

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}


}
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public void setFirstname ( string name )
{
Firstname = name;
}
public string getFirstname()
{
return firstName;
}
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Mark Adams
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
}
 
Bartender
Posts: 2661
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

//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 ]
 
Mark Adams
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1367
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1367
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, I will check it out.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic