91 lines
5.0 KiB
HTML
91 lines
5.0 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
<html>
|
|
<head>
|
|
<title>TestEdible.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}
|
|
.ST4 {color: #9876aa}
|
|
.number {color: #6897bb}
|
|
.string {color: #6a8759}
|
|
.comment {color: #808080}
|
|
.whitespace {color: #505050}
|
|
.ST2 {color: #ffc66d; font-family: monospace; font-style: italic}
|
|
.ST3 {color: #9876aa; font-family: monospace; font-style: italic}
|
|
.ST1 {color: #808080; font-family: monospace; font-weight: bold}
|
|
.ST0 {color: #287bde}
|
|
.literal {color: #cc7832}
|
|
.ST5 {font-family: monospace; font-style: italic}
|
|
-->
|
|
</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/TestEdible.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.ArrayList;
|
|
|
|
<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> TestEdible {
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">testWithArrayList</span>() {
|
|
ArrayList<Edible> edible = <span class="literal">new</span> ArrayList();
|
|
|
|
edible.add(<span class="literal">new</span> Chicken());
|
|
edible.add(<span class="literal">new</span> Apple());
|
|
edible.add(<span class="literal">new</span> Orange());
|
|
edible.add(<span class="literal">new</span> Duck());
|
|
edible.add(<span class="literal">new</span> Brocoli());
|
|
|
|
<span class="literal">for</span> (Edible eat: edible) {
|
|
System.<span class="ST3">out</span>.println(eat.howToEat());
|
|
}
|
|
}
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">testWithArrayOfInterfaces</span>() {
|
|
Edible[] edible = {
|
|
<span class="literal">new</span> Chicken(), <span class="literal">new</span> Apple(), <span class="literal">new</span> Orange(), <span class="literal">new</span> Duck(), <span class="literal">new</span> Brocoli()
|
|
};
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < edible.<span class="ST4">length</span>; ++i) {
|
|
System.<span class="ST3">out</span>.println(edible[i].howToEat());
|
|
}
|
|
}
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">testWithArrayOfObjects</span>() {
|
|
Object[] objects = {
|
|
<span class="literal">new</span> Tiger(), <span class="literal">new</span> Chicken(), <span class="literal">new</span> Apple(), <span class="literal">new</span> Duck(), <span class="literal">new</span> Brocoli()
|
|
};
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < objects.<span class="ST4">length</span>; ++i) {
|
|
<span class="literal">if</span> (objects[i] <span class="literal">instanceof</span> Edible) {
|
|
System.<span class="ST3">out</span>.println(((Edible) objects[i]).howToEat());
|
|
}
|
|
<span class="literal">if</span> (objects[i] <span class="literal">instanceof</span> Animal) {
|
|
System.<span class="ST3">out</span>.println(((Animal) objects[i]).sound());
|
|
}
|
|
}
|
|
}
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">main</span>(String[] args)
|
|
{
|
|
<span class="ST5">testWithArrayOfObjects</span>();
|
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">-------------------</span><span class="string">"</span>);
|
|
<span class="ST5">testWithArrayOfInterfaces</span>();
|
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">------------------ </span><span class="string">"</span>);
|
|
<span class="ST5">testWithArrayList</span>();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
</pre></body>
|
|
</html>
|