Skip to content

Commit 37d3421

Browse files
committed
Add solution Exercise16_07.java
1 parent 3ca6e34 commit 37d3421

File tree

4 files changed

+375
-1
lines changed

4 files changed

+375
-1
lines changed

ch_12/exercise12_12/Test.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package ch_12.exercise12_12;
22

3-
//TODO: Reset before testing
3+
// Reset brackets before testing
44
public class Test {
55

66
public static void main(String[] args) {

ch_16/exercise16_07/ClockPane.java

+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
package ch_16.exercise16_07;
2+
3+
import java.util.Calendar;
4+
import java.util.GregorianCalendar;
5+
6+
import javafx.scene.layout.Pane;
7+
import javafx.scene.paint.Color;
8+
import javafx.scene.shape.Circle;
9+
import javafx.scene.shape.Line;
10+
import javafx.scene.text.Text;
11+
12+
/**
13+
* ClockPane from Listing 14.21
14+
*/
15+
public class ClockPane extends Pane {
16+
private int hour;
17+
private int minute;
18+
private int second;
19+
20+
// Clock pane's width and height
21+
private double w = 250, h = 250;
22+
23+
/**
24+
* Construct a default clock with the current time
25+
*/
26+
public ClockPane() {
27+
setCurrentTime();
28+
29+
}
30+
31+
/**
32+
* Construct a clock with specified hour, minute, and second
33+
*/
34+
public ClockPane(int hour, int minute, int second) {
35+
this.hour = hour;
36+
this.minute = minute;
37+
this.second = second;
38+
paintClock();
39+
40+
}
41+
42+
/**
43+
* Return hour
44+
*/
45+
public int getHour() {
46+
return hour;
47+
48+
}
49+
50+
/**
51+
* Set a new hour
52+
*/
53+
public void setHour(int hour) {
54+
this.hour = hour;
55+
paintClock();
56+
57+
}
58+
59+
/**
60+
* Return minute
61+
*/
62+
public int getMinute() {
63+
return minute;
64+
65+
}
66+
67+
/**
68+
* Set a new minute
69+
*/
70+
public void setMinute(int minute) {
71+
this.minute = minute;
72+
paintClock();
73+
}
74+
75+
/**
76+
* Return second
77+
*/
78+
public int getSecond() {
79+
return second;
80+
}
81+
82+
/**
83+
* Set a new second
84+
*/
85+
public void setSecond(int second) {
86+
this.second = second;
87+
paintClock();
88+
}
89+
90+
/**
91+
* Return clock pane's width
92+
*/
93+
public double getW() {
94+
return w;
95+
}
96+
97+
/**
98+
* Set clock pane's width
99+
*/
100+
public void setW(double w) {
101+
this.w = w;
102+
paintClock();
103+
}
104+
105+
/**
106+
* Return clock pane's height
107+
*/
108+
public double getH() {
109+
return h;
110+
}
111+
112+
/**
113+
* Set clock pane's height
114+
*/
115+
public void setH(double h) {
116+
this.h = h;
117+
paintClock();
118+
}
119+
120+
/* Set the current time for the clock */
121+
public void setCurrentTime() {
122+
// Construct a calendar for the current date and time
123+
Calendar calendar = new GregorianCalendar();
124+
// Set current hour, minute and second
125+
this.hour = calendar.get(Calendar.HOUR_OF_DAY);
126+
this.minute = calendar.get(Calendar.MINUTE);
127+
this.second = calendar.get(Calendar.SECOND);
128+
129+
paintClock(); // Repaint the clock
130+
}
131+
132+
133+
/**
134+
* Paint the clock
135+
*/
136+
protected void paintClock() {
137+
// Initialize clock parameters
138+
double clockRadius = Math.min(w, h) * 0.8 * 0.5;
139+
double centerX = w / 2;
140+
double centerY = h / 2;
141+
142+
// Draw circle
143+
144+
145+
Circle circle = new Circle(centerX, centerY, clockRadius);
146+
circle.setFill(Color.WHITE);
147+
circle.setStroke(Color.BLACK);
148+
Text t1 = new Text(centerX - 5, centerY - clockRadius + 12, "12");
149+
Text t2 = new Text(centerX - clockRadius + 3, centerY + 5, "9");
150+
Text t3 = new Text(centerX + clockRadius - 10, centerY + 3, "3");
151+
Text t4 = new Text(centerX - 3, centerY + clockRadius - 3, "6");
152+
153+
// Draw second hand
154+
double sLength = clockRadius * 0.8;
155+
double secondX = centerX + sLength *
156+
Math.sin(second * (2 * Math.PI / 60));
157+
double secondY = centerY - sLength *
158+
Math.cos(second * (2 * Math.PI / 60));
159+
Line sLine = new Line(centerX, centerY, secondX, secondY);
160+
sLine.setStroke(Color.RED);
161+
162+
// Draw minute hand
163+
double mLength = clockRadius * 0.65;
164+
double xMinute = centerX + mLength *
165+
Math.sin(minute * (2 * Math.PI / 60));
166+
double minuteY = centerY - mLength *
167+
Math.cos(minute * (2 * Math.PI / 60));
168+
Line mLine = new Line(centerX, centerY, xMinute, minuteY);
169+
mLine.setStroke(Color.BLUE);
170+
171+
// Draw hour hand
172+
double hLength = clockRadius * 0.5;
173+
double hourX = centerX + hLength *
174+
Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12));
175+
double hourY = centerY - hLength *
176+
Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12));
177+
Line hLine = new Line(centerX, centerY, hourX, hourY);
178+
hLine.setStroke(Color.GREEN);
179+
180+
getChildren().clear();
181+
getChildren().addAll(circle, t1, t2, t3, t4, sLine, mLine, hLine);
182+
}
183+
}
+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package ch_16.exercise16_07;
2+
3+
import javafx.application.Application;
4+
import javafx.beans.property.SimpleIntegerProperty;
5+
import javafx.beans.value.ChangeListener;
6+
import javafx.beans.value.ObservableValue;
7+
import javafx.scene.Scene;
8+
import javafx.scene.control.Label;
9+
import javafx.scene.control.TextField;
10+
import javafx.scene.layout.HBox;
11+
import javafx.scene.layout.Priority;
12+
import javafx.scene.layout.VBox;
13+
import javafx.scene.paint.Color;
14+
import javafx.scene.shape.Circle;
15+
import javafx.scene.text.TextAlignment;
16+
import javafx.stage.Stage;
17+
import javafx.util.converter.NumberStringConverter;
18+
19+
/**
20+
* *16.7 (Set clock time) Write a program that displays a clock and sets the time with the
21+
* input from three text fields, as shown in Figure 16.38b. Use the ClockPane in
22+
* Listing 14.21. Resize the clock to the center of the pane.
23+
*/
24+
public class Exercise16_07 extends Application {
25+
private double WIDTH = 450;
26+
private double HEIGHT = 400;
27+
SimpleIntegerProperty hourProperty = new SimpleIntegerProperty(12);
28+
SimpleIntegerProperty minuteProperty = new SimpleIntegerProperty(0);
29+
SimpleIntegerProperty secondProperty = new SimpleIntegerProperty(0);
30+
31+
@Override
32+
public void start(Stage primaryStage) {
33+
34+
VBox mainLayoutBox = new VBox(5);
35+
HBox inputsBox = new HBox(1);
36+
ClockPane clockPane = new ClockPane();
37+
38+
Label hourLabel = new Label("Hour");
39+
Label minutelabel = new Label("Minute");
40+
Label secondsLabel = new Label("Second");
41+
42+
TextField hourInput = new TextField();
43+
TextField minuteInput = new TextField();
44+
TextField secondInput = new TextField();
45+
hourInput.textProperty().bindBidirectional(hourProperty, new NumberStringConverter());
46+
minuteInput.textProperty().bindBidirectional(minuteProperty, new NumberStringConverter());
47+
secondInput.textProperty().bindBidirectional(secondProperty, new NumberStringConverter());
48+
49+
inputsBox.getChildren().add(hourLabel);
50+
inputsBox.getChildren().add(hourInput);
51+
inputsBox.getChildren().add(minutelabel);
52+
inputsBox.getChildren().add(minuteInput);
53+
inputsBox.getChildren().add(secondsLabel);
54+
inputsBox.getChildren().add(secondInput);
55+
56+
57+
/* Add ClockPane to Parent Pane*/
58+
mainLayoutBox.getChildren().add(clockPane);
59+
VBox.setVgrow(clockPane, Priority.ALWAYS);
60+
/* Add input footer to parent Pane */
61+
mainLayoutBox.getChildren().add(inputsBox);
62+
63+
mainLayoutBox.widthProperty().addListener((observable, oldValue, newValue) -> {
64+
hourInput.setMaxWidth(newValue.doubleValue() / 6); // Set max width of input boxes to 1/6th total width of parent Pane
65+
secondInput.setMaxWidth(newValue.doubleValue() / 6);
66+
minuteInput.setMaxWidth(newValue.doubleValue() / 6);
67+
clockPane.setW(newValue.doubleValue());
68+
});
69+
mainLayoutBox.heightProperty().addListener((observable, oldValue, newValue) -> {
70+
Number inputFooterHeight = inputsBox.getHeight();
71+
clockPane.setH(newValue.doubleValue() - inputFooterHeight.doubleValue());
72+
});
73+
74+
clockPane.setHour(hourProperty.intValue());
75+
clockPane.setMinute(minuteProperty.intValue());
76+
clockPane.setSecond(secondProperty.intValue());
77+
78+
hourProperty.addListener((o, ov, nv) -> {
79+
clockPane.setHour(nv.intValue());
80+
});
81+
minuteProperty.addListener((o, ov, nv) -> {
82+
clockPane.setMinute(nv.intValue());
83+
});
84+
secondProperty.addListener((o, ov, nv) -> {
85+
clockPane.setSecond(nv.intValue());
86+
});
87+
88+
Scene scene = new Scene(mainLayoutBox, WIDTH, HEIGHT);
89+
primaryStage.setTitle(getClass().getName());
90+
primaryStage.setScene(scene);
91+
primaryStage.setResizable(false);
92+
primaryStage.show();
93+
94+
95+
}
96+
}

ch_23/resources/Heap.java

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package ch_23.resources;
2+
3+
/**
4+
* Listing 23.9 Heap.java, head class provides operations for manipulating
5+
* a heap
6+
*
7+
* @param <E> Type of Heap nodes
8+
*/
9+
public class Heap<E extends Comparable<E>> {
10+
private java.util.ArrayList<E> list = new java.util.ArrayList<>();
11+
12+
/**
13+
* Create a default heap
14+
*/
15+
public Heap() {
16+
}
17+
18+
/**
19+
* Create a heap from an array of objects
20+
*/
21+
public Heap(E[] objects) {
22+
for (int i = 0; i < objects.length; i++)
23+
add(objects[i]);
24+
}
25+
26+
/**
27+
* Add a new object into the heap
28+
*/
29+
public void add(E newObject) {
30+
list.add(newObject); // Append to the heap
31+
int currentIndex = list.size() - 1; // The index of the last node
32+
33+
while (currentIndex > 0) {
34+
int parentIndex = (currentIndex - 1) / 2;
35+
// Swap if the current object is greater than its parent
36+
if (list.get(currentIndex).compareTo(
37+
list.get(parentIndex)) > 0) {
38+
E temp = list.get(currentIndex);
39+
list.set(currentIndex, list.get(parentIndex));
40+
list.set(parentIndex, temp);
41+
} else {
42+
break; // The tree is a heap now
43+
}
44+
currentIndex = parentIndex;
45+
46+
}
47+
}
48+
49+
/**
50+
* Remove the root from the heap
51+
*/
52+
public E remove() {
53+
if (list.size() == 0) return null;
54+
55+
E removedObject = list.get(0);
56+
list.set(0, list.get(list.size() - 1));
57+
list.remove(list.size() - 1);
58+
59+
int currentIndex = 0;
60+
while (currentIndex < list.size()) {
61+
62+
int leftChildIndex = 2 * currentIndex + 1;
63+
int rightChildIndex = 2 * currentIndex + 2;
64+
65+
// Find the maximum between two children
66+
if (leftChildIndex >= list.size()) break; // The tree is a heap
67+
int maxIndex = leftChildIndex;
68+
if (rightChildIndex < list.size()) {
69+
if (list.get(maxIndex).compareTo(
70+
list.get(rightChildIndex)) < 0) {
71+
maxIndex = rightChildIndex;
72+
}
73+
}
74+
75+
// Swap if the current node is less than the maximum
76+
if (list.get(currentIndex).compareTo(
77+
list.get(maxIndex)) < 0) {
78+
E temp = list.get(maxIndex);
79+
list.set(maxIndex, list.get(currentIndex));
80+
list.set(currentIndex, temp);
81+
currentIndex = maxIndex;
82+
} else
83+
break; // The tree is a heap
84+
}
85+
86+
return removedObject;
87+
}
88+
89+
/**
90+
* Get the number of nodes in the tree
91+
*/
92+
public int getSize() {
93+
return list.size();
94+
}
95+
}

0 commit comments

Comments
 (0)