1

Problem Statement is "To develop 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 contains only the method printArea() that prints the area of the given shape."

In this program, I want to get the two integer values contained by the abstract class Shape, from the user (run time) instead of compile time.

This is my code

abstract class Shape

{   

    abstract void Printarea();

    int a=10,b=2;;

}

class Rectangle extends Shape

{

    void Printarea()

    {       

       System.out.println("area of rectangle is "+(a*b));

    }

}     

class Triangle extends Shape

{

    void Printarea()

    {       

        System.out.println("area of triangle is "+(0.5*a*b));

    }

}   



class Circle extends Shape

{       

    void Printarea()

   {       

        System.out.println("area of circle is "+(3.14*a*a));

   }  

}   

class Main

{

    public static void main(String []args)       

    {     

       Shape=b;

       b=new Circle();      

       b.Printarea();

       b=new Rectangle();

       b.Printarea();    

       b=new Triangle();

       b.Printarea();

   }

}
Michael Lihs
  • 5,664
  • 12
  • 40
  • 67
Kishan
  • 118
  • 3
  • 11

4 Answers4

0

Add Setter and getter method for variables a,b ; here's my code

package com.docker.container.controllers;

 /**
 * @author atwa Jul 29, 2018
 */
public abstract class Shape {

abstract void Printarea();

int a = 10, b = 2;

/**
 * @return the a
 */
public int getA() {
    return a;
}

/**
 * @param a
 *            the a to set
 */
public void setA(int a) {
    this.a = a;
}

/**
 * @return the b
 */
public int getB() {
    return b;
}

/**
 * @param b
 *            the b to set
 */
public void setB(int b) {
    this.b = b;
}

static public class Rectangle extends Shape {

    void Printarea()

    {

        System.out.println("area of rectangle is " + (a * b));

    }

}

static class Triangle extends Shape

{

    void Printarea()

    {

        System.out.println("area of triangle is " + (0.5 * a * b));

    }

}

static class Circle extends Shape

{

    void Printarea()

    {

        System.out.println("area of circle is " + (3.14 * a * a));

    }

}

// area of circle is 314.0
// area of rectangle is 20
// area of triangle is 10.0
public static void main(String[] args)

{

    Shape b = new Circle();
    b.setA(5);
    b.setB(5);

    b.Printarea();

    b = new Rectangle();

    b.Printarea();

    b = new Triangle();

    b.Printarea();

}

}

Salah Atwa
  • 1,232
  • 1
  • 10
  • 11
0

Try with this:

public class Main {

public static void main(String[] args) {
    Shape b;

    Scanner in = new Scanner(System.in);
    int x = in.nextInt();
    int y = in.nextInt();

    b = new Circle(x, y);

    b.Printarea();

    b = new Rectangle(x, y);

    b.Printarea();

    b = new Triangle(x, y);

    b.Printarea();

}

private static int Abs(int a) {
    return a < 0 ? -a : a;
}
}

abstract class Shape {

int a, b;

public Shape(int a, int b) {
    this.a = a;
    this.b = b;
}

abstract void Printarea();

}

class Rectangle extends Shape {
 public Rectangle(int a, int b) {
    super(a, b);
}

void Printarea()

{

    System.out.println("area of rectangle is " + (a * b));

}

}

class Triangle extends Shape {
public Triangle(int a, int b) {
    super(a, b);
}

void Printarea()

{

    System.out.println("area of triangle is " + (0.5 * a * b));

}

}
GolamMazid Sajib
  • 6,630
  • 4
  • 17
  • 32
0

you can use Scanner class like this

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int i = scan.nextInt();
}
0

Some quick code but works as expected and would give you idea:

import java.util.Scanner;

abstract class Shape {

    int a = 10, b = 2;

    Shape(int a, int b){
        this.a=a;
        this.b=b;
    }

    abstract void Printarea();

}

class Rectangle extends Shape {

    Rectangle(int a, int b) {
        super(a, b);
    }

    void Printarea() {
        System.out.println("area of rectangle is " + (a * b));
    }

}

class Triangle extends Shape {

    Triangle(int a, int b) {
        super(a, b);
    }

    void Printarea(){
        System.out.println("area of triangle is " + (0.5 * a * b));
    }

}

class Circle extends Shape {

    Circle(int a, int b) {
        super(a, b);
    }

    void Printarea() {
        System.out.println("area of circle is " + (3.14 * a * a));
    }

}

class Z {

    public static void main(String[] args){

        Shape shape=null;

        String input;
        int width, height;

        while (true) {

            Scanner scanner = new Scanner(System.in);

            System.out.println("which shape? circle/rectangle/triangle (write any other thing for quitting): ");
            input = scanner.nextLine();

            if(!"circle".equalsIgnoreCase(input) && !"rectangle".equalsIgnoreCase(input) && !"triangle".equalsIgnoreCase(input) ){
                System.exit(0);
            }

            System.out.println("height: ");
            height  = scanner.nextInt();

            System.out.println("width: ");
            width = scanner.nextInt();

            if("circle".equalsIgnoreCase(input)){
                shape=new Circle(width, height);
            }
            else if("rectangle".equalsIgnoreCase(input)){
                shape=new Rectangle(width, height);
            }
            else{ // == triangle
                shape=new Triangle(width, height);
            }

            shape.Printarea();

        }

    }

}
yılmaz
  • 1,690
  • 10
  • 15