Skip to main content

FEATURED POST

Why the World is Running Out of Computers

Write a java program to create an abstract class named Shape that contains two integers and an empty method named printArea().

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

import java.util.*;

class Demo{

    public static void main(String ts7[]){

        int ch;

        Rectangle ob1=new Rectangle();

        Triangle ob2=new Triangle();

        Circle ob3=new Circle();

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter:\n1.Rectangle\n2.Triangle\n3.Circle\nEnter your Choice:");

ch=sc.nextInt();

        switch(ch){

            case 1: ob1.printArea(); break;

            case 2: ob2.printArea(); break;

            case 3: ob3.printArea(); break;

            default: System.out.println("Wrong Choice!");   }

    }}

abstract class Shape{

    int i1,i2;

    abstract void printArea();}

class Rectangle extends Shape{

    Scanner sc=new Scanner(System.in);

    int area=0;

    void printArea()    {

System.out.println("Enter length of the Rectangle:");

        i1=sc.nextInt();

System.out.println("Enter breadth of the Rectangle:");

        i2=sc.nextInt();

            area=i1*i2;

System.out.println("Area of the rectangle:"+area);}}

class Triangle extends Shape{

    Scanner sc=new Scanner(System.in);

    int area=0;

    void printArea(){

System.out.println("Enter height of the Triangle:");

        i1=sc.nextInt();

System.out.println("Enter base of the Triangle:");

        i2=sc.nextInt();

        area=(i1*i2)/2;

System.out.println("Area of the Triangle:"+area);

    }}

class Circle extends Shape{

    Scanner sc=new Scanner(System.in);

    double area=0;

    void printArea()    {

System.out.println("Enter radius of the Circle:");

        i1=sc.nextInt();

        area=(22*i1*i1)/7;

System.out.println("Area of the Circle:"+area);

    }

}








Keyword:
How to recover the classnotfoundexception in Java?
What are the top most requirements or expectations of client for develop Java web application?
Java program question paper in 2020
java applet
applet programming in java
creating applet in java


Comments

Popular Posts

Question: Create a base class called “Vehicle” that stores number of wheels and speed. Create the following derived classes

Question: Create a base class called “Vehicle” that stores number of wheels and speed. Create the following derived classes:  Ø “Car” that inherits “Vehicle” and also stores number of passengers.  Ø “Truck” that inherits “Vehicle” and also stores the load limit.  Write a main() function to create objects of these classes and display all the information about Car and Truck. Also, compare the speed of the two vehicles, Car and Truck and display “faster” or “slower” if Car is faster or slower than Truck. import java.util.*; class Vehicle{   int wheels;   double speed; } class Car extends Vehicle{   int pass;   void input(){    Scanner sc=new Scanner(System.in); System.out.println("Enter the car's details:\nNo. of wheels:");    wheels=sc.nextInt(); System.out.println("Speed of Car(Km/hr):");    speed=sc.nextDouble(); System.out.println("No. of passengers:");   ...

Question: Write a java program that implements bank transactions using user defined exception.

Question: Write a java program that implements bank transactions using user defined exception. import java.util.*; class UDE{     public static void main(String ts7[]) throws MinimumBal {         Scanner sc= new Scanner(System.in);         double bal; System.out.println("Enter current balance:"); bal=sc.nextDouble(); System.out.println("Enter amount to withdrawal");         int n = sc.nextInt();         try {             if (bal<n)                   throw new MinimumBal("Account ae taka nei bhai !\nYour Current balance is "+bal);             else      System.out.print...

Question: Develop an abstract class “GeometricObject” which will have two variables colourand weight. It would have constructor function for setting the colour as “white” and the weight as 1.0 as default values. The class should have methods getColour() and getWeight() to return the colour and weight values to the caller. The class should have two abstract methods findArea() and findCircumference(). Write a subclass for “GeometricObject” called “Triangle” which will be able to calculate area and circumference for a triangle.

Question: Develop an abstract class “GeometricObject” which will have two variables colourand weight. It would have constructor function for setting the colour as “white” and the weight as 1.0 as default values. The class should have methods getColour() and getWeight() to return the colour and weight values to the caller. The class should have two abstract methods findArea() and findCircumference().  Write a subclass for “GeometricObject” called “Triangle” which will be able to calculate area and circumference for a triangle. import java.util.*; import java.math.*; abstract class GeometricObject {             String colour;             double weight;             public GeometricObject()             {      ...