Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2006

Tip: Looking for answers? Try searching our database.

Help with classes

Thread view: 
machoextreme - 15 Jan 2006 17:19 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
(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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.