5 Replies - 910 Views - Last Post: 19 November 2011 - 03:08 PM Rate Topic: -----

#1 joedem   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-May 10

an interface & abstract class

Posted 18 November 2011 - 06:40 AM

Heelo
I need help in solving this question
(A)Develop an interface ShapeInterface to the following specifications:
•The interface includes one method: public double getArea(). Later when getArea() is implemented in a class, it should return a number representing the area of the relevant shape.
(B) Develop a public abstract class Shape to the following specifications:
•The class implements ShapeInterface.
•The class has two private instance variables: int x, and int y .
•Accessor and mutator methods should be implemented for all instance variables.
©Develop a public class Circle to the following specifications:
•The class has a private instance variable int radius.
•The class has a three-argument constructor that sets the values of x, y, and radius to given values.
•The class has a one-argument constructor that sets the values of rad to given value, and x and y to zeros. This should be done by invoking the above three- argument constructor using this keyword.
(D) Develop a public class Rectangle to the following specifications:
•The class is a subclass of Shape.
•The class has two private instance variables int height and int width.
•The class has a four-argument constructor that sets the values of x, y, width and height to given values.
•The class has a two-argument constructor that sets the values of width and height to given values, and x and y to zeros. This should be done by invoking the above four-argument constructor using this keyword.
(E)Both Circle and Rectangle classes should:

•override toString() in java.lang.Object to return a string representation of the instance variables in the relevant class.
•include implementation of any further mandatory methods.
•include accessor and mutator methods for all instance variables.

*** Develop a class ShapesTest to test the classes in Q2.1. The class ShapesTest will only have the main method that should:

•Create a Circle instance using the three argument constructor.
•Create a Rectangle instance using the two argument constructor, and sets the values of the remaining variables using the mutator methods.
•Print on the screen the values of the instance variables of the two above instances using toString().


My Solution a and b , I'll add the rest of the answer after step by step to make sure
first a & b

      public interface ShapeInterface {
	public double getArea();}

       public static void main(String[] args) {
        // TODO code application logic here
        abstract class Shape implements ShapeInterface 
           {
            private int x;
            private int y;

            //public Shape () {  xPos = 0;  yPos = 0;  }

            /*public Shape ( int x, int y )
            {  xPos = x;  yPos = y;  }*/

            public double getXPos () {  return x;  }
            public double getYPos () {  return y;  }

            public void setXPos ( int x ) {  x = x;  }
            public void setYPos ( int y ) {  y = y;  }

            abstract public double getArea ();
            @Override
            public String toString()
            {
            String str = "(X,Y) position: ("+x+","+y+") \n";
            return str;
            }
         }

This post has been edited by Atli: 18 November 2011 - 06:58 AM
Reason for edit:: Use [code] tags when posting code.


Is This A Good Question/Topic? 0
  • +

Replies To: an interface & abstract class

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: an interface & abstract class

Posted 18 November 2011 - 09:31 AM

his should be in its own file ShapeInterface.java

     public interface ShapeInterface {
        public double getArea();
     }



Now in Shape.java you should define the class befor method like here main()

       // TODO code application logic here
       abstract class Shape implements ShapeInterface 
          {
           private int x;
           private int y;

           //public Shape () {  xPos = 0;  yPos = 0;  }

           /*public Shape ( int x, int y )
           {  xPos = x;  yPos = y;  }*/

           public double getXPos () {  return x;  }
           public double getYPos () {  return y;  }

           public void setXPos ( int x ) {  x = x;  }
           public void setYPos ( int y ) {  y = y;  }

           abstract public double getArea ();
           @Override
           public String toString()
           {
           String str = "(X,Y) position: ("+x+","+y+") \n";
           return str;
           }
        
 
        // here main() if you want/need it
        public static void main(String[] args) {
           ....
        }
}


Was This Post Helpful? 0
  • +
  • -

#3 robertmarks62   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-November 11

Re: an interface & abstract class

Posted 18 November 2011 - 09:34 AM

Here is the Difference Between Interface and Abstract Class


Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior.
Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.
Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc..
Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.
An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
A Java class can implement multiple interfaces but it can extend only one abstract class.
Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists.
In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: an interface & abstract class

Posted 18 November 2011 - 11:39 AM

View Postrobertmarks62, on 18 November 2011 - 12:34 PM, said:

In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.

Can you elaborate on that one please ? If if possible post figures/numbers to prove that point.
Was This Post Helpful? 0
  • +
  • -

#5 joedem   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-May 10

Re: an interface & abstract class

Posted 19 November 2011 - 11:56 AM

thanks all now
©Develop a public class Circle to the following specifications:
•The class has a private instance variable int radius.
•The class has a three-argument constructor that sets the values of x, y, and radius to given values.
•The class has a one-argument constructor that sets the values of rad to given value, and x and y to zeros. This should be done by invoking the above three- argument constructor using this keyword.

class Circle extends Shape
{
private int radius;

public Circle ( int x, int y, int rad )
{x = 0; y =0; radius = rad; }

public double getRadius () { return radius; }
public void setRadius ( int rad ) { radius = rad; }

@Override
public double getArea () { return Math.PI * radius * radius; }
}
Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: an interface & abstract class

Posted 19 November 2011 - 03:08 PM

Please :code:

public Circle ( int x, int y, int rad ) {
    x = 0; 
    y =0; 
    radius = rad;   // this is OK
}
 

The x = 0; and y = 0; are useless. They simply set to 0 the values of the parameters x and y which are not used further down in the constructor.

You'll have to call super() to pass it the value of x and y which are private variable of shape so propably
    super(x, y);


but you commented out that constructor of Shape in your first post

You also miss your constructor

public Circle(int rad) that will call your constructor
public Circle(int x, int y, int rad)

Happy coding
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1