/home/caleb/ASDV-Java/Semester 2/Assignments/lab8_CalebFontenot/src/main/java/com/calebfontenot/lab8_calebfontenot/House.java |
nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt
nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java
package com.calebfontenot.lab8_calebfontenot;
import java.util.Date;
@author
public class House implements Cloneable{
private int id;
private double area;
private Date whenBuilt;
public House(int id, double area)
{
this.id = id;
this.area = area;
this.whenBuilt = new Date();
}
@Override
protected Object clone() throws CloneNotSupportedException
{
Object o = super.clone();
((House) o).whenBuilt = new Date(this.whenBuilt.getTime());
return o;
}
@return
public Date getWhenBuilt()
{
return whenBuilt;
}
@param whenBuilt
public void setWhenBuilt(Date whenBuilt)
{
this.whenBuilt = whenBuilt;
}
@return
public double getArea()
{
return area;
}
@param area
public void setArea(double area)
{
this.area = area;
}
@return
public int getId()
{
return id;
}
@param id
public void setId(int id)
{
this.id = id;
}
@Override
public String toString()
{
return "House{" + "id=" + id + ", area=" + area + ", whenBuilt=" + whenBuilt + '}';
}
public int compareTo(House o) {
if (this.getId() == o.getId() &&
this.getArea() == o.getArea() &&
this.getWhenBuilt().equals(o.getWhenBuilt())) {
return 0;
}
if (this.area > o.area) {
return 1;
} else if (this.area < o.area) {
return -1;
} else {
return -2;
}
}
public static void main(String[] args) throws CloneNotSupportedException, InterruptedException {
House house1 = new House(1, 1000);
House house2 = (House) house1.clone();
house2.whenBuilt.setDate(8);
System.out.println(house1);
System.out.println(house2);
House house3 = new House(3, 2000);
House house4 = new House(4, 2000);
System.out.println("house 1 and house 2 are identical: "+ house1.compareTo(house2));
System.out.println("house 3 and house 3 have the same area: " + house3.compareTo(house4));
System.out.println("House 4 is harger than house 2: " + house4.compareTo(house2));
}
}