Two Laptop Bag*
The moose likes Beginning Java and the fly likes Problem on interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem on interface" Watch "Problem on interface" New topic
Author

Problem on interface

Megha Jain
Ranch Hand

Joined: Sep 12, 2005
Posts: 30
Hi,

i was trying one program on interface, i don't know where i am getting wrong, but when i complied the program at some other system some time back, it complied successfully and gave me the output, but when i tried it today i was getting errors.
Well one more thing, actually i was doing this to practise interface, but i think the logic or code which i am writing is wrong, so please see to it and suggest me,

Here's the code:

/* Program on Interface:

1. Create an interface Area with two methods:
a)Area(int side1, int side2).
b)Perimeter(int side1, int side2)

2. Create class Square that implements an interface Area, and defines above two methods to calculate area and perimeter of square.

3. Create class Rectangle that implements an interface and defines above two methods to calculate area and perimeter of rectangle.

4.Create class DemoInterface that uses class Square and Rectangle to calculate the area and perimeter of Square and Rectangle.*/

/********* This inteface i saved in same folder by name Area1.java *******8*/
interface Area1{
AreaCalculate(int length, int breadth);
Perimeter(int side1, int side2);
}
/******************************************************************/
class Square implements Area1
{
//Area1 a1=new Area1();
//Area1 a2=new Area1();
public int AreaCalculate(int l, int b)
{
int AreaSquare=0;
AreaSquare=l*b;
return AreaSquare;
}

public int Perimeter(int s1, int s2)
{
int perimeterSquare=0;
perimeterSquare=2*(s1+s2);
return perimeterSquare;
}
}

class Rectangle1 implements Area1
{
//Area1 a3=new Area1();
//Area1 a4=new Area1();
public int AreaCalculate(int l, int b)
{
int AreaRect=0;
AreaRect=l*b;
return AreaRect;
}

a4.Perimeter(int s1, int s2)
{
int perimeterRect=0;
perimeterRect=2*(s1+s2);
return perimeterRect;
}
}

public class DemoInterface
{
public static void main(String[] args)
{
int AreaOfSquare=0, AreaOfRect=0, PerimeterOfSquare=0, PerimeterOfRectangle=0;

Square sq1=new Square();
AreaOfSquare=sq1.AreaCalculate(10,10);
System.out.println("Area of Square is ="+AreaOfSquare);

Square sq2=new Square();
PerimeterOfSquare=sq2.Perimeter(10,10);
System.out.println("Perimeter of Square is ="+PerimeterOfSquare);

Rectangle1 rect1=new Rectangle1();
Rectangle1 rect2= new Rectangle1();

AreaOfRect=rect1.AreaCalculate(4,4);
System.out.println("Area of Rectangle is ="+AreaOfRect);

PerimeterOfRectangle=rect2.Perimeter(4,4);
System.out.println("Perimeter of Rectangle is ="+PerimeterOfRectangle);

}
}
Janaranjani K
Greenhorn

Joined: Jan 20, 2006
Posts: 13
a4.Perimeter(int s1, int s2)-------the error is here.
{
int perimeterRect=0;
perimeterRect=2*(s1+s2);
return perimeterRect;
}
It should be
public int Perimeter(int s1,int s2)
{
//your code....
}

Try to follow the name convention specified in Java i.e method's name should start with small letter.

Code will execute if you don't follow also.......but that is not a good programming practise.


Janaranjani<br />SCJP 1.5(97%)
Megha Jain
Ranch Hand

Joined: Sep 12, 2005
Posts: 30
sorry i forgot to change that to public int Perimeter inspite of a4.perimeter, i tried that as well initially but still it was not working.
Madhan Kumar
Greenhorn

Joined: Jan 09, 2006
Posts: 7
interface Area1{
AreaCalculate(int length, int breadth);----->this is where the error is
Perimeter(int side1, int side2);----->error
}
There is no return type for the above method. Since we are returning a value we would do it as.....

interface Area1{
int AreaCalculate(int length, int breadth);
int Perimeter(int side1, int side2);
}
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Please post the exact error messages and indicate which line the message comes from. This will help us explain what the error messages mean so that you will be able to fix them on your own in the future.

Layne


Java API Documentation
The Java Tutorial
Megha Jain
Ranch Hand

Joined: Sep 12, 2005
Posts: 30
Hi,

Actually for this program i had made two files,
1. DemoInterface .java
2. Area1.java

When i tried to complie any of the two files following errors were dispalyed

java 15:<identifier >expected
AreaCalculate(int length, int breadth);

java 16::<identifier >expected
Perimeter(int side1, int side2);

java 15:cannot resolve symbol Class AreaCalculate(int length, int breadth);

java 16:cannot resolve symbol Class Perimeter(int side1, int side2);
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I don't think you posted the entire error message here. At least for Sun's compiler, it will show which file the error is found in. I assume that these errors are from the Area1.java file:

The problem is that your methods AreaCalculate() and Perimeter() need to have a return type. In other words, then need to declare the kind of value that the result will be. Does this help you understand how you need to change it? What types will you use in this case?

Layne
Megha Jain
Ranch Hand

Joined: Sep 12, 2005
Posts: 30
I think, in interfaces we only declare the methods there is no need to declare the return type.

Actually once before when i have written the same problem it runned successfully, here when i am trying to do it once again it is showing problem. I am not able to point out where actually the problem is
Maximilian Xavier Stocker
Ranch Hand

Joined: Sep 20, 2005
Posts: 381
Originally posted by Megha Jain:
I think, in interfaces we only declare the methods there is no need to declare the return type.



This is incorrect.

By looking at your implementation the Interface should look like this



You may NOT have a body to methods listed in an interface but you MUST have have return type.
Maximilian Xavier Stocker
Ranch Hand

Joined: Sep 20, 2005
Posts: 381
You may find this tutorial link helpful http://java.sun.com/docs/books/tutorial/java/concepts/interface.html
Megha Jain
Ranch Hand

Joined: Sep 12, 2005
Posts: 30
Yeah, you are correct, actually when u said there should be a return type is thought you are saying this was
Area1(int side1, side2){
return someVarible;
}

you are correct.
I modified my code and now it's working fine.
Thanks everyone.


One more thing in my code which i have posted above i don't think i am actually making good use of interface , i mean can anyone suggest me some good implementation of it so that i can reduce my good length making use of interface.
Maximilian Xavier Stocker
Ranch Hand

Joined: Sep 20, 2005
Posts: 381
Here is a better idea (I think) based on what you have.



Why do I think this better? Well what if you want to add new implementations for Triangles, Circles or other shapes?

This design supports that. Yours kind of ties the implementation specifics into the interface directly by having it take the length and width parameters.

Not that you can't have paramaters in methods in an interface. Far from it. But in your particular case it defines the implementation too which kind of defeats the purpose of abstraction with an interface.

I hope this helps.
Megha Jain
Ranch Hand

Joined: Sep 12, 2005
Posts: 30
Thank you for your suggection, i got it.
Megha Jain
Ranch Hand

Joined: Sep 12, 2005
Posts: 30
Thank you for your suggection, i got it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem on interface
 
Similar Threads
Help with Value Returning Methods
Whyis the error "Exception in thread "main" java.lang.NoSuchMethodError: main"?
Triangle Problem
Beginner Question: Class Capabilities
whats wrong with my code ?