-1

What's the mistake in this algorith? How can I resolve it? Eclipse tell me a mistake in the main on

area c1 = new area();

import java.awt.Rectangle;

public class ClasseRettangolo {

    public class area {
        Rectangle box = new Rectangle(5 , 10, 15, 20);
        public double surface() {
            return ( box.getHeight() * box.getWidth());
        }
    }
    public class perimeter {
        Rectangle box = new Rectangle(5 , 10, 15, 20);
        public double outline() {
            return ((box.getHeight() + box.getWidth())* 2);
        }
    }
    public static void main(String[] args){
        area c1 = new area();
        perimeter c2 = new perimeter();

        System.out.println("The area of the Rectangle is: " + c1.surface());
        System.out.println("The perimeter of the Rectangle is: " + c2.outline());
    }
}
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
Vaiz
  • 1

4 Answers4

3

Either make the area and the perimeter classes static

public static class area { .. }

public static class perimeter { .. }

Or you will have to instantiate the ClasseRettangolo class and then the nested ones.

new ClasseRettangolo().new area();
new ClasseRettangolo().new perimeter();

Not related to the problem:

  • Your class names don't meet the Java naming convertions : They should start with a capital letter.
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
2

The problem is that you're using non-static inner classes. It seems like you're a newbie, so, for the time being, obey this simple rule: each class should be defined as a top-level class in its own .java file. Once you understand how simple classes work, then read the tutorial about nested classes.

Also, respect the Java naming conventions: classes start with an upper-case letter.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

make area class and perimeter class as static class like below

import java.awt.Rectangle;

public class ClasseRettangolo {

public static class area {
    Rectangle box = new Rectangle(5 , 10, 15, 20);
    public double surface() {
        return ( box.getHeight() * box.getWidth());
    }
}
public static class perimeter {
    Rectangle box = new Rectangle(5 , 10, 15, 20);
    public double outline() {
        return ((box.getHeight() + box.getWidth())* 2);
    }
}
public static void main(String[] args){
    area c1 = new area();
    perimeter c2 = new perimeter();

    System.out.println("The area of the Rectangle is: " + c1.surface());
    System.out.println("The perimeter of the Rectangle is: " + c2.outline());
}
}

read more about static inner class from oracle docs

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
0

The classes area and perimeter belong to the class ClasseRettangolo. Because they are not static but inner classes, it is not possible to create instances of them without having an instance of the outer class. And even then the name will not be area, it will be ClasseRettangolo.area.

Sven Painer
  • 194
  • 1
  • 10