Skip to content

Commit 846be1a

Browse files
committed
Inheritance, Polymorphism, and Interfaces
1 parent 23a18d1 commit 846be1a

File tree

8 files changed

+409
-0
lines changed

8 files changed

+409
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package chapter_08;
2+
3+
public interface MessageDecoder
4+
{
5+
public String decode(String cipherText);
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package chapter_08;
2+
3+
public interface MessageEncoder
4+
{
5+
String encode(String plainText);
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package chapter_08;
2+
3+
import javafx.application.Application;
4+
import javafx.event.ActionEvent;
5+
import javafx.event.EventHandler;
6+
import javafx.scene.Scene;
7+
import javafx.scene.control.Button;
8+
import javafx.scene.control.Label;
9+
import javafx.scene.control.TextField;
10+
import javafx.scene.layout.HBox;
11+
import javafx.scene.layout.VBox;
12+
import javafx.stage.Stage;
13+
14+
/**
15+
* 8. Create a JavaFX application that uses a TextField to get a message and
16+
* encode or decode it using the classes described in Programming Project 5.
17+
* Use buttons to control the kind of cipher used and to specify whether to
18+
* encode or decode the message. Also use a TextField to get the number
19+
* used in the constructor for the ciphers.
20+
*
21+
*
22+
* @author Sharaf Qeshta
23+
* */
24+
public class Project_08_08 extends Application
25+
{
26+
@Override
27+
public void start(Stage stage)
28+
{
29+
Label lblWord = new Label("Enter a word: ");
30+
TextField txtWord = new TextField();
31+
Label lblNumber = new Label("Enter a number: ");
32+
TextField txtNumber = new TextField();
33+
HBox inputs = new HBox(5, lblWord, txtWord, lblNumber, txtNumber);
34+
35+
Button encryptSubstitution = new Button("Encrypt using Substitution");
36+
Button encryptShuffle = new Button("Encrypt using Shuffle");
37+
HBox encryption = new HBox(5, encryptSubstitution, encryptShuffle);
38+
39+
Button decryptSubstitution = new Button("Decrypt using Substitution");
40+
Button decryptShuffle = new Button("Decrypt using Shuffle");
41+
HBox decryption = new HBox(5, decryptSubstitution, decryptShuffle);
42+
43+
VBox pane = new VBox(5, inputs, encryption, decryption);
44+
Scene scene = new Scene(pane, 500, 500);
45+
stage.setScene(scene);
46+
stage.setTitle("Project_08_08");
47+
stage.show();
48+
49+
encryptSubstitution.setOnAction(new EventHandler<ActionEvent>()
50+
{
51+
@Override
52+
public void handle(ActionEvent event)
53+
{
54+
String word = txtWord.getText().trim();
55+
int number = Integer.parseInt(txtNumber.getText().trim());
56+
SubstitutionCipher substitutionCipher = new SubstitutionCipher(number);
57+
txtWord.setText(substitutionCipher.encode(word));
58+
}
59+
});
60+
61+
encryptShuffle.setOnAction(new EventHandler<ActionEvent>()
62+
{
63+
@Override
64+
public void handle(ActionEvent event)
65+
{
66+
String word = txtWord.getText().trim();
67+
int number = Integer.parseInt(txtNumber.getText().trim());
68+
ShuffleCipher shuffleCipher = new ShuffleCipher(number);
69+
txtWord.setText(shuffleCipher.encode(word));
70+
}
71+
});
72+
73+
74+
decryptSubstitution.setOnAction(new EventHandler<ActionEvent>()
75+
{
76+
@Override
77+
public void handle(ActionEvent event)
78+
{
79+
String word = txtWord.getText().trim();
80+
int number = Integer.parseInt(txtNumber.getText().trim());
81+
SubstitutionCipher substitutionCipher = new SubstitutionCipher(number);
82+
txtWord.setText(substitutionCipher.decode(word));
83+
}
84+
});
85+
86+
decryptShuffle.setOnAction(new EventHandler<ActionEvent>()
87+
{
88+
@Override
89+
public void handle(ActionEvent event)
90+
{
91+
String word = txtWord.getText().trim();
92+
int number = Integer.parseInt(txtNumber.getText().trim());
93+
ShuffleCipher shuffleCipher = new ShuffleCipher(number);
94+
txtWord.setText(shuffleCipher.decode(word));
95+
}
96+
});
97+
}
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package chapter_08;
2+
3+
public class ShuffleCipher implements MessageEncoder, MessageDecoder
4+
{
5+
private int n;
6+
7+
public ShuffleCipher(int n)
8+
{
9+
this.n = n;
10+
}
11+
12+
@Override
13+
public String encode(String plainText)
14+
{
15+
String cipher = plainText;
16+
for (int i = 0; i < n; i++)
17+
{
18+
String part1 = cipher.substring(0, cipher.length()/2 + 1);
19+
String part2 = cipher.substring(cipher.length()/2 + 1);
20+
21+
cipher = "";
22+
int part1Index = 0, part2Index = 0;
23+
while (cipher.length() < plainText.length())
24+
{
25+
if (part1Index < part1.length()
26+
&& part2Index < part2.length())
27+
{
28+
cipher += part1.charAt(part1Index++);
29+
cipher += part2.charAt(part2Index++);
30+
}
31+
else if (part1Index < part1.length())
32+
cipher += part1.charAt(part1Index++);
33+
else if (part2Index < part2.length())
34+
cipher += part2.charAt(part2Index++);
35+
}
36+
}
37+
return cipher;
38+
}
39+
40+
@Override
41+
public String decode(String cipherText)
42+
{
43+
String plain = cipherText;
44+
for (int i = 0; i < n; i++)
45+
{
46+
int part1Length = plain.length()/2 + 1;
47+
int part2Length = plain.length() - part1Length;
48+
49+
int index = 0;
50+
String part1 = "", part2 = "";
51+
while (index < plain.length())
52+
{
53+
if (part1.length() < part1Length)
54+
part1 += plain.charAt(index++);
55+
if (part2.length() < part2Length)
56+
part2 += plain.charAt(index++);
57+
}
58+
plain = part1 + part2;
59+
}
60+
return plain;
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package chapter_08;
2+
3+
public class SubstitutionCipher implements MessageEncoder, MessageDecoder
4+
{
5+
private int shift;
6+
7+
public SubstitutionCipher(int shift)
8+
{
9+
this.shift = shift;
10+
}
11+
12+
@Override
13+
public String encode(String plainText)
14+
{
15+
String cipher = "";
16+
for (int i = 0; i < plainText.length(); i++)
17+
cipher += (char) (plainText.charAt(i) + shift);
18+
return cipher;
19+
}
20+
21+
@Override
22+
public String decode(String cipherText)
23+
{
24+
String plain = "";
25+
for (int i = 0; i < cipherText.length(); i++)
26+
plain += (char) (cipherText.charAt(i) - shift);
27+
return plain;
28+
}
29+
}
+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package chapter_08;
2+
3+
import javafx.application.Application;
4+
import javafx.event.ActionEvent;
5+
import javafx.event.EventHandler;
6+
import javafx.geometry.Pos;
7+
import javafx.scene.Scene;
8+
import javafx.scene.control.Button;
9+
import javafx.scene.control.TextField;
10+
import javafx.scene.layout.BorderPane;
11+
import javafx.scene.layout.GridPane;
12+
import javafx.scene.layout.VBox;
13+
import javafx.stage.Stage;
14+
15+
16+
/**
17+
* 9. Create a JavaFX application that acts as a simple calculator. Create buttons
18+
* for digits 0-9 and a text field that concatenates digits for the current
19+
* number as the buttons are clicked. Add additional buttons for operators +,
20+
* −, *, and /. Add a final button for = that calculates the previously computed
21+
* value with the value entered in the text field. Consider using the layout
22+
* described in Programming Project 17 from Chapter 7.
23+
*
24+
*
25+
* @author Sharaf Qeshta
26+
* */
27+
public class Project_08_09 extends Application
28+
{
29+
@Override
30+
public void start(Stage stage)
31+
{
32+
BorderPane pane = new BorderPane();
33+
34+
TextField result = new TextField();
35+
result.setEditable(false);
36+
pane.setTop(result);
37+
38+
GridPane numbersButtons = new GridPane();
39+
numbersButtons.setAlignment(Pos.CENTER);
40+
41+
int buttonNumber = 1;
42+
for (int i = 0; i < 3; i++)
43+
{
44+
for (int j = 0; j < 3; j++)
45+
{
46+
Button button = new Button(buttonNumber++ + "");
47+
numbersButtons.add(button, j, i);
48+
button.setOnAction(new EventHandler<ActionEvent>()
49+
{
50+
@Override
51+
public void handle(ActionEvent event)
52+
{
53+
if (result.getText().equals("Invalid Pattern"))
54+
result.setText("");
55+
result.setText(result.getText() + button.getText());
56+
}
57+
});
58+
}
59+
}
60+
61+
62+
Button zero = new Button("0");
63+
zero.setOnAction(new EventHandler<ActionEvent>()
64+
{
65+
@Override
66+
public void handle(ActionEvent event)
67+
{
68+
if (result.getText().equals("Invalid Pattern"))
69+
result.setText("");
70+
result.setText(result.getText() + "0");
71+
}
72+
});
73+
numbersButtons.add(zero, 1, 3);
74+
75+
pane.setCenter(numbersButtons);
76+
77+
78+
VBox operations = new VBox();
79+
Button multiply = new Button("*");
80+
Button divide = new Button("/");
81+
Button add = new Button("+");
82+
Button subtract = new Button("-");
83+
operations.getChildren().add(multiply);
84+
operations.getChildren().add(divide);
85+
operations.getChildren().add(add);
86+
operations.getChildren().add(subtract);
87+
88+
multiply.setOnAction(new EventHandler<ActionEvent>()
89+
{
90+
@Override
91+
public void handle(ActionEvent event)
92+
{
93+
result.setText(result.getText() + " * ");
94+
}
95+
});
96+
97+
divide.setOnAction(new EventHandler<ActionEvent>()
98+
{
99+
@Override
100+
public void handle(ActionEvent event)
101+
{
102+
result.setText(result.getText() + " / ");
103+
}
104+
});
105+
106+
add.setOnAction(new EventHandler<ActionEvent>()
107+
{
108+
@Override
109+
public void handle(ActionEvent event)
110+
{
111+
result.setText(result.getText() + " + ");
112+
}
113+
});
114+
115+
subtract.setOnAction(new EventHandler<ActionEvent>()
116+
{
117+
@Override
118+
public void handle(ActionEvent event)
119+
{
120+
result.setText(result.getText() + " - ");
121+
}
122+
});
123+
124+
pane.setLeft(operations);
125+
126+
Button equal = new Button("=");
127+
equal.setOnAction(new EventHandler<ActionEvent>()
128+
{
129+
@Override
130+
public void handle(ActionEvent event)
131+
{
132+
String[] operationElements = result.getText().trim().split(" ");
133+
if (operationElements.length == 3)
134+
{
135+
int x = Integer.parseInt(operationElements[0]);
136+
String op = operationElements[1];
137+
int y = Integer.parseInt(operationElements[2]);
138+
139+
switch (op)
140+
{
141+
case "*": result.setText(x * y + ""); break;
142+
case "/": result.setText(x / y + ""); break;
143+
case "+": result.setText((x + y) + ""); break;
144+
case "-": result.setText((x - y) + "");break;
145+
}
146+
}
147+
else
148+
{
149+
result.setText("Invalid Pattern");
150+
}
151+
}
152+
});
153+
154+
155+
equal.setMinSize(20, 100);
156+
pane.setRight(equal);
157+
158+
Scene scene = new Scene(pane, 120, 130);
159+
stage.setScene(scene);
160+
stage.setTitle("Project_08_09");
161+
stage.show();
162+
}
163+
}

0 commit comments

Comments
 (0)