Not sure where/what my error is.
I have two classes: Person and PersonDemo.
Person holds all the methods, etc, etc, whilst PersonDemo is as named, just for a demo.
However, PersonDemo throws up a lot of errors in my Eclipse console, but won't
Code Java:
public class Person {
String forename;
String surname;
int age;
public String getName(Person nameToGet) {
return forename + surname;
}
public Person(Person ID, String createdForename, String createdSurname, int createdAge) {
this.forename = createdForename;
this.surname = createdSurname;
this.age = createdAge;
}
public void printPersonsInfo(Person infoToGet) {
System.out.println("Name = "+getName(infoToGet)+"; Age = " + infoToGet.age);
}
}
^No errors in that class.
Code Java:
public class PersonDemo {
Person(johnSmith001, "John", "Smith", 43);//Error here, on "Person("
public static void main(String args[]) {
printPersonsInfo(johnSmith001);//Error here, on "johnSmith001"
}
}
^I get errors on lines 2 and 5.
Upon running the demo, "Name = John Smith; Age = 43" should be printed into the console.
Re: Not sure where/what my error is.
Move the first line inside of main. Then at the beginning of that line, declare a Person variable, and add the new keyword (because that is how you create new objects when calling constructors)
Person myPerson = new (what you already have goes here).
Then when you call printPersonsInfo, you need to use your reference to the Person object you just created
myPerson.printPersonsInfo(...)
Re: Not sure where/what my error is.
Person(johnSmith001, "John", "Smith", 43);//Error here, on "Person("
Person p = new Person(Person x, "John", "Smith", 43);
Ok, you don't have a variable called ID.
Re: Not sure where/what my error is.
Code java:
public class Person {
String forename;
String surname;
int age;
public String getName(Person nameToGet) {
return forename + " " + surname;
}
public Person(String createdForename, String createdSurname, int createdAge) {
this.forename = createdForename;
this.surname = createdSurname;
this.age = createdAge;
}
public void printPersonsInfo() {
System.out.println("Name = "+getName(this)+"; Age = " + this.age);
}
}
Code java:
public class PersonDemo {
public static void main(String args[]) {
Person johnSmith001 = new Person("John", "Smith", 43);
johnSmith001.printPersonsInfo();
}
}
Now if I was to create a random Person object (via the program) how would I do it? (I wanted the object's name to derive from their first name and last name.
Re: Not sure where/what my error is.
Your methods don't need to take in a Person object as an argument...... They already have a reference to the person in the "this" variable (or you can just call getName(), and directly access the age variable
What exactly do you mean by a random person?
Re: Not sure where/what my error is.
Let's see what this does:
Code java:
public class Person {
private String forename;
private String surname;
private int age;
private String personID;
private String name;
public Person(String personID, String createdForename, String createdSurname, int createdAge) {
this.forename = createdForename;
this.surname = createdSurname;
this.age = createdAge;
this.personID = personID;
setAge(createdAge);
setPersonID(personID);
setName(createdForename, createdSurname);
}
public void setPersonID(String personID2)
{
personID = personID2;
}
public String getPersonID()
{
return personID;
}
public void setForename(String forename2)
{
forename = forename2;
}
public String getForename()
{
return forename;
}
public void setSurname(String surname2)
{
surname = surname2;
}
public String getSurname()
{
return surname;
}
public void setAge (int age2)
{
age = age2;
}
public int getAge()
{
return age;
}
public void setName(String aForename, String aSurname)
{
name = aForename + " " + aSurname;
setForename(aForename);
setSurname(aSurname);
}
public void setName2()
{
name = getForeName() + " " + getSurname();
}
public String getName() {
return name;
}
public void printPersonsInfo() {
System.out.println("Name = "+getName()+ ";
Age = " + infoToGet.age);
}
}
Code java:
public class PersonDemo {
public static void main(String args[]) {
Person p = new Person("johnSmith001", "John", "Smith", 43);
p.printPersonsInfo();
}
}
Re: Not sure where/what my error is.
Thankyou Javapenguin! It worked, with a bit of tweaking!
Re: Not sure where/what my error is.
Do you have any idea how I could list all of the people created so far?
Re: Not sure where/what my error is.
The people's name or the Person themselves?
There is a class called ArrayList<aClass> which works like an array in that it uses arrays internally but creates new arrays when it exceeds a certain size so that it can expand.
Assuming you want the names, you could do this(remember to import java.util.*)
Code java:
ArrayList<String> aList = new ArrayList<String>();
// for every person do this(remember index starts at 0)
String str = p.getName();
aList.add( index, str);
// when you are done
String str2 = aList.toString();
System.out.println(str2);
// that should get you a list, separated by commas, of all the names of the people you've added
// but they won't be in alphabetical order unless you add them all that way
Re: Not sure where/what my error is.
If you do want alphabetical order, and as long as you don't have duplicates, you can
Code java:
TreeSet<String> tSet = new TreeSet<String>();
// for each person
String str = p.getName();
tSet.add(str);
// when done
String str2 = tSet.toString();
System.out.println(str2);
instead, but you cannot have duplicates.
Re: Not sure where/what my error is.
For the second one, where would I add that? When the person is created?
Silly me, I see now :)
Thanks, I'll test it now!