111 lines
7.3 KiB
HTML
111 lines
7.3 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
<html>
|
|
<head>
|
|
<title>A4.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}
|
|
.number {color: #6897bb}
|
|
.string {color: #6a8759}
|
|
.ST1 {color: #9876aa}
|
|
.ST3 {color: #ffc66d}
|
|
.comment {color: #808080}
|
|
.whitespace {color: #505050}
|
|
.ST2 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
|
|
.ST4 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
|
|
.ST0 {color: #287bde}
|
|
.literal {color: #cc7832}
|
|
.ST5 {font-family: monospace; font-weight: bold; font-style: italic}
|
|
-->
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/JavaFXBallsWithComparator/src/main/java/edu/slcc/asdv/caleb/javafxballswithcomparator/A4.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> edu.slcc.asdv.caleb.javafxballswithcomparator;
|
|
|
|
<span class="literal">import</span> java.util.Arrays;
|
|
<span class="literal">import</span> java.util.Collections;
|
|
<span class="literal">import</span> java.util.Comparator;
|
|
<span class="literal">import</span> java.util.List;
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">class</span> A4<E <span class="literal">extends</span> Comparable<E>> {
|
|
|
|
E <span class="ST1">x</span>;
|
|
|
|
<span class="literal">public</span> A4() {}
|
|
<span class="literal">public</span> A4(E x) {<span class="literal">this</span>.<span class="ST1">x</span> = x;}
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> Comparator<A4> <span class="ST2">comparator</span>() {
|
|
Comparator<A4> c = <span class="literal">new</span> Comparator<A4>() {
|
|
@Override
|
|
<span class="literal">public</span> <span class="literal">int</span> <span class="ST3">compare</span>(A4 o1, A4 o2) {
|
|
<span class="literal">return</span> o1.<span class="ST1">x</span>.compareTo(o2.<span class="ST1">x</span>);
|
|
}
|
|
};
|
|
<span class="literal">return</span> c;
|
|
}
|
|
<span class="literal">public</span> <span class="literal">static</span> Comparator<A4> <span class="ST2">comparatorReverse</span>() {
|
|
Comparator<A4> c = <span class="literal">new</span> Comparator<A4>() {
|
|
@Override
|
|
<span class="literal">public</span> <span class="literal">int</span> <span class="ST3">compare</span>(A4 o1, A4 o2) {
|
|
<span class="literal">switch</span> (o1.<span class="ST1">x</span>.compareTo(o2.<span class="ST1">x</span>)) {
|
|
<span class="literal">case</span> -<span class="number">1</span>:
|
|
<span class="literal">return</span> <span class="number">1</span>;
|
|
<span class="literal">case</span> <span class="number">0</span>:
|
|
<span class="literal">return</span> <span class="number">0</span>;
|
|
<span class="literal">case</span> <span class="number">1</span>:
|
|
<span class="literal">return</span> -<span class="number">1</span>;
|
|
}
|
|
<span class="literal">return</span> -<span class="number">112315234</span>;
|
|
}
|
|
};
|
|
<span class="literal">return</span> c;
|
|
}
|
|
|
|
@Override
|
|
<span class="literal">public</span> String <span class="ST3">toString</span>() {
|
|
<span class="literal">return</span> <span class="string">"</span><span class="string">A4{</span><span class="string">"</span> + <span class="string">"</span><span class="string">x=</span><span class="string">"</span> + <span class="ST1">x</span> + <span class="string">'</span><span class="string">}</span><span class="string">'</span>;
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">main</span>(String[] args) {
|
|
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Sorting in ascending order</span><span class="string">"</span>);
|
|
List<A4> list1 = Arrays.<span class="ST5">asList</span>(
|
|
<span class="literal">new</span> A4(<span class="literal">new</span> Integer(<span class="number">4</span>)),
|
|
<span class="literal">new</span> A4(<span class="literal">new</span> Integer(<span class="number">1</span>)),
|
|
<span class="literal">new</span> A4(<span class="literal">new</span> Integer(<span class="number">2</span>))
|
|
);
|
|
Collections.<span class="ST5">sort</span>(list1, A4.<span class="ST5">comparator</span>());
|
|
List<A4> list2 = Arrays.<span class="ST5">asList</span>(
|
|
<span class="literal">new</span> A4(<span class="literal">new</span> String(<span class="string">"</span><span class="string">once</span><span class="string">"</span>)),
|
|
<span class="literal">new</span> A4(<span class="literal">new</span> String(<span class="string">"</span><span class="string">upon</span><span class="string">"</span>)),
|
|
<span class="literal">new</span> A4(<span class="literal">new</span> String(<span class="string">"</span><span class="string">a</span><span class="string">"</span>)),
|
|
<span class="literal">new</span> A4(<span class="literal">new</span> String(<span class="string">"</span><span class="string">time</span><span class="string">"</span>)),
|
|
<span class="literal">new</span> A4(<span class="literal">new</span> String(<span class="string">"</span><span class="string">in</span><span class="string">"</span>)),
|
|
<span class="literal">new</span> A4(<span class="literal">new</span> String(<span class="string">"</span><span class="string">America</span><span class="string">"</span>))
|
|
);
|
|
Collections.<span class="ST5">sort</span>(list2, A4.<span class="ST5">comparator</span>());
|
|
System.<span class="ST4">out</span>.println(list1);
|
|
System.<span class="ST4">out</span>.println(list2);
|
|
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Now, in descending order:</span><span class="string">"</span>);
|
|
Collections.<span class="ST5">sort</span>(list2, A4.<span class="ST5">comparatorReverse</span>());
|
|
System.<span class="ST4">out</span>.println(list2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</pre></body>
|
|
</html>
|