Help with trycatch blocks I just cant seem them to work righ
Help with try-catch blocks. I just can\'t seem them to work right. Help!
import java.text.DecimalFormat;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.text.*;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javafx.application.Platform;
public class LoanComparison extends Application
{
public static void main(String[] args) {
launch(args);
}
// Creating and naming private variables for text fields(user input).
private TextField LoanAmount = new TextField();
private TextField NumberOfYears = new TextField();
private TextArea ResultsTable = new TextArea();
//Creating button that when pressed displays the results table.
private Button ShowTable = new Button(\"Show Table\");
@Override
public void start(Stage primaryStage)
{
//Creating new Border Pane
final BorderPane pane = new BorderPane();
//Creating HBox for user interface
HBox Interface = new HBox(12);
//Creating labels for text fields
Label LoanAmt = new Label(\"Loan Amount: \");
Label NumYears = new Label(\"Years: \");
//Setting width of text fields
LoanAmount.setPrefWidth(150);
NumberOfYears.setPrefWidth(35);
//Centering interface
Interface.setAlignment(Pos.CENTER);
//Adding TextFields, Labels and Button to Interface.
Interface.getChildren().addAll(LoanAmt, LoanAmount, NumberOfYears, NumYears, ShowTable);
//Adding Interface to the top of pane and ResultsTable to the center.
pane.setTop(Interface);
pane.setCenter(ResultsTable);
//Setting margins and padding for
BorderPane.setMargin(ResultsTable, new Insets(5, 1, 1, 1));
pane.setPadding(new Insets(5, 5, 5, 5));
//Method that caculates and outputs the results when the user presses the \"Show Table\" button
ShowTable.setOnAction(e -> Calculate());
//Setting the size the pane
Scene scene = new Scene(pane,500, 500);
//Naming the pane
primaryStage.setTitle(\"Loan Comparison\");
primaryStage.setScene(scene);
primaryStage.show();
ResultsTable.setEditable(false);
}
private void Calculate()
{
//Declaring/Defining variables for calculations and try catch blocks.
boolean ValidInput = true;
Loan Lloan = new Loan ();
//Try Catch block for catches vales less than 1000 or greater than 100000.
try
{
Lloan.setAnnualInterestRate(5);
Lloan.setLoanAmount(Double.parseDouble(LoanAmount.getText()));
if (Lloan.getLoanAmount() < 1000 || Lloan.getLoanAmount() > 100000)
{
throw new Exception();
}
}
catch(Exception e){
new Alert(Alert.AlertType.ERROR,(\"Invalid Input: Only loan amounts from 1000 to 100000 will be accepted.\")).showAndWait();
ValidInput = false;
}
try{
Double LoanYears = 0.0;
Lloan.setNumberOfYears(Integer.parseInt(NumberOfYears.getText()));
if(LoanYears < 1 || LoanYears > 20)
{
throw new Exception();
}
}
catch(Exception f)
{
new Alert(Alert.AlertType.ERROR,(\"Invalid Input: The number of years must be from 1 to 20.\")).showAndWait();
ValidInput = false;
}
if (ValidInput = true)
{
//Adding column names to table.
ResultsTable.setText(\"Interest Rate Monthly Payment \"
+ \"Total Payment\ \");
//Do While loop for calculating/printing values until the interest rate reaches 8%
do
{
ResultsTable.appendText(new DecimalFormat(\"0.000%\").format(Lloan.getAnnualInterestRate()/100.0) + \" \" +
new DecimalFormat(\"$###,##0.00\").format(Lloan.getMonthlyPayment()) + \" \" +
new DecimalFormat(\"$###,##0.00\").format(Lloan.getTotalPayment()) + \"\ \");
Lloan.setAnnualInterestRate(Lloan.getAnnualInterestRate() + 0.125);
} while (Lloan.getAnnualInterestRate() <= 8);
}
}
}
public class Loan {
private double annualInterestRate;
private int numberOfYears;
private double loanAmount;
private java.util.Date loanDate;
/** Default constructor */
public Loan() {
this(2.5, 1, 1000);
}
/** Construct a loan with specified annual interest rate,
number of years, and loan amount
*/
public Loan(double annualInterestRate, int numberOfYears,
double loanAmount) {
this.annualInterestRate = annualInterestRate;
this.numberOfYears = numberOfYears;
this.loanAmount = loanAmount;
loanDate = new java.util.Date();
}
/** Return annualInterestRate */
public double getAnnualInterestRate() {
return annualInterestRate;
}
/** Set a new annualInterestRate */
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
/** Return numberOfYears */
public int getNumberOfYears() {
return numberOfYears;
}
/** Set a new numberOfYears */
public void setNumberOfYears(int numberOfYears) {
this.numberOfYears = numberOfYears;
}
/** Return loanAmount */
public double getLoanAmount() {
return loanAmount;
}
/** Set a newloanAmount */
public void setLoanAmount(double loanAmount) {
this.loanAmount = loanAmount;
}
/** Find monthly payment */
public double getMonthlyPayment() {
double monthlyInterestRate = annualInterestRate / 1200;
double monthlyPayment = loanAmount * monthlyInterestRate / (1 -
(1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12)));
return monthlyPayment;
}
/** Find total payment */
public double getTotalPayment() {
double totalPayment = getMonthlyPayment() * numberOfYears * 12;
return totalPayment;
}
/** Return loan date */
public java.util.Date getLoanDate() {
return loanDate;
}
}
Solution
import java.util.Scanner; import java.lang.Math; import java.text.DecimalFormat; public class Logs { //integer x field & encapsulation private static double x; // x encapsulation public double getX(){ return x; } public void setX (double ex){ if (ex >= 0){ x = ex; } else{ System.out.println(\"You may not choose a negative number, \'x\' = 0\"); } } //integer y field private static double y; // y encapsulation public double getY(){ return y; } public void setY (double why){ if (why >= 0){ y = why; } else{ System.out.println(\"You may not choose a negative number, \'y\' = 0\"); } } //tester public static void main (String [] args){ Logs sample =new Logs(); Scanner var = new Scanner(System.in); DecimalFormat fmt = new DecimalFormat (\"0.###\"); sample.setX (var.nextDouble()); sample.setY (var.nextDouble()); x = sample.getX(); y = sample.getY(); double z = (x*y); double logX = Math.log(y); double logY = Math.log(x); double logZ = (logX +logY); double result = (Math.exp(logZ)); if (z != result){ System.out.printf(\"%s\", \"Incorrect answer: be a better coder\"); } else { System.out.printf(\"%s %.3d %s %.3d %s %.3d\", \"The product of the values you chose is: \", + z, \"\ ln(x) + ln(y) is: \", + logZ, \"\ Compute to e: \", + result); } } }


