Simple question regarding objects.
Hey guys. I'm having trouble on a beginners java assignment, and not touching Java in two years has not helped at all.
My question is relatively easy and is for beginners;
I am to create a main.java class which I have to define a circle object and instantiate it, then using instance methods to call upon the results from Circle.java class to Main.java class.
How do I call upon the Circle.class object? I'm supposed to ask the user to input data from the main.java class, where the circle.java class uses the input data to calculate results, which then I have to use instance methods to call upon the results.
I thought to call upon an object it would be:
Circle c = new Circle();
However the error I get is:
constructor circle in class Circle cannot be applied to given types;
required: double
found: no arguments
reason: actual and formal argument lists differ in length
Is it wrong because instead of passing no arguments, I was supposed to put (double radius)?
Creating Objects (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
I am using this website to try and figure it out myself, but I get confused because:
my first step is to define the object, which would be the Circle c = new Circle(); yes? in the website they give an example to: Point originOne = new Point(23, 94);
I am then to instantiate it, which creates an instance of the object. However, they give the same example for instantiating which is: Point originOne = new Point(23, 94);
So I'm a bit confused as to what the difference is between defining an object and instantiating it.
EDIT:
I think I got the defining and instantiating the object correctly, I changed the code to:
Circle info;
double radius;
radius = getRadius();
info = new Circle (radius);
which covers the defining and intiantiazing, but how do I output the results as an instance method?
Re: Simple question regarding objects.
Alex, I appreciate you're in need of help, but you've already started 4 threads surrounding this assignment.
Unless you do some core reading of Java, you're going to struggle big time with future work.
If you specify that the circle class constructor takes 1 and only 1 parameter of type double (as has been done in the Circle class), then you need to programmatically construct that object with a specified number, or prompt one from the user, using mechanisms such as the Scanner class.
Re: Simple question regarding objects.
I read the links you gave me, and I'm very sorry in advance for the multiple threads.
I've extensively read the links, but I'm just confused on the instance methods.
objectReference.variableName is the format they provide, so to provide my results as an instance method, wouldn't I do:
system.out.println(" your results are " + Circle.getCircumference());
I get the error:
non-static method getCircumference() cannot be referenced from a static context
Where Circle is the circle.java class and getCircumference is the method from Circle.java
Re: Simple question regarding objects.
Quote:
Originally Posted by
alex067
Is it wrong because instead of passing no arguments, I was supposed to put (double radius)?
Your constructor for the Circle class requires the radius and you did not supply one.
Quote:
Originally Posted by
alex067
So I'm a bit confused as to what the difference is between defining an object and instantiating it.
Nothing really.
Declare a variable:
<type> <name>;
int i;
double d;
MyObject o;
Define a variable:
i = 3;
d = 3.0;
o = new MyObject();
i and d are said to be defined, and o is said to be instantiated, but basically the same thing happens in all 3 cases. A value of <type> is stored in the memory location pointed to by <name>.
Quote:
Originally Posted by
alex067
I think I got the defining and instantiating the object correctly, I changed the code to:
Circle info;
double radius;
radius = getRadius();
info = new Circle (radius);
Yes that would be correct. A shorthand version to that would be:
info = new Circle(getRadius());
Re: Simple question regarding objects.
Quote:
Originally Posted by
alex067
system.out.println(" your results are " + Circle.getCircumference());
I get the error:
non-static method getCircumference() cannot be referenced from a static context
Circle.getCircumference() would only work if getCircumference() was a static method of the Circle class. What you need is an object of that class, like:
Circle myCircle = new Circle(8.0); Then you can say myCircle.getCircumference()
The difference being that if the method is a static method of the class, then any object of the class will use the same (static) method. When it is an instance method, each object has to use it's own version of the method, and the way to access that method is through an object of that class.