-5

I don't know what pot I was smoking when I posted the original, but I came to my senses and came up with this. I am not an experienced coder, but the entire post was made mostly as a question that for the most part has been answered. I now know classes can't have code directly in them, and more so about the core structure.

class Shape {

    public static void ShapeAttemptTwo()
    {
        class Circle extends Shape
            {
                public static CircleAttemptTwo()
                {
                int pi = 3.14;
                int r=4;
                }
            }
        class Rectangle extends Shape
        {
                public static Rectangle()
                {
                int l = 14;
                int b = 10;
                int z = l*b;
                }
        }
        class Square extends Shape
        {
            public static Square()
            {
            int a = 11;
            System.out.println(a * a);
            }
        }

// Java code 7, invalid method declare, return type required. (public static //CircleAttemptTwo()) // I'm lost on this one, could I have some help? // and reached end of file while parsing }, confusing to me.

/EDIT THANK YOU VERY MUCH. The constructive crit. really helped, as I ended up with a lot of knowledge and my final code was

class Shape {

public static void ShapeAttemptTwo()
{
    class Circle extends Shape
        {
            public static CircleAttemptTwo()
            {
            int pi = 3.14;
            int r=4;
            }
        }
    class Rectangle extends Shape
    {
            public static Rectangle()
            {
            int l = 14;
            int b = 10;
            int z = l*b;
            }
    }
    class Square extends Shape
    {
        public static Square()
        {
        int a = 11;
        System.out.println(a * a);
        }
    }
tshepang
  • 12,111
  • 21
  • 91
  • 136
Dont
  • 35
  • 7

4 Answers4

1

You're doing several things wrong:

1) Your "static main()" belongs inside a class

2) You can only have one "public class" per module.

SUGGESTED CHANGE:

public abstract class Shape 
{

    public static void main(String[] args)  {
      Shape rectangle = new Rectangle (14, 10);
      System.println ("rectangle's area=" + rectangle.getArea ());
      ...
    }

}

class Circle extends Shape  {
   ...
}

class Rectangle extends Shape  {

   int l;
   int b;

   public Rectangle (int l, int b) {
     this.l = l;
     this.b = b;
   }

   public int getArea () {
     return l * b;
   }
}
...
FoggyDay
  • 11,962
  • 4
  • 34
  • 48
0

Are you joking? You're missing basic knowledges of Java:

1) Method yea doesn't have a return type.

2) There's a code directly inside the class Rectangle, not in method.

3) There's a code directly inside the class Square, not in method.

4) You're using very strange and weird code-style and formatting. Update: Formatting has been fixed.

2nd Update:

1) Code has been changed and it's formatted wrong again.

2) There are three static methods without defined return type.

Here are some more informations related on the topic.

tzima
  • 1,285
  • 1
  • 10
  • 23
0

Aside from the errors Tomas pointed out:

5) You forgot the word class in the Square declaration.

6) You cannot declare a public class directly inside a method. Java does allow you to declare classes, called "local classes" inside methods. However, I'm not sure if that's what you really want to do; if you do, you can only use Circle inside your main method, so you're not really creating a hierarchy. Anyway, when you declare a local class, it can't have public, protected, or private keywords on it, so that explains the first error message you're seeing.

EDIT: Based on the second post: Every method has to have a return type; if you don't actually want the method to return anything, the return type should be void. Thus:

public static void CircleAttemptTwo() 
{
    //int pi = 3.14;  should be
    double pi = 3.14;
    int r=4;
}

However, constructors don't need a return type, but they can't be static. So public Square() and public Rectangle() are OK. CircleAttemptTwo doesn't match the class name, so it isn't a constructor.

"reached end of file" usually means you're missing a }, as you did here.

And 3.14 isn't an integer.

ajb
  • 31,309
  • 3
  • 58
  • 84
  • Isn't the 'static' tag a type? Thanks, I realized the double }} issue. This site is amazing, thank you all for the help. – Dont Mar 29 '14 at 00:51
  • No, `static` is not a type. Types describe what kind of data you're working with, e.g. `int` is an integer, `double` is a double-precision float, `String` is--I'll bet you can guess. `static` is a modifier (like `public`, `private`, `final`) that conveys some information about how a class or method or variable is used, but it isn't a type. – ajb Mar 29 '14 at 00:54
-1

You need to change

int pi = 3.14;

to

double pi = 3.14; 

Also, static method is in wrong place.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83