9. You will be learning how to instantiate the second class with the variables to send, and also you will learn to code
Posted: Sun May 15, 2022 8:24 am
import javafx.application.Application; // entry point for all
javafx classes
import static javafx.application.Application.launch;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.geometry.*;
import javafx.scene.control.*;
import javafx.event.ActionEvent; //action for the button,
textfield
import javafx.event.EventHandler; //handler for the listener
public class LabMultipleClassesInvestment extends Application
implements EventHandler<ActionEvent>
{
//declare your instance objects/variables
(fields) here
GridPane mainGridPane = new GridPane();
Label nameLabel = new Label("Enter your
name");
TextField nameTextField = new TextField();
Label interestRateLabel = new Label("Enter
interest rate as decimal");
TextField interestRateTextField = new
TextField();
Label investmentAmountLabel = new Label("Enter
amount of investment");
TextField investmentAmountTextField = new
TextField();
Button displayButton = new Button("Display
Investments");
TextArea outputTextArea = new TextArea();
ScrollPane outputScrollPane = new
ScrollPane();
// This is the first method called in an
application
//We will create an object of ourselves to call
the
//constructor and then execute the start
public static void main(String[] args)
{
launch(args);
}//end of main
public LabMultipleClassesInvestment()
{
//call the designClass
method will add GUI components to the appropriate container
designClass();
//call the addAction method
to register the listeners
addAction();
}//end of constructor
//This method will add components to the
appropriate container
public void designClass()
{
//set the scroll pane to the
output text area
outputScrollPane.setContent(outputTextArea);
outputScrollPane.setFitToWidth(true);
outputScrollPane.setPrefWidth(200);
outputScrollPane.setPrefHeight(190);
//set the
Insets and horizontal and vertical gap
Insets
gridPaneInsets = new Insets(5,5,5,5);
mainGridPane.setPadding(gridPaneInsets);
mainGridPane.setHgap(10);
mainGridPane.setVgap(10);
//add the
components to the mainGridPane
mainGridPane.add(new
Label("Calculates the your investment"),0,0);
mainGridPane.add(nameLabel
,0, 1);
mainGridPane.add(nameTextField,1,1);
mainGridPane.add(interestRateLabel,0,2);
mainGridPane.add(interestRateTextField,1,2);
mainGridPane.add(investmentAmountLabel,0,3);
mainGridPane.add(investmentAmountTextField,1,3);
mainGridPane.add(displayButton,0,4);
mainGridPane.add(outputScrollPane,0,5);
}
//This method will register the components to
the handler
public void addAction()
{
investmentAmountTextField.setOnAction(this);
displayButton.setOnAction(this);
}//end of addListeners
//This method will be used to call validation
and calculateInvestment method
public void handle(ActionEvent evt)
{
//call the validation
method to validate input then call the calculateInvestment
method
if(validation())
{
calculateInvestment();
}
}//end of handle
public boolean validation()
{
boolean validationBoolean
= false;
/*1. You will code
the following:
* a.- you
will check if the nameTextField, interestRateTextField and
amountInvestmetTextField are not blank
* b.- you
will assign a boolean depending on the if statement
* c.- you
will return the boolean where it was called
*/
return
validationBoolean;
}//end of validation
public void calculateInvestment()
{
//Declare your local
variables here! You will have to create variables for the
values
//that you bring in from
Investment class
String
nameString;
double
interestRateDouble, investmentAmountDouble; // calculation requires
a double
//retrieve the name,
course name and course grade input from the user
nameString =
nameTextField.getText();
interestRateDouble =
Double.parseDouble(interestRateTextField.getText());
investmentAmountDouble =
Double.parseDouble(investmentAmountTextField.getText());
/*1. You will
code the following:
* a.- you
will have to code for try and catch
* b.- you
will instantiate a second class with the name, interest rate and
investment
* b.- you
will retrieve from the second class the name, interest rate, amount
of investment,
*
future value of the investment at 5 years, 10 years, and 20
years
* c.- you
display the retrieved output from the Investment class
*/
/* WHAT SHOULD I
THINK OF:
* - think of
overloading constructors
* - what
would be the most efficient way to write the code
* - remember
to use more methods
*
*/
//Display your output:
think about having them on each line of its own
outputTextArea.setText("The name is: " +
"The
interest rate is: " +
"The
investment is: " +
"The
investment at 5 years is: " +
"The
investment at 10 years is: " +
"The
investment at 20 years is: " );
}//end of calculateInvestment
public void start(Stage primaryStage) throws
Exception
{
//
primaryStage.setTitle("Learning Multiple Classes");
Scene scene= new
Scene(mainGridPane, 300, 450);
primaryStage.setScene(scene);
primaryStage.show();
}//end start
}
9. You will be learning how to instantiate the second class with the variables to send, and also you will learn to code the constructors, set methods, get methods and the modifiers public and private. 10. Your lab is to write code in the validation method of the LabMultipleClassesInvestment class below the numbered comments. Here are the instructions and do them in the order: 1. Write statement(s) that will validate that input of name, interest rate and amount of investment is not blank. 2. Write statement(s) that will assign validationBoolean true or false depending on your checking that it is not blank. 3. Write statement(s) that will return a boolean to where validation was called. 11. Your lab is to write code in the calculateInvestment method of the LabMultipleClassesinvestment class below the numbered comments. Here are the instructions and do them in the order: 1. Write statement(s) for the variables that you will retrieving from the Investment class. 2. Write statement(s) of try and catch for both variables of interestRate Double and investmentAmountDouble. 3. Write statement(s) that will declare and create an object of the Investment class locally with arguments of name, interest rate and amount of investment. 4. Write statement(s) that will retrieve from the Investment class the name, interest rate, amount of investment, future value of the investment at 5 years, 10 years, and 20 years. (Remember to assign them to variables) 5. Write statement(s) that will display the retrieved values from the Investment class. Write code within the given code of the text area to display the values with the prompts. Make sure that you have formatted certain outputs 12. Your lab is to write code in the Investment class below the numbered comments. Here are the instructions in order: 1. Write statement(s) to create private variables that you will need for the class. Remember to create constants as well. 2. Write statement(s) to create a default constructor. 3. Write statement(s) to another constructor that will have parameters. (Hint: you need to create parameters that match the arguments sent by the LabMultipleClassesInvestment). 4. Write statement(s) to three private set methods that will be called from the constructor. Remember why set methods are created. (public variables to private variables). 5. Write statement(s) to create a private method for the calculations of the future values. The mathematical expression is: (Remember this has to be called), (Remember that all variables/constants - no hard code numbers except for the 1) future value of investment = amount inputted * Math.pow((1 + interest rate),#years) 6. Write statement(s) to create public get methods that will allow the user to retrieve the values.
javafx classes
import static javafx.application.Application.launch;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.geometry.*;
import javafx.scene.control.*;
import javafx.event.ActionEvent; //action for the button,
textfield
import javafx.event.EventHandler; //handler for the listener
public class LabMultipleClassesInvestment extends Application
implements EventHandler<ActionEvent>
{
//declare your instance objects/variables
(fields) here
GridPane mainGridPane = new GridPane();
Label nameLabel = new Label("Enter your
name");
TextField nameTextField = new TextField();
Label interestRateLabel = new Label("Enter
interest rate as decimal");
TextField interestRateTextField = new
TextField();
Label investmentAmountLabel = new Label("Enter
amount of investment");
TextField investmentAmountTextField = new
TextField();
Button displayButton = new Button("Display
Investments");
TextArea outputTextArea = new TextArea();
ScrollPane outputScrollPane = new
ScrollPane();
// This is the first method called in an
application
//We will create an object of ourselves to call
the
//constructor and then execute the start
public static void main(String[] args)
{
launch(args);
}//end of main
public LabMultipleClassesInvestment()
{
//call the designClass
method will add GUI components to the appropriate container
designClass();
//call the addAction method
to register the listeners
addAction();
}//end of constructor
//This method will add components to the
appropriate container
public void designClass()
{
//set the scroll pane to the
output text area
outputScrollPane.setContent(outputTextArea);
outputScrollPane.setFitToWidth(true);
outputScrollPane.setPrefWidth(200);
outputScrollPane.setPrefHeight(190);
//set the
Insets and horizontal and vertical gap
Insets
gridPaneInsets = new Insets(5,5,5,5);
mainGridPane.setPadding(gridPaneInsets);
mainGridPane.setHgap(10);
mainGridPane.setVgap(10);
//add the
components to the mainGridPane
mainGridPane.add(new
Label("Calculates the your investment"),0,0);
mainGridPane.add(nameLabel
,0, 1);
mainGridPane.add(nameTextField,1,1);
mainGridPane.add(interestRateLabel,0,2);
mainGridPane.add(interestRateTextField,1,2);
mainGridPane.add(investmentAmountLabel,0,3);
mainGridPane.add(investmentAmountTextField,1,3);
mainGridPane.add(displayButton,0,4);
mainGridPane.add(outputScrollPane,0,5);
}
//This method will register the components to
the handler
public void addAction()
{
investmentAmountTextField.setOnAction(this);
displayButton.setOnAction(this);
}//end of addListeners
//This method will be used to call validation
and calculateInvestment method
public void handle(ActionEvent evt)
{
//call the validation
method to validate input then call the calculateInvestment
method
if(validation())
{
calculateInvestment();
}
}//end of handle
public boolean validation()
{
boolean validationBoolean
= false;
/*1. You will code
the following:
* a.- you
will check if the nameTextField, interestRateTextField and
amountInvestmetTextField are not blank
* b.- you
will assign a boolean depending on the if statement
* c.- you
will return the boolean where it was called
*/
return
validationBoolean;
}//end of validation
public void calculateInvestment()
{
//Declare your local
variables here! You will have to create variables for the
values
//that you bring in from
Investment class
String
nameString;
double
interestRateDouble, investmentAmountDouble; // calculation requires
a double
//retrieve the name,
course name and course grade input from the user
nameString =
nameTextField.getText();
interestRateDouble =
Double.parseDouble(interestRateTextField.getText());
investmentAmountDouble =
Double.parseDouble(investmentAmountTextField.getText());
/*1. You will
code the following:
* a.- you
will have to code for try and catch
* b.- you
will instantiate a second class with the name, interest rate and
investment
* b.- you
will retrieve from the second class the name, interest rate, amount
of investment,
*
future value of the investment at 5 years, 10 years, and 20
years
* c.- you
display the retrieved output from the Investment class
*/
/* WHAT SHOULD I
THINK OF:
* - think of
overloading constructors
* - what
would be the most efficient way to write the code
* - remember
to use more methods
*
*/
//Display your output:
think about having them on each line of its own
outputTextArea.setText("The name is: " +
"The
interest rate is: " +
"The
investment is: " +
"The
investment at 5 years is: " +
"The
investment at 10 years is: " +
"The
investment at 20 years is: " );
}//end of calculateInvestment
public void start(Stage primaryStage) throws
Exception
{
//
primaryStage.setTitle("Learning Multiple Classes");
Scene scene= new
Scene(mainGridPane, 300, 450);
primaryStage.setScene(scene);
primaryStage.show();
}//end start
}
9. You will be learning how to instantiate the second class with the variables to send, and also you will learn to code the constructors, set methods, get methods and the modifiers public and private. 10. Your lab is to write code in the validation method of the LabMultipleClassesInvestment class below the numbered comments. Here are the instructions and do them in the order: 1. Write statement(s) that will validate that input of name, interest rate and amount of investment is not blank. 2. Write statement(s) that will assign validationBoolean true or false depending on your checking that it is not blank. 3. Write statement(s) that will return a boolean to where validation was called. 11. Your lab is to write code in the calculateInvestment method of the LabMultipleClassesinvestment class below the numbered comments. Here are the instructions and do them in the order: 1. Write statement(s) for the variables that you will retrieving from the Investment class. 2. Write statement(s) of try and catch for both variables of interestRate Double and investmentAmountDouble. 3. Write statement(s) that will declare and create an object of the Investment class locally with arguments of name, interest rate and amount of investment. 4. Write statement(s) that will retrieve from the Investment class the name, interest rate, amount of investment, future value of the investment at 5 years, 10 years, and 20 years. (Remember to assign them to variables) 5. Write statement(s) that will display the retrieved values from the Investment class. Write code within the given code of the text area to display the values with the prompts. Make sure that you have formatted certain outputs 12. Your lab is to write code in the Investment class below the numbered comments. Here are the instructions in order: 1. Write statement(s) to create private variables that you will need for the class. Remember to create constants as well. 2. Write statement(s) to create a default constructor. 3. Write statement(s) to another constructor that will have parameters. (Hint: you need to create parameters that match the arguments sent by the LabMultipleClassesInvestment). 4. Write statement(s) to three private set methods that will be called from the constructor. Remember why set methods are created. (public variables to private variables). 5. Write statement(s) to create a private method for the calculations of the future values. The mathematical expression is: (Remember this has to be called), (Remember that all variables/constants - no hard code numbers except for the 1) future value of investment = amount inputted * Math.pow((1 + interest rate),#years) 6. Write statement(s) to create public get methods that will allow the user to retrieve the values.