Hi can you please help me with the following code program as I am new to java.

Step1: define the interface named Ashape

write an interface called Ashape that defines:
double DEFAULT_SIZE equal to 1.0 [by default it is constant]
method calcPerim() that returns a double equal to the perimeter of the shape.

step2: write the classes ATriangle and ARectangle

Atriangle class has three double instance variables; side1, side2 and hypot.
ARectangle class has two double instance variables; width and height.

both classes have the following attributes and additional methods:
Have a constructor that has a parameter to initialize each instance variable.

have a default constaructor to initialize each instance variable to the value of DEFAULT_SIZE.

Have a tostring() method which prints the class name [Triangle or Rectangle] attributes wit text identifying them, and the "perimeter" with its value (see sample output)

step3 write the main class TestShapes

Define four objects:
Triangle has sides 3,4,5
Rectangle with the width of 6 and the height of 8
Triangle object created by the default constructor
Rectangle object created by the default constructor

Print the four method use the toString() method

Step4 Using the interface constants:

In the TestShapes class, print DEFAULT_SIZE five times as shown in the sample output below:
1. using AShape interface name
2. using ATriangle class name
3. using ATriangle object name
4. using ARectangle object

sample output:

default shapes
Triangle: side1 = 1.0 side2 = 1.0 hypot = 1.0 perimeter = 3.0
Rectangle: length = 1.0 width = 1.0 perimeter = 4.0

specified shapes
Triangle: side1 = 3.0 side2 = 4.0 hypot = 5.0 perimeter = 12.0
Rectangle: length = 8.0 width = 6.0 perimeter = 28.0

Accessing Constant in AShape interface
Access via Ashape interface: 1.0
Access via Atriangle class: 1.0
Access via Atriangle object: 1.0
Access via Arectangle object:1.0
Access via Arectangle object: 1.0

Can you please tell me although TestShapes does not inplement the AShape interface why we can access AShape.DEFAULT_SIZE in the TestShape class?

Thank you sooo much

8 Years
Discussion Span
Last Post by javaAddict

You can access it because you write:

If DEFAULT_SIZE is declared as static it means you don't need to initialize the class in order to call it. So whenever you call:
AShape.DEFAULT_SIZE you have access to the DEFAULT_SIZE of the AShape class.

If TestShapes implemented the AShape then you didn't have to put the name of the class ahead, you could just call the DEFAULT_SIZE right away like this:


But now that it doesn't implement the interface you need to "call" the class fist:


Actually I made a mistake.
You don't need to declare it static in the interface, just use the:
AShape. in front

If it was not an interface but a simple class then you would have in order to call it like this:


Edited by javaAddict: n/a


Thank you soo much for your help, but I am stuck near step4 that is using interface constants to define objects.

Here is my program, can you please correct it where I went wrong?

Ashape interface

public interface AShape
  public static final double DEFAULT_SIZE = 1.0;
  public static final double perimeter = 0.0;

   public abstract double calcPerim();


 public abstract class ATriangle implements AShape
      double side1;
      double side2;
      double hypot;

       public ATriangle()
         side1 = DEFAULT_SIZE;
         side2 = DEFAULT_SIZE;
         hypot = DEFAULT_SIZE;

       public ATriangle(double _side1, double _side2,double _hypot)

       public double getSide1()
         return side1;
       public double getSide2()
         return side2;
       public double getHypot()
         return hypot;

       public abstract void setSide1(double DEFAULT_SIZE);

       public abstract void setSide2(double DEFAULT_SIZE);

       public abstract void setHypot(double DEFAULT_SIZE);      

       public String toString()
         return "ATriangle:  = side1 = " + getSide1() + " side2 = " + getSide2() + " hypot = " + getHypot();


 public abstract class ARectangle implements AShape
      double width;
      double height;

       public ARectangle()
         width = DEFAULT_SIZE;
         height = DEFAULT_SIZE;
       public ARectangle(double _width, double _height)
       public double getWidth()
         return width;
       public double getHeight()
         return height;

       public void setWidth(double DEFAULT_SIZE)
       public void setHeight(double DEFAULT_SIZE)

       public String toString()
         return "ARectangle: length = " + getHeight() + " width = " + getWidth() + " perimeter  = " ;       

TestShape class:
In this I understand that I cannot create an object using interface class, but in step 4 they asked me to define object how do we do that?

 import java.util.*;
    public class TestShapes
       public static void main (String[] args)
         ArrayList<AShape> al = new ArrayList<AShape>();

         AShape tri = new ATriangle();// this is triangle

       //create triangle object
         ATriangle tri1 = new ATriangle (3,4,5);

         ARectangle rec = new ARectangle();
         ARectangle rec1 = new Arectangle(6,8);



Can you please help me, I am stuck in between...thanks a lot!!

Edited by mike_2000_17: Fixed formatting


You declared your rectange & triange classes as "abstract". That means they cannot be instantiated. Becuase these are classes that you want to use to create rectange & triange objects, they should not be declared abstract.


Also you don't need to define the method as abstract since you have it in an interface. If the class was abstract some can be implemented and others defined as abstract.
but in interfaces all methods are declared like this:

public interface AShape
public static double DEFAULT_SIZE = 1.0;

public double calcPerim();

Also you don't need the "perimeter" variable. The method will calculate the perimeter and return it, You will implement it in your classes that implement the interface.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.