Results 1 to 2 of 2
  1. #1
    stephanie904 is offline Member
    Join Date
    Feb 2012
    Posts
    13
    Rep Power
    0

    Default Im having trouble with my Shape classes in java! im lost please help

    hi I dont know what im doing anymore ...im lost and Im getting an error message please help me...I dont know where to go from here ..im getting the error below and these are my classes im stuck! please help!

    this is my error message
    XML Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    	Void methods cannot return a value
    
    	at Shape.<init>(Shape.java:40)
    	at Circle.<init>(Circle.java:6)
    	at ShapeApp.main(ShapeApp.java:17)

    XML Code:
    public class Rectangle  implements Triangle {
    	
    	
    	
    	private double length;
    	private double heigth;
    	
    	public Rectangle() {
    		super();
    		setLength( super.DEFAULT_SIZE );
    		setHeight( Shape.DEFAULT_SIZE );
    		
    	}
         
    	public Rectangle(String string, String string2 ){
    		super();
    		
    
    	}
    	
    	
    	
    	public double getSurfaceArea() {
    		return this.length * this.heigth;
    	}
    	
    	public double getPeremeter() {
    		return 2 * this.length + 2 * this.length;
    		
    	}
    	
    	public double getLength(){
    		return this.length;
    	}
    	
    	public double getHeight(){
    		return  this.heigth;
    	}
    	
    	public void setLength( double theLength ){
    		if ( theLength <= 0 )
    			return;
    		this.length = theLength;
    	}
    	
    	public void setHeight( double theHeight ){
    		if (theHeight <= 0 )
    			return;
    		this.heigth = theHeight;
    		
    	}
    	
    	public String toString(){
    		return String.format("%s: \n%s: %s (%s) \n%s: %d \n%s: $%,.2f",
    				          "Rect Surface Area ", "Rect Peremeter", getSurfaceArea(), getPeremeter());
    	}
    	
         public double getSizeAmount(){
    		return heigth;
    		
    	}
    	
    }
    XML Code:
    public abstract class Shape implements Triangle{
    	
    	protected static final double DEFAULT_SIZE = (double) 1.0;
    	protected static final String DEFAULT_NAME = "Unknown";
    	private String shapeName;
    	
    	public Shape(){
    		this.shapeName = DEFAULT_NAME;
    	}
    	
    	public Shape(String name) {
    		setShapeName(name);
    		
    	}
    	
    	protected void setShapeName(String name) {
    	if( name.trim().length() == 0 )
    		shapeName = DEFAULT_NAME;
    	else
    		shapeName = new String ( name );
    	
    	}
    	
    	public String getShapeName() {
            //name
    	return shapeName;
    	
    }
        
    
    	
    	public abstract double getSurfaceArea();
    	
    	public abstract double getPerimeter();
    	
    
    	public String toString;{
    		return String.format("%s %s\nshape name: %s",getShapeName());
    		
    	}
    }
    XML Code:
    //triangle interface decleration
    public interface Triangle {
    	
    	
    	
    	double getSizeAmount(); //calculate sizes of triangle; no implementation
    
    }//end interface triangle

    XML Code:
    public class Circle extends Shape {
    	private double radius;
    	
    	public Circle( double theRadius ){
    		super();
    		if ( theRadius <= 0.0 )
    			setRadius( Shape.DEFAULT_SIZE );
    		else
    			setRadius( theRadius );
    	}
    	
    	
    
    	public double getSurfaceArea(){
    		
    		return this.radius * this.radius * Math.PI;
    	}
    	
    	public double getPeremeter(){
    		;
    		return 2 * this.radius + Math.PI;
    	}
    	
    	public double getRadius(){
    		return this.radius;
    		
    	}
    	
    	public void setRadius( double theRadius ) {
    		if( theRadius <= 0 )
    			return;
    		this.radius = theRadius;
    	}
    
    	@Override
    	public double getPerimeter() {
    		// TODO Auto-generated method stub
    		return 0;
    		
    
    	     
    	     public String toString(){
    	    	 return String.format("%s: \n%s: %s (%s) \n%s: %d \n%s: $%,.2f",
    			          "Circle Surface Area ", "Circle Peremeter", getSurfaceArea(), getPeremeter());
    	     }
    	}
    
    	@Override
    	public double getSizeAmount() {
    		// TODO Auto-generated method stub
    		return 0;
    	}
    }
    XML Code:
    import javax.swing.JOptionPane;
    
    
    
    
    public class ShapeApp {
    
    	//public static void main(String args[])
    	public static void main(String[] args) {
    		
    		Triangle triangleObjects[] = new Triangle[ 3 ];
    		
    		triangleObjects[ 0 ] = new Rectangle("3.5","4.6");
    		triangleObjects[ 1 ] = new Rectangle("3","2");
    
    		triangleObjects[ 2 ] = new Circle(0);
    		
    		System.out.println( "List of all Shapes:\n" );
    		
    		for( Triangle currentTriangle : triangleObjects ){
    			
    			System.out.printf("%s \n%s: $%,.2f\n\n",
    					currentTriangle.toString(),
    					"ovo testiramo", currentTriangle.getSizeAmount());
    			
    		}
    		
    		
    		
    		
    		
    		
    		
    		
    		/*
    		Shape[] object = new Shape[5];
    		int x = 0;
    		object[0] = new Rectangle("this is rectanlge",0,0);
    		object[1] = new Circle();
    		do{
    		try{
    		for(Shape p : object) {
    		    String msg = p.getSurfaceArea() +","+ p.getPerimeter();
    		    JOptionPane.showMessageDialog(null, "Message", msg, JOptionPane.INFORMATION_MESSAGE);
    		    x=2;
    		}
    		
    		}catch(Exception e) {
    			
    		}
    		
    	}while(x==1);*/
    		
    	}
    	
    	}

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    270
    Rep Power
    4

    Default Re: Im having trouble with my Shape classes in java! im lost please help

    Hi try to clear up your code
    public String toString;{
    return String.format("%s %s\nshape name: %s",getShapeName());
    }
    and please comment those lines which give you errors so we can analyze...

Similar Threads

  1. Trouble with loops/classes/something
    By SykoPyro in forum New To Java
    Replies: 3
    Last Post: 09-05-2012, 12:34 PM
  2. More trouble with classes and arrays
    By CuddlyKittens11 in forum Advanced Java
    Replies: 9
    Last Post: 04-27-2011, 01:25 AM
  3. Trouble with arrays and classes
    By CuddlyKittens11 in forum Advanced Java
    Replies: 3
    Last Post: 04-25-2011, 12:42 AM
  4. I'm having trouble with extending classes
    By ziongio in forum New To Java
    Replies: 2
    Last Post: 03-15-2011, 07:33 AM
  5. Having trouble with Classes
    By Skyton in forum Eclipse
    Replies: 7
    Last Post: 03-04-2011, 01:33 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
  •