Results 1 to 10 of 10
Thread: Geometric shapes
- 10-25-2011, 01:26 AM #1
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
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
- 10-25-2011, 01:29 AM #2
- 10-25-2011, 01:30 AM #3
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
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); } }
- 10-25-2011, 01:35 AM #4
Re: Geometric shapes
and your question is?
- 10-25-2011, 01:36 AM #5
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
Re: Geometric shapes
Why is my volume and surface area returning 0's?
- 10-25-2011, 01:39 AM #6
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?
- 10-25-2011, 01:40 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Geometric shapes
You are calling volume() and surfaceArea() while the radius is still zero. Set this.r=... first.Java Code:public sphere(String color, double a, double c, double r) { super (color); volume(); surfaceArea(); this.r = r; }
(Also it's a good idea to follow Java coding conventions and call the class Sphere starting with a capital letter.)
- 10-25-2011, 01:40 AM #8
Re: Geometric shapes
They aren't returning anything.Java Code:public void volume () {
- 10-25-2011, 01:52 AM #9
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
Re: Geometric shapes
ok, so i've added this snippet of code to my sphere class:
how do i go about getting the volume and surface area properly?Java Code:@Override public String toString() { return color; }
- 10-25-2011, 01:55 AM #10
Similar Threads
-
Recognize shapes from pictures
By acole5 in forum New To JavaReplies: 9Last Post: 06-20-2011, 08:14 PM -
Combining shapes?
By snj00u in forum Java 2DReplies: 2Last Post: 06-14-2011, 09:09 PM -
java shapes
By Kyle227 in forum New To JavaReplies: 7Last Post: 05-20-2010, 12:21 AM -
erase shapes ???
By h9h in forum Java 2DReplies: 1Last Post: 10-12-2009, 08:11 PM -
Colors and shapes.
By Torgero in forum New To JavaReplies: 14Last Post: 10-13-2008, 05:25 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks