I'm relatively new to java, and really new to the concept of classes
and their applications. I managaged to get through a few exercises
(largely based off of examples) and now I'm stuck on the main method of
a new exercise. I feel I don't fully understand how to "apply" methods
from classes. I was hoping someone could enlighten me(maybe
particularly how to use constructors in the main method). Any help
would be greatly appreciated.
Here's my class
class Question2 { //aka Circle
private int radius;
private double circumference;
public Question2 (int radius) {
this.radius = radius;
circumference = 2 * Math.PI * radius;
}
public double getCirc() {
return circumference;
}
}
And here's my main method
import java.util.*;
class Q2Main {
public static void main (String args[]) {
int radius;
Question2 smallCircle, largeCircle;
//compute circumference of a smaller circle
smallCircle = new Question2();
Scanner input = new Scanner(System.in);
System.out.println("Enter the radius of the Smaller Circle");
radius = input.nextInt();
smallCircle.getCirc();
//compute circumference of a larger circle
largeCircle = new Question2();
System.out.println("Enter the radius of the Larger Circle");
radius = input.nextInt();
largeCircle.getCirc();
//Display the difference
System.out.println("Difference in circumference of two circles");
System.out.println("Circumference of smaller circle: " +
smallCircle);
System.out.println("circumference of larger circle: " +
largeCircle);
System.out.println("Difference: " + (largeCircle - smallCircle));
}
}
And here's the errors I get;
cannot find symbol contructor Question2()
cannot find symbol contructor Question2()
Operator cannot be applied to Question2(), Question2()
Allan Bruce - 15 Jan 2006 17:55 GMT
> I'm relatively new to java, and really new to the concept of classes
> and their applications. I managaged to get through a few exercises
[quoted text clipped - 58 lines]
> cannot find symbol contructor Question2()
> Operator cannot be applied to Question2(), Question2()
I dont understand how you can be relatively new to java - suggesting that
you have some knowledge, yet you havent come across classes!?!
Anyway, your problem above is when you make a new instance of your object
Question2. To make a new one you need to use the constructor, which
constructs the objecy for you. In your class definition, you have defined
one constructor which requires an int as a parameter for the radius - i.e.
the following line:
public Questions2(int radius){
however when you come to creating an instance of this object in your main
method, you dont supply any parameters. Instead you need to give a
parameter OR create a constructor which takes no parameters. The first
option would require something along these lines:
smallCircle = new Question2(radius);
but this would have to be placed AFTER you got the radius from the user
otherwise the variable radius would not make sense.
Hope this helps, if not I suggest you get a good book (although I recommend
that anyway).
Allan
IchBin - 15 Jan 2006 18:36 GMT
> I'm relatively new to java, and really new to the concept of classes
> and their applications. I managaged to get through a few exercises
[quoted text clipped - 3 lines]
> particularly how to use constructors in the main method). Any help
> would be greatly appreciated.
[snp code]
In other words..
public static void main (String args[])
{
Question2 smallCircle, largeCircle;
//compute circumference of a smaller circle
Scanner input = new Scanner(System.in);
System.out.println("Enter the radius of the Smaller Circle");
smallCircle = new Question2(input.nextInt());
//compute circumference of a larger circle
System.out.println("Enter the radius of the Larger Circle");
largeCircle = new Question2(input.nextInt());
//Display the difference
System.out.println("Difference in circumference of two circles");
System.out.println("Circumference of smaller circle: "
+ smallCircle.getCirc());
System.out.println("circumference of larger circle: "
+ largeCircle.getCirc() );
System.out.println("Difference: "
+ (largeCircle.getCirc() - smallCircle.getCirc()));
}
Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
Paulus de Boska - 16 Jan 2006 10:50 GMT
Here you'll find a lesson on constructors :
http://javalessons.com/cgi-bin/fun/java-tutorials-main.cgi?ses=ao789
then lookup 24.Construction under Java Fundamentals.
---
Paul Hamaker, SEMM
http://javalessons.com