Question: Java program, Add comments where possible please. 1) a) My code class Rectangle { private double width; private double height; //Initializing the height, width property

Java program,
Add comments where possible please.
1)
a) My code
class Rectangle {
private double width;
private double height;
//Initializing the height, width property values with 1.0 in the default constructor
public Rectangle() {
width = 1.0;
height = 1.0;
}
//Assigning the height, width property values with given inputs in the parameterized constructor
public Rectangle(double w, double h) {
width = w;
height = h;
}
//This method used to get the width property value
public double getWidth() {
return width;
}
//This method used to get the height property value
public double getHeight() {
return height;
}
//This method calculates the area of the rectangle using this propety values and returns the result
public double getArea() {
return width * height;
}
//This method calculates the perimeter of the rectangle using this propety values and returns the result
public double getPerimeter() {
return 2 * width * height;
}
}
//This is the main class - we are implementing the rectangle operations in this class
public class Ex8 {
public static void main(String[] args) {
/*Creating instance for the rectangle class using default constructor. This constructor intitialize the properties as 1.0 for each*/
Rectangle rect1 = new Rectangle();
/*Creating second instance for the rectangle class using default constructor. This constructor intitialize the properties with the given values*/
Rectangle rect2 = new Rectangle(3.5, 12);
System.out.println("Area of first Rectangle: " + rect1.getArea() + " Perimeter of first Rectangle: " + rect1.getPerimeter());
System.out.println("Area of second Rectangle: " + rect2.getArea() + " Perimeter of second Rectangle: " + rect2.getPerimeter());
}
}
Requirements
Modify the implementation of your Rectangle class so that it now throws an instance of IllegalArgumentException from the constructor if a width less than or equal to zero or a height less than or equal to zero is supplied.
Finally, modify the main program so that it attempts to create an invalid rectangle, thereby triggering the exception. Test the program to see how the exception changes program behaviour. Then modify the program further to intercept the exception using try and catch.
b)
My code
class BankAccount {
private int id;
private String name;
private int balance;
//Initializing the balance with 0 in the constructor
public BankAccount(int id, String name) {
this.id = id;
this.name = name;
this.balance = 0;
}
public BankAccount(int id, String name, int bal) {
this.id = id;
this.name = name;
this.balance = bal;
}
//This method is used to get the id property value
public int getId() {
return id;
}
//This method is used to get the name property value
public String getName() {
return name;
}
//This method is used to get the balance property value
public int getBalance() {
return balance;
}
//This method is used to deposit the amount
public boolean deposit(int amount) {
//If the amount is greater than 0, then we are adding the current balance with the given amount as deposit
//Once the deposit is success, then we are returning true, otherwise returning false
if(amount > 0) {
this.balance += amount;
return true;
}
return false;
}
//This method is used to withdraw the amount
public boolean withdraw(int amount) {
/*If the amount is greater than 0 and the amount is less than or equal to current balance,
*then we are reducing the current balance with the given amount as withdrawal
*Once the withdrawal is success, then we are returning true, otherwise returning false*/
if(amount > 0 && amount <= this.balance) {
this.balance -= amount;
return true;
}
return false;
}
}
//This is the main class - This is used to implement the banking transactions
public class Ex9 {
public static void main(String[] args) {
String pound = "\u00a3";
//Creating the instance of the 'BankAccount' class with the initializing value.
BankAccount account1 = new BankAccount(1, "Maria Tahir", 20000);
account1.withdraw(2500);
account1.deposit(4000);
System.out.println("Current Balance of " + account1.getName() + " is : " + "" + pound + account1.getBalance());
}
}
Requirements
Replace the contents of the main method in Ex9 with a new program that
Creates a list capable of holding BankAccount objects
Reads IDs, account holder names and initial balances from a file
Uses each name and balance to create a BankAccount object
Adds each BankAccount object to the list
The data file used by the program should have lines that look like this:
1,John Smith,100
2,Sarah Davies,350
...
The simplest approach is probably to read each line as a string and then call its split method to split it on commas - see the API documentation for further details.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!