Skip to content

Commit 6d153e8

Browse files
committed
created sorting with comparison, and part of method shuffle
1 parent bd63943 commit 6d153e8

File tree

5 files changed

+190
-0
lines changed

5 files changed

+190
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Fig. 20.9: Sort3.java
2+
// Collections method sortwith a custom Comparator object.
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
public class Sort3
7+
{
8+
public static void main(String [] args)
9+
{
10+
List<Time2> list = new ArrayList<Time2>(); // create List
11+
12+
list.add(new Time2(6, 24, 40));
13+
list.add(new Time2(18, 14, 58));
14+
list.add(new Time2(6, 05, 34));
15+
list.add(new Time2(12, 14, 58));
16+
list.add(new Time2(6, 24, 22));
17+
18+
// output List elements
19+
System.out.printf("Unsorted array elements:\n%s\n", list);
20+
21+
// sort in order using a comparator
22+
Collections.sort(list, new TimeComparator());
23+
24+
// output List elements
25+
System.out.printf("Sorted list elements:\n%s\n", list);
26+
}
27+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Fig. 8.5: Time2.java
2+
// Time2 class declaration with overloaded constructors.
3+
4+
public class Time2
5+
{
6+
private int hour; // 0 - 23
7+
private int minute; // 0 - 59
8+
private int second; // 0 - 59
9+
10+
// Time2 no-argument constructor;
11+
// initializes each instance variable to zero
12+
public Time2()
13+
{
14+
this(0,0,0); // invote Time2 constructor with three arguments
15+
} // end Time2 no-argument constructor
16+
17+
// Time2 constructor: hour supplied, minute and second defaulted to 0
18+
public Time2(int h)
19+
{
20+
this(h, 0, 0); // invoke Time2 constructor with three arguments
21+
} // end Time2 one-argument constructor
22+
23+
// Time2 constructor: hour and minute supplied, second defaulted to 0
24+
public Time2(int h, int m)
25+
{
26+
this(h, m, 0); // invoke Time2 constructor with three arguments
27+
} // end Time2 two-argument constructor
28+
29+
// Time2 constructor: hour, mintute, and second supplied
30+
public Time2(int h, int m, int s)
31+
{
32+
setTime(h, m, s); // invoke setTime to validate time
33+
} // end Time2 three-argument constructor
34+
35+
// Time2 constructor: another Time2 object supplied
36+
public Time2(Time2 time)
37+
{
38+
// invoke Time2 threes-argument constructor
39+
this(time.getHour(), time.getMinute(), time.getSecond());
40+
} // end Time2 constructor with a Time2 object argument
41+
42+
// Set Methods
43+
// set a new time valiue using universal time;
44+
// validate the data
45+
public void setTime(int h, int m, int s)
46+
{
47+
setHour(h); // set the hour
48+
setMinute(m); // set the minute
49+
setSecond(s); // set the second
50+
} // end method setTime
51+
public void setHour(int h)
52+
{
53+
if(h >= 0 && h < 24)
54+
hour = h;
55+
else
56+
throw new IllegalArgumentException("hour must be 0-23");
57+
} // end method setHour
58+
59+
// validate and set minute
60+
public void setMinute(int m)
61+
{
62+
if (m >= 0 && m < 60)
63+
minute = m;
64+
else
65+
throw new IllegalArgumentException("minute must be 0-59");
66+
} // end method setMinute
67+
68+
// validate and set second
69+
public void setSecond(int s)
70+
{
71+
if (s >= 0 && s < 60)
72+
second = ((s >= 0 && s < 60) ? s : 0);
73+
else
74+
throw new IllegalArgumentException("second must be 0-59");
75+
} // end method setSecond
76+
77+
// Get Methods
78+
// get hour value
79+
public int getHour()
80+
{
81+
return hour;
82+
} // end method getHour
83+
84+
// get minute value
85+
public int getMinute()
86+
{
87+
return minute;
88+
} // end method getMinute
89+
90+
// get second value
91+
public int getSecond()
92+
{
93+
return second;
94+
} // end method getSecond
95+
96+
// convert to String in universal-time format (HH:MM:SS)
97+
public String toUniversalString()
98+
{
99+
return String.format(
100+
"%20d:%02d:%02d", getHour(), getMinute(), getSecond());
101+
} // end method toUniversalString
102+
103+
// convert to String in standard-time format (H:MM:SS AM or PM)
104+
public String toString()
105+
{
106+
return String.format("%d:%02d:%02d %s",
107+
( (getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12 ),
108+
getMinute(), getSecond(), (getHour() < 12 ? "AM" : "PM"));
109+
} // end method toString
110+
} // end class Time2
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Fig. 20.8: TimeComparator.java
2+
// Custom Comparator class that compares two Time2 objects
3+
import java.util.Comparator;
4+
5+
public class TimeComparator implements Comparator<Time2>
6+
{
7+
public int compare(Time2 time1, Time2 time2)
8+
{
9+
int hourCompare = time1.getHour() - time2.getHour(); // compare hour
10+
11+
// test the hour first
12+
if (hourCompare != 0)
13+
return hourCompare;
14+
15+
int minuteCompare =
16+
time1.getMinute() - time2.getMinute(); // compare minute
17+
18+
// then test the minute
19+
if (minuteCompare != 0)
20+
return minuteCompare;
21+
22+
int secondCompare =
23+
time1.getSecond() - time2.getSecond(); // compare second
24+
25+
return secondCompare; // return result of comparing seconds
26+
} // end method compare
27+
} // end class TimeComparator
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Fig. 20.10: DeckOfCards.java
2+
// Card shuffle and dealing with Collections method shuffle
3+
4+
// class to represent a Card in a deck of cards
5+
class Card
6+
{
7+
public static enum Face{Ace, Deuce, Three, Four, Five, Six,
8+
Seven, Eight, Nine, Ten, Jack, Queen, King };
9+
10+
public static enum Suit {Club, Diamonds, Heart, Spades};
11+
12+
private final Face face; // face of card
13+
private final Suit suit; // suit of card
14+
15+
}

0 commit comments

Comments
 (0)