Results 1 to 6 of 6

Thread: Getting errors in this program...any ideas?

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    44

    Getting errors in this program...any ideas?

    This is my first Java program. I am teaching myself Java, and I have a pretty good book I am using, but I am getting 5 compile errors, even though I am doing this by the book. Here is the code:

    Code:
    import javax.swing.JOptionPane;
    
    
    // Create "Circle" class
    public class Circle
    {
    	// Create default constructor that sets value of radius to 1
    	public DefaultRadius()
    	{
    		defultRadius = 1;	
    	}
    	
    	// Create main method
    	public static void main(String[] args)
    	{
    		// Declare variables
    		Radius defaultRadius = new DefaultRadius();			
    		double radius = setRadius();				
    			
    		// Output total to dialog box
    		JOptionPane.showMessageDialog(null, "A circle with a radius of " + newRadius + " has a diameter of " + diameter + " and an area of " + area);
    		
    		System.exit(0);
    
    	}
    	
    	// Create method to set the value of the radius
    	pubic static void setRadius()
    	{
    		// Declare variables
    		double radiusString, radius;
    		
    		// Assign values to variables
    		radiusString = JOptionPane.showInputDialog(null, 
    		    "Enter the value of the radius of the circle.", "Enter Radius",
    			 JOptionPane.INFORMATION_MESSAGE);
    		
    		// Convert inputs to integers and assign values to variables
    		radius =Double.parseDouble(radiusString);	
    		
    		System.exit(0); 
    		
    	}
    	
    	// Create method to return the value of the radius
    	pubic static double returnRadius(double radius)
    	{
    			
    		double newRadius;		
    		newRadius = radius;
    		return newRadius;
    			
    	}
    	
    	// Create method to compute and return the circle's diameter
    	pubic static double computeDiameter(double newRadius)
    	{			
    		double diameter;		
    		diameter = newRadius * 2;
    		return diameter;			
    	}
    	
    		// Create method to compute and return the circle's area
    	pubic static double computeDiameter(double newRadius)
    	{			
    		double area;		
    		area = (newRadius * newRadius) *3.14;
    		return area;			
    	}
    
    
    }

    Here are the errors:

    invalid method declaration; return type required
    public DefaultRadius()
    ^
    <identifier> expected
    pubic static void setRadius()
    ^
    <identifier> expected
    pubic static double returnRadius(double radius)
    ^
    <identifier> expected
    pubic static double computeDiameter(double newRadius)
    ^
    <identifier> expected
    pubic static double computeDiameter(double newRadius)
    ^

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Getting errors in this program...any ideas?

    Your constructor must have the same name as your class, so if you're using this in your Circle class, your constructor must be called "Circle" and not "DefaultRadius."

    I assume the rest of the errors are because you're spelling "pubic" instead of "public" for your methods.

    *edit* Looking at your code, it seems you don't quite grasp what the book is teaching you. You're attempting to access variables "newRadius," "diameter" and "area" which do not exist in your entire project.

    What I assume you really want is to call the functions that return the area and diameter based on the radius. For example, the line

    Code:
    JOptionPane.showMessageDialog(null, "A circle with a radius of " + newRadius + " has a diameter of " + diameter + " and an area of " + area);
    Should actually be closer to this. It can be a little different depending on how you fix your code.

    Code:
    JOptionPane.showMessageDialog(null, "A circle with a radius of " + radius + " has a diameter of " + computeDiameter(radius) + " and an area of " + computeArea(radius));

    You should also note, that when you're creating a method, the parameters are sent into variables that only have scope within the current method.

    So for example:

    Code:
    public static double returnRadius(double radius)
    The "radius" variable only exists within that method. Even if you have it declared elsewhere, it will not reference those unless you explicitly specify what scope you're pulling the variable from.
    Last edited by kfcSmitty; Mar 8th, 2010 at 10:03 PM.

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    44

    Re: Getting errors in this program...any ideas?

    I made some of the changes you suggested. The real problem, I guess, is that I do not understand the constructor part. I changed it to Circle(), and I am still getting an error. Am I not declaring this the correct way, or in the correct part of the program? Java thinks it is a method, rather than a constructor.

    Code:
    import javax.swing.JOptionPane;
    public class TestProgram
    {
    
    // Create "Circle" class
    public class circle
    {
    	// Create default constructor that sets value of radius to 1
    	public Circle()
    	{
    		defultRadius = 1;	
    	}
    	
    	// Create main method
    	public static void main(String[] args)
    	{
    		// Declare variables
    		Radius defaultRadius = new DefaultRadius();			
    		double radius = setRadius();				
    			
    		// Output total to dialog box
    		JOptionPane.showMessageDialog(null, "A circle with a radius of " + newRadius + " has a diameter of " + diameter + " and an area of " + area);
    		
    		System.exit(0);
    
    	}
    	
    	// Create method to set the value of the radius
    	public static void setRadius()
    	{
    		// Declare variables
    		double radiusString, radius;
    		
    		// Assign values to variables
    		radiusString = JOptionPane.showInputDialog(null, 
    		    "Enter the value of the radius of the circle.", "Enter Radius",
    			 JOptionPane.INFORMATION_MESSAGE);
    		
    		// Convert inputs to integers and assign values to variables
    		radius =Double.parseDouble(radiusString);	
    		
    		System.exit(0); 
    		
    	}
    	
    	// Create method to return the value of the radius
    	public static double returnRadius(double radius)
    	{
    			
    		double newRadius;		
    		newRadius = radius;
    		return newRadius;
    			
    	}
    	
    	// Create method to compute and return the circle's diameter
    	public static double computeDiameter(double newRadius)
    	{			
    		double diameter;		
    		diameter = newRadius * 2;
    		return diameter;			
    	}
    	
    		// Create method to compute and return the circle's area
    	public static double computeDiameter(double newRadius)
    	{			
    		double area;		
    		area = (newRadius * newRadius) *3.14;
    		return area;			
    	}
    
    
    }
    }

    ERROR:

    invalid method declaration; return type required
    public Circle()
    ^
    1 error

  4. #4

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    44

    Re: Getting errors in this program...any ideas?

    The more corrections I make, the more errors I get. I think I need to start this over from scratch. This should not be that difficult, but it is for some reason. Here is what I am supposed to accomplish with this program according to the book I am using:

    Create a class named Circle with a field for the radius. Be sure to include the following

    • a default constructor that sets the radius to 1

    • a method to set the value of the radius

    • a method to return the value of the radius

    • a method to compute and return the circle's diameter

    • a method to compute and return the circle's area

    Recall that the diameter of a circle is twice its radius and that the area of a circle is 3.14 times the square of its radius.

    The Circle with radius 10.0 has diameter 20.0 and area 314.0.
    The Circle with radius 1.0 has diameter 2.0 and area 3.14.

  5. #5
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Getting errors in this program...any ideas?

    The error in your previous code was because your constructor name did not match your class name. One had an uppercase C and one had a lowercase c.

    Your last bit of code looks pretty close to me. You just need to learn how to use your functions. You never call them.

    When you return a value, it is returning it to where that function is called. So, here is how one of your functions should be called.

    Code:
    //you would place this piece of code in a method that is calling the function. 
    //You would want to throw it in your main.
    //set your radius to 5.
    int radius = 5;
    //now call your function. This runs the function "computeDiameter"
    //and sets the variable diameter to whatever is returned from your function.
    double diameter = computeDiameter(radius);
    
    //this is just your function that I copied from above. 
    //It would not go in the main, it would go exactly where you have it now.
    public static double computeDiameter(double newRadius)
    {			
    		double area;		
    		area = (newRadius * newRadius) *3.14;
    		return area;			
    }
    Revise your code and post it again. If you're still stuck I can attempt to give you more help.

  6. #6
    Fanatic Member kregg's Avatar
    Join Date
    Feb 2006
    Location
    UK
    Posts
    524

    Re: Getting errors in this program...any ideas?

    Quote Originally Posted by CarlMartin10 View Post
    The more corrections I make, the more errors I get. I think I need to start this over from scratch.
    I don't think so. From the look of things, the only thing that is currently holding you back is the spelling mistakes of your code.

    Now that isn't the worst thing in the world - at least you seem to be understanding some concepts, which is the crucial part. As long as you realise that Java is heavily reliant on Object Oriented Programming approach i.e. every class is supposed to represent an object of some kind - the variables are the properties of that object, and the methods are the ways you can interact with an object.

    To give you a small example, let's look at a ball as a possible object we can put into Java. A ball has a size and colour (for simplicity sakes), and in our example, we can interact with this object by picking it up, and dropping it.

    In this example, the Java code could possibly look like this (there is strictly no right answer, as it depends completely on the situation):

    java Code:
    1. public class Ball
    2. {
    3.     private int size;
    4.     private String colour;
    5.  
    6.     public Ball()
    7.     {
    8.         size = 1;
    9.         colour = "red";
    10.     }
    11.  
    12.     public Ball(int size, String colour)
    13.     {
    14.         this.size = size;
    15.         this.colour = colour;
    16.     }
    17.  
    18.     public void pickUpBall()
    19.     {
    20.         //some code here that represents this Ball object being picked up
    21.     }
    22.  
    23.     public void dropBall()
    24.     {
    25.         //some code here that represents this Ball object being picked up
    26.     }
    27. }

    That is how I'd represent it in Java, at the very least. Also, I have two constructors; the whole idea is that if this Ball object is called by another class, they can either mention the ball size and colour (using the constructor Ball(int size, String colour)), or I can set a default size (using the constructor Ball()). I can force whoever calls my object in their code to specify a size and colour by getting rid of the Ball() constructor or just force them to take the default one by deleting the Ball(int size, String colour) constructor.

    I at least hope you understand this stuff, as for me, it really helps if I understand the core concepts behind the code. If you already knew this and I came across patronizing, then I'm very sorry for that. If you didn't know, and you want to try learning more, then try making a simple Java class that runs via the console and interacts with the Ball class code I've given (you will obviously need to add more to the class to make it interactive).

    Good luck with learning Java though! It's nice to see someone actually actively learning a language by themselves.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width