Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PURPOSE: This lab will introduce you to Javas framework for working with simple user interfaces, as well as dialog boxes for I/O. PROCEDURES: You are

PURPOSE: This lab will introduce you to Javas framework for working with simple user interfaces, as well as dialog boxes for I/O.

PROCEDURES: You are provided with a starter project for this lab. Import this project file (in NetBeans Use File->Import->From Zip). CS1181GradeCalculator uses JavaFX to display a series of text fields that allow the user to enter in their scores on two assignments, along with the number of points possible. There is also a button called Calculate Grade that computes the users overall grade based on the total points they earned divided by the total points possible across both assignments. Step One: Clean up the code - use an array or list of objects to represent related information As written, the class is a coding horror.

The assignments are represented by a series of variables within the main class, which can make it difficult to debug, change code, or add additional assignments. To address this problem, your first task is to develop an Assignment class that will contain all of the information needed to display the labels and text fields and compute the overall grade. Update CS1181GradeCalculator as necessary to use instances of your new Assignment class instead of the variables used in the initial code.

The Assignment class should have a constructor that correctly instantiates each of the classs attributes, as well as accessor methods as necessary to allow the main class to achieve the desired functionality (i.e. add the HBox panes for each assignment to the root pane and retrieve the pointsMade and pointsPossible values necessary for computing the overall grade).

The start method and the action listener for the Calculate Grade button should call these accessor methods as needed. Your code should be flexible. It should allow for any number of assignments using some sort of container (such as an ArrayList).

To begin, create two instances of your Assignment class that are identical to those in the starter code, add those to your list, and demonstrate that your more elegant code performs the same task as the starter code. Next, prompt the user (I recommend using a TextInputDialog, as shown to the right) to select the number of assignments that will appear in the panel.

Create and add this number of assignments to your collection and then check that the overall grade computation computes the correct value.

=========================================================

THIS IS THE CODE WAS GIVEN:

=========================================================

package cs1181lab03startercode;

import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Alert; import javafx.scene.control.Alert.AlertType; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage;

/** * JavaFX CS1181GradeCalculator */ public class CS1181GradeCalculator extends Application {

private final int INITIAL_WIDTH_IN_PIXELS = 500; private final int INITAL_HEIGHT_IN_PIXELS = 400;

private HBox pane1; private Label label1; // WARNING: This is a coding HORROR private TextField pointsMade1; // You will implement this elegantly private Label slash1; // by creating an Assignment class that private TextField pointsPossible1; // encapsulates all of this information private HBox pane2; // in a single class. private Label label2; private TextField pointsMade2; private Label slash2; private TextField pointsPossible2;

private Scene scene; @Override public void start(Stage stage) {

// Initialize the Stage stage.setTitle("Grade Calculator"); VBox root = new VBox(10); root.setPadding(new Insets(10, 10, 10, 10)); scene = new Scene(root, INITIAL_WIDTH_IN_PIXELS, INITAL_HEIGHT_IN_PIXELS); stage.setScene(scene); stage.show();

VBox assignmentPane = new VBox(10); // Initialize two Assignments and add them to the panel/scene pane1 = new HBox(10); label1 = new Label("Assignment 1"); pointsMade1 = new TextField(); slash1 = new Label("/"); pointsPossible1 = new TextField(); pane1.getChildren().addAll(label1, pointsMade1, slash1, pointsPossible1);

pane2 = new HBox(10); label2 = new Label("Assignment 2"); pointsMade2 = new TextField(); slash2 = new Label("/"); pointsPossible2 = new TextField(); pane2.getChildren().addAll(label2, pointsMade2, slash2, pointsPossible2);

assignmentPane.getChildren().add(pane1); assignmentPane.getChildren().add(pane2); root.getChildren().add(assignmentPane); Button calculateButton = new Button("Calculate Grade"); root.getChildren().add(calculateButton); calculateButton.setOnAction(e -> { int pointsMade = 0; int pointsPossible = 0; pointsMade += Integer.parseInt(pointsMade1.getText()); pointsPossible += Integer.parseInt(pointsPossible1.getText()); pointsMade += Integer.parseInt(pointsMade2.getText()); pointsPossible += Integer.parseInt(pointsPossible2.getText()); // calculate grade and round to the nearest tenth double grade = pointsMade / (double) pointsPossible; grade = Math.round(grade * 1000); grade /= 10; Alert alert = new Alert(AlertType.INFORMATION); alert.setTitle("Grade Calculator"); alert.setHeaderText(null); alert.setContentText("Grade: " + grade); alert.showAndWait(); });

} // end method start

public static void main(String[] args) { launch(args); } // end method main

} // end class

I really need help with this!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Concepts, Theories, And Applications

Authors: Shui Yu, Song Guo

1st Edition

3319277634, 9783319277639

More Books

Students also viewed these Databases questions

Question

Why is it critical to develop a time-phased baseline?

Answered: 1 week ago

Question

1. Identify the sources for this conflict.

Answered: 1 week ago

Question

3. The group answers the questions.

Answered: 1 week ago