A. Notes
In the following class, you can instantiate an object without declaring a constructor. This is because Java always provide a default constructor which takes no parameters and it sets all instance variables to the default values ("null" for strings, "0" for int, "0.0" for double, "false" for boolean)
class Main {
public static void main(String[] args) {
Fish a = new Fish(); // Calling the default constructor to create an object
}
}
class Fish {
private String name;
private String color;
private double weight;
private int lifespan;
private String watertype;
private double price;
public boolean isPopular(){
if(price>5 && price <10 && (color.equals("red")|| color.equals("orange"))){
return true;
}
else return false;
}
}
2. But if you declare a constructor with parameters to initialize the instance variables, the default constructor is removed and thus can not be called anymore.
class Main {
public static void main(String[] args) {
Fish a = new Fish(); // This line will generate a compilation error now
}
}
class Fish {
private String name;
private String color;
private double weight;
private int lifespan;
private String watertype;
private double price;
Fish(String name, String color, double weight, int lifespan, String watertype, double price){
this.name=name;
this.color=color;
this.weight=weight;
this.lifespan=lifespan;
this.watertype=watertype;
this.price=price;
}
public boolean isPopular(){
if(price>5 && price <10 && (color.equals("red")|| color.equals("orange"))){
return true;
}
else return false;
}
}
3. More than often, we do not have all the initial values when instantiating an object, so we might need multiple constructors so we can create objects no matter what. This is called constructor overloading.
class Main {
public static void main(String[] args) {
Table one = new Table();
Table mytable = new Table("The color is red", 123);
Table two = new Table("Made of oak", "rectangular", 500);
}
class Table {
private String material;
private String type;
private String color;
private double price;
private double length;
private double width;
private double height;
public Table() {} // This is necessary even the constructor doesn't do anything
public Table(String color, double width) {
this.color = color;
this.width = width;
}
public Table(String material, String type, double price) {
this.material = material;
this.type = type;
this.price = price;
}
4. Not only constructors, any method can be overloaded. Method overloading means a method can be defined multiple times with different number of parameters or return types.
5. The keyword "this"
The parameters in the constructors are used to initialize the corresponding instance variables so naturally they might have the same name. But sometimes it might be confusing to write:
public Table(String color, double width) {
color = color;
width = width;
}
Since color is both the name of the instance variable and the parameter. To clarify, we use this.color to indicate the instance variable. "this" refers to the object that is created by calling the constructor, thus this.color refers to the instance variable color of the object. All other instance variables can be indicated similarly.
B. HW
Design two classes Money and BankAccount to manage international currency deposits and withdraws. Design instance variables, methods, static variables, methods and overloaded constructors of each class. In Main class, create 3 objects of each class by calling different constructors.
Design a class Point and a class Triangle as described below. You only need to implement the constructors, and use the constructors to create 2 triangles.