
Tausif K.
asked 11/20/20java program to
a) An interface Polygon containing the members as given below: float area, float perimeter void calcArea( ); abstract method to calculate area of a particular polygon given its dimensions void calcPeri( ); abstract method to calculate perimeter of a particular polygon given its dimensions void display( ); method to display the area and perimeter of the given polygon. Create a class Square that implements Polygon and has the following member: float side Square(float s); constructor to initialize side of square Create another class Rectangle that implements Polygon and has the following member: float length float breadth Rectangle(int len, int bre); constructor to initialize length and breadth of a rectangle 10 10 1,2 10,11 12 8/7/2020 Academic Documents (A-Z): L. Outside the package, create a class that imports the above package an instantiates an object of the Square class and an object of the Rectangle class. Call the above methods on each of the classes to calculate the area and perimeter given the side and the length/breadth of the Square class and the Rectangle class respectively.
(b) Create a class called CalcAverage that has the following method: public double avgFirstN(int N) This method receives an integer as a parameter and calculates the average of first N natural numbers. If N is not a natural number, throw an exception IllegalArgumentException with an appropriate message.
(c) Create a class Number having the following features: Attributes: int first number int second number result double stores the result of arithmetic operations performed on a and b Member functions: · Number(x, y) : constructor to initialize the values of a and b · add( ) : stores the sum of a and b in result · sub( ) : stores difference of a and b in result · mul( ) : stores product in result · div( ) : stores a divided by b in result Test to see if b is 0 and throw an appropriate exception since division by zero is undefined. Display a menu to the user to perform the above four arithmetic operations. (d) Create a class BankAccount having the members as given below: accNo integer custName string accType string (indicates ‘Savings’ or ‘Current’) balance float Include the following methods in the BankAccount class: · void deposit(float amt); · void withdraw(float amt); · float getBalance(); deposit(float amt) method allows you to credit an amount into the current balance. If amount is negative, throw an exception NegativeAmount to block the operation from being performed. 8/7/2020 Academic Documents (A-Z): L. withdraw(float amt) method allows you to debit an amount from the current balance. Please ensure a minimum balance of Rs. 1000/- in the account for savings account and Rs. 5000/- for current account, else throw an exception InsufficientFunds and block the withdrawal operation. Also throw an exception NegativeAmount to block the operation from being performed if the amt parameter passed to this function is negative. getBalance( ) method returns the current balance. If the current balance is below the minimum required balance, then throw an exception LowBalanceException accordingly. Have constructor to which you will pass, accno, cust_name, acctype and initial balance. And check whether the balance is less than 1000 or not in case of savings account and less than 5000 in case of a current account. If so, then raise a LowBalanceException. In either case if the balance is negative then raise the NegativeAmount exception accordingly. (e) Create a class with following specifications. Class Emp empId int empName string designation string basic double hra double readOnly Methods · printDET( ) · kalculateHRA( ) · printDET( ) methods will show details of the EMP. calculateHRA( ) method will calculate HRA based on basic. There will 3 designations supported by the application. If designation is “Manager” - HRA will be 10% of BASIC if designation is “Officer” - HRA will be 12% of BASIC if category is “CLERK” - HRA will be 5% of BASIC Have constructor to which you will pass, empId, designation, basic and price. And checks whether the BASIC is less than 500 or not. If it is less than 500 raise a custom Exception as given below Create LowSalException class with proper user message to handle BASIC less than 500.
(e) Create a class USERTRAIL with following specifications. val1, val2 type int Method 8/7/2020 Academic Documents (A-Z): L. boolean show( ) will check if val1 and val2 are greater or less than Zero Have constructor which will val1, val2 and check whether if it is less than 0 then raise a custom Exception (name: Illegal value exception.)
1 Expert Answer

Patrick B. answered 11/21/20
Math and computer tutor/teacher
/***************
You CANNOT declare member vars in the interface...
they will be STATIC and FINAL and cannot be changed...
************************/
interface Polygon
{
public float CalculateArea();
public float CalculatePerimeter();
public void Display();
}
class Square implements Polygon
{
protected float s;
protected float area;
protected float perimeter;
Square(float S) { s=S; CalculateArea(); CalculatePerimeter(); }
public float CalculateArea() { return (this.area = s*s); }
public float CalculatePerimeter() { return (this.perimeter = 4*s); }
public float GetSide() { return(s); }
public void Display()
{
System.out.println(" Square");
System.out.println(" side measure " + this.s);
System.out.println(" area = " + this.area);
System.out.println(" perimeter = " + this.perimeter);
}
}
class Rectangle implements Polygon
{
protected float length;
protected float width;
protected float area;
protected float perimeter;
Rectangle(float L, float W) { length=L; width=W; CalculateArea(); CalculatePerimeter(); }
public float CalculateArea() { return (this.area = length*width); }
public float CalculatePerimeter()
{
return (
(this.perimeter = 2*(length+width))
);
}
public float GetWidth() { return(width); }
public float GetLength() { return(length); }
public void Display()
{
System.out.println(" Rectangle ");
System.out.println(" length " + this.length);
System.out.println(" width " + this.width);
System.out.println(" area = " + this.area);
System.out.println(" perimeter = " + this.perimeter);
}
}
class Circle implements Polygon
{
protected float radius;
protected float area;
protected float perimeter;
Circle(float r) { radius=r; CalculateArea(); CalculatePerimeter(); }
public float CalculateArea() { return (this.area = 22*radius*radius/7.0f); }
public float CalculatePerimeter()
{
return (
(this.perimeter = 44/7.0f*radius)
);
}
public float GetRadius() { return(radius); }
public void Display()
{
System.out.println(" Circle ");
System.out.println(" radius " + this.radius);
System.out.println(" area = " + this.area);
System.out.println(" perimeter = " + this.perimeter);
}
}
class PolyMain
{
public static void main(String args[])
{
Square square = new Square(5);
Rectangle rectangle = new Rectangle(15,8);
Circle circle = new Circle(10);
square.Display();
rectangle.Display();
circle.Display();
}
}
/*************************************************************************************/
import java.lang.IllegalArgumentException;
class CalcAverage
{
private int N;
public CalcAverage(int n) throws IllegalArgumentException
{
if (n<1)
{
throw ( new IllegalArgumentException());
}
N=n;
}
public int GetN() { return(N); }
public long SumFirstN() { return (N*(N+1)/2); }
public static void main(String args[])
{
int N=10;
System.out.println(" N = " + N);
try
{
CalcAverage x = new CalcAverage(N);
System.out.println(x.SumFirstN());
}
catch (IllegalArgumentException ex) { System.out.println(" Illegal argument exception " + N); }
N=-3;
System.out.println(" N = " + N);
try
{
CalcAverage x = new CalcAverage(N);
System.out.println(x.SumFirstN());
}
catch (IllegalArgumentException ex) { System.out.println(" Illegal argument exception " + N); }
}
}
//***********************************************************************************
class Numbers
{
private double x;
private double y;
public Numbers( double X, double Y) { x=X; y=Y; }
public double GetX() { return(x); }
public double GetY() { return(y); }
public double Add() { return(x+y); }
public double Subtract() { return(x-y); }
public double Multiply() { return(x*y); }
public static void main(String args[])
{
Numbers numbers = new Numbers(7,3); // testing comment out
//Numbers numbers = new Numbers(5,0); //<--- throws the exception: uncomment to test
System.out.println(numbers.Add());
System.out.println(numbers.Subtract());
System.out.println(numbers.Multiply());
try
{
System.out.println(numbers.Divide());
}
catch(ArithmeticException ex) { System.out.println(" arithmetic exception : divides by zero? "); }
}
public double Divide() throws ArithmeticException
{
if (y==0) { throw( new ArithmeticException()); }
return(x/y);
}
//****************************************************************************//
import java.lang.Exception;
class LowBalanceException extends Exception
{
public LowBalanceException( String ex) { super(ex); }
}
class AccountOverdrawnException extends Exception
{
public AccountOverdrawnException(String ex) { super(ex); }
}
class BankAccount
{
public static int LOW_BALANCE_SAVINGS = 1000;
public static int LOW_BALANCE_CHECKING = 5000;
double balance;
long accountNum;
String customerName;
boolean accountType; //true = savings, false = checking
BankAccount(long acctNum, String custName, boolean acctType, double initBal) throws LowBalanceException,IllegalArgumentException
{
if (initBal<0) { throw ( new IllegalArgumentException("negative balance not allowed")); }
if (acctType)
{
if (initBal<=BankAccount.LOW_BALANCE_SAVINGS) { throw ( new LowBalanceException("Low Balance Exception")); }
}
else
{
if (initBal<=BankAccount.LOW_BALANCE_CHECKING) { throw ( new LowBalanceException("Low Balance Exception")); }
}
balance = initBal;
customerName = new String(custName);
accountType = acctType;
accountNum = acctNum;
}
public String GetCustomerName() { return(customerName); }
public double GetBalance() { return(balance); }
public long GetAccountNum() { return(accountNum); }
public boolean GetAccountType() { return(accountType); }
public double Deposit( double depAmt) throws IllegalArgumentException
{
if (depAmt<0) { throw ( new IllegalArgumentException("negative balance not allowed")); }
return ( balance += depAmt) ;
}
public double Withdraw( double wdAmt) throws AccountOverdrawnException,LowBalanceException,IllegalArgumentException
{
if (wdAmt<0) { throw ( new IllegalArgumentException("negative balance not allowed")); }
balance -= wdAmt;
if (balance<0) { throw ( new AccountOverdrawnException("Account Overdrawn") ); }
if (accountType)
{
if (balance<=BankAccount.LOW_BALANCE_SAVINGS) { throw ( new LowBalanceException("Low Balance Exception")); }
}
else
{
if (balance<=BankAccount.LOW_BALANCE_CHECKING) { throw ( new LowBalanceException("Low Balance Exception")); }
}
return(balance);
}
public static void main(String args[])
{
try
{
BankAccount acct = new BankAccount(52769,"Patrick",true,5555);
acct.Deposit(500);
acct.Withdraw(10000);
System.out.println(acct.GetBalance());
}
catch (AccountOverdrawnException ex) { System.out.println(" account overdrawn "); }
catch (LowBalanceException ex) { System.out.println(" low balance exception "); }
}
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
All of this code is not going to fit here... send email so I can pass you the source code files.11/21/20