need help with java Please DO NOT copy and paste from other chegg questions use
ID: 3911743 • Letter: N
Question
need help with java Please DO NOT copy and paste from other chegg questions
use JavaFX only
Write a GUI that estimates your grade for this course. Write a GUI that shows the user each item and its grading weight, and allows the user to enter a score for each item. The GUI should have a button that the user presses when all items are entered, and the GUI then displays the final grade. A sample GUI is below:
The event handler for the button to calculate a grade must be either an anonymous class or a lambda expression (your choice).
Actual grading weights.
Grading Weights Item Weight Labs 40% Midterm 25% Final Exam 25% Term Project 10% abs Weight 0.3 95 midterm Weight 0.25 85 project 1 Weight 0.125 95 project 2 Weight 0.125 93 final Weight 0.2 85 Calculate Grade Final Grade: 90.25Explanation / Answer
import javafx.application.Application;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.geometry.Insets;
public class MyApp extends Application{
/*
All the subclasses of JavaFx Application must implement the start() method of the Application class. This is the part where the program starts.
JavaFx intialises the start() method. So that is where we need to put all our components.
*/
/*
It is still preferable to add a main class, because it helps us take in command line arguments and it also makes it clear where our program starts.
When to use @Override --> Use it whenever you want to override a method, the advantage of it is it actually helps you use confirm that you are actually overriding method and compiler warns you if you misspelled your override method.
*/
double score;
@Override
public void start(Stage primStage) throws Exception{
/*
If we don't specify any layout, the Default layout gets used. The Default layout puts all the elements in the middle of the stage.
We don't want that to happen. So We choose a different layout. The most Flexible Layout I find is GridLayout. It gives us the authority
to set the positin of our components ourselves. Thus we need not worry about the looks.
So we create a GridPane which is a container to add all our components
It is important to know that the add() method of GridPane is used to add the component to the Panel. add() takes in 3 parameters. The first one is the component to be added and the the next one is thecolumn number we want to add to and the last parameter is the row.
*/
// Creating a GridPane Container
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20,20,20,20));
// Adding the first row
Label lab = new Label("Lab - Weight 0.4 ");
TextField lab_score = new TextField(" ");
//This means we will add our lab label to the 0th cloumn of the 1st row and lab_score textfield to the 1st cloumn of the 1st row
grid.add(lab, 0, 1);
grid.add(lab_score, 1, 1);
//Adding the second row
Label mid = new Label("MidTerm - Weight 0.25 ");
TextField mid_score = new TextField(" ");
//This means we will add our mid label to the 0th cloumn of the 2nd row and mid_score textfield to the 1st cloumn of the 2nd row
grid.add(mid, 0, 2);
grid.add(mid_score, 1, 2);
//Adding the third row
Label final_exam = new Label("Final Exam - Weight 0.25 ");
TextField final_exam_score = new TextField(" ");
grid.add(final_exam, 0, 3);
grid.add(final_exam_score, 1, 3);
/Adding the fourth row
Label proj = new Label("Term Project - Weight 0.1 ");
TextField proj_score = new TextField(" ");
grid.add(proj, 0, 4);
grid.add(proj_score, 1, 4);
//Adding the Button To calculate grade
Button grade_btn = new Button("Calculate grade");
grid.add(grade_btn,0,5);
//Adding the TextField of Final Score
TextField final_grade = new TextField("");
grid.add(final_grade,0,6);
//Setting an action for the 'Calculate Grade' button
// In this method we will take all the input from the textfield of all the above component and add them
grade_btn.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent e){
/*Remember the value that we get from TextField is an string we need to convert it to integer.We will use parseInt() to convert string to intege*/
float lab = Float.parseFloat(lab_score.getText());
float mid = Float.parseFloat(mid_score.getText());
float fin = Float.parseFloat(final_exam_score.getText());
float proj = Float.parseFloat(proj_score.getText());
// Calculate the score depending on the weight
score = (0.4*lab) + (0.25*mid) + (0.25*fin) + (0.1*proj);
String fin_score = Double.toString(score);
//Setting the text of the Final Score TextField.
final_grade.setText(fin_score);
}
});
//Let's add a scene now
Scene scene = new Scene(grid,400,500);
primStage.setTitle("Grade Calculator");
primStage.setScene(scene);
primStage.show();
}
public static void main(String[] args){
Application.launch(args);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.