Click here to Skip to main content
15,881,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
abstract class Shapes{
    public abstract double area();
    public abstract double perimeter ();
}
class Rectangle extends Shapes{
    private double width;
    private double length;
    public Rectangle(){
        this(1,1);
    }
    public Rectangle(double width, double length){
        this.width = width;
        this.length = length;
    }
    @ Override
    public double area(){
        return width * length;
    }
    @ Override
    public double perimeter(){
        return 2*(width + length);
    }
}


class Triangle extends Shapes{
    double a;
    double b;
    double c;
    public Triangle(){
        this(1,1,1);
    }
    public Triangle (double a, double b, double c){
        this.a = a;
        this.b = b;
        this.c = c;
    }
    @ Override
    public double area(){
        double s = (a+b+c)/2;
        return Math.sqrt(s*(s-a)*(s-b)*(s-c));
    }
    @ Override
    public double perimeter(){
        return a+ b + c;
    }
}
class Square extends Rectangle{
    Square s = new Square(50d);
    double area = s.area();
    double perimeter = s.perimeter();
}

public class Main
{
	public static void main(String[] args) {
	    width = 10;
	    length = 25;
	    Shapes Rectangle = new Rectangle (width, length);
	    System.out.println("The area of the rectangle is: " + Rectangle.area());
}
}


What I have tried:

I am a bit new to inheritance in java, and currently there is a problem where the variables I created in the classes aren't found in the main code. I tried making the variables in the rectangle class private however that did not work. What should I try doing?
Posted
Updated 12-Dec-22 4:53am
v3

Replace
Quote:
public class Square extends Rectangle{
Square s = new Square(50d);
double area = s.area();
double perimeter = s.perimeter();
}

public class Main
{
public static void main(String[] args) {
width = 10;
length = 25;
Shapes Rectangle = new Rectangle (width, length);
System.out.println("The area of the rectangle is: " + Rectangle.area());
}
}
with
Java
class Square extends Rectangle
{
    public Square(int a){ super(a,a); }
}

public class Main
{
  public static void main(String[] args) 
  {
      int width = 10;
      int length = 25;
      Shapes rectangle = new Rectangle (width, length);
      Shapes square = new Square(width);
      System.out.println("The area of the rectangle is: " + rectangle.area());
      System.out.println("The area of the square is: " + square.area());

      int a = 12;
      int b = 20;
      int c = 15;

      Shapes triangle = new Triangle(a,b,c);
      System.out.println("The area of the triangle is: " + triangle.area());
      System.out.println("The perimeter of the triangle is: " + triangle.perimeter());

  }
}
 
Share this answer
 
v2
Comments
CPallini 12-Dec-22 11:36am    
See my updated solution.
Aisha Yosuf 12-Dec-22 12:44pm    
Thank you so much! Before you replied I was able to figure it out. I just had to look carefully at how the other two shapes were printed; by creating a new Triangle or square or rectangle. Once again, thank you!! I appreciate your help :)
This is your third question on this issue. I would suggest you spend some time at Java Tutorials Learning Paths[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900