14.
WAP to create an abstract class "Shape" consist of an abstract
method “area()” , the class
is inherited by the classes "Square" , "Rectangle" , “Triangle” and
"Circle" and the inherited
area() method is implemented by these classes to compute the area of
corresponding
shapes. Process the shapes polymorphically.
abstract class shape
{
public abstract double area();
}
class circle extends shape
{
private double radius;
public circle(double r)
{
radius=r;
}
public double area()
{
return(Math.PI*radius*radius);
}
}
class square extends shape
{
private double side;
public square(double s)
{
side=s;
}
public double area()
{
return(side*side);
}
}
class rectangle extends shape
{
private int aa,uu;
public rectangle(int l,int w)
{
aa=l;
uu=w;
}
public double area()
{
return(aa*uu);
}
}
class triangle extends shape
{
private double base,height;
public triangle(double bas,double h)
{
base=bas;
height=h;
}
public double area()
{
return(0.5*base*height);
}
}
public class Assignment14
{
public static void main(String args[])
{
shape sp[]=new shape[4];
sp[0]=new circle(4.5);
sp[1]=new square(3.5);
sp[2]=new rectangle(5,6);
sp[3]=new triangle(2.3,4.5);
for(shape sh:sp)
{
if(sh instanceof circle){
System.out.println("Area of circle:"+sh.area());
}
else if(sh instanceof square){
System.out.println("Area of Square:"+sh.area());
}
else if(sh instanceof rectangle){
System.out.println("Area of
rectangle:"+sh.area());
}
else if(sh instanceof triangle){
System.out.println("Area of triangle:"+sh.area());
}
}
}
}
output:
Area of circle:63.61725123519331
Area of Square:12.25
Area of rectangle:30.0
Area of triangle:5.175
15. WAP to declare an interface “Payable” consist of GST=5%, DA=55%,
HRA=20% as
constants and “getPayment()” method. A class “Invoice” consist of
itemName, quantity
and pricePerItem as variables and implements the Payable interface to
calculate the invoice
amount. An another class “Staff” consist of name and basic salary as
variables and
implements the Payable interface to calculate the gross salary. Test your
interface
polymorphically for two invoices and two staffs.
interface Payable
{
double GST=0.05;
double DA=0.55;
double HRA=0.2;
double getPaymentAmount();
}
class Invoice implements Payable
{
private String itemName;
private int quantity;
private double pricePerItem;
public Invoice(String name,int count,double price)
{
itemName=name;
quantity=count;
pricePerItem=price;
}
@Override
public double getPaymentAmount()
{
double amount=quantity*pricePerItem;
return(amount+amount*GST);
}
@Override
public String toString()
{
return String.format("%s\n Name=%s\n Quantity=%d\n
price=%.2f\n","Invoice:",itemName,quantity,pricePerItem);
}
}
class Staff implements Payable
{
private String name;
private double basic;
public Staff(String n,double b)
{
name=n;
basic=b;
}
@Override
public double getPaymentAmount()
{
return(basic+basic*DA+basic*HRA);
}
@Override
public String toString()
{
return String.format("%s\n Name=%s\n
Basic=%.2f\n","Staff:",name,basic);
}
}
public class Assignment15
{
public static void main(String args[])
{
Payable[] payable=new Payable[4];
payable[0]=new Invoice("Bat",4,200);
payable[1]=new Invoice("ball",5,200);
payable[2]=new Invoice("Ramesh",5000,0.0);
payable[3]=new Invoice("shyam",8000,0.0);
System.out.println("Invoice and Staff processed
polymorphically:\n");
for(Payable pbi:payable)
{
System.out.printf("%s Payment due = %.2f%n",
pbi.toString(), pbi.getPaymentAmount());
}
}
}
output:
Invoice and Staff processed polymorphically:
Invoice:
Name=Bat
Quantity=4
price=200.00
Payment due = 840.00
Invoice:
Name=ball
Quantity=5
price=200.00
Payment due = 1050.00
Invoice:
Name=Ramesh
Quantity=5000
price=0.00
Payment due = 0.00
Invoice:
Name=shyam
Quantity=8000
price=0.00
Payment due = 0.00
16. WAP that handles divide-by-zero problem(ArithmeticException) and
invalid input
problem(InputMismatchException) using exception handling .
import java.util.InputMismatchException;
import java.util.Scanner;
public class Assignment16
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
try
{
System.out.print("Enter first number: ");
int a = scanner.nextInt();
System.out.print("Enter second number: ");
int b = scanner.nextInt();
int result = a / b;
System.out.println("Result: " + result);
}
catch (ArithmeticException e)
{
System.out.println("Error: Division by zero is not
allowed.");
}
catch (InputMismatchException e)
{
System.out.println("Error: Please enter valid
integer input.");
}
}
}
output:
Enter first number: 20
Enter second number: 10
Result: 2
Enter first number: 20
Enter second number: 0
Error: Division by zero is not allowed.
Enter first number: 20
Enter second number: 3.5
Error: Please enter valid integer input.
17. WAP that read a string and copy its characters into a character
array using getChars( )
method also handles the problems of accessing index out of string
(StringIndexOutOfBoundsException) and array
(ArrayIndexOutOfBoundsException)
range using exception handling.
public class Assignment17
{
public static void main(String[] args)
{
String str = "Java";
char[] charArray = new char[10];
try {
str.getChars(0, str.length(), charArray, 0);
System.out.print("Characters copied: ");
for (char c : charArray)
{
System.out.print(c + " ");
}
char ch = str.charAt(10);
charArray[15] = 'X';
}
catch (StringIndexOutOfBoundsException e)
{
System.out.println("\nError: String index out of bounds.");
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("\nError: Array index out of
bounds.");
}
}
}
output:
Characters copied: J a v a
Error: String index out of bounds.
18. WAP to create an user-defined exception that is generated when the
age of a person is not
valid to cast a vote. Enter the age of person and validate it and handle
the exception if
generated.
import java.util.Scanner;
class InvalidAgeException extends Exception {
public InvalidAgeException(String message) {
super(message);
}
}
public class Assignment18 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter age: ");
int age = scanner.nextInt();
try {
if (age < 18) {
throw new InvalidAgeException("Age is not valid to cast a
vote.");
} else {
System.out.println("You are eligible to vote.");
}
} catch (InvalidAgeException e) {
System.out.println("Exception: " + e.getMessage());
}
}
}
output:
Enter age: 45
You are eligible to vote.
19.write a program that rename the main thread and print the number form
1 to 10 with the delay
of 1 seconds between each a numbers.
class Assignment19
{
public static void main(String args[])
{
Thread mainThread=Thread.currentThread();
mainThread.setName("my mainthread");
System.out.println("current Thread:"+mainThread.getName());
for(int i=1;i<=10;i++)
{
System.out.println(i);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("Thraed is interrupt");
}
}
}
}
output:
current Thread:my mainthread
1
2
3
4
5
6
7
8
9
10