Menu


Notice: Undefined index: alg in /home/u681245571/domains/studyglance.in/public_html/labprograms/javadisplay.php on line 82

Notice: Undefined index: url2 in /home/u681245571/domains/studyglance.in/public_html/labprograms/javadisplay.php on line 84

Notice: Undefined index: url3 in /home/u681245571/domains/studyglance.in/public_html/labprograms/javadisplay.php on line 85

Notice: Undefined index: url4 in /home/u681245571/domains/studyglance.in/public_html/labprograms/javadisplay.php on line 86

Notice: Undefined index: opurl2 in /home/u681245571/domains/studyglance.in/public_html/labprograms/javadisplay.php on line 89

Notice: Undefined index: opurl3 in /home/u681245571/domains/studyglance.in/public_html/labprograms/javadisplay.php on line 90

Notice: Undefined index: opurl4 in /home/u681245571/domains/studyglance.in/public_html/labprograms/javadisplay.php on line 91

Notice: Undefined index: opurl5 in /home/u681245571/domains/studyglance.in/public_html/labprograms/javadisplay.php on line 92

Java Programming [ Lab Programs ]


Aim:

Write a Java program to create an abstract class named Shape that contains two integers and an empty method named print Area (). Provide three classes named Rectangle, Triangle, and Circle such that each one of the classes extends the class Shape. Each one of the classes contains only the method print Area () that prints the area of the given shape.

Source Code:

AreaOfShapes.java


import java.util.*;

abstract class Shape {    
    public int x,y;
    public abstract void printArea();
}
class Rectangle1 extends Shape {
    public void printArea() {
        float area;
        area= x * y;
        System.out.println("Area of Rectangle is " +area);
    }
}
class Triangle extends Shape {
    public void printArea() {
        float area;
        area= (x * y) / 2.0f;
        System.out.println("Area of Triangle is " + area);
    }
}
class Circle extends Shape {
    public void printArea() {
        float area;
        area=(22 * x * x) / 7.0f;
        System.out.println("Area of Circle is " + area);
    }
}
public class AreaOfShapes {
    public static void main(String[] args) {
        int choice;
        Scanner sc=new Scanner(System.in);
        System.out.println("Menu \n 1.Area of Rectangle \n 2.Area of Traingle \n 3.Area of Circle "); 
        System.out.print("Enter your choice : ");
        choice=sc.nextInt();
        switch(choice) {
            case 1: System.out.println("Enter length and breadth for area of rectangle : ");
                    Rectangle1 r = new Rectangle1();
                    r.x=sc.nextInt();
                    r.y=sc.nextInt();
                    r.printArea();
                    break;
            case 2: System.out.println("Enter bredth and height for area  of traingle : ");
                    Triangle t = new Triangle();
                    t.x=sc.nextInt();
                    t.y=sc.nextInt();
                    t.printArea();
                    break;
            case 3: System.out.println("Enter radius for area of circle : ");
                    Circle c = new Circle();
                    c.x = sc.nextInt();
                    c.printArea();
                    break;
            default:System.out.println("Enter correct choice"); 
        } 
    }
}

Output:

image

Related Content :

Java Programming Lab Programs

1) Use eclipse or Netbean platform and acquaint with the various menus, create a test project, add a test class and run it see how you can use auto suggestions, auto fill. Try code formatter and code refactoring like renaming variables, methods and classes. Try debug step by step with a small program of about 10 to 15 lines which contains at least one if else condition and a for loop. View Solution

2) Write a Java program that works as a simple calculator. Use a grid layout to arrange buttons for the digits and for the +, -,*, % operations. Add a text field to display the result. Handle any possible exceptions like divided by zero. View Solution

3) a) Develop an applet in Java that displays a simple message. View Solution

3) b) Develop an applet in Java that receives an integer in one text field, and computes its factorial Value and returns it in another text field, when the button named “Compute” is clicked. View Solution

4) Write a Java program that creates a user interface to perform integer divisions. The user enters two numbers in the text fields, Num1 and Num2. The division of Num1 and Num 2 is displayed in the Result field when the Divide button is clicked. If Num1 or Num2 were not an integer, the program would throw a Number Format Exception. If Num2 were Zero, the program would throw an Arithmetic Exception. Display the exception in a message dialog box. View Solution

5) Write a Java program that implements a multi-thread application that has three threads. First thread generates random integer every 1 second and if the value is even, second thread computes the square of the number and prints. If the value is odd, the third thread will print the value of cube of the number. View Solution

6) Write a Java program for the following: i) Create a doubly linked list of elements. ii) Delete a given element from the above list. iii)Display the contents of the list after deletion. View Solution

7) Write a Java program that simulates a traffic light. The program lets the user selects one of three lights: red, yellow, or green with radio buttons. On selecting a button, an appropriate message with Stop, Ready, or Go should appear above the buttons in the selected color. Initially, there is no message shown View Solution

8) Write a Java program to create an abstract class named Shape that contains two integers and an empty method named print Area (). Provide three classes named Rectangle, Triangle, and Circle such that each one of the classes extends the class Shape. Each one of the classes contains only the method print Area () that prints the area of the given shape. View Solution

9) Suppose that a table named Table.txt is stored in a text file. The first line in the file is the header, and the remaining lines correspond to rows in the table. The elements are separated by commas. Write a java program to display the table using Labels in Grid Layout. View Solution

10) Write a java program that handles all mouse events and shows the event name at the center of the window when a mouse event is fired (Use Adapter classes). View Solution