0% found this document useful (0 votes)
100 views

Lab Exception Questions + GUI

1. Write a class that handles exceptions when accessing array elements outside the bounds. It randomly generates an array of 100 integers and prompts the user for an index. 2. Modify the Loan class to throw an IllegalArgumentException if loan amount, interest rate, or number of years is less than or equal to zero. 3. Design a Triangle class that throws an IllegalTriangleException if the sides entered would not form a valid triangle based on the triangle inequality rule. It contains fields for the three sides and a constructor that validates the sides.

Uploaded by

Sajad Ullah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views

Lab Exception Questions + GUI

1. Write a class that handles exceptions when accessing array elements outside the bounds. It randomly generates an array of 100 integers and prompts the user for an index. 2. Modify the Loan class to throw an IllegalArgumentException if loan amount, interest rate, or number of years is less than or equal to zero. 3. Design a Triangle class that throws an IllegalTriangleException if the sides entered would not form a valid triangle based on the triangle inequality rule. It contains fields for the three sides and a constructor that validates the sides.

Uploaded by

Sajad Ullah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Lab Task 9 (Exception + GUI)

1. Write a class that meets the following requirements:


 Creates an array with 100 randomly chosen integers.
 Prompts the user to enter the index of the array, then displays the corresponding
element value. If the specified index is out of bounds, display the message Out of
Bounds.

Note: use Exception Handling

2. Modify the Loan class throw IllegalArgumentException if the loan amount, interest rate, or number
of years is less than or equal to zero.

3. Design a class named Triangle. The class contains:


 Three double data fields named side1, side2, and side3 to denote three sides of
the triangle.
 An argument constructor that creates a triangle with the specified side1, side2,
and side3.
 A method named getArea() that returns the area of this triangle.
Note:
In a triangle, the sum of any two sides is greater than the other side. The Triangle class
must adhere to this rule. Create the IllegalTriangleException class, and modify the
constructor of the Triangle class to throw an IllegalTriangleException object if a triangle is
created with sides that violate the rule, as follows:

public Triangle(double side1, double side2, double side3) throws IllegalTriangleException


{
// Implement it
}

4. A method that returns a special error code is usually better accomplished throwing an
exception instead. The following class maintains an account balance.
class Account
{
private double balance;
public Account()
{
balance = 0;
}
// returns new balance or -1 if error
public double deposit( double amount) throws
invalidAmountException
{
if (amount > 0)
balance += amount;
else
return -1;// Code indicating error
return balance;
}
// returns new balance or -1 if invalid amount
public double withdraw(double amount)
{
if ((amount > balance) || (amount < 0))
return -1;
else
balance -= amount;
return balance;
}
}
Rewrite the class so that it throws appropriate exceptions instead of returning −1 as an error code. Write
test code that attempts to withdraw and deposit invalid amounts and catches the exceptions that are
thrown.

5. Create a frame with two buttons. Display appropriate messages on clicking.

6. Create a frame with one label, one textbox and a button. Display the information entered in
textbox on button click.

You might also like