10/13/2010

BCS2234 13/10/ 2010 Inheritance

Programming Exercises

Sections 9.2–9.4

9.1 (The Triangle class) Design a class named Triangle that extends GeometricObject. The class contains:
  • Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle.

  • A no-arg constructor that creates a default triangle.

  • A constructor that creates a rectangle with the specified side1, side2, and side3.

  • The accessor methods for all three data fields.

  • A method named getArea() that returns the area of this triangle.

  • A method named getPerimeter() that returns the perimeter of this triangle.

  • A method named toString() that returns a string description for the triangle.

For the formula to compute the area of a triangle, see Exercise 5.19. The toString() method is implemented as follows:

return "Triangle: side1 = " + side1 + " side2 = " + side2 +   " side3 = " + side3; 

Draw the UML diagram that involves the classes Triangle and GeometricObject. Implement the class. Write a test program that creates a Triangle object with sides 1, 1.5, 1, sets color yellow and filled true, and displays the area, perimeter, color, and whether filled or not.


1 comment:

Mohd Nazri Ibrahim said...

/**
* @(#)Perwarisan.java
*
* Perwarisan application
*
* @author
* @version 1.00 2010/10/20
*/

public class Perwarisan {

public static void main(String[] args) {

// TODO, add your application code
Circle circle = new Circle(1);
System.out.println("A circle " + circle.toString());
System.out.println(circle.getRadius());
System.out.println("The radius is " + circle.getRadius());
System.out.println("The area is " + circle.getArea());
System.out.println("The diameter is " + circle.getDiameter());

Triangle segi3 = new Triangle (3.1,5.5,6.3);
System.out.println("A Segi3 " + segi3.toString());
System.out.println("The area is " + segi3.getArea());
System.out.println("The diameter is " + segi3.getPerimeter());

}
}

class GeometricObject { //buang public
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;

/** Construct a default geometric object */
public GeometricObject() {
dateCreated = new java.util.Date();
}

/** Return color */
public String getColor() {
return color;
}

/** Set a new color */
public void setColor(String color) {
this.color = color;
}

/** Return filled. Since filled is boolean,
so, the get method name is isFilled */
public boolean isFilled() {
return filled;
}

/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}

/** Get dateCreated */
public java.util.Date getDateCreated() {
return dateCreated;
}

/** Return a string representation of this object */
public String toString() {
return "created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
}
}


class Circle extends GeometricObject { // buang public
private double radius;

public Circle() {
}

public Circle(double radius) {
this.radius = radius;
}

/** Return radius */
public double getRadius() {
return radius;
}

/** Set a new radius */
public void setRadius(double radius) {
this.radius = radius;
}

/** Return area */
public double getArea() {
return radius * radius * Math.PI;
}

/** Return diameter */
public double getDiameter() {
return 2 * radius;
}

/** Return perimeter */
public double getPerimeter() {
return 2 * radius * Math.PI;
}

/** Print the circle info */
public void printCircle() {
System.out.println("The circle is created " + getDateCreated() +
" and the radius is " + radius);
}
}

class Triangle extends GeometricObject {
private double side1,side2,side3;

public Triangle(){
}

public Triangle(double side1, double side2 , double side3){
this.side1=side1;
this.side2=side2;
this.side3=side3;
}
public double getArea(){
double s; double area;
s=(side1+side2+side3)/2;
//area = sqrt(s*(s-side1)*(s-side2)*(s-side 3)); // perlu lengkapkan formula kira luas segi3
return (s);
}

public double getPerimeter(){

return (side1+side2+side3);
}


public String toString()
{
return "Triangle: side1 = " + side1 + " side2 = " + side2 +
" side3 = " + side3;

}
}

Cara download Installer windows 10 dalam format ISO

1. Jika anda bercadang untuk download windows 10 melalui website rasmi windows - pilihan untuk download dalam format ISO tidak di berikan.  ...