• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

Problem on interface

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);

}
}
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Megha Jain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Megha Jain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may find this tutorial link helpful http://java.sun.com/docs/books/tutorial/java/concepts/interface.html
 
Megha Jain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your suggection, i got it.
 
Megha Jain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your suggection, i got it.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic