In BlueJay , are you using the Windows or Mac Version ?
Either way I will go through the Mac version and hope it works.
Create a New Class and enter Person as the class.
Bring up the editor window by Right-Clicking on the class and clicking Open Editor.
Your class should have the following format.
/*** Write a description of class Person here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Person
{
// instance variables - replace the example below with your own
private int x;
/**
* Constructor for objects of class Person
*/
public Person()
{
// initialise instance variables
x = 0;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public int sampleMethod(int y)
{
// put your code here
return x + y;
}
}
The first requirement you stated is
- Its constructor receives 3 parameters, two Strings representing first and last names and an int representing age
Which you did well with the code
public Person(String firstName, String lastName, int age)
So no changes there. :D
NOT
public Person(String first, String last, int age)
[We could use this instead but lets not confuse ourselves it seems the person wanted you to use the first one]
Though we want this class to get these 3 parameters so we need to
1)have private variables to hold these values
2) assign them within the constructor.
Looking back at what BlueJ gave with these lines
// instance variables - replace the example below with your own
private int x;
/**
* Constructor for objects of class Person
*/
public Person()
{
// initialise instance variables
x = 0;
}
We want to place in your change in the constructor and the additional changes 1) and 2)
// instance variables - replace the example below with your own
// private int x; replace blueJ sample variable
private String firstName; //not the same as the one given in the constructor
private String lastName; //not the same as the one given in the constructor
private int age; //not the same as the one given in the constructor
/**
* Constructor for objects of class Person
*/
public Person(String firstName, String lastName, int age)
{
// initialise instance variables
//x = 0; replace BlueJ sample with
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
Ok so just breathe and watch it. We have two sets of each variable !!! Well one is for the class and the other is what is being supplied to the constructor. To explain everything now is just too many lines :( , so you will need to read a Java book [Just the first few chapters ... read it while watching season 1 of 24], a majority of Books comes with this Person class example.
Alright so killed the first requirement. Lets look at the second one.
-its method getName has no parameters and returns a String with format "Lastname, Firstname"
So its a method, for the sake of brevity , jump to this section
http://java.sun.com/docs/books/tutorial/java/javaOO/methods.html
It will explain it a lot better then if I were to do it. OK, I will give you some time.
Did you read it ? No ? Go read it Grr! :( It is important
Ok so now that you have done that the following should make sense to you.
/**
* The getName Method - put in a little description here
*
* @return the String with the format "Lastname, Firstname"
*/
public String getName()
{
// put your code here
// I did and this is called string concatenation in java
// Google it:"string concatenation in java"
return this.lastName + ", " + this.firstName;
}
So I made a method similar to the one BlueJ made and this how it looked. It gets the names (this.lastName and this.firstName NOT lastName and firstName which would work as well but lets not confuse ourselves k?)
The full code with in BlueJ
/**
* Write a description of class Person here.
*
* @user208639 (Is that your real name ?)
* @version (a version number or a date)
*/
public class Person
{
// instance variables - replace the example below with your own
// private int x; replace blueJ sample variable
private String firstName;
private String lastName;
private int age;
/**
* Constructor for objects of class Person
*/
public Person(String firstName, String lastName, int age)
{
// initialise instance variables
//x = 0; replace BlueJ sample with
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
/**
* The getName Method - put in a little description here
*
* @return the String with the format "Lastname, Firstname"
*/
public String getName()
{
// put your code here
// I did and this is called string concatenation in java
// Google it:"string concatenation in java"
return this.lastName + ", " + this.firstName;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public int sampleMethod(int y)
{
// put your code here
return x + y;
}
}
Are you going about it the right way ?
Sort of ? If you did not know anything about coding,variables,data types and methods I would say it was a fair guess :) ... but you should really read a Java Intro book.
Right program ? naw... This BlueJ program is weird.
Google for "NetBeans" it is free.
Ok it is way past breakfast time on the West Coast.