Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Help!

  1. #1
    nutdeep is offline Member
    Join Date
    Sep 2015
    Posts
    4
    Rep Power
    0

    Default Help!

    This won't run. No errors are showing up. Just don't know why it won't run...


    Java Code:
    package week4;
    
    public class Shape {
    	
    	public static void main(String[] args){} 
    	
    	    protected double surfaceArea;
    	    protected double volume;
    	    protected double radius;
    	    protected double pi = Math.PI;
    	    boolean end = false;
    
    	    public double surfaceArea(double surfaceArea) {
    	        this.surfaceArea = surfaceArea;
    	        return this.surfaceArea;
    	    }
    	    public double volume(double volume) {
    	        this.volume = volume;
    	        return this.volume;
    	    }
    	    public double radius(double radius) {
    	        this.radius = radius;
    	        return this.radius;
    	    }
    	     
    	    // Getting 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 {
    	                // aka doing nothing here, loop over and over
    	            }
    	        }
    	        return newString;
    	    }
    	
    	  public String toString() {
    	        // Get name of shape we want to work with
    	        String thisClassIs = getThisClass();
    	         
    	        // For a sphere
    	        if ((thisClassIs).equals("Sphere")) {
    	            end = true;
    	            return "SPHERE\nVolume: " + this.volume + "\nSurface Area: " + this.surfaceArea + "\nRadius: " + this.radius;
    	        } 
    	        // For a cube
    	        else if((thisClassIs).equals("Cube") && end == false) {
    	            return "\n\nCUBE\nVolume: " + this.volume + "\nSurface Area: " + this.surfaceArea + "\nRadius: " + this.radius;
    	        }
    	        else {
    	            return "Volume: " + this.volume + "/nSurface Area: " + this.surfaceArea + "\nRadius: " + this.radius;
    	        }
    	    }
    
    class Sphere extends Shape {
        
        private double r; 
         
        public Sphere(double a, double c, double r) {
        	this.radius = r;
        	this.volume = c;
            this.surfaceArea = a;
        }
        public double volume () {
            volume = (4/3) * super.pi * Math.pow(r, 3);
            return volume;
        }
         
        public double surfaceArea() {
            surfaceArea = 4 * super.pi * Math.pow(r, 2);
            return surfaceArea;
        }
    }
    
    class Cube extends Shape {
        
        private double r; 
         
        public Cube(double a, double c, double r) {
        	this.radius = r;
        	this.volume = c;
            this.surfaceArea = a;
        }
        public double volume() {
            volume = (4/3) * super.pi * Math.pow(r, 3);
            return volume;
        }
        public double surfaceArea() {
            surfaceArea = 4 * super.pi * Math.pow(r, 2);
            return surfaceArea;
        	}
    }  
    class ShapesDriver {
       	 
    	public void main(String[] args){
            // Puts in surface area, volume, and radius
            Cube cuboid = new Cube(5.7, 6.9, 8.5);
            System.out.print(cuboid);
            
            Sphere globe = new Sphere(5.7, 6.9, 5.0);
            System.out.print(globe);
    	}
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Help!

    Please go through the Forum Rules with special reference to the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Help!

    Quote Originally Posted by nutdeep View Post
    Just don't know why it won't run...
    Really?
    Quote Originally Posted by nutdeep View Post
    Java Code:
    	public static void main(String[] args){}
    db
    benji2505 likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Help!

    Uh, I might point out that.

    1. Cubes don't have radii.
    2. The supplied volume for the supplied radius is wrong (why not just provide the radius and calculate the other?)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Help!

    This won't run.
    Try executing the class in a command prompt window with the java command, copy the full contents of the console window and pasted it here.
    If you don't understand my response, don't ignore it, ask a question.

Posting Permissions

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