Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab Exercise 01.1 Handle exceptions Assignment : The first lab assignment is an exercise in the use of the exception and handling of exceptions. You

Lab Exercise 01.1

Handle exceptions

Assignment:

The first lab assignment is an exercise in the use of the exception and handling of exceptions. You will write a main method to test the Triangle class which I have written. Triangle takes doubles as constructor arguments for the A and B sides of a right triangle and calculates the hypotenuse. However it does nothing to handle the situation where a user enters data which is negative, zero or non numeric. Your main should handle these situations.

Input Commands:

A enter value for side A

B enter value for side B

C calculate

Q quit the program

The program will repeat itself in order to make it easier to test. You will create a new object of the Triangle class for each test.

Your task today is to write that program to handle the bad input situation with the use of exception handling. Bad inputs are:

Non-numeric values where numeric values are expected

Input command, not A, B, C or Q

The program should not terminate but should inform the user of the specific error and should re-request only the input value which was in error

Testing:

Test your program with the following test cases;

A

A

A

A

B

A

B

X

Q

3

3

0

s

s

3

3

B

B

B

C

C

4

0

3

C

C

C

In all cases of invalid input the program should ask for only the input values that were invalid and should continue with the values that were correct. The program should never crash!

Files:

Main.java supplied by you

Triangle.java supplied by blackboard

Lab Exercise 01.2

Handle exceptions

Assignment:

The next lab assignment is another exercise in the use of the exception and handling of exceptions. You will write a main method to test the Quadratic class which I have written. Quadratic takes doubles as constructor arguments for the a, b and c coefficients of a quadratic equation and calculates the two values for X. However it does nothing to handle the situation where a user enters data which either includes a value of 0.0 for a or is non numeric. The program should always display two values as the result since a quadratic equation, by definition, always has two values as the solution even when the values are identical.

The program will repeat itself in order to make it easier to test.

However it does nothing to handle the situation where a user enters data which is non-double or even non numeric. Your task today is to develop that program to handle the bad input situation with the use of exception handling. Bad inputs are:

Non-numeric values where numeric values are expected

Any input that causes the output to be NaN (Not a Number)

Any input that causes the output to be INFINITY

The program will not terminate but will inform the user of the specific error and should re-request the input value which was in error

Testing:

Test your program with Main.java supplied by blackboard.

The program should never crash!

Files:

Main.java supplied by blackboard

Quadratic.java supplied by you

-------------------------------------------triangle.java-----------------------------

public class Triangle { private double sideA; private double sideB; public Triangle() { sideA = 0.0; sideB = 0.0; } public Triangle(double a, double b) { if (a <= 0.0) { throw new IllegalArgumentException("A"); } else { sideA = a; } if (b <= 0.0) { throw new IllegalArgumentException("B"); } else { sideB = b; } } public void setA(double a) { sideA = a; } public void setB(double b) { sideB = b; } public double calcHypotenuse() { return(Math.sqrt((sideA*sideA) + (sideB*sideB))); } }

---------------------------- main.java--------------------------------

import java.util.*; public class Main { private static Quadratic q = null; public static void main(String [] args) { helper(0, 1, 1); helper(1, 1, 1); helper(1, 17, 1); helper(1, 2, 3); helper(1, 2, 1); helper(17,1000,2533); helper(-17,1000,2533); helper(17,-1000,2533); helper(17,1000,-2533); } private static void helper(double a, double b, double c) { try { q = new Quadratic(a, b, c); } catch(IllegalArgumentException err) { System.out.println("Not a quadratic."); return; } if (!q.calcQuadratic()) { System.out.println("No Real solutions"); return; } System.out.println("results are: X1 = " + q.getX1() + " and X2 = " + q.getX2()); } }

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

Transact SQL Cookbook Help For Database Programmers

Authors: Ales Spetic, Jonathan Gennick

1st Edition

1565927567, 978-1565927568

Students also viewed these Databases questions