1Build a class called TestExceptionsjavaPut the following co
1.Build a class called “TestExceptions.java”.Put the following code in the main method.
int myArr = new int[10];
int x,y,z;
x=0;
y=10;
z=y/x;
myArr[10] = 0;
Compile and run this class. What happens?
Now add try/catch blocks to catch these exceptions?
Is there another way that you can prevent a run-time error without
using try-catch blocks?
2.next build an Account class.The Account class should have 3 Properties: Balance, Owner and AcctNo.The Account class should have 2 constructors, one that takes all 3 properties and one that takes no args(the default).The Account class should have set and get methods as well as deposit and withdraw methods.Also include a display() method that outputs AcctNo, Owner and Balance. Use a main() method to test out this class.
Use the following code, in the main method, to test the Account class?
Account a1;
a1 = new Account(2222, “Frank”, 1000);
a1.deposit(100.00);
a1.display();
3.now in your Account main() method, attempt to withdraw more than the current balance that is available.What happens now?
Use the following code to test the Account class?
Account a1;
a1 = new Account(2222, “Frank”, 500.00);
a1.deposit(100.00);
a1.withdraw(900.00);
a1.display();
What happened?
Solution
a)
If we compile the code as given following exception occurs:
Exception in thread \"main\" java.lang.ArithmeticException: / by zero
at TestExceptions.main(TestExceptions.java:9)
Now by adding try and catch statement, modified code:
TestExceptions.java
public class TestExceptions
{
public static void main(String[] args)
{
int myArr[] = new int[10];
int x,y,z;
x=0;
y=10;
try
{
z=y/x;
}
catch(ArithmeticException e)
{
System.out.println(e);
}
myArr[9] = 0;
}
}
Another way that you can prevent a run-time error:
public class TestExceptions
{
public static void main(String[] args)
{
int myArr[] = new int[10];
int x,y,z;
x=0;
y=10;
if(x!=0)
{
z=y/x;
}
else
{
System.out.println(\"Divide by zero Exception\");
}
myArr[9] = 0;
}
}
b)
Account.java
public class Account
{
public String owner ;
public double balance;
public int accnumber;
public Account(int accnumber, String owner , double balance) {
this.owner = owner;
this.balance = balance;
this.accnumber = accnumber;
}
public Account() {
this.owner = \"Defaulname\";
this.balance = 0;
this.accnumber = 0;
}
public void getBalance()
{
System.out.println(\"Your balance is \" + this.balance);
}
public void setBalance(int b)
{
this.balance = b;
}
public void deposit(double amount)
{
this.balance = this.balance + amount;
}
public void display()
{
System.out.println(\"Owner: \" + this.owner);
System.out.println(\"balance: \" + this.balance);
System.out.println(\"Account Number: \" + this.accnumber);
}
public void withdraw(double amount)
{
if( amount > this.balance)
{
System.out.println(\"Sorry, you have insufficient balance!\");
}
else
{
this.balance = this.balance - amount;
}
}
}
driver.java
public class driver
{
public static void main(String[] args)
{
Account a1;
a1 = new Account(2222, \"Frank\", 1000);
a1.deposit(100.00);
a1.display();
}
}
Sample output:
Owner: Frank
balance: 1100.0
Account Number: 2222
c)
driver.java
public class driver
{
public static void main(String[] args)
{
Account a1;
a1 = new Account(2222, \"Frank\", 500.00);
a1.deposit(100.00);
a1.withdraw(900.00);
a1.display();
}
}
Sample Output:
Sorry, you have insufficient balance!
Owner: Frank
balance: 600.0
Account Number: 2222
![1.Build a class called “TestExceptions.java”.Put the following code in the main method. int myArr = new int[10]; int x,y,z; x=0; y=10; z=y/x; myArr[10] = 0; Com 1.Build a class called “TestExceptions.java”.Put the following code in the main method. int myArr = new int[10]; int x,y,z; x=0; y=10; z=y/x; myArr[10] = 0; Com](/WebImages/37/1build-a-class-called-testexceptionsjavaput-the-following-co-1111140-1761589190-0.webp)
![1.Build a class called “TestExceptions.java”.Put the following code in the main method. int myArr = new int[10]; int x,y,z; x=0; y=10; z=y/x; myArr[10] = 0; Com 1.Build a class called “TestExceptions.java”.Put the following code in the main method. int myArr = new int[10]; int x,y,z; x=0; y=10; z=y/x; myArr[10] = 0; Com](/WebImages/37/1build-a-class-called-testexceptionsjavaput-the-following-co-1111140-1761589190-1.webp)
![1.Build a class called “TestExceptions.java”.Put the following code in the main method. int myArr = new int[10]; int x,y,z; x=0; y=10; z=y/x; myArr[10] = 0; Com 1.Build a class called “TestExceptions.java”.Put the following code in the main method. int myArr = new int[10]; int x,y,z; x=0; y=10; z=y/x; myArr[10] = 0; Com](/WebImages/37/1build-a-class-called-testexceptionsjavaput-the-following-co-1111140-1761589190-2.webp)
![1.Build a class called “TestExceptions.java”.Put the following code in the main method. int myArr = new int[10]; int x,y,z; x=0; y=10; z=y/x; myArr[10] = 0; Com 1.Build a class called “TestExceptions.java”.Put the following code in the main method. int myArr = new int[10]; int x,y,z; x=0; y=10; z=y/x; myArr[10] = 0; Com](/WebImages/37/1build-a-class-called-testexceptionsjavaput-the-following-co-1111140-1761589190-3.webp)