Hello I am learning Java at my spare time as a hobby. It only been a week and at the moment I am trying to get my head around the following things fields, constructor, accessors etc.


There is an exercise which I ma having trouble with:

Human
1. Create a human class
2. This class has fields to hold name, age, color and temperament.
3. The constructor takes as parameters values for all the fields above and a constructor that takes only a name and sets the age field to 1 and color to blue and temperament to happy
4. Human has methods to set and get each of the field described above and in addition two methods named. Yessss and Nooo which will print out "Yess" and "Nooo" on the terminal window. there is also a method named markeSound will display Yesss if the human is happy or Noooo if he is sad.

this is how far I have progressed, I am not sure If I am doing this properly.

public class Human{
String name, age, colour, temperament;
}

Can someone help me out please.

Thanks in advance

By the way I am using BlueJ

Recommended Answers

All 7 Replies

public class Human {
  private String name = null;
  private int age = 0;
// ....

// constructor
// these arguments are not the same as the ones declared above
public Human(String name, int age, String  colour, String  temperament) {
     // use the set methods to set the values
}

public Human(String name) {
   // do what the exercise describes for this constructor
}

// get method
public String getName() {
    return name;
}

// set method
public void setName(String name) {
   this.name = name;
}
// the name argument is the argument of the method
// this.name is the global class variable you have declared at the beginning of your class
// that is how you distinguish them 

}

If you did something like this:

public void setName(String na) {
   name = na;
}

This time the 'name' variable is the global class variable since there is no other 'name' variable declared locally

public class Human {
public String name = "";
public int age = 0;

public Human(String name, int age, String colour, String temperament){
}

public Human (String name){
this.name = name;
this.age = -1;
this.colour = "blue";
this.temperament = "happy"
}

}

This is how far I have gotten so far. Am I doing this properly

try to make your class vars protected instead of public, and then provide accessor functions to set and get them (e.g. getColor(), setColor(color)). this will make your class more reliable.

We did this sort of thing when extending Pacific Timesheet software (http://www.pacifictimesheet.com) with some additional classes once we bought a source license.

B.S.

public class Human {
public String name = "";
public int age = 0;

public Cat(String name, int age, String colour, String temperament){
}

public Human (String name){
this.name = name;
this.age = 1;
this.colour = colour;
this.disposition = temperament;
}

public void setName (String name){
this.name = name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
public void setColour(String colour){
this.colour =  colour;
}

public String getColour(){
return colour;
}

public void setTemperament(String temperament){
this.disposition = disposition; 
}
public String setTemperament(){
return temperament;
}
public markeSound(){
if (human == "happy"){
System.out.println("Yesss")
else
System.out.println("Noooo")
}
}
}

I finished the code but its doesn't work...What I am doing wrong?

Can someone help

Thanks in advance

you didn't declared the disposition, temperament, and colour variables

you didn't declared the disposition, temperament, and colour variables

Yes that is right. You weren't suppose to just copy my example, but try to understand it.
Since I declared the name and age and used them at the constructor, when you added the rest of the variable you should have followed the same pattern of declaring the rest of the variables as well creating get/set methods as explained.

Also you are using these variables and you do not declare them: disposition, temperament and human. They all represent if his is happy or not.
So decide a name for that characteristic and do what is done for the other variables.

Also don't use the '==' to compare String, but this:

if (temperament.equals("happy")) {

}

Java Addict is absolutely correct.

From online (would credit source, closed the tab accidentally): "Since Strings are objects, the equals(Object) method will return true if two Strings have the same objects. The == operator will only be true if two String references point to the same underlying String object. Hence two Strings representing the same content will be equal when tested by the equals(Object) method, but will only by equal when tested with the == operator if they are actually the same object."

In summary, if you wanted to test if two People objects were the same person, you would probably consider them the same person if they had the same name, social security number, and birthday. You would therefore need to use the equals() method to make this comparison, since using '==' would tell you if those two Java Objects had the same memory address. Similarly, you would probably consider two Strings that both said "Same thing" to be the same String, however, if you tested this with '==', it would only test to see if those Strings were the same Object... and therefore, resided in the same place in your computer's memory. . not to see if they are logically the same String.

commented: Helpful +7
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.