Sample Java Program for Triangle

Download Solution: Click to Download Solution
Solution File Name: JavaProgramTriangle.docx
Unzip Password: prestobear.com

Problem:
Sample Java Program for Triangle


1. GeometricObject.java

/*
 * This is the GeometricObject
 * This is the base class for the Triangle Class
 * This class provide basic methods for the Triangle class 
 */

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

	public GeometricObject() {
		dateCreated = new java.util.Date();
	}

	/*
	 * Create the shape with the color and filled
	 */
	public GeometricObject(String color, boolean filled) {

	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public boolean isFilled() {
		return filled;
	}

	public void setFilled(boolean filled) {
		this.filled = filled;
	}

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

	public String toString() {
		return "Created on " + dateCreated + "\n color: " + color + " and filled ";                 
	}   
}

2. TestTriangle.java

import java.util.Scanner;

/*
 * This class is the clas to Test for the Triangle class
 */

public class TestTriangle  {

	public static void main(String[] args) {

	}
}

3. Triangle.java
/*
 * This Class is to take the Triangle parameter and compute
 * 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 triangle 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.
 */

public class Triangle extends GeometricObject {
	private double side1 = 1.0;
	private double side2 = 1.0;
	private double side3 = 1.0;

	public Triangle() {
	}

	/*
	 * Constructor for the Triangle
	 */
	public Triangle(double side1, double side2, double side3) {
		this.side1 = side1;
		this.side2 = side2;
		this.side3 = side3;
	}

	public double setSide1() {
		return side1;
	}

	public double setSide2() {
		return side2;
	}

	public double setSide3() {
		return side3;
	}

	public void setSide1(double side1) {
		this.side1 = side1;
	}

	public void setSide2(double side2) {
		this.side2 = side2;
	}

	public void setSide3(double side3) {
		this.side3 = side2;
	}

	/*
	 * Compute the Area
	 */
	public double getArea() {
		return (side1 + side2 + side3) / 2;
	}

	/*
	 * Compute the Perimeter
	 */
	public double getPerimeter() {
		return side1 + side2 + side3;
	}

	public String toString() {
		return " Triangle: Side 1 = " + side1 + " Side 2 = " + side2
				+ " Side 3 = " + side3;
	}
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.