-1

I have this code which will find the area of different shapes depending on the inputted shape of the user. The problem is how can I get the inputted measurements(e.g. length, width) from the main class to the Circle, triangle, rectangle and square classes? here's my code.

    import java.lang.Math;
    import java.util.Scanner;
    import java.text.DecimalFormat;

    class Circle{
    double radius;
    void CircleMeasurement(){
        radius = r;
    }

    double getCircleArea(){
        return(Math.PI*Math.pow(radius,2));
    }
    }

    class Triangle{
    int base, height;
    void TriangleMeasurement(){
        base = b;
        height = h;
    }
    int getTriangleArea(){
        return((base*height)/2);
    }
   }

    class Rectangle{
    int length, width;
    void RectangleMeasurement(){
        length = l;
        width = w;
    }
    int getRectangleArea(){
        return(length*width);
    }
     }

    class Square{
    int sides;
    void SquareMeasurement(){
        sides = s;
    }
    int getSquareArea(){
        return( sides * sides);
    }
   }


    class Shapes{
    public static void main(String[] args){
    String key;
    double r;
    int b, h, l, w, s;

        System.out.println("Welcome!");
        System.out.println("Choose your option:");
        System.out.println("1 - Circle, 2 - Triangle, 3 - Rectangle, 4 - Square");
        Scanner in = new Scanner(System.in);
        key = in.nextLine();


    if (key=="1" || key =="circle"){
        System.out.println("Area for Circle");
        System.out.println("Enter radius:");
        Scanner.in = new Scanner(System.in);
        r = in.nextInt;
        Circle circle1 = new Circle();
        System.out.println("The area is equal to" + circle1.getCircleArea());
    }


    else if (key == "2"){
        System.out.println("Area for Triangle");
        System.out.println("Enter base:");
        Scanner.in = new Scanner(System.in);
        b = in.nextInt;
        System.out.println("Enter height:");
        h = in.nextInt;
        Triangle triangle1 = new Triangle();
        System.out.println("The area is equal to" + triangle1.getTriangleArea());

    }

    else if (key == "3"){
        System.out.println("Area for Rectangle");
        System.out.println("Enter length:");
        Scanner.in = new Scanner(System.in);
        l = in.nextInt;
        System.out.println("Enter width:");
        w = in.nextInt;
        Rectangle rectangle1 = new Rectangle();
        System.out.println("The area is equal to" + rectangle1.getRectangleArea());
    }

    else if (key == "4"){
        System.out.println("Area for Square");
        System.out.println("Enter side:");
        Scanner.in = new Scanner(System.in);
        s = in.nextInt;
        Square square1 = new Square();
        System.out.println("The area is equal to" + square1.getSquareArea());
   }

   }
   }
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55

5 Answers5

1

You can set the appropriate variables at the time of Object creation using constructor.

Circle(int r){
 radius = r;
}

Rectangle(int l, int b){
length = l;
breadth = b;
}

Circle c = new Circle(9); //creates a new Circle with radius 9
Rectangle r = new Rectangle(2,3) //Creates a new Rectangle with length as 2, breadth as 3

That said, you can also use setter methods.

Also, using == to compare Strings is frowned upon and often will give you wrong results. Use .equals() instead.

"Circle".equals(input);
rocketboy
  • 9,573
  • 2
  • 34
  • 36
  • This is the best answer to the question, +1. The main question is answered first, then the `==` problem as a side note. – Magnilex Aug 21 '13 at 11:25
0

Two things to mention here:

  1. Pass the values taken from scanner to classes as argument to method or constructor. So it can be used by method for furthur calculations. Also using inheritance here will be a good option.
  2. Never use == for string comparision. It will not work, use .equals instead.
Community
  • 1
  • 1
Harry Joy
  • 58,650
  • 30
  • 162
  • 207
0

One of the problem in your code is that you are comparing string using ==. Strings should be compared using equals method.

change such statements

if (key=="1" || key =="circle"){

to

if (key.equals("1") || key.equals("circle")){

The other problem is that you have not defined constructors with proper arguemnts in your classes such as Circle, Triangle etc. As you are not setting the proper attributes so calling the methods will not provide you the correct results.

For example you need to have constructor in Circle class,to initialize the radius param:

public Circle(int r) {
    radius = r;
}

And creating the object of Circle like this, followed by call to getCircleArea should give u the desired result:

 Circle circle1 = new Circle(r);
 System.out.println("The area is equal to" + circle1.getCircleArea());
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • I would just like to ask why i had this error saying: error: constructor Circle in class Circle cannot be applied to given types Circle circle1 = new Circle(r); – Tru Gamer Aug 21 '13 at 11:32
  • @TruGamer You haven't implemented a constructor with the correct arguments yet. As I said in a comment for your question, read about constructors in Java: http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html Without understanding this, you won't get far, as you notice. – Magnilex Aug 21 '13 at 11:39
  • @Magniflex class Circle{ int radius; public int Circle(int r) { radius = r; } int getCircleArea(){ return(Math.PI*Math.pow(radius,2)); } }System.out.println("Enter radius:"); Scanner.in = new Scanner(System.in); r = in.nextInt(); Circle circle1 = new Circle(r); System.out.println("The area is equal to" + circle1.getCircleArea()); isn't this the right thing? – Tru Gamer Aug 21 '13 at 11:46
0

You can set values of variables in several ways. First way it's constructor, see rocketboy answer. Second one it's mark your variables as private and use mutator methods. It's recomended way/ See example below:

 class Circle{
        private double radius;
        Circle(double radius){
            this.radius = radius;
        }
        public void setRadius(double radius){
            this.radius = radius;
        }
        public double getRadius(){
            return radius;
        }
Michael Kazarian
  • 4,376
  • 1
  • 21
  • 25
-2

Firs you should create getters and setters.Then

 class Circle{
    double radius;
    public double getRadius(){
    return radius;
    }
    public void setRadius(double r){
    radius=r;
    }
    void CircleMeasurement(){
        radius = r;
    }
....
    r = in.nextInt;
        Circle circle1 = new Circle();
    circle1.setRadious(r);
ihsan kocak
  • 1,541
  • 1
  • 17
  • 26
  • 1
    This is a bad idea. He should probably avoid having private constructors. A circle doesn't make much sense if it doesn't have a radius, right? Instead a constructor with mandatory fields should be implemented. – Magnilex Aug 21 '13 at 11:24