-5

I have an assignment for my computer science class. I have put everything I need in the code, however I can not figure out how to debug it. I am getting 8 errors, however I need these fixed without taking out important code.

import java.awt.*;
import java.io;
import java.lang.Object.*;
import java.util.Scanner;
import java.lang.Math;

public class Circle()
{

public double xCor;
public double yCor;
public double r;
public circle()
{
xCor = 5.0;
yCor = 5.0;
r = Math.PI;
}

public static void main(String args[])
{

public circle(double a, double b, double c)
{

xCor = a;
yCor = b;
r = c;
}

Circle c1 = new Circle();
Circle c2 = new Circle(1.0, 3.0, 2.0);

c1.x = c1.x + c1.R;
c2.R = 1.0 + c2.y;
c1.R = 2.0;

double n;
n = c1.getArea();
System.out.println(“Circle1’s area is “ + n);

System.out.println(“The area of a circle with radius 3 is:”);
calcAreaForThisRadius(3);
}

public double getArea() 
{
return Math.PI * r * r;
}

public static calcAreaForThisRadius(double radius)
{
return radius * radius * Math.PI;

}
}
}
  • Welcome to StackOverflow. Please visit the [help] and read [ask]. StackOverflow is not a site where you can post homework and expect others to do your work for you. The whole point is fo r you to learn how to solve problems such as this. – Jim Garrison Feb 06 '16 at 00:49
  • Or download eclipse and fix with ctrl + 1. That should resolve most of the problems you have here. – Jean-François Savard Feb 06 '16 at 00:51
  • That is bad advice. 1) Auto-correction is liable to correct the problem the wrong way. 2) It provides little insight to the learner as to what he / she did wrong in the first place. – Stephen C Feb 06 '16 at 00:54
  • 1
    Just a small hint: constructor-definitions inside method-bodies isn't valid. –  Feb 06 '16 at 00:55
  • Be specific, tell exactly what errors you have. Paste in relevant error messages. – nolexa Feb 06 '16 at 00:57
  • Check your letter case in your constructor declarations too. – Voicu Feb 06 '16 at 01:04

2 Answers2

1

Here is my advice (to a new Java programmer) as to how to approach this problem.

  1. Read the compilation error messages. Try to understand what they are saying. This doesn't always work because the compiler errors will be based on what the compiler (writer) thinks you were probably trying to do, and it is easy to confuse it.

  2. Read your own code carefully. Silly typos often make a huge difference.

  3. If the compilation error doesn't seem to make any sense, then go back to your lecture handouts / text books / online tutorials and compare the syntax you are using in your code with the syntax in the examples1.

    For instance, you code says:

        public class Circle()  {
    

    If you look at examples of classes, you should notice something different between your code and the examples. The round brackets should not be there.

  4. Correcting errors that you can understand will often cause some of the other (inexplicable) errors to disappear. Sometime, your bad syntax can confuse the compiler sufficiently that it produces phantom errors elsewhere in the code.


A couple more hints:

  1. In Java it is important that you get the capitalization right. Circle and circle are different identifiers.

  2. In Java, every class should be in a separate file. (Should be ...)

  3. In Java, constructs must be properly nested, and every { should have a matching }.


1 - For a non-beginner programmer (e.g. a 3rd year CS student) I would advise differently. For them I would advise consulting the syntax specification for the programming language. Learn to "read" BNF / EBNF / syntax diagrams ... or whatever notation is used.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

As others have said, this is not the kind of questions StackOverflow expects, but here is a hint.

Retry your code after moving these two lines

public static void main(String args[])
{

to the line immediately before

Circle c1 = new Circle();

The moral of the story is: you should clarify what to enclose in { and }. As your code stands, you are putting the constructor of the class and its other methods inside the main() method. Java (any version, not only 8) does not allow the nesting of methods within methods.

PNS
  • 19,295
  • 32
  • 96
  • 143