Tuesday, July 28, 2009

Beginner Java help?

The assignment is this:





In this assigment you will write an object oriented program using Java that will consist of a class hierarchy of shapes (e.g., square, triangle, circle) that implement a shape interface. The shape interface requires that a shape provides instance methods to reveal its name, and its perimeter and area measurements.





I just gave you the triangle part. It's a GUI, I don't know how to compile it all together.





public class Triangle extends AbstractShape {


protected double sideA;


protected double sideB;


protected double sideC;


public Triangle() {


sideA = sideB = sideC = 0;


}


public Triangle(double a, double b, double c) {


sideA = a;


sideB = b;


sideC = c;


}


public String name() {


return "Triangle";


}


public double area() {


double s = (sideA + sideB + sideC) / 2.0;


return Math.sqrt( s*(s-sideA)*(s-sideB)*(s-sideC) );


}


public double perimeter() {


return sideA + sideB + sideC;


}


}





Please help? thanks

Beginner Java help?
You seem to have some confusion about interfaces. You are not supposed to be extending an abstract class. The assignment is to implement a Shape interface. The Shape interface is to specify contracts for three methods. The contracts given by Shape must be implemented explicitly by the classes.





Create a package to contain your code...call it whatever you want (or use some existing package). Add your interface and class definitions in your package.








// save as Shape.java


package my.lessons.shape





public interface Shape


{


// specify the contracts that implementing


// classes must fulfill


public String getName();


public double getPerimeter();


public double getArea();


}





~~~~~~~~~~~~


// save as Triangle.java


package my.lessons.shape





public class Triangle implements Shape


{


// leave these as protected, since more


// specialized triangle classes might


// extend this class, e.g., there might


// be an IsoscelesTriangle descendant


protected double sideA;


protected double sideB;


protected double sideC;





public Triangle() {


sideA = 0;


sideB = 0;


sideC = 0;


}





public Triangle(double a, double b, double c) {


sideA = a;


sideB = b;


sideC = c;


}





// implement Shape contracts


public String getName() {


return "Triangle";


}





public double getArea() {


double s = (sideA + sideB + sideC) / 2.0;


return Math.sqrt( s*(s-sideA)*(s-sideB)*(s-sideC) );


}





public double getPerimeter() {


return sideA + sideB + sideC;


}


}





~~~~~~~~~~~~


// save as Square.java


package my.lessons.shape





public class Square implements Shape


{


// not likely that a more specialized


// form of Square will develop - make


// this private


private double side;





public Square() {


side = 0;


}





public Square(double s) {


side = s;


}





// implement Shape contracts


public String getName() {


return "Square";


}





public double getArea() {


return Math.pow(side, 2);


}





public double getPerimeter() {


return 4*side;


}


}





~~~~~~~~~~~~


// save as Circle.java


package my.lessons.shape





public class Circle implements Shape


{


// not likely that a more specialized


// form of Circle will develop - make


// this private


private double radius;





public Circle() {


radius = 0;


}





public Circle(double r) {


radius = r;


}





// implement Shape contracts


public String getName() {


return "Circle";


}





public double getArea() {


return Math.PI*Math.pow(radius, 2);


}





public double getPerimeter() {


return 2*Math.PI*side;


}


}
Reply:you need to implement main, make some shapes with the New method, and then print out some info about them.


No comments:

Post a Comment