Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By Junky

Thread: Geometric shapes

  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Geometric shapes

    I need help returning some values in this Geometric shapes class. I have to use extends. here are my three classes that go together. For some reason, I am only getting the color.

    [Java] Shape - Pastebin.com
    [Java] ShapesDriver - Pastebin.com
    [Java] Sphere - Pastebin.com

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

    Default Re: Geometric shapes

    Quote Originally Posted by droidus View Post
    For some reason, I am only getting the color.
    Your description of the problem is very vague.
    The chances of people following those links and wading through all your code is very low. Instead post a SSCCE and ask a specific question.

  3. #3
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: Geometric shapes

    Java Code:
    public class Shape {
    
    	protected String color;
    	protected double surfaceArea;
    	protected double volume;
    	protected double pi = Math.PI;
    	boolean end = false;
    	
    	public Shape(String a) {
    		color = a;
    	}
    
    	public String color(String color) {
    		this.color = color;
    		return this.color;
    	}
    	public double surfaceArea(double surfaceArea) {
    		this.surfaceArea = surfaceArea;
    		return this.surfaceArea;
    	}
    	public double volume(double volume) {
    		this.volume = volume;
    		return this.volume;
    	}
    	
    	// Get the name of the class
    	public String getThisClass() {
    		String thisClassString = this.getClass().toString();
    		String newString = "";
    		char character;
    		boolean start = false;
    		
    		for (int i = 0; i<thisClassString.length(); i++) {
    			character = thisClassString.charAt(i);
    			
    			if(character == ' ') {
    				start = true;
    			} 
    			else if(start == true) {
    				newString = newString + character;
    			}
    			else {
    				// do nothing -- continue the loop
    			}
    		}
    		return newString;
    	}
    
    	/*
    	 * (non-Javadoc)
    	 * 
    	 * @see java.lang.Object#toString()
    	 */
    	@Override
    	public String toString() {
    		// Get the name of the class (or the name of the shape that we are working with)
    		String thisClassIs = getThisClass();
    		
    		// For a sphere
    		if ((thisClassIs).equals("sphere")) {
    			end = true;
    			return "SPHERE\nVolume: " + this.volume + "\nColor: " + this.color +  "\nSurface Area: " + this.surfaceArea;
    		} 
    		// For a cube
    		else if((thisClassIs).equals("Cube") && end == false) {
    			return "\n\nCUBE\nVolume: " + this.volume + "\nColor: " + this.color +  "\nSurface Area: " + this.surfaceArea;
    		}
    		else {
    			return "Volume: " + this.volume + "\nColor: " + this.color + "/nSurface Area: " + this.surfaceArea;
    		}
    	}
    }
    Java Code:
    public class ShapesDriver {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// Puts in a color, surface area, and volume
    		sphere ball = new sphere("Green", 5.7, 6.9 ,5);
    		System.out.print(ball);
    		
    		/*Cube cuboid = new Cube("Yellow", 5.7, 6.9, 8.5);
    		System.out.print(cuboid);*/
    	}
    }
    Java Code:
    public class sphere extends Shape {
    	
    	private double r;
    	
    	public sphere(String color, double a, double c, double r) {
    		super (color);
    		volume();
    		surfaceArea();
    		this.r = r;
    	}
    	
    	public void volume () {
    		volume = (4/3) * super.pi * Math.pow(r, 3);
    	}
    	
    	public void surfaceArea() {
    		surfaceArea = 4 * super.pi * Math.pow(r, 2);
    	}
    }

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

    Default Re: Geometric shapes

    and your question is?

  5. #5
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: Geometric shapes

    Why is my volume and surface area returning 0's?

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

    Default Re: Geometric shapes

    I have an issue with your toString method. Why is it doing things based upon the subclasses? Any subclasses should have their own toString methods. Think of it this way, before Sphere, Cube, Triangle etc classes exist the toString of Shape shoudl be fully functional. But as you have it now it is doing soemthing based on a class that does not exist, Cube. In real life if someone ask you what your name is would you reply "My name is John and my son is called Peter" even though you do not have a son?
    pbrockway2 likes this.

  7. #7
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Geometric shapes

    Java Code:
    public sphere(String color, double a, double c, double r) {
        super (color);
        volume();
        surfaceArea();
        this.r = r;
     }
    You are calling volume() and surfaceArea() while the radius is still zero. Set this.r=... first.

    (Also it's a good idea to follow Java coding conventions and call the class Sphere starting with a capital letter.)

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

    Default Re: Geometric shapes

    Java Code:
    public void volume () {
    They aren't returning anything.

  9. #9
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: Geometric shapes

    ok, so i've added this snippet of code to my sphere class:

    Java Code:
    @Override
    	public String toString() {
    		return color;
    	}
    how do i go about getting the volume and surface area properly?

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

    Default Re: Geometric shapes

    Did you bother to read replies 7 & 8? Between them you should be able to fix your problem.

Similar Threads

  1. Recognize shapes from pictures
    By acole5 in forum New To Java
    Replies: 9
    Last Post: 06-20-2011, 09:14 PM
  2. Combining shapes?
    By snj00u in forum Java 2D
    Replies: 2
    Last Post: 06-14-2011, 10:09 PM
  3. java shapes
    By Kyle227 in forum New To Java
    Replies: 7
    Last Post: 05-20-2010, 01:21 AM
  4. erase shapes ???
    By h9h in forum Java 2D
    Replies: 1
    Last Post: 10-12-2009, 09:11 PM
  5. Colors and shapes.
    By Torgero in forum New To Java
    Replies: 14
    Last Post: 10-13-2008, 06:25 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
  •