EX NO:5 IMPLEMENTATION OF ABSTRACT CLASS
DATE:
Algorithm
Step1: Start the program
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.
Step4: printArea () method get the inputs for the subclasses.
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.
EX NO:6 Java Application to Find the Area of different Shapes – Interface
DATE:
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
Step 2 Prompt user with List Operation Choices 1. Rectangle 2.Circle.
Step 3 Get the Length and Breath of a Rectangle.
Step 4 Compute Area = Length * Breath
Step 5 Display Area
Step 6 Get the Radius of the Circle
Step 7 Compute Area = 3.14 * Radius * Radius
Step 8 Display Area
Step 9 End the process
4
Program:
interface area
{
double pi = 3.14;
double calc(double x,double y);
}
class rect implements area
{
public double calc(double x,double y)
{
return(x*y);
}
}
class cir implements area
{
public double calc(double x,double y)
{
return(pi*x*x);
}
}
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 2 Enter the value of a and b.
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
exception thrown by the try block.
Step 5 If the value of a=20 and b=0 then divide a/b it will throw arithmetic Exception.
Step 6 Display the Result.
Step 7 Stop the Process.
8
Program:
public class ArithmeticExcep {
public static void main(String[] args) {
int a,b,c;
a=20;
b=0;
try {
c=a/b;
}
catch(ArithmeticException e)
{
System.out.println("\n Arithmetic Exception occurs divided by zero");
}
System.out.println("\n The value of a:"+a);
System.out.println("\n The value of b:"+b);
9
Output:
10
Result:
The Java console application for implementation of exception handling in java was developed and tested
successfully.
EX NO:7b Java Application for Creation of User defined Exception
DATE:
Aim:
To create a Java console application for Creation of User defined Exception.
Algorithm:
Step 1 Start the Process
Step 2 The constructor of parent Exception super(str) is called.
Step 3 The class that uses custom exception InvalidAgeException to check the age.
Step 4 Check the condition if age < 18.
Step 5 If the age less than 18 then throw an object of user defined exception that the age is not
valid to vote.
Step 6 Display the Result.
Step 7 Stop the Process.
11
Program:
class InvalidAgeException extends Exception
{
public InvalidAgeException (String str)
{
// calling the constructor of parent Exception
super(str);
}
}
// class that uses custom exception InvalidAgeException
public class UserDefException
{
// method to check the age
static void validate (int age) throws InvalidAgeException{
if(age < 18){
// throw an object of user defined exception
throw new InvalidAgeException("\n Age is not valid to vote");
}
else {
System.out.println("welcome to vote");
}
}
// main method
public static void main(String args[])
{
12
try
{
// calling the method
validate(13);
}
catch (InvalidAgeException ex)
{
System.out.println("Caught the exception");
// printing the message from InvalidAgeException object
System.out.println("Exception occured: " + ex);
System.out.println("Rest of the code...");
}
}
Output:
13
Result:
Thus the Java console application for implementation of user defined exception was developed
and executed successfully.
14