Advertisement
droidus

Shape

Oct 24th, 2011
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. public class Shape {
  2.  
  3.     protected String color;
  4.     protected double surfaceArea;
  5.     protected double volume;
  6.     protected double pi = Math.PI;
  7.     boolean end = false;
  8.    
  9.     public Shape(String a) {
  10.         color = a;
  11.     }
  12.  
  13.     public String color(String color) {
  14.         this.color = color;
  15.         return this.color;
  16.     }
  17.     public double surfaceArea(double surfaceArea) {
  18.         this.surfaceArea = surfaceArea;
  19.         return this.surfaceArea;
  20.     }
  21.     public double volume(double volume) {
  22.         this.volume = volume;
  23.         return this.volume;
  24.     }
  25.    
  26.     // Get the name of the class
  27.     public String getThisClass() {
  28.         String thisClassString = this.getClass().toString();
  29.         String newString = "";
  30.         char character;
  31.         boolean start = false;
  32.        
  33.         for (int i = 0; i<thisClassString.length(); i++) {
  34.             character = thisClassString.charAt(i);
  35.            
  36.             if(character == ' ') {
  37.                 start = true;
  38.             }
  39.             else if(start == true) {
  40.                 newString = newString + character;
  41.             }
  42.             else {
  43.                 // do nothing -- continue the loop
  44.             }
  45.         }
  46.         return newString;
  47.     }
  48.  
  49.     /*
  50.      * (non-Javadoc)
  51.      *
  52.      * @see java.lang.Object#toString()
  53.      */
  54.     @Override
  55.     public String toString() {
  56.         // Get the name of the class (or the name of the shape that we are working with)
  57.         String thisClassIs = getThisClass();
  58.        
  59.         // For a sphere
  60.         if ((thisClassIs).equals("sphere")) {
  61.             end = true;
  62.             return "SPHERE\nVolume: " + this.volume + "\nColor: " + this.color +  "\nSurface Area: " + this.surfaceArea;
  63.         }
  64.         // For a cube
  65.         else if((thisClassIs).equals("Cube") && end == false) {
  66.             return "\n\nCUBE\nVolume: " + this.volume + "\nColor: " + this.color +  "\nSurface Area: " + this.surfaceArea;
  67.         }
  68.         else {
  69.             return "Volume: " + this.volume + "\nColor: " + this.color + "/nSurface Area: " + this.surfaceArea;
  70.         }
  71.     }
  72. }      
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement