LabExercise All DesignPrinciples Part1 Student
LabExercise All DesignPrinciples Part1 Student
LabExercise All DesignPrinciples Part1 Student
Requirements: the student able to apply all the design principles using Java concept.
Part 1: Example
Example 1(Apply correctness using Assertion)
Another usage of assertion is to assert "internal invariants". In other words, to assert the possible values
of an internal variable. For example
public class Assertion1 {
import javax.swing.*;
public class lab4Q1robustness
{
public static void main(String[] args)
{
String s1;
String s2;
SDI/madamRobiah/LabExercise2Jan2021 1
double num1 = 0;
double num2 = 0;
double average = 0;
s1 = JOptionPane.showInputDialog("Enter a number:");
s2 = JOptionPane.showInputDialog("Great! Now enter another number:");
try
{ num1 = Double.parseDouble(s1);
num2 = Double.parseDouble(s2);
}
catch(NumberFormatException n)
{
JOptionPane.showMessageDialog(null, "You must enter a number","InputDataError",
JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
catch(NullPointerException n)
{
JOptionPane.showMessageDialog(null, "You Pressed the Cancel Button", "Program Terminiation",
JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
"The average of " + num1 + " and " + num2 + " is " + average,
"QuickTest Program 4.5", JOptionPane.INFORMATION_MESSAGE);
System.exit(0); } }
SDI/madamRobiah/LabExercise2Jan2021 2
Part 2 : You Do it
Robustness
Filenames: EnterYourAge.java NumberFormat Exception
Write a program that ask user to enter his or her age and uses exception handling to catch a
NumberFormatException in case the user enters a nonnumeric character
Write a program to perform division for two integer numbers. Requires user to enter 2 numbers in a
void method named InputNumber(). Your program will divide the first number entered with the second
number entered in a non void method named Division().
Add an ArithmeticException clause to check whether the operation can be done or an exception will be
thrown.
Test the program in class TestDivideByZero
Write a program to demonstrate some multiple catch exceptions. Requires user to enter two integer
numbers. If the number entered is a non numeric value, throw a NumberFormatException. In the same
program, perform a division for both numbers, and if the second number entered is 0, throw an
ArithmeticException.
Use an appropriate method to perform the operation.
Test the program in class TestMultipleCatch.java
SDI/madamRobiah/LabExercise2Jan2021 3
Write a user defined exception, IllegalRadiusException that extends Exception. In the class, defined one
default constructor with no implementation and one constructor that will override the Exception
constructor
Create a test class TestCircle.java. Add the try and catch clause in this test class
Question 2:
1. Your supervisor asked you to review the Bank system contains the class CustomerAccount
with the following requirement:
One integer data member accNum with private access modifier
One double data member balance with private access modifier
One constant named HIGH_CREDIT_LIMIT = 20000.00
To enhance the module, your team needs to include the robustness element in this module such as:
a. Write a user defined exception, HighBalanceException that extends Exception. Its constructor
contains a single statement that passes a description of an error to the parent Exception
constructor. This String would be retrieved if you called the getMessage() method with a
HighBalanceException object.
b. Create a method named CustomerAccount(int num, double bal) that will throw
HighBalanceException if user balance is exceeds than high credit limit.
SDI/madamRobiah/LabExercise2Jan2021 4