PDA

Click to See Complete Forum and Search --> : Getting errors in this program...any ideas?


CarlMartin10
Mar 8th, 2010, 07:47 PM
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:


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)
^

kfcSmitty
Mar 8th, 2010, 08:51 PM
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


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.


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:


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.

CarlMartin10
Mar 9th, 2010, 06:50 PM
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.

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

CarlMartin10
Mar 9th, 2010, 08:32 PM
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.

kfcSmitty
Mar 9th, 2010, 11:02 PM
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.


//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.

kregg
Mar 11th, 2010, 09:34 AM
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):


public class Ball
{
private int size;
private String colour;

public Ball()
{
size = 1;
colour = "red";
}

public Ball(int size, String colour)
{
this.size = size;
this.colour = colour;
}

public void pickUpBall()
{
//some code here that represents this Ball object being picked up
}

public void dropBall()
{
//some code here that represents this Ball object being picked up
}
}


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. :D