Error need help and correction

Exception in thread'main; java.lang.NoSuchMethodError: main

Page 1 of 1

12 Replies - 1281 Views - Last Post: 18 March 2009 - 09:08 AM Rate Topic: -----

#1 calsonstyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 22-February 09

Error need help and correction

Posted 16 March 2009 - 11:00 PM

public class Circle
{
private double radius;
public Circle() // Default constructor
{
radius = 1.0;
}
// Construct circle with specified radius
public Circle(double r)
{
radius = r;
}
// Implement the findArea method
public double findArea()
{
return radius*radius*Math.PI;
}

class Cylinder extends Circle // Cylinder.java
{
private double length;
public Cylinder() // Default constructor
{
super(); // Invoke the default superclass constructor
length = 1.6;
}
// Construct a cylinder with specified radius, and length
public Cylinder(double radius, double len)
{
super(radius);
length = len;
}
// Find cylinder volume
public double findVolume()
{
return super.findArea()*length; // Use the superclass method
}

// Find cylinder length
public double getlength()
{
return length; // Use the superclass method
}

// Find cylinder radius
public double getradius()
{
return radius; // Use the superclass method
}
public class TestCylinder
{
public void main(String[] args)
{
//create a Cylinder object and display its properties
Cylinder myCylinder = new Cylinder(5.0, 2.0);
System.out.println("The length is " + myCylinder.getlength());
System.out.println("The radius is " + myCylinder.getradius());
System.out.println("The volume of the cylinder is " +
myCylinder.findVolume());
System.out.println("The area of the circle is " +
myCylinder.findArea());
}
}
}
} 


I can compile the code but cannot run, anyone can help me to correct the code?

Is This A Good Question/Topic? 0
  • +

Replies To: Error need help and correction

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Error need help and correction

Posted 16 March 2009 - 11:03 PM

Where your main method is, you have a slight error in that header.

This is what it should look like -- public static void main(String[] args)

This is what you have -- public void main(String[] args)

That static has to be there, or else the program can't run since it can't instantiate an object of the class before that program is run. In other words, the OS has to be able to access the program independently, which is what the static enables it to do.

This post has been edited by Locke: 16 March 2009 - 11:04 PM

Was This Post Helpful? 0
  • +
  • -

#3 calsonstyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 22-February 09

Re: Error need help and correction

Posted 16 March 2009 - 11:13 PM

View PostLocke, on 17 Mar, 2009 - 01:03 PM, said:

Where your main method is, you have a slight error in that header.

This is what it should look like -- public static void main(String[] args)

This is what you have -- public void main(String[] args)

That static has to be there, or else the program can't run since it can't instantiate an object of the class before that program is run. In other words, the OS has to be able to access the program independently, which is what the static enables it to do.


If i add the static void main, this is the error i get

Posted Image
Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Error need help and correction

Posted 16 March 2009 - 11:24 PM

You're missing a brace -- } -- to close the Cylinder class right before you declare the public class TestCylinder
Was This Post Helpful? 0
  • +
  • -

#5 calsonstyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 22-February 09

Re: Error need help and correction

Posted 16 March 2009 - 11:32 PM

I have edited the code
public class Circle
{
private double radius;
public Circle() // Default constructor
{
radius = 1.0;
}
// Construct circle with specified radius
public Circle(double r)
{
radius = r;
}
// Implement the findArea method
public double findArea()
{
return radius*radius*Math.PI;
}

class Cylinder extends Circle // Cylinder.java
{
private double length;
public Cylinder() // Default constructor
{
super(); // Invoke the default superclass constructor
length = 1.6;
}
// Construct a cylinder with specified radius, and length
public Cylinder(double radius, double len)
{
super(radius);
length = len;
}
// Find cylinder volume
public double findVolume()
{
return super.findArea()*length; // Use the superclass method
}

// Find cylinder length
public double getlength()
{
return length; // Use the superclass method
}

// Find cylinder radius
public double getradius()
{
return radius; // Use the superclass method
}
public class TestCylinder {
{
public static void main(String[] args);
{
//create a Cylinder object and display its properties
Cylinder myCylinder = new Cylinder(5.0, 2.0);

System.out.println("The length is " + myCylinder.getlength());
System.out.println("The radius is " + myCylinder.getradius());

System.out.println("The volume of the cylinder is " +
myCylinder.findVolume());

System.out.println("The area of the circle is " +
myCylinder.findArea());
}
}
}
}
}


And this is the last error

circle.java:52: illegal start of expression
public static void main(String[] args);
Was This Post Helpful? 0
  • +
  • -

#6 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Error need help and correction

Posted 16 March 2009 - 11:33 PM

Take out the semicolon on the line mentioned in the error.

That should do it. :)

Edit: Upon further examination...I still don't see a closing brace - } - closing off the Cylinder class. That should cause trouble when you try to run the program.

This post has been edited by Locke: 16 March 2009 - 11:36 PM

Was This Post Helpful? 0
  • +
  • -

#7 calsonstyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 22-February 09

Re: Error need help and correction

Posted 16 March 2009 - 11:37 PM

I'm quite blur and not really know which one . Can you remove from the code and rewrite it on [code] to show me? thanks
Was This Post Helpful? 0
  • +
  • -

#8 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Error need help and correction

Posted 16 March 2009 - 11:40 PM

// previous Cylinder class code

    public double getRadius()
    {
        return radius;
    }
} // CLOSING BRACE HERE, to close the Cylinder class

public class TestCylinder
{
    public static void main(String[] args) // NO semicolon
    {
        //...
    }
}


That should do it. :)
Was This Post Helpful? 0
  • +
  • -

#9 calsonstyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 22-February 09

Re: Error need help and correction

Posted 16 March 2009 - 11:48 PM

Sorry but another error still occur

public class Circle
{
private double radius;
public Circle() // Default constructor
{
radius = 1.0;
}
// Construct circle with specified radius
public Circle(double r)
{
radius = r;
}
// Implement the findArea method
public double findArea()
{
return radius*radius*Math.PI;
}

class Cylinder extends Circle // Cylinder.java
{
private double length;
public Cylinder() // Default constructor
{
super(); // Invoke the default superclass constructor
length = 1.6;
}
// Construct a cylinder with specified radius, and length
public Cylinder(double radius, double len)
{
super(radius);
length = len;
}
// Find cylinder volume
public double findVolume()
{
return super.findArea()*length; // Use the superclass method
}

// Find cylinder length
public double getlength()
{
return length; // Use the superclass method
}

// Find cylinder radius
public double getradius()
{
return radius; // Use the superclass method
}
}
public class TestCylinder {
{
public static void main(String[] args)
{
//create a Cylinder object and display its properties
Cylinder myCylinder = new Cylinder(5.0, 2.0);

System.out.println("The length is " + myCylinder.getlength());
System.out.println("The radius is " + myCylinder.getradius());

System.out.println("The volume of the cylinder is " +
myCylinder.findVolume());

System.out.println("The area of the circle is " +
myCylinder.findArea());
}
}
}
}
}


The error

Posted Image

Can you try to modify and use on your system to find out the solution?
Was This Post Helpful? 0
  • +
  • -

#10 calsonstyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 22-February 09

Re: Error need help and correction

Posted 17 March 2009 - 10:33 AM

anyone can help?
Was This Post Helpful? 0
  • +
  • -

#11 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Error need help and correction

Posted 17 March 2009 - 10:49 AM

You still have a few brace problems. This is where proper indentation can help.

You never closed off the Circle class, so that is screwing it up. You need to close that off before you start the Cylinder class.

You have an extra opening brace when you start the TestCylinder class, before the main method, there should only be 1, not 2.

You also have 2 extra braces at the end of the program that are serving no purpose.

And on a brace-free note, the instance variableradius is private, so you can't access it with just return radius;. Either make the variable protected, or make an accessor method to gain access to it.

This post has been edited by Locke: 17 March 2009 - 10:52 AM

Was This Post Helpful? 0
  • +
  • -

#12 calsonstyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 22-February 09

Re: Error need help and correction

Posted 17 March 2009 - 10:10 PM

After all the implementation this was the final code

import java.lang.Math.*;

public class Circle
	{
		public double radius;
		
		public Circle() // Default constructor
	{
		radius = 1.0;
	}
	
// Construct circle with specified radius
public Circle(double r)
	{
		radius = r;
	}
	
// Implement the findArea method
public double findArea()
	{
		return radius*radius*3.14;
	}
}

class Cylinder extends Circle 
	{
		public double length;
		public Cylinder() // Default constructor
		{
			super(); // Invoke the default superclass constructor
			length = 1.6;
		}
		
// Construct a cylinder with specified radius, and length
		public Cylinder(double radius, double len)
		{
			super(radius);
			length = len;
		}
		
// Find cylinder volume
		public double findVolume()
		{
			return super.findArea()*length; // Use the superclass method
		}

// Find cylinder length
		public double getLength()
		{
			return length; 
		}

// Find cylinder radius
		public double getRadius()
		{
			return radius; 
		}
	}

class TestCylinder extends Cylinder{

public static void main(String[] args){

	
//create a Cylinder object and display its properties
Cylinder myCylinder = new Cylinder(5.0, 2.0);
System.out.println("The length is " + myCylinder.getLength());
System.out.println("The radius is " + myCylinder.getRadius());
System.out.println("The volume of the cylinder is " + myCylinder.findVolume());
System.out.println("The area of the circle is " + myCylinder.findArea());
	}
	}



But i got the error of "Exception in thread'main; java.lang.NoSuchMethodError: main"

Please review my code and help me to correct it ^^

If possible, kindly edit the code and post it with your reply. It can be used by me as a reference
Was This Post Helpful? 0
  • +
  • -

#13 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Error need help and correction

Posted 18 March 2009 - 09:08 AM

Did you name the file TestCylinder.java? If you did, then I don't see why it's messing up.

Edit: Ah, found it. Remove the public from the declaration of the Circle class. -- class Cylinder

You still, however, need to have the file saved as TestCylinder.java

This post has been edited by Locke: 18 March 2009 - 09:09 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1