java help again

Discussion in 'OT Technology' started by Grifter, Mar 19, 2005.

  1. Grifter

    Grifter Silver Member

    Joined:
    Dec 21, 2000
    Messages:
    3,117
    Likes Received:
    0
    Location:
    the southwest
    perimeter and area both display 0

    Code:
    // Shape.java
    // shape abstract superclass
    
    public abstract class Shape
    {
    	private String type;		// type of shape
    	private double length;		// shape's length or height
    	private double width;		// shape's width or base
    //	public double area;		// shape's area
    //	public double perimeter;	// shape's perimeter
    
    	// constructor
    	public Shape(String t, double l, double w)
    	{
    		type = t;
    		length = l;
    		width = w;
    	}
    
    	// set type
    	public void setType(String t)
    	{
    		type = t;
    	}
    
    	// return type
    	public String getType()
    	{
    		return type;
    	}
    
    	// set length
    	public void setLength(double l)
    	{
    		length = l;
    	}
    
    	// return length
    	public double getLength()
    	{
    		return length;
    	}
    
    	// set width
    	public void setWidth(double w)
    	{
    		width = w;
    	}
    
    	// return width
    	public double getWidth()
    	{
    		return width;
    	}
    
    	// return String representation of Shape object
    	public String toString()
    	{
    		return getType();
    	}
    
    	// abstract method overridden by subclasses
    	public abstract void calcPerimeter(double l, double w);
    	public abstract void calcArea(double l, double w);
    	public abstract double getPerimeter();
    	public abstract double getArea();
    
    } // end abstract class Shape
    Code:
    // Square.java
    // Square class extends Shape
    
    public class Square extends Shape
    {
    	private double perimeter;
    	private double area;
    
    	// constructor
    	public Square(String t, double l, double w)
    	{
    		super(t, l, w);
    	}
    
    	// calculate perimeter
    	public void calcPerimeter(double l, double w)
    	{
    		perimeter = l * 4;
    	}
    
    	// return perimeter
    	public double getPerimeter()
    	{
    		return perimeter;
    	}
    
    	// calculate area
    	public void calcArea(double l, double w)
    	{
    		area = l * l;
    	}
    
    	// return area
    	public double getArea()
    	{
    		return area;
    	}
    
    	// return String representation of Square object
    	public String toString()
    	{
    		return super.toString() + "\nLength = " + getLength() + "\nPerimeter = " + getPerimeter() + 
    
    "\nArea = " + getArea() + "\n\n";
    	}
    
    } // end class Square
     
  2. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    You never called calcPerimeter, or calcArea, change the constructor to this,
    Code:
    public Square(String t, double l, double w)
    {
    super(t);
    calcPerimeter(l);
    calcArea(l);
    }
    
    you should also change the shape to just have a title, not all shapes are gonna have a length and a width
    Code:
    public Shape(String t)
    {
    type = t;
    length = l;
    width = w;
    }
    
    and also, calcPerimeter only needs the length so chang it to
    Code:
    public void calcPerimeter(double l)
    {
    perimeter = l * 4;
    }
    
     
  3. Grifter

    Grifter Silver Member

    Joined:
    Dec 21, 2000
    Messages:
    3,117
    Likes Received:
    0
    Location:
    the southwest
    thanks :cool:
    the other shapes are rectangle and triangle (equilateral) and im cant get square to compile without using width
     
  4. Jericho

    Jericho Active Member

    Joined:
    Oct 21, 2002
    Messages:
    34,140
    Likes Received:
    0
    you use entirely too many comments, they're not there for you to explain every simple step
     
  5. OmniFire

    OmniFire *************************

    Joined:
    Oct 19, 2003
    Messages:
    3,778
    Likes Received:
    0
    Location:
    Austin
    have you seen the java src files?
     
  6. Jericho

    Jericho Active Member

    Joined:
    Oct 21, 2002
    Messages:
    34,140
    Likes Received:
    0
    nope
     
  7. Grifter

    Grifter Silver Member

    Joined:
    Dec 21, 2000
    Messages:
    3,117
    Likes Received:
    0
    Location:
    the southwest
    thats what the professor wants, thats what he gets
     
  8. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
    check them out one day, interesting design features in there that make you wonder WTF were they thinking!
     
  9. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    But a circle doesn't have a length or a width and a triangle doesn't have a length or a width, the length and width variables should be declared within the classes of rectangle, square, and whatever else has a length and width.

    I hate trying to read those, they make me so mad :mad:
     
  10. aphoric

    aphoric Even if god did exist, it would be necessary to ab

    Joined:
    Aug 29, 2003
    Messages:
    918
    Likes Received:
    0
    Location:
    Leaving Afghanistan
    your args have bad names, wtf is a "t?" call it "title," these are not the days of punch cards, we can use names that are longer than 3 chars. Javadoc, you gotta use javadoc style comments, any of the guys that work for me do that and I get pissed. Are you ever going to create a Square of a different name? In your CTOR you should set the name to Square as we know it's a square since that is the concrete instance class.
     

Share This Page