package Chapter_20;import ToolKit.PostfixNotation;import javaf.pdf

package Chapter_20; import ToolKit.PostfixNotation; import javafx.application.Application; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.stage.Stage; import java.util.ArrayList; import java.util.Stack; public class Exercise_13 extends Application { int[] validNumbers = new int[4]; @Override public void start(Stage primaryStage) { // Top pane Button btRefresh = new Button(\"Shuffle\"); Label lblStatus = new Label(\"\"); HBox topPane = new HBox(lblStatus, btRefresh); topPane.setAlignment(Pos.BASELINE_RIGHT); topPane.setSpacing(10); // Center Pane HBox centerPane = new HBox(); centerPane.setAlignment(Pos.CENTER); centerPane.setSpacing(10); centerPane.setPadding(new Insets(10)); // set first 4 random cards setRandomCards(centerPane); // Bottom pane TextField tfExpression = new TextField(); Label lblExpression = new Label(\"Enter an expression:\"); Button btVerify = new Button(\"Verify\"); HBox bottomPane = new HBox(10, lblExpression, tfExpression, btVerify); // Container Pane BorderPane borderPane = new BorderPane(); borderPane.setPadding(new Insets(10)); borderPane.setTop(topPane); borderPane.setCenter(centerPane); borderPane.setBottom(bottomPane); // Listeners btRefresh.setOnAction(e -> { lblStatus.setText(\"\"); setRandomCards(centerPane); }); btVerify.setOnAction(e -> { String expression = tfExpression.getText(); if (isValid(expression) && isCorrect(expression)) { lblStatus.setText(\"Good job! \" + expression + \" = 24\"); } else { lblStatus.setText(\"Invalid Expression\"); } }); Scene scene = new Scene(borderPane); primaryStage.setTitle(\"4 Random Cards\"); primaryStage.setScene(scene); primaryStage.show(); // Debug } private void setRandomCards(HBox pane) { boolean[] usedCards = new boolean[52]; // choose 4 random distinct cards from the deck int count = 0; pane.getChildren().clear(); while (count < 4) { int card = (int) (Math.random() * 52); if (!usedCards[card]) { usedCards[card] = true; pane.getChildren().add(new ImageView(new Image(\"image/card/\" + (++card) + \".png\"))); int value = card % 13; validNumbers[count] = (value == 0) ? 13 : value; count++; } } } private static boolean isOperator(char ch) { return (ch == \'(\' || ch == \')\' || isArithmeticOperator(ch)); } private static boolean isArithmeticOperator(char ch) { return (ch == \'/\' || ch == \'+\' || ch == \'-\' || ch == \'*\'); } private static String[] separateExpression(String s) { ArrayList tokens = new ArrayList<>(30); char[] chars = s.toCharArray(); String numBuffer = \"\"; for (char ch : chars) { if (isOperator(ch)) { if (numBuffer.length() > 0) { tokens.add(numBuffer); numBuffer = \"\"; } tokens.add(ch + \"\"); } else { if (ch != \' \') numBuffer += ch; } } if (numBuffe.

package Chapter_20;
import ToolKit.PostfixNotation;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.Stack;
public class Exercise_13 extends Application {
int[] validNumbers = new int[4];
@Override
public void start(Stage primaryStage) {
// Top pane
Button btRefresh = new Button("Shuffle");
Label lblStatus = new Label("");
HBox topPane = new HBox(lblStatus, btRefresh);
topPane.setAlignment(Pos.BASELINE_RIGHT);
topPane.setSpacing(10);
// Center Pane
HBox centerPane = new HBox();
centerPane.setAlignment(Pos.CENTER);
centerPane.setSpacing(10);
centerPane.setPadding(new Insets(10));
// set first 4 random cards
setRandomCards(centerPane);
// Bottom pane
TextField tfExpression = new TextField();
Label lblExpression = new Label("Enter an expression:");
Button btVerify = new Button("Verify");
HBox bottomPane = new HBox(10, lblExpression, tfExpression, btVerify);
// Container Pane
BorderPane borderPane = new BorderPane();
borderPane.setPadding(new Insets(10));
borderPane.setTop(topPane);
borderPane.setCenter(centerPane);
borderPane.setBottom(bottomPane);
// Listeners
btRefresh.setOnAction(e -> {
lblStatus.setText("");
setRandomCards(centerPane);
});
btVerify.setOnAction(e -> {
String expression = tfExpression.getText();
if (isValid(expression) && isCorrect(expression)) {
lblStatus.setText("Good job! " + expression + " = 24");
} else {
lblStatus.setText("Invalid Expression");
}
});
Scene scene = new Scene(borderPane);
primaryStage.setTitle("4 Random Cards");
primaryStage.setScene(scene);
primaryStage.show();
// Debug
}
private void setRandomCards(HBox pane) {
boolean[] usedCards = new boolean[52];
// choose 4 random distinct cards from the deck
int count = 0;
pane.getChildren().clear();
while (count < 4) {
int card = (int) (Math.random() * 52);
if (!usedCards[card]) {
usedCards[card] = true;
pane.getChildren().add(new ImageView(new Image("image/card/" + (++card) +
".png")));
int value = card % 13;
validNumbers[count] = (value == 0) ? 13 : value;
count++;
}
}
}
private static boolean isOperator(char ch) {
return (ch == '(' ||
ch == ')' ||
isArithmeticOperator(ch));
}
private static boolean isArithmeticOperator(char ch) {
return (ch == '/' ||
ch == '+' ||
ch == '-' ||
ch == '*');
}
private static String[] separateExpression(String s) {
ArrayList tokens = new ArrayList<>(30);
char[] chars = s.toCharArray();
String numBuffer = "";
for (char ch : chars) {
if (isOperator(ch)) {
if (numBuffer.length() > 0) {
tokens.add(numBuffer);
numBuffer = "";
}
tokens.add(ch + "");
} else {
if (ch != ' ')
numBuffer += ch;
}
}
if (numBuffer.length() > 0) {
tokens.add(numBuffer);
}
return tokens.toArray(new String[tokens.size()]);
}
private boolean isCorrect(String infixExpression) {
return (24 == PostfixNotation.evaluateInfix(infixExpression));
}
private boolean isValid(String infixExpression) {
String[] tokens = separateExpression(infixExpression);
// Check is tokens contains any letters
for (String s : tokens) {
for (char ch : s.toCharArray()) {
if (Character.isAlphabetic(ch))
return false;
}
}
Stack operands = new Stack<>();
for (String token : tokens) {
if (!isOperator(token.charAt(0))) {
operands.push(Integer.parseInt(token));
}
}
if (operands.size() != validNumbers.length)
return false;
// Put validNumbers into a buffer ArrayList
ArrayList validOperands = new ArrayList<>();
for (int num : validNumbers) {
validOperands.add(num);
}
for (int i = 0; i < validOperands.size(); i++) {
int number = operands.pop();
int index = validOperands.indexOf(number);
if (index != -1) {
validOperands.remove(index);
} else {
return false;
}
}
return true;
}
public static void main(String[] args) {
Application.launch(args);
}
}
import ToolKit.PostfixNotation;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.Stack;
public class Exercise_13 extends Application {
int[] validNumbers = new int[4];
@Override
public void start(Stage primaryStage) {
// Top pane
Button btRefresh = new Button("Shuffle");
Label lblStatus = new Label("");
HBox topPane = new HBox(lblStatus, btRefresh);
topPane.setAlignment(Pos.BASELINE_RIGHT);
topPane.setSpacing(10);
// Center Pane
HBox centerPane = new HBox();
centerPane.setAlignment(Pos.CENTER);
centerPane.setSpacing(10);
centerPane.setPadding(new Insets(10));
// set first 4 random cards
setRandomCards(centerPane);
// Bottom pane
TextField tfExpression = new TextField();
Label lblExpression = new Label("Enter an expression:");
Button btVerify = new Button("Verify");
HBox bottomPane = new HBox(10, lblExpression, tfExpression, btVerify);
// Container Pane
BorderPane borderPane = new BorderPane();
borderPane.setPadding(new Insets(10));
borderPane.setTop(topPane);
borderPane.setCenter(centerPane);
borderPane.setBottom(bottomPane);
// Listeners
btRefresh.setOnAction(e -> {
lblStatus.setText("");
setRandomCards(centerPane);
});
btVerify.setOnAction(e -> {
String expression = tfExpression.getText();
if (isValid(expression) && isCorrect(expression)) {
lblStatus.setText("Good job! " + expression + " = 24");
} else {
lblStatus.setText("Invalid Expression");
}
});
Scene scene = new Scene(borderPane);
primaryStage.setTitle("4 Random Cards");
primaryStage.setScene(scene);
primaryStage.show();
// Debug
}
private void setRandomCards(HBox pane) {
boolean[] usedCards = new boolean[52];
// choose 4 random distinct cards from the deck
int count = 0;
pane.getChildren().clear();
while (count < 4) {
int card = (int) (Math.random() * 52);
if (!usedCards[card]) {
usedCards[card] = true;
pane.getChildren().add(new ImageView(new Image("image/card/" + (++card) +
".png")));
int value = card % 13;
validNumbers[count] = (value == 0) ? 13 : value;
count++;
}
}
}
private static boolean isOperator(char ch) {
return (ch == '(' ||
ch == ')' ||
isArithmeticOperator(ch));
}
private static boolean isArithmeticOperator(char ch) {
return (ch == '/' ||
ch == '+' ||
ch == '-' ||
ch == '*');
}
private static String[] separateExpression(String s) {
ArrayList tokens = new ArrayList<>(30);
char[] chars = s.toCharArray();
String numBuffer = "";
for (char ch : chars) {
if (isOperator(ch)) {
if (numBuffer.length() > 0) {
tokens.add(numBuffer);
numBuffer = "";
}
tokens.add(ch + "");
} else {
if (ch != ' ')
numBuffer += ch;
}
}
if (numBuffer.length() > 0) {
tokens.add(numBuffer);
}
return tokens.toArray(new String[tokens.size()]);
}
private boolean isCorrect(String infixExpression) {
return (24 == PostfixNotation.evaluateInfix(infixExpression));
}
private boolean isValid(String infixExpression) {
String[] tokens = separateExpression(infixExpression);
// Check is tokens contains any letters
for (String s : tokens) {
for (char ch : s.toCharArray()) {
if (Character.isAlphabetic(ch))
return false;
}
}
Stack operands = new Stack<>();
for (String token : tokens) {
if (!isOperator(token.charAt(0))) {
operands.push(Integer.parseInt(token));
}
}
if (operands.size() != validNumbers.length)
return false;
// Put validNumbers into a buffer ArrayList
ArrayList validOperands = new ArrayList<>();
for (int num : validNumbers) {
validOperands.add(num);
}
for (int i = 0; i < validOperands.size(); i++) {
int number = operands.pop();
int index = validOperands.indexOf(number);
if (index != -1) {
validOperands.remove(index);
} else {
return false;
}
}
return true;
}
public static void main(String[] args) {
Application.launch(args);
}
}
Solution
package Chapter_20;
import ToolKit.PostfixNotation;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.Stack;
public class Exercise_13 extends Application {
int[] validNumbers = new int[4];
@Override
public void start(Stage primaryStage) {
// Top pane
Button btRefresh = new Button("Shuffle");
Label lblStatus = new Label("");
HBox topPane = new HBox(lblStatus, btRefresh);
topPane.setAlignment(Pos.BASELINE_RIGHT);
topPane.setSpacing(10);
// Center Pane
HBox centerPane = new HBox();
centerPane.setAlignment(Pos.CENTER);
centerPane.setSpacing(10);
centerPane.setPadding(new Insets(10));
// set first 4 random cards
setRandomCards(centerPane);
// Bottom pane
TextField tfExpression = new TextField();
Label lblExpression = new Label("Enter an expression:");
Button btVerify = new Button("Verify");
HBox bottomPane = new HBox(10, lblExpression, tfExpression, btVerify);
// Container Pane
BorderPane borderPane = new BorderPane();
borderPane.setPadding(new Insets(10));
borderPane.setTop(topPane);
borderPane.setCenter(centerPane);
borderPane.setBottom(bottomPane);
// Listeners
btRefresh.setOnAction(e -> {
lblStatus.setText("");
setRandomCards(centerPane);
});
btVerify.setOnAction(e -> {
String expression = tfExpression.getText();
if (isValid(expression) && isCorrect(expression)) {
lblStatus.setText("Good job! " + expression + " = 24");
} else {
lblStatus.setText("Invalid Expression");
}
});
Scene scene = new Scene(borderPane);
primaryStage.setTitle("4 Random Cards");
primaryStage.setScene(scene);
primaryStage.show();
// Debug
}
private void setRandomCards(HBox pane) {
boolean[] usedCards = new boolean[52];
// choose 4 random distinct cards from the deck
int count = 0;
pane.getChildren().clear();
while (count < 4) {
int card = (int) (Math.random() * 52);
if (!usedCards[card]) {
usedCards[card] = true;
pane.getChildren().add(new ImageView(new Image("image/card/" + (++card) +
".png")));
int value = card % 13;
validNumbers[count] = (value == 0) ? 13 : value;
count++;
}
}
}
private static boolean isOperator(char ch) {
return (ch == '(' ||
ch == ')' ||
isArithmeticOperator(ch));
}
private static boolean isArithmeticOperator(char ch) {
return (ch == '/' ||
ch == '+' ||
ch == '-' ||
ch == '*');
}
private static String[] separateExpression(String s) {
ArrayList tokens = new ArrayList<>(30);
char[] chars = s.toCharArray();
String numBuffer = "";
for (char ch : chars) {
if (isOperator(ch)) {
if (numBuffer.length() > 0) {
tokens.add(numBuffer);
numBuffer = "";
}
tokens.add(ch + "");
} else {
if (ch != ' ')
numBuffer += ch;
}
}
if (numBuffer.length() > 0) {
tokens.add(numBuffer);
}
return tokens.toArray(new String[tokens.size()]);
}
private boolean isCorrect(String infixExpression) {
return (24 == PostfixNotation.evaluateInfix(infixExpression));
}
private boolean isValid(String infixExpression) {
String[] tokens = separateExpression(infixExpression);
// Check is tokens contains any letters
for (String s : tokens) {
for (char ch : s.toCharArray()) {
if (Character.isAlphabetic(ch))
return false;
}
}
Stack operands = new Stack<>();
for (String token : tokens) {
if (!isOperator(token.charAt(0))) {
operands.push(Integer.parseInt(token));
}
}
if (operands.size() != validNumbers.length)
return false;
// Put validNumbers into a buffer ArrayList
ArrayList validOperands = new ArrayList<>();
for (int num : validNumbers) {
validOperands.add(num);
}
for (int i = 0; i < validOperands.size(); i++) {
int number = operands.pop();
int index = validOperands.indexOf(number);
if (index != -1) {
validOperands.remove(index);
} else {
return false;
}
}
return true;
}
public static void main(String[] args) {
Application.launch(args);
}
}
import ToolKit.PostfixNotation;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.Stack;
public class Exercise_13 extends Application {
int[] validNumbers = new int[4];
@Override
public void start(Stage primaryStage) {
// Top pane
Button btRefresh = new Button("Shuffle");
Label lblStatus = new Label("");
HBox topPane = new HBox(lblStatus, btRefresh);
topPane.setAlignment(Pos.BASELINE_RIGHT);
topPane.setSpacing(10);
// Center Pane
HBox centerPane = new HBox();
centerPane.setAlignment(Pos.CENTER);
centerPane.setSpacing(10);
centerPane.setPadding(new Insets(10));
// set first 4 random cards
setRandomCards(centerPane);
// Bottom pane
TextField tfExpression = new TextField();
Label lblExpression = new Label("Enter an expression:");
Button btVerify = new Button("Verify");
HBox bottomPane = new HBox(10, lblExpression, tfExpression, btVerify);
// Container Pane
BorderPane borderPane = new BorderPane();
borderPane.setPadding(new Insets(10));
borderPane.setTop(topPane);
borderPane.setCenter(centerPane);
borderPane.setBottom(bottomPane);
// Listeners
btRefresh.setOnAction(e -> {
lblStatus.setText("");
setRandomCards(centerPane);
});
btVerify.setOnAction(e -> {
String expression = tfExpression.getText();
if (isValid(expression) && isCorrect(expression)) {
lblStatus.setText("Good job! " + expression + " = 24");
} else {
lblStatus.setText("Invalid Expression");
}
});
Scene scene = new Scene(borderPane);
primaryStage.setTitle("4 Random Cards");
primaryStage.setScene(scene);
primaryStage.show();
// Debug
}
private void setRandomCards(HBox pane) {
boolean[] usedCards = new boolean[52];
// choose 4 random distinct cards from the deck
int count = 0;
pane.getChildren().clear();
while (count < 4) {
int card = (int) (Math.random() * 52);
if (!usedCards[card]) {
usedCards[card] = true;
pane.getChildren().add(new ImageView(new Image("image/card/" + (++card) +
".png")));
int value = card % 13;
validNumbers[count] = (value == 0) ? 13 : value;
count++;
}
}
}
private static boolean isOperator(char ch) {
return (ch == '(' ||
ch == ')' ||
isArithmeticOperator(ch));
}
private static boolean isArithmeticOperator(char ch) {
return (ch == '/' ||
ch == '+' ||
ch == '-' ||
ch == '*');
}
private static String[] separateExpression(String s) {
ArrayList tokens = new ArrayList<>(30);
char[] chars = s.toCharArray();
String numBuffer = "";
for (char ch : chars) {
if (isOperator(ch)) {
if (numBuffer.length() > 0) {
tokens.add(numBuffer);
numBuffer = "";
}
tokens.add(ch + "");
} else {
if (ch != ' ')
numBuffer += ch;
}
}
if (numBuffer.length() > 0) {
tokens.add(numBuffer);
}
return tokens.toArray(new String[tokens.size()]);
}
private boolean isCorrect(String infixExpression) {
return (24 == PostfixNotation.evaluateInfix(infixExpression));
}
private boolean isValid(String infixExpression) {
String[] tokens = separateExpression(infixExpression);
// Check is tokens contains any letters
for (String s : tokens) {
for (char ch : s.toCharArray()) {
if (Character.isAlphabetic(ch))
return false;
}
}
Stack operands = new Stack<>();
for (String token : tokens) {
if (!isOperator(token.charAt(0))) {
operands.push(Integer.parseInt(token));
}
}
if (operands.size() != validNumbers.length)
return false;
// Put validNumbers into a buffer ArrayList
ArrayList validOperands = new ArrayList<>();
for (int num : validNumbers) {
validOperands.add(num);
}
for (int i = 0; i < validOperands.size(); i++) {
int number = operands.pop();
int index = validOperands.indexOf(number);
if (index != -1) {
validOperands.remove(index);
} else {
return false;
}
}
return true;
}
public static void main(String[] args) {
Application.launch(args);
}
}

Contenu connexe

Similaire à   package Chapter_20;import ToolKit.PostfixNotation;import javaf.pdf

H base programmingH base programming
H base programmingMuthusamy Manigandan
769 vues20 diapositives

Similaire à   package Chapter_20;import ToolKit.PostfixNotation;import javaf.pdf(20)

The java language cheat sheetThe java language cheat sheet
The java language cheat sheet
anand_study36 vues
H base programmingH base programming
H base programming
Muthusamy Manigandan769 vues
SDC - Einführung in ScalaSDC - Einführung in Scala
SDC - Einführung in Scala
Christian Baranowski1.2K vues
Java cheatsheetJava cheatsheet
Java cheatsheet
Anass SABANI269 vues
WOTC_ImportWOTC_Import
WOTC_Import
Luther Quinn193 vues
Google GuavaGoogle Guava
Google Guava
Alexander Korotkikh6.9K vues
Railway reservation systemRailway reservation system
Railway reservation system
Prashant Sharma2.9K vues
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
Nithin Kumar,VVCE, Mysuru3.5K vues
week-18xweek-18x
week-18x
KITE www.kitecolleges.com489 vues
week-17xweek-17x
week-17x
KITE www.kitecolleges.com201 vues
AngularJS TestingAngularJS Testing
AngularJS Testing
Eyal Vardi8.6K vues
JBUG 11 - Scala For Java ProgrammersJBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java Programmers
Tikal Knowledge1K vues

Plus de sudhirchourasia86(20)

Dernier

Narration lesson plan.docxNarration lesson plan.docx
Narration lesson plan.docxTARIQ KHAN
92 vues11 diapositives
ICANNICANN
ICANNRajaulKarim20
61 vues13 diapositives
Drama KS5 BreakdownDrama KS5 Breakdown
Drama KS5 BreakdownWestHatch
54 vues2 diapositives
Narration  ppt.pptxNarration  ppt.pptx
Narration ppt.pptxTARIQ KHAN
76 vues24 diapositives

Dernier(20)

Narration lesson plan.docxNarration lesson plan.docx
Narration lesson plan.docx
TARIQ KHAN92 vues
ICANNICANN
ICANN
RajaulKarim2061 vues
Drama KS5 BreakdownDrama KS5 Breakdown
Drama KS5 Breakdown
WestHatch54 vues
Narration  ppt.pptxNarration  ppt.pptx
Narration ppt.pptx
TARIQ KHAN76 vues
ACTIVITY BOOK key water sports.pptxACTIVITY BOOK key water sports.pptx
ACTIVITY BOOK key water sports.pptx
Mar Caston Palacio275 vues
SIMPLE PRESENT TENSE_new.pptxSIMPLE PRESENT TENSE_new.pptx
SIMPLE PRESENT TENSE_new.pptx
nisrinamadani2159 vues
discussion post.pdfdiscussion post.pdf
discussion post.pdf
jessemercerail85 vues
Azure DevOps Pipeline setup for Mule APIs #36Azure DevOps Pipeline setup for Mule APIs #36
Azure DevOps Pipeline setup for Mule APIs #36
MysoreMuleSoftMeetup84 vues
Scope of Biochemistry.pptxScope of Biochemistry.pptx
Scope of Biochemistry.pptx
shoba shoba119 vues
Streaming Quiz 2023.pdfStreaming Quiz 2023.pdf
Streaming Quiz 2023.pdf
Quiz Club NITW97 vues
Sociology KS5Sociology KS5
Sociology KS5
WestHatch52 vues
Structure and Functions of Cell.pdfStructure and Functions of Cell.pdf
Structure and Functions of Cell.pdf
Nithya Murugan256 vues
Chemistry of sex hormones.pptxChemistry of sex hormones.pptx
Chemistry of sex hormones.pptx
RAJ K. MAURYA107 vues
2022 CAPE Merit List 2023 2022 CAPE Merit List 2023
2022 CAPE Merit List 2023
Caribbean Examinations Council3.5K vues

  package Chapter_20;import ToolKit.PostfixNotation;import javaf.pdf

  • 1. package Chapter_20; import ToolKit.PostfixNotation; import javafx.application.Application; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.stage.Stage; import java.util.ArrayList; import java.util.Stack; public class Exercise_13 extends Application { int[] validNumbers = new int[4]; @Override public void start(Stage primaryStage) { // Top pane Button btRefresh = new Button("Shuffle"); Label lblStatus = new Label(""); HBox topPane = new HBox(lblStatus, btRefresh); topPane.setAlignment(Pos.BASELINE_RIGHT); topPane.setSpacing(10); // Center Pane HBox centerPane = new HBox(); centerPane.setAlignment(Pos.CENTER); centerPane.setSpacing(10); centerPane.setPadding(new Insets(10)); // set first 4 random cards setRandomCards(centerPane); // Bottom pane TextField tfExpression = new TextField();
  • 2. Label lblExpression = new Label("Enter an expression:"); Button btVerify = new Button("Verify"); HBox bottomPane = new HBox(10, lblExpression, tfExpression, btVerify); // Container Pane BorderPane borderPane = new BorderPane(); borderPane.setPadding(new Insets(10)); borderPane.setTop(topPane); borderPane.setCenter(centerPane); borderPane.setBottom(bottomPane); // Listeners btRefresh.setOnAction(e -> { lblStatus.setText(""); setRandomCards(centerPane); }); btVerify.setOnAction(e -> { String expression = tfExpression.getText(); if (isValid(expression) && isCorrect(expression)) { lblStatus.setText("Good job! " + expression + " = 24"); } else { lblStatus.setText("Invalid Expression"); } }); Scene scene = new Scene(borderPane); primaryStage.setTitle("4 Random Cards"); primaryStage.setScene(scene); primaryStage.show(); // Debug } private void setRandomCards(HBox pane) { boolean[] usedCards = new boolean[52]; // choose 4 random distinct cards from the deck int count = 0; pane.getChildren().clear(); while (count < 4) { int card = (int) (Math.random() * 52); if (!usedCards[card]) {
  • 3. usedCards[card] = true; pane.getChildren().add(new ImageView(new Image("image/card/" + (++card) + ".png"))); int value = card % 13; validNumbers[count] = (value == 0) ? 13 : value; count++; } } } private static boolean isOperator(char ch) { return (ch == '(' || ch == ')' || isArithmeticOperator(ch)); } private static boolean isArithmeticOperator(char ch) { return (ch == '/' || ch == '+' || ch == '-' || ch == '*'); } private static String[] separateExpression(String s) { ArrayList tokens = new ArrayList<>(30); char[] chars = s.toCharArray(); String numBuffer = ""; for (char ch : chars) { if (isOperator(ch)) { if (numBuffer.length() > 0) { tokens.add(numBuffer); numBuffer = ""; } tokens.add(ch + ""); } else { if (ch != ' ') numBuffer += ch; } }
  • 4. if (numBuffer.length() > 0) { tokens.add(numBuffer); } return tokens.toArray(new String[tokens.size()]); } private boolean isCorrect(String infixExpression) { return (24 == PostfixNotation.evaluateInfix(infixExpression)); } private boolean isValid(String infixExpression) { String[] tokens = separateExpression(infixExpression); // Check is tokens contains any letters for (String s : tokens) { for (char ch : s.toCharArray()) { if (Character.isAlphabetic(ch)) return false; } } Stack operands = new Stack<>(); for (String token : tokens) { if (!isOperator(token.charAt(0))) { operands.push(Integer.parseInt(token)); } } if (operands.size() != validNumbers.length) return false; // Put validNumbers into a buffer ArrayList ArrayList validOperands = new ArrayList<>(); for (int num : validNumbers) { validOperands.add(num); } for (int i = 0; i < validOperands.size(); i++) { int number = operands.pop(); int index = validOperands.indexOf(number); if (index != -1) { validOperands.remove(index); } else {
  • 5. return false; } } return true; } public static void main(String[] args) { Application.launch(args); } } import ToolKit.PostfixNotation; import javafx.application.Application; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.stage.Stage; import java.util.ArrayList; import java.util.Stack; public class Exercise_13 extends Application { int[] validNumbers = new int[4]; @Override public void start(Stage primaryStage) { // Top pane Button btRefresh = new Button("Shuffle"); Label lblStatus = new Label(""); HBox topPane = new HBox(lblStatus, btRefresh); topPane.setAlignment(Pos.BASELINE_RIGHT); topPane.setSpacing(10); // Center Pane HBox centerPane = new HBox();
  • 6. centerPane.setAlignment(Pos.CENTER); centerPane.setSpacing(10); centerPane.setPadding(new Insets(10)); // set first 4 random cards setRandomCards(centerPane); // Bottom pane TextField tfExpression = new TextField(); Label lblExpression = new Label("Enter an expression:"); Button btVerify = new Button("Verify"); HBox bottomPane = new HBox(10, lblExpression, tfExpression, btVerify); // Container Pane BorderPane borderPane = new BorderPane(); borderPane.setPadding(new Insets(10)); borderPane.setTop(topPane); borderPane.setCenter(centerPane); borderPane.setBottom(bottomPane); // Listeners btRefresh.setOnAction(e -> { lblStatus.setText(""); setRandomCards(centerPane); }); btVerify.setOnAction(e -> { String expression = tfExpression.getText(); if (isValid(expression) && isCorrect(expression)) { lblStatus.setText("Good job! " + expression + " = 24"); } else { lblStatus.setText("Invalid Expression"); } }); Scene scene = new Scene(borderPane); primaryStage.setTitle("4 Random Cards"); primaryStage.setScene(scene); primaryStage.show(); // Debug } private void setRandomCards(HBox pane) {
  • 7. boolean[] usedCards = new boolean[52]; // choose 4 random distinct cards from the deck int count = 0; pane.getChildren().clear(); while (count < 4) { int card = (int) (Math.random() * 52); if (!usedCards[card]) { usedCards[card] = true; pane.getChildren().add(new ImageView(new Image("image/card/" + (++card) + ".png"))); int value = card % 13; validNumbers[count] = (value == 0) ? 13 : value; count++; } } } private static boolean isOperator(char ch) { return (ch == '(' || ch == ')' || isArithmeticOperator(ch)); } private static boolean isArithmeticOperator(char ch) { return (ch == '/' || ch == '+' || ch == '-' || ch == '*'); } private static String[] separateExpression(String s) { ArrayList tokens = new ArrayList<>(30); char[] chars = s.toCharArray(); String numBuffer = ""; for (char ch : chars) { if (isOperator(ch)) { if (numBuffer.length() > 0) { tokens.add(numBuffer); numBuffer = "";
  • 8. } tokens.add(ch + ""); } else { if (ch != ' ') numBuffer += ch; } } if (numBuffer.length() > 0) { tokens.add(numBuffer); } return tokens.toArray(new String[tokens.size()]); } private boolean isCorrect(String infixExpression) { return (24 == PostfixNotation.evaluateInfix(infixExpression)); } private boolean isValid(String infixExpression) { String[] tokens = separateExpression(infixExpression); // Check is tokens contains any letters for (String s : tokens) { for (char ch : s.toCharArray()) { if (Character.isAlphabetic(ch)) return false; } } Stack operands = new Stack<>(); for (String token : tokens) { if (!isOperator(token.charAt(0))) { operands.push(Integer.parseInt(token)); } } if (operands.size() != validNumbers.length) return false; // Put validNumbers into a buffer ArrayList ArrayList validOperands = new ArrayList<>(); for (int num : validNumbers) { validOperands.add(num);
  • 9. } for (int i = 0; i < validOperands.size(); i++) { int number = operands.pop(); int index = validOperands.indexOf(number); if (index != -1) { validOperands.remove(index); } else { return false; } } return true; } public static void main(String[] args) { Application.launch(args); } } Solution package Chapter_20; import ToolKit.PostfixNotation; import javafx.application.Application; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.stage.Stage; import java.util.ArrayList; import java.util.Stack; public class Exercise_13 extends Application {
  • 10. int[] validNumbers = new int[4]; @Override public void start(Stage primaryStage) { // Top pane Button btRefresh = new Button("Shuffle"); Label lblStatus = new Label(""); HBox topPane = new HBox(lblStatus, btRefresh); topPane.setAlignment(Pos.BASELINE_RIGHT); topPane.setSpacing(10); // Center Pane HBox centerPane = new HBox(); centerPane.setAlignment(Pos.CENTER); centerPane.setSpacing(10); centerPane.setPadding(new Insets(10)); // set first 4 random cards setRandomCards(centerPane); // Bottom pane TextField tfExpression = new TextField(); Label lblExpression = new Label("Enter an expression:"); Button btVerify = new Button("Verify"); HBox bottomPane = new HBox(10, lblExpression, tfExpression, btVerify); // Container Pane BorderPane borderPane = new BorderPane(); borderPane.setPadding(new Insets(10)); borderPane.setTop(topPane); borderPane.setCenter(centerPane); borderPane.setBottom(bottomPane); // Listeners btRefresh.setOnAction(e -> { lblStatus.setText(""); setRandomCards(centerPane); }); btVerify.setOnAction(e -> { String expression = tfExpression.getText(); if (isValid(expression) && isCorrect(expression)) { lblStatus.setText("Good job! " + expression + " = 24");
  • 11. } else { lblStatus.setText("Invalid Expression"); } }); Scene scene = new Scene(borderPane); primaryStage.setTitle("4 Random Cards"); primaryStage.setScene(scene); primaryStage.show(); // Debug } private void setRandomCards(HBox pane) { boolean[] usedCards = new boolean[52]; // choose 4 random distinct cards from the deck int count = 0; pane.getChildren().clear(); while (count < 4) { int card = (int) (Math.random() * 52); if (!usedCards[card]) { usedCards[card] = true; pane.getChildren().add(new ImageView(new Image("image/card/" + (++card) + ".png"))); int value = card % 13; validNumbers[count] = (value == 0) ? 13 : value; count++; } } } private static boolean isOperator(char ch) { return (ch == '(' || ch == ')' || isArithmeticOperator(ch)); } private static boolean isArithmeticOperator(char ch) { return (ch == '/' || ch == '+' || ch == '-' ||
  • 12. ch == '*'); } private static String[] separateExpression(String s) { ArrayList tokens = new ArrayList<>(30); char[] chars = s.toCharArray(); String numBuffer = ""; for (char ch : chars) { if (isOperator(ch)) { if (numBuffer.length() > 0) { tokens.add(numBuffer); numBuffer = ""; } tokens.add(ch + ""); } else { if (ch != ' ') numBuffer += ch; } } if (numBuffer.length() > 0) { tokens.add(numBuffer); } return tokens.toArray(new String[tokens.size()]); } private boolean isCorrect(String infixExpression) { return (24 == PostfixNotation.evaluateInfix(infixExpression)); } private boolean isValid(String infixExpression) { String[] tokens = separateExpression(infixExpression); // Check is tokens contains any letters for (String s : tokens) { for (char ch : s.toCharArray()) { if (Character.isAlphabetic(ch)) return false; } } Stack operands = new Stack<>();
  • 13. for (String token : tokens) { if (!isOperator(token.charAt(0))) { operands.push(Integer.parseInt(token)); } } if (operands.size() != validNumbers.length) return false; // Put validNumbers into a buffer ArrayList ArrayList validOperands = new ArrayList<>(); for (int num : validNumbers) { validOperands.add(num); } for (int i = 0; i < validOperands.size(); i++) { int number = operands.pop(); int index = validOperands.indexOf(number); if (index != -1) { validOperands.remove(index); } else { return false; } } return true; } public static void main(String[] args) { Application.launch(args); } } import ToolKit.PostfixNotation; import javafx.application.Application; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.image.Image;
  • 14. import javafx.scene.image.ImageView; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.stage.Stage; import java.util.ArrayList; import java.util.Stack; public class Exercise_13 extends Application { int[] validNumbers = new int[4]; @Override public void start(Stage primaryStage) { // Top pane Button btRefresh = new Button("Shuffle"); Label lblStatus = new Label(""); HBox topPane = new HBox(lblStatus, btRefresh); topPane.setAlignment(Pos.BASELINE_RIGHT); topPane.setSpacing(10); // Center Pane HBox centerPane = new HBox(); centerPane.setAlignment(Pos.CENTER); centerPane.setSpacing(10); centerPane.setPadding(new Insets(10)); // set first 4 random cards setRandomCards(centerPane); // Bottom pane TextField tfExpression = new TextField(); Label lblExpression = new Label("Enter an expression:"); Button btVerify = new Button("Verify"); HBox bottomPane = new HBox(10, lblExpression, tfExpression, btVerify); // Container Pane BorderPane borderPane = new BorderPane(); borderPane.setPadding(new Insets(10)); borderPane.setTop(topPane); borderPane.setCenter(centerPane); borderPane.setBottom(bottomPane); // Listeners btRefresh.setOnAction(e -> {
  • 15. lblStatus.setText(""); setRandomCards(centerPane); }); btVerify.setOnAction(e -> { String expression = tfExpression.getText(); if (isValid(expression) && isCorrect(expression)) { lblStatus.setText("Good job! " + expression + " = 24"); } else { lblStatus.setText("Invalid Expression"); } }); Scene scene = new Scene(borderPane); primaryStage.setTitle("4 Random Cards"); primaryStage.setScene(scene); primaryStage.show(); // Debug } private void setRandomCards(HBox pane) { boolean[] usedCards = new boolean[52]; // choose 4 random distinct cards from the deck int count = 0; pane.getChildren().clear(); while (count < 4) { int card = (int) (Math.random() * 52); if (!usedCards[card]) { usedCards[card] = true; pane.getChildren().add(new ImageView(new Image("image/card/" + (++card) + ".png"))); int value = card % 13; validNumbers[count] = (value == 0) ? 13 : value; count++; } } } private static boolean isOperator(char ch) { return (ch == '(' ||
  • 16. ch == ')' || isArithmeticOperator(ch)); } private static boolean isArithmeticOperator(char ch) { return (ch == '/' || ch == '+' || ch == '-' || ch == '*'); } private static String[] separateExpression(String s) { ArrayList tokens = new ArrayList<>(30); char[] chars = s.toCharArray(); String numBuffer = ""; for (char ch : chars) { if (isOperator(ch)) { if (numBuffer.length() > 0) { tokens.add(numBuffer); numBuffer = ""; } tokens.add(ch + ""); } else { if (ch != ' ') numBuffer += ch; } } if (numBuffer.length() > 0) { tokens.add(numBuffer); } return tokens.toArray(new String[tokens.size()]); } private boolean isCorrect(String infixExpression) { return (24 == PostfixNotation.evaluateInfix(infixExpression)); } private boolean isValid(String infixExpression) { String[] tokens = separateExpression(infixExpression); // Check is tokens contains any letters
  • 17. for (String s : tokens) { for (char ch : s.toCharArray()) { if (Character.isAlphabetic(ch)) return false; } } Stack operands = new Stack<>(); for (String token : tokens) { if (!isOperator(token.charAt(0))) { operands.push(Integer.parseInt(token)); } } if (operands.size() != validNumbers.length) return false; // Put validNumbers into a buffer ArrayList ArrayList validOperands = new ArrayList<>(); for (int num : validNumbers) { validOperands.add(num); } for (int i = 0; i < validOperands.size(); i++) { int number = operands.pop(); int index = validOperands.indexOf(number); if (index != -1) { validOperands.remove(index); } else { return false; } } return true; } public static void main(String[] args) { Application.launch(args); } }