165 lines
11 KiB
HTML
165 lines
11 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
<html>
|
|
<head>
|
|
<title>House.java</title>
|
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
|
<style type="text/css">
|
|
<!--
|
|
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
|
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
|
table {color: #888888; background-color: #313335; font-family: monospace}
|
|
.ST2 {color: #9876aa}
|
|
.ST3 {color: #ffc66d}
|
|
.string {color: #6a8759}
|
|
.number {color: #6897bb}
|
|
.comment {color: #808080}
|
|
.whitespace {color: #505050}
|
|
.ST5 {color: #ffc66d; font-family: monospace; font-style: italic}
|
|
.ST6 {color: #9876aa; font-family: monospace; font-style: italic}
|
|
.ST1 {color: #808080; font-family: monospace; font-weight: bold}
|
|
.ST4 {color: #8a653b}
|
|
.ST0 {color: #287bde}
|
|
.literal {color: #cc7832}
|
|
-->
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 2/Assignments/lab8_CalebFontenot/src/main/java/com/calebfontenot/lab8_calebfontenot/House.java</td></tr></table>
|
|
<pre>
|
|
<span class="comment">/*</span>
|
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
|
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
|
|
<span class="comment"> */</span>
|
|
<span class="literal">package</span> com.calebfontenot.lab8_calebfontenot;
|
|
|
|
<span class="literal">import</span> java.util.Date;
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">caleb</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">class</span> House <span class="literal">implements</span> Cloneable{
|
|
|
|
<span class="literal">private</span> <span class="literal">int</span> <span class="ST2">id</span>;
|
|
<span class="literal">private</span> <span class="literal">double</span> <span class="ST2">area</span>;
|
|
<span class="literal">private</span> Date <span class="ST2">whenBuilt</span>;
|
|
|
|
<span class="literal">public</span> House(<span class="literal">int</span> id, <span class="literal">double</span> area)
|
|
{
|
|
<span class="literal">this</span>.<span class="ST2">id</span> = id;
|
|
<span class="literal">this</span>.<span class="ST2">area</span> = area;
|
|
<span class="literal">this</span>.<span class="ST2">whenBuilt</span> = <span class="literal">new</span> Date();
|
|
}
|
|
|
|
@Override
|
|
<span class="literal">protected</span> Object <span class="ST3">clone</span>() <span class="literal">throws</span> CloneNotSupportedException
|
|
{
|
|
Object o = <span class="literal">super</span>.clone();
|
|
((House) o).<span class="ST2">whenBuilt</span> = <span class="literal">new</span> Date(<span class="literal">t</span><span class="literal">his</span>.<span class="ST2">whenBuilt</span>.getTime());
|
|
<span class="literal">return</span> o;
|
|
}
|
|
|
|
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> * </span><span class="comment">Get</span> <span class="comment">the</span> <span class="comment">value</span> <span class="comment">of</span> <span class="comment">whenBuilt</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="ST1">@return</span> <span class="comment">the</span> <span class="comment">value</span> <span class="comment">of</span> <span class="comment">whenBuilt</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> Date <span class="ST3">getWhenBuilt</span>()
|
|
{
|
|
<span class="literal">return</span> <span class="ST2">whenBuilt</span>;
|
|
}
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> * </span><span class="comment">Set</span> <span class="comment">the</span> <span class="comment">value</span> <span class="comment">of</span> <span class="comment">whenBuilt</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="ST1">@param</span> <span class="ST4">whenBuilt</span> <span class="comment">new</span> <span class="comment">value</span> <span class="comment">of</span> <span class="comment">whenBuilt</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">void</span> <span class="ST3">setWhenBuilt</span>(Date whenBuilt)
|
|
{
|
|
<span class="literal">this</span>.<span class="ST2">whenBuilt</span> = whenBuilt;
|
|
}
|
|
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> * </span><span class="comment">Get</span> <span class="comment">the</span> <span class="comment">value</span> <span class="comment">of</span> <span class="comment">area</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="ST1">@return</span> <span class="comment">the</span> <span class="comment">value</span> <span class="comment">of</span> <span class="comment">area</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">double</span> <span class="ST3">getArea</span>()
|
|
{
|
|
<span class="literal">return</span> <span class="ST2">area</span>;
|
|
}
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> * </span><span class="comment">Set</span> <span class="comment">the</span> <span class="comment">value</span> <span class="comment">of</span> <span class="comment">area</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="ST1">@param</span> <span class="ST4">area</span> <span class="comment">new</span> <span class="comment">value</span> <span class="comment">of</span> <span class="comment">area</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">void</span> <span class="ST3">setArea</span>(<span class="literal">double</span> area)
|
|
{
|
|
<span class="literal">this</span>.<span class="ST2">area</span> = area;
|
|
}
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> * </span><span class="comment">Get</span> <span class="comment">the</span> <span class="comment">value</span> <span class="comment">of</span> <span class="comment">id</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="ST1">@return</span> <span class="comment">the</span> <span class="comment">value</span> <span class="comment">of</span> <span class="comment">id</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">int</span> <span class="ST3">getId</span>()
|
|
{
|
|
<span class="literal">return</span> <span class="ST2">id</span>;
|
|
}
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> * </span><span class="comment">Set</span> <span class="comment">the</span> <span class="comment">value</span> <span class="comment">of</span> <span class="comment">id</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="ST1">@param</span> <span class="ST4">id</span> <span class="comment">new</span> <span class="comment">value</span> <span class="comment">of</span> <span class="comment">id</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">void</span> <span class="ST3">setId</span>(<span class="literal">int</span> id)
|
|
{
|
|
<span class="literal">this</span>.<span class="ST2">id</span> = id;
|
|
}
|
|
|
|
@Override
|
|
<span class="literal">public</span> String <span class="ST3">toString</span>()
|
|
{
|
|
<span class="literal">return</span> <span class="string">"</span><span class="string">House{</span><span class="string">"</span> + <span class="string">"</span><span class="string">id=</span><span class="string">"</span> + <span class="ST2">id</span> + <span class="string">"</span><span class="string">, area=</span><span class="string">"</span> + <span class="ST2">area</span> + <span class="string">"</span><span class="string">, whenBuilt=</span><span class="string">"</span> + <span class="ST2">whenBuilt</span> + <span class="string">'</span><span class="string">}</span><span class="string">'</span>;
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">int</span> <span class="ST3">compareTo</span>(House o) {
|
|
<span class="literal">if</span> (<span class="literal">this</span>.getId() == o.getId() &&
|
|
<span class="literal">this</span>.getArea() == o.getArea() &&
|
|
<span class="literal">this</span>.getWhenBuilt().equals(o.getWhenBuilt())) {
|
|
<span class="literal">return</span> <span class="number">0</span>;
|
|
}
|
|
<span class="literal">if</span> (<span class="literal">this</span>.<span class="ST2">area</span> > o.<span class="ST2">area</span>) {
|
|
<span class="literal">return</span> <span class="number">1</span>;
|
|
} <span class="literal">else</span> <span class="literal">if</span> (<span class="literal">this</span>.<span class="ST2">area</span> < o.<span class="ST2">area</span>) {
|
|
<span class="literal">return</span> -<span class="number">1</span>;
|
|
} <span class="literal">else</span> {
|
|
<span class="literal">return</span> -<span class="number">2</span>; <span class="comment">// Same area for this and o but either var id or var whenBuilt are different</span>
|
|
}
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST5">main</span>(String[] args) <span class="literal">throws</span> CloneNotSupportedException, InterruptedException {
|
|
House house1 = <span class="literal">new</span> House(<span class="number">1</span>, <span class="number">1</span><span class="number">000</span>);
|
|
House house2 = (House) house1.clone();
|
|
house2.<span class="ST2">whenBuilt</span>.setDate(<span class="number">8</span>);
|
|
System.<span class="ST6">out</span>.println(house1);
|
|
System.<span class="ST6">out</span>.println(house2);
|
|
House house3 = <span class="literal">new</span> House(<span class="number">3</span>, <span class="number">2</span><span class="number">000</span>);
|
|
House house4 = <span class="literal">new</span> House(<span class="number">4</span>, <span class="number">2</span><span class="number">000</span>);
|
|
System.<span class="ST6">out</span>.println(<span class="string">"</span><span class="string">house 1 and house 2 are identical: </span><span class="string">"</span>+ house1.compareTo(house2));
|
|
System.<span class="ST6">out</span>.println(<span class="string">"</span><span class="string">house 3 and house 3 have the same area: </span><span class="string">"</span> + house3.compareTo(house4));
|
|
System.<span class="ST6">out</span>.println(<span class="string">"</span><span class="string">House 4 is harger than house 2: </span><span class="string">"</span> + house4.compareTo(house2));
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</pre></body>
|
|
</html>
|