TurkuBasicOOPinJava / Week 4: Writing classes /01b. Three objects [FROM CLASS CREATE OBJECTS]
KaiquanMah's picture
ClassName variableName = new ClassName("str1", "str2", int3);
4a2a54d verified
A category called Nephew is defined in the programme.
The class has a constructor which receives the following parameters in order:
name (string)
hat colour (string)
height (integer)
Create three objects from the class with the following information:
name: Dewey, hat color: blue
name: Huey, hat colour: green
name: Louie, hat colour: red
All objects have a length of 95.
Save the references of the objects to the variables dewey, huey and louie.
import java.util.Random;
public class Test {
public static void main(String[] args) {
System.out.println("Testing object creation...");
//ADD
Nephew dewey = new Nephew("Dewey", "blue", 95);
Nephew huey = new Nephew("Huey", "green", 95);
Nephew louie = new Nephew("Louie", "red", 95);
System.out.println("Dewey: " + dewey);
System.out.println("Huey: " + huey);
System.out.println("Louie: " + louie);
}
}
class Nephew {
private String name;
private String hatColor;
private int height;
public Nephew(String name, String hatColor, int height) {
this.name = name;
this.hatColor = hatColor;
this.height = height;
}
@Override
public String toString() {
return "Nephew [name=" + name + ", hat color=" + hatColor + ", height=" + height + "]";
}
}
Testing object creation...
Dewey: Nephew [name=Dewey, hat color=blue, height=95]
Huey: Nephew [name=Huey, hat color=green, height=95]
Louie: Nephew [name=Louie, hat color=red, height=95]