Results 1 to 12 of 12
  1. #1
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Thumbs up difficulty understanding writing classes

    Hey again guys, I got stuck again and was wondering if people wouldn't mind helping out this guy again. This time I got stuck learning how to create classes and then using them in different methods. So here's what the assignment is about:

    Design and implement a class called Sphere that contains instance data that represents the sphere's diameter.
    1. Define the Sphere constructor to and initialize the diameter, and include getter and setter methods for the diameter.
    2. Include methods that calculate and return the volume and surface area of the sphere
    3. Include a toString method that returns a one-line description of the sphere.
    4. Create a driver class called MultiSphere, whose main method instantiates and updates several Sphere objects.

    Java Code:
    // Sphere project for pp0401. *Works together with MultiSphere project.
    
    public class Sphere {
    	{
    		
    		private double diameter;
    		
    	public Sphere (double width)	
    	{
    		diameter = width;
    	}
    	
    	public double radius()
    	{
    		radius = diameter / 2;
    		
    		r = radius;
    	}
    	
    	public double volume()
    	{
    		volume = 4 * Math.PI * Math.pow(r, 3);
    	}
    	
    	public double SA()
    	{
    		SA = 4 * Math.PI * Math.pow(r, 2);
    	}
    	
    	public String toString()
    	{
    		return diameter + "/t" + radius + "/t" + volume "/t" + SA;
    	}
    	}
    }
    Java Code:
    // MultiSphere project for pp0401. *Works together with Sphere project.
    public class MultiSphere {
    
    	public static void main (String[] args)
    	{
    		
    		Sphere sphere1 = new Sphere (54);
    		Sphere sphere2 = new Sphere (24);
    		Sphere sphere3 = new Sphere (1);
    		
    		System.out.println (sphere1);
    		System.out.println (sphere2);
    		System.out.println (sphere3);
    	}
    }
    WHEN I RUN MultiSphere I GET THE ERROR:

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The constructor Sphere(int) is undefined
    The constructor Sphere(int) is undefined
    The constructor Sphere(int) is undefined

    WHEN I RUN Sphere I GET THE ERROR:

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The constructor Sphere(int) is undefined
    The constructor Sphere(int) is undefined
    The constructor Sphere(int) is undefined

    at MultiSphere.main(MultiSphere.java:7)

    What am I doing wrong? Please tell me in details, but in English... Speaking in Java still confuses me to the point where I say "huh?" and would point back the the question like you might not have understood my question. :P

    Thanks in advance guys~! I am just waiting until I get to the point I am fluent enough in Java to give back to the community, but it's not happening as quickly as possible... please be understanding.
    Last edited by elecleoalune; 04-14-2011 at 07:11 AM. Reason: adding code tags

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your code is chock full of errors telling me a couple of things:
    1) you're not compiling often enough
    2) you're trying to make things up that just isn't Java.

    I recommend that you scrap this code and start over. When you do this, remember to:
    1) compile early and often, probably after adding each new line of code.
    2) If you encounter a compilation error, don't add any more code until you fix the current error(s), lest you continue to add bad code to bad code.
    3) use your texts and tutorials to guide you as you can't guess in Java.
    4) you've posted several times before on this site, so it's time you started using code tags when posting code. My signature below can help you out with this.

    Good luck, and come on back if you're still stuck.

  3. #3
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    thanks buddy~ I'll check out your code tags post!

  4. #4
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    okay, although i get compilation error, I don't exactly know how to compile it correctly.
    I guess I am having such a difficult time with this is because the textbook examples are giving me limited examples to understand it from.
    does anyone know any example codes I could take a look at?
    I've been trying to find it through searching "class" but I get hundreds of search results...
    if someone is willing to even give me a keyword that i could search for, it would be really helpful!

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Fubar's comments aer very valid and will help you learn and write better code in the future. Your immediate problem is explained in the error message. When calling methods and constructors the number, the order and the type of parameters must match. In your code you do not have a constructor that accepts a single int.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It's even more basic that that. You've got an extra set of braces in your code:
    Java Code:
    public class Sphere {
    	{ // !!! this guy here and its pair at the end
    That will mess up everything (and that's just error number one).

    Again, get rid of that code as it's borked and use class examples that are in the Java tutorials as the basis for your class skeleton.

  7. #7
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    I'll keep your tips in mind.
    I guess I don't quite understand this whole chapter very well so I guess I'll reread it and then work on this project after trying to understand it fully.

    The weird thing is that I am getting error when I am following the textbook's examples.
    Maybe I shouldn't learn from this text book and learn straight from Java Tutorial's web site?
    What do you guys think?

  8. #8
    victor8247 is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    There is no getter and setter methods for the diameter. At least, no standard getter and setter methods.

  9. #9
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    oh is that what the problem is??
    darn it! I have to figure out how to do that.
    Any suggestion or tips on setting getter and setter method?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You need to read the tutorials. Please start here: Tutorials

  11. #11
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    yup, I am reading it every time I have time available. It's just difficult with my time schedule currently. I will read the turorial and work on it again and then tell you guys what the result is.

  12. #12
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    Hi again guys, I just wanted to let you guys know that after putting the time in whenever possible, I have finished reading the "Object-Oriented Programming" section of Java as you guys suggested. I thank you guys again for showing me the way. I just wanted to post my codes for futures noobs like me, if they were to need it for any reason.

    Java Code:
    // Sphere project for pp0401. *Works together with MultiSphere project.
    import java.text.DecimalFormat;
    
    public class Sphere {
    double diameter = 0, radius, volume, SA, r;
    
    void changeDiameter (double newValue) {
    	diameter = newValue;
    
    radius = diameter / 2;
    r = radius;
    volume = 4* Math.PI * Math.pow(r, 3);
    SA = 4 * Math.PI * Math.pow(r, 2);
    
    
    }
    
    void printStates() {
    	DecimalFormat fmt = new DecimalFormat ("0.###");
    	System.out.println ("Diamter: " + fmt.format(diameter) + " Volume: " + fmt.format(volume) + " SA: " + fmt.format(SA));
    }
    }
    Java Code:
    // MultiSphere project for pp0401. *Works together with Sphere project.
    public class MultiSphere {
    
    	public static void main (String[] args)
    	{
    		
    		// Create 2 Sphere objects
    		Sphere sphere1 = new Sphere ();
    		Sphere sphere2 = new Sphere ();
    		
    		// Invoke methods on those objects
    		sphere1.changeDiameter(2);
    		sphere1.printStates();
    		
    		sphere2.changeDiameter(10);
    		sphere2.printStates();
    		
    	}
    }

Similar Threads

  1. Writing classes???
    By Bgreen7887 in forum New To Java
    Replies: 1
    Last Post: 11-04-2010, 08:06 AM
  2. Applications in different difficulty
    By bubbless in forum New To Java
    Replies: 2
    Last Post: 03-11-2009, 12:31 AM
  3. Writing importable classes
    By Katsu in forum New To Java
    Replies: 6
    Last Post: 03-08-2009, 09:10 PM
  4. difficulty
    By Daniela_v in forum New To Java
    Replies: 2
    Last Post: 03-04-2009, 05:36 PM
  5. Writing classes in graphics
    By CyberFrog in forum Java Applets
    Replies: 2
    Last Post: 04-05-2008, 05:37 PM

Posting Permissions

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