Free Starter Class, MIT License
package application;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import performance.PerformanceUtil;
public class Main extends Application {
private Logger logger = Logger.getLogger(Main.class.getName());
private Random random = new Random(System.nanoTime());
private ArrayList<Integer> recordedTimes = new ArrayList<>();
private int value1=2;
private int value2=2;
public Canvas createCanvas() {
Canvas canvas = new Canvas(25,25);
canvasToColor(canvas,Color.RED);
canvas.setWidth(25);
canvas.setHeight(25);
return canvas;
}
public void canvasToColor(Canvas canvas, Color color) {
GraphicsContext graphicsContext = canvas.getGraphicsContext2D();
graphicsContext.setFill(Paint.valueOf(color.toString()));
graphicsContext.fillRect(0, 0, 25, 25);
}
Label encouragementLabel = new Label("Never Say Die!");
Label problemSolvedInLabel = new Label("Problem Solved in: ms");
Label averageLabel = new Label("Average: ms");
class Question {
int a;
int b;
int reanswerdRight=0;
}
ArrayList<Question> questionsAnsweredWrong = new ArrayList<>();
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
Scene scene = new Scene(root,400,500);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
VBox vbox = new VBox();
root.getChildren().add(vbox);
Canvas canvas = createCanvas();
vbox.getChildren().add(canvas);
vbox.setMinHeight(400);
Label answerCheckLabel = new Label("Test");
answerCheckLabel.setMinWidth(400);
answerCheckLabel.setMinHeight(200);
answerCheckLabel.setAlignment(Pos.CENTER);
vbox.getChildren().add(answerCheckLabel);
HBox hbox = new HBox();
vbox.getChildren().add(hbox);
hbox.setPadding(new Insets(10,10,10,10));
hbox.setMinWidth(400);
hbox.setMinHeight(200);
hbox.setAlignment(Pos.CENTER);
Label problemLabel = new Label(value1+" + "+value2);
problemLabel.setFont(Font.font(20));
PerformanceUtil.getInstance().start();
hbox.getChildren().add(problemLabel);
Label equalsLabel = new Label(" = ");
hbox.getChildren().add(equalsLabel);
TextField answerField = new TextField();
hbox.getChildren().add(answerField);
hbox.autosize();
vbox.getChildren().addAll(encouragementLabel,problemSolvedInLabel,averageLabel);
vbox.autosize();
answerField.setFont(Font.font(20));
answerField.setOnKeyReleased((event) -> {
KeyEvent keyEvent = (KeyEvent) event;
if (event.getCode()==KeyCode.ENTER) {
System.out.println("Enter Key Released: "+answerField.getText());
try {
int value = Integer.parseInt(answerField.getText());
int sum = value1+value2;
if (value == sum) {
canvasToColor(canvas,Color.GREEN);
answerCheckLabel.setText("Answer is correct");
System.out.println("Answer is correct");
PerformanceUtil.getInstance().stop();
System.out.println("Problem solved in: "+PerformanceUtil.getInstance().getRecordedTime()+" ns");
System.out.println("Problem solved in: "+PerformanceUtil.getInstance().getRecordedTimeMilliseconds()+" ms");
recordedTimes.add(PerformanceUtil.getInstance().getRecordedTimeMilliseconds());
problemSolvedInLabel.setText("Problem Solved in: "+PerformanceUtil.getInstance().getRecordedTimeMilliseconds()+" ms");
computeAverage();
List<Question> questions = questionsAnsweredWrong.stream().filter(q -> q.a==value1 && q.b==value2).toList();
if (questions.size()>0) {
Question question = questions.get(0);
question.reanswerdRight++;
if (question.reanswerdRight==3) { // Answered right 3 times, remove from questionsAnsweredWrong
logger.info("Wrong Question Answered Right 3 times");
questionsAnsweredWrong.remove(question);
}
}
answerField.setText("");
// 1 out of 4 times ask missed question
if (questionsAnsweredWrong.size()>0 && random.nextInt(0,3)==0) {
logger.info("Reasking Wrong Question");
// get random element from questonsAnsweredWrong
int randomElement = random.nextInt(0,questionsAnsweredWrong.size());
Question question = questionsAnsweredWrong.get(randomElement);
value1 = question.a;
value2 = question.b;
} else {
value1 = random.nextInt(0,10);
value2 = random.nextInt(0,10);
}
PerformanceUtil.getInstance().start();
problemLabel.setText(value1+" + "+value2);
} else {
List<Question> questions = questionsAnsweredWrong.stream().filter(q -> q.a==value1 && q.b==value2).toList();
if (questions.size()>0) {
questions.get(0).reanswerdRight--;
} else {
Question question = new Question();
question.a = value1;
question.b = value2;
questionsAnsweredWrong.add(question);
}
canvasToColor(canvas,Color.RED);
answerCheckLabel.setText("Answer is incorrect");
System.out.println("Answer is incorrect");
System.out.println("Problem solving running time: "+PerformanceUtil.getInstance().getRunningTime()+" ns");
}
} catch (NumberFormatException e) {
System.out.println("Could not parse number");
}
}
});
} catch(Exception e) {
e.printStackTrace();
}
}
public void computeAverage() {
int sum=0;
for (int i=0; i<recordedTimes.size(); i++) {
sum+=recordedTimes.get(i);
}
int average = sum/recordedTimes.size();
System.out.println("Current Average: "+average+" ms");
averageLabel.setText("Average: "+average+" ms");
}
public static void main(String[] args) {
launch(args);
}
}
Had an issue with stream already being processed when I tried to use a Stream variable to hold the list rather than using the toList() collector method.
Supplier is another option though I think there is the potential for different lists to be generated when the underlying stream generation is dynamic.
https://www.baeldung.com/java-stream-operated-upon-or-closed-exception
3 videos I found useful to help improve my coding skills
Another video I found useful to help improve my own general intelligence
Understanding Streams API, Atomic Integer and other Atomic Operations classes, and BlockingQueues can be helpful for enabling more gains with Java 19 Project Loom.
https://en.wikipedia.org/wiki/Frontier_(supercomputer)
https://www.olcf.ornl.gov/frontier/
606,208 cores – CPU cores
Not sure what operating system or middleware they use, but my guess is more enabled with virtual threads could lead to many less threads blocking and many more threads enabling giant amounts of throughput.
Throughput gains without Throughput gains gives feeling of bad deal.
https://en.wikipedia.org/wiki/Network_throughput
If Java could be leveraged in a way that included OpenCL integration by default could be much much more powerful
https://en.wikipedia.org/wiki/OpenCL
Graphics card support as a second class citizen until end of time? How about we enable real graphics support, until then Java gives a feeling of bad deal.
Ability to utilize specialized hardware for processing is the way of the future, Java either evolves or has greater potential to be lost to sands of time.
$600,000,000 investment for 600,000 cores. Basically $1000 per core if you add in facility, support, and power costs?
Power Efficiency likely h
AMD Ryzen 7 7730U looks like it has a really low TDP
https://www.newegg.com/classic-black-msi-modern-14-c7m-048us-work-business/p/N82E16834156444