Malaysia Institute Of Information Technology
Universiti Kuala Lumpur
SOFTWARE DESIGN and INTEGRATION
Lab Exercise Chapter 4: design principles part 1
: Correctness and Robustness
Name : ______________________________________ Class : _________________
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 {
public static void main(String[] args) {
int number = -5; // assumed number is not negative
// This assert also serve as documentation
assert (number >= 0) : "number is negative: " + number;
// do something
System.out.println("The number is " + number);
}
Example 2 (Apply Robustness using exception handling)
Find the average for three numbers ,
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);
}
average = (num1 + num2) / 2.0;
JOptionPane.showMessageDialog(null,
"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
Filename: MyAge.java Assertion
Write a program that prompts user to find your current age today. Add an assertion to ensure your year
born value is not zero or negative.
Filename: VotingRules.java Assertion
This simple application of an assert statement. The program VotingRules.java is designed to check either
you able to do a voting process.
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
Filenames : DivideByZero.java , ArithmeticException – Divide by zero
TestDivideByZero.java
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
Filenames : MultipleCatch.java , NumberFormatException,
TestMultipleCatch.java ArithmeticException
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
Filenames : Circle.java, IllegalRadiusException.java,TestCircle.java Construct a user defined
exception
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
Write another class Circle with the following requirement
One data member radius with private access modifier
One method getRadius() that will accept user input on radius and return the radius
Create another method setRadius(double newRadius) that will throw IllegalRadiusException if
the radius entered is less or equal to zero
Add another method to calculate the area of the circle
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