71 lines
2.5 KiB
HTML
71 lines
2.5 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
<html>
|
|
<head>
|
|
<title>Max.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; font-weight: bold}
|
|
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
|
|
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
|
|
.literal {color: #cc7832}
|
|
.comment {color: #808080}
|
|
.whitespace {color: #505050}
|
|
-->
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/lab6_generics_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/lab6_generics_calebfontenot/Max.java</td></tr></table>
|
|
<pre>
|
|
<span class="comment">/*</span>
|
|
<span class="comment"> * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license</span>
|
|
<span class="comment"> * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template</span>
|
|
<span class="comment"> */</span>
|
|
<span class="literal">package</span> edu.slcc.asdv.caleb.lab6_generics_calebfontenot;
|
|
|
|
<span class="comment">/**</span>
|
|
*
|
|
* @author caleb
|
|
*/
|
|
public class Max {
|
|
|
|
public static Comparable max(Comparable o1, Comparable o2)
|
|
{
|
|
if (o1.compareTo(o2) > 0) {
|
|
return o1;
|
|
} else {
|
|
return o2;
|
|
}
|
|
}
|
|
public static <E extends Comparable<E>> E maxSafe(E e1, E e2) {
|
|
if(e1.compareTo(e2) > 0) {
|
|
return e1;
|
|
} else {
|
|
return e2;
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args)
|
|
{
|
|
System.out.println(max(1, 2));
|
|
try {
|
|
System.out.println(maxSafe(1, 2));
|
|
System.out.println(maxSafe("abc", "ABC"));
|
|
System.out.println();
|
|
//System.out.println(maxSafe(1, "two"));
|
|
|
|
GenericStack stackUnsafe = new GenericStack();
|
|
GenericStack<Integer> stackSafe = new GenericStack();
|
|
stackSafe.push(1); stackSafe.push(2);
|
|
System.out.println(stackSafe);
|
|
stackUnsafe.push(1); stackUnsafe.push("two");
|
|
System.out.println("This line compiles but crashes the program " + max(1, "two"));
|
|
} catch (ClassCastException e) {
|
|
System.err.println("RAW TYPES ARE UNSAFE " + e.getMessage()) ;
|
|
}
|
|
}
|
|
}
|
|
|
|
</pre></body>
|
|
</html>
|