Oops Program 5,6 & 7
Oops Program 5,6 & 7
DATE:
Algorithm
Step2: Create an abstract class for shapes and declare the variables length, breadth and radius.
Step3: Rectangle, Triangle and Circle subclass extends the abstract class shape.
Step5: Calculate the area of rectangle, triangle and circle and in main class Print Area () method
is invoked.
Step6: Display the result for area of rectangle triangle and circle.
Step7: Stop the program.
1
Program:
import java.util.*;
abstract class Shape {
int length, breadth, radius;
Scanner input = new Scanner(System.in);
abstract void printArea();
}
class Rectangle extends Shape {
void printArea() {
System.out.println("*** Finding the Area of Rectangle ***");
System.out.print("Enter length and breadth: ");
length = input.nextInt();
breadth = input.nextInt();
System.out.println("The area of Rectangle is: " + length * breadth);
}
}
class Triangle extends Shape {
void printArea() {
System.out.println("\n*** Finding the Area of Triangle ***");
System.out.print("Enter Base And Height: ");
length = input.nextInt();
breadth = input.nextInt();
System.out.println("The area of Triangle is: " + (length * breadth) / 2);
}
}
class Circle1 extends Shape {
void printArea() {
System.out.println("\n*** Finding the Area of Circle ***");
System.out.print("Enter Radius: ");
radius = input.nextInt();
System.out.println("The area of Circle is: " + 3.14 * radius * radius);
}
}
public class AbstractClassExample {
public static void main(String[] args) {
Rectangle rec = new Rectangle();
rec.printArea();
Triangle tri = new Triangle();
tri.printArea();
Circle1 cir = new Circle1();
cir.printArea();
}
}
2
Output:
3
Result:
Thus the java application for abstract class using shapes was implemented and the program was executed
successfully.
Aim:
To create a Java console application to find the area of different shapes using interface concept in java.
Algorithm:
Step 1 Start the Process
4
Program:
interface area
{
double pi = 3.14;
double calc(double x,double y);
}
class InterfaceShapes
{
public static void main(String arg[])
{
rect r = new rect();
cir c = new cir();
5
area a;
a = r;
System.out.println("\nArea of Rectangle is : " +a.calc(10,20));
a = c;
System.out.println("\nArea of Circle is : " +a.calc(15,15));
}
}
Output:
6
Result:
The Java console application to find the area of different shapes using interface concept in java was developed
and tested successfully.
7
EX NO:7a Java Application to Implementation of exception handling
DATE:
Aim:
To create a Java console application for Implementation of exception handling.
Algorithm:
Step 1 Start the Process
Step 3 The statement that are likely to cause an exception are enclosed with in a try block.
Step 4 There is an another block defined by the keyword catch which is responsible for handling the
Step 5 If the value of a=20 and b=0 then divide a/b it will throw arithmetic Exception.
8
Program:
9
Output:
10
Result:
The Java console application for implementation of exception handling in java was developed and tested
successfully.
Aim:
To create a Java console application for Creation of User defined Exception.
Algorithm:
Step 1 Start the Process
Step 3 The class that uses custom exception InvalidAgeException to check the age.
Step 5 If the age less than 18 then throw an object of user defined exception that the age is not
valid to vote.
11
Program:
// main method
public static void main(String args[])
{
12
try
{
// calling the method
validate(13);
}
catch (InvalidAgeException ex)
{
System.out.println("Caught the exception");
Output:
13
Result:
Thus the Java console application for implementation of user defined exception was developed
14