Wednesday, 10 February 2016

cs java programs


1. {50 points}Design a new Triangle class that extends the abstract class GeometricObject class. Write a test program that prompts the user to enter the 3 sides of the triangle, a color, and a Boolean value to indicate whether the triangle is filled. The program should create a Triangle object with these sides and set the color and filled properties using the input. The program should display the area, perimeter,color and true or false to indicate whether it is filled or not. (s=side) Area of a triangle = Square-root of ((s*(s-1)*(s-2)*(s-3)).


Program:

GeometricObject  class

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package week11;

/**
 *
 * @author kishore
 */
public abstract class GeometricObject
{
           
            private String color = "white";
            private boolean filled;
           
           
            protected GeometricObject()
            {
                       
            }
           
            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 String toString()
            {
                        return  " Color: " + color + " and filled: " + filled;
            }
           
            public abstract double getArea();
           
            public abstract double getPerimeter();             
}



Triangle class

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package week11;

/**
 *
 * @author kishore
 */
public class Triangle extends GeometricObject {
    private double side1 = 1.0;
    private double side2 = 1.0;
    private double side3 = 1.0;

    public Triangle() {
    }

    public Triangle(double side1, double side2, double side3) {
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
    }
    public double getSide1() {
        return side1;
    }

    public double getSide2() {
        return side2;
    }

    public double getSide3() {
        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;
    }
    public double getArea() {
        double p = getPerimeter() / 2;
        return Math.sqrt(p * ((p - side1) * (p - side2) * (p - side3)));
    }
    public double getPerimeter() {
        return side1 + side2 + side3;
    }

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

TestTriangle class:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package week11;

/**
 *
 * @author kishore
 */
import java.util.Scanner;

public class TestTriangle  {
    private double side1 = 1.0;
    private double side2 = 1.0;
    private double side3 = 1.0;

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter three sides of the Triangle");
        double side1 = input.nextDouble();
        double side2 = input.nextDouble();
        double side3 = input.nextDouble();
        Triangle triangle = new Triangle(side1, side2, side3);
        System.out.println("Enter the color of the Triangle");
        String color = input.next();
        triangle.setColor(color);

        System.out.println(" Is the Triangle filled? Reply with 'True' or 'False' ");

        String filled = input.next();
  
    Triangle triangle1 = new Triangle(side1, side2, side3);
    System.out.println("The Triangle Sides are \n side 1: "
                   + triangle1.getSide1() + "\n Side 2: " + triangle1.getSide2()
                   + "\n Side 3: " + triangle1.getSide3());

            System.out.println("The Triangle's Area is " + triangle1.getArea());

            System.out.println("The Triangle's Perimeter is " + triangle1.getPerimeter());

            System.out.println("The Triangle's Color is " + triangle1.getColor());
            System.out.println("Is the Triangle filled? " + triangle1.isFilled());
    }
}
   

OutPut:

run:
Enter three sides of the Triangle
5
6
8
Enter the color of the Triangle
white
 Is the Triangle filled? Reply with 'True' or 'False'
t
The Triangle Sides are
 side 1: 5.0
 Side 2: 6.0
 Side 3: 8.0
The Triangle's Area is 14.981238266578634
The Triangle's Perimeter is 19.0
The Triangle's Color is white
Is the Triangle filled? false
BUILD SUCCESSFUL (total time: 14 seconds)




 2. {50 points} Write a class named Octagon that extends GeometricObject and implements Comparable and Cloneable  interfaces. Assume that all eight sides of the octagon are equal.
Write a test program that creates an Octagon object with side value 5 and displays its area and perimeter. Create a new object using the clone method and compare the objects using the
compareTo method. Area of a regular octagon = (2+4/sqrt(2)) * side*side.

Program:

GeometricObject  class

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package week11;

/**
 *
 * @author kishore
 */
public abstract class GeometricObject
{
           
            private String color = "white";
            private boolean filled;
           
            //default constructor
            protected GeometricObject()
            {
                       
            }
           
            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 String toString()
            {
                        return  " Color: " + color + " and filled: " + filled;
            }
           
            public abstract double getArea();
           
            public abstract double getPerimeter();             
}


Octagon class

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package week11;

/**
 *
 * @author kishore
 */
public class Octagon extends GeometricObject implements Comparable<Octagon>
{
            private double side = 1.0;
            protected native Object clone() throws CloneNotSupportedException;

                       
            public Octagon()
            {
            }
           
            public Octagon(double side)
            {
                        super();
                        this.side = side;
            }
           
                       
           
            public void setSide(double side)
            {
                        this.side = side;
            }         
           
            public double getSide(double side)
            {
                        return side;
            }
           
            public double getArea()
            {
                        return (2 + 4 / (Math.sqrt(2)) * side * side);
            }
           
            public double getPerimeter()
            {
                        return side * 8;
            }                     
           
            public String toString()
            {
                        return "The length of each side is: " + side;
            }
           
            public int compareTo(Octagon octagon1)
            {
                        if (getArea() >= octagon1.getArea())
                                    return 1;
                       
                        else if (getArea() < octagon1.getArea())
                                    return -1;
                       
                        else
                                    return 0;
            }                     
           
            public interface Cloneable
            {
            }
           
}


OctagonTester class:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package week11;

/**
 *
 * @author kishore
 */
public class OctagonTester
{
            public static void main(String[] args) throws CloneNotSupportedException

            {
                        Octagon octagon = new Octagon();
                        Octagon octagon1 = new Octagon(5.0);
                       
                        System.out.println("The area of the octagon is: " + octagon1.getArea());
                        System.out.println("The perimeter of the octagon is: " + octagon1.getPerimeter());
                        System.out.println("When you compare the two objects, they return: " + octagon.compareTo(octagon1));
            }
}
Output:
run:
The area of the octagon is: 72.71067811865474
The perimeter of the octagon is: 40.0
When you compare the two objects, they return: -1
BUILD SUCCESSFUL (total time: 0 seconds)





1. {50 points}Design a new Triangle class that extends the abstract class GeometricObject class. Write a test program that prompts the user to enter the 3 sides of the triangle, a color, and a Boolean value to indicate whether the triangle is filled. The program should create a Triangle object with these sides and set the color and filled properties using the input. The program should display the area, perimeter,color and true or false to indicate whether it is filled or not. (s=side) Area of a triangle = Square-root of ((s*(s-1)*(s-2)*(s-3)).


Program:

GeometricObject  class

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package week11;

/**
 *
 * @author kishore
 */
public abstract class GeometricObject
{
           
            private String color = "white";
            private boolean filled;
           
           
            protected GeometricObject()
            {
                       
            }
           
            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 String toString()
            {
                        return  " Color: " + color + " and filled: " + filled;
            }
           
            public abstract double getArea();
           
            public abstract double getPerimeter();             
}



Triangle class

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package week11;

/**
 *
 * @author kishore
 */
public class Triangle extends GeometricObject {
    private double side1 = 1.0;
    private double side2 = 1.0;
    private double side3 = 1.0;

    public Triangle() {
    }

    public Triangle(double side1, double side2, double side3) {
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
    }
    public double getSide1() {
        return side1;
    }

    public double getSide2() {
        return side2;
    }

    public double getSide3() {
        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;
    }
    public double getArea() {
        double p = getPerimeter() / 2;
        return Math.sqrt(p * ((p - side1) * (p - side2) * (p - side3)));
    }
    public double getPerimeter() {
        return side1 + side2 + side3;
    }

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

TestTriangle class:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package week11;

/**
 *
 * @author kishore
 */
import java.util.Scanner;

public class TestTriangle  {
    private double side1 = 1.0;
    private double side2 = 1.0;
    private double side3 = 1.0;

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter three sides of the Triangle");
        double side1 = input.nextDouble();
        double side2 = input.nextDouble();
        double side3 = input.nextDouble();
        Triangle triangle = new Triangle(side1, side2, side3);
        System.out.println("Enter the color of the Triangle");
        String color = input.next();
        triangle.setColor(color);

        System.out.println(" Is the Triangle filled? Reply with 'True' or 'False' ");

        String filled = input.next();
  
    Triangle triangle1 = new Triangle(side1, side2, side3);
    System.out.println("The Triangle Sides are \n side 1: "
                   + triangle1.getSide1() + "\n Side 2: " + triangle1.getSide2()
                   + "\n Side 3: " + triangle1.getSide3());

            System.out.println("The Triangle's Area is " + triangle1.getArea());

            System.out.println("The Triangle's Perimeter is " + triangle1.getPerimeter());

            System.out.println("The Triangle's Color is " + triangle1.getColor());
            System.out.println("Is the Triangle filled? " + triangle1.isFilled());
    }
}
   

OutPut:

run:
Enter three sides of the Triangle
5
6
8
Enter the color of the Triangle
white
 Is the Triangle filled? Reply with 'True' or 'False'
t
The Triangle Sides are
 side 1: 5.0
 Side 2: 6.0
 Side 3: 8.0
The Triangle's Area is 14.981238266578634
The Triangle's Perimeter is 19.0
The Triangle's Color is white
Is the Triangle filled? false
BUILD SUCCESSFUL (total time: 14 seconds)




 2. {50 points} Write a class named Octagon that extends GeometricObject and implements Comparable and Cloneable  interfaces. Assume that all eight sides of the octagon are equal.
Write a test program that creates an Octagon object with side value 5 and displays its area and perimeter. Create a new object using the clone method and compare the objects using the
compareTo method. Area of a regular octagon = (2+4/sqrt(2)) * side*side.

Program:

GeometricObject  class

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package week11;

/**
 *
 * @author kishore
 */
public abstract class GeometricObject
{
           
            private String color = "white";
            private boolean filled;
           
            //default constructor
            protected GeometricObject()
            {
                       
            }
           
            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 String toString()
            {
                        return  " Color: " + color + " and filled: " + filled;
            }
           
            public abstract double getArea();
           
            public abstract double getPerimeter();             
}


Octagon class

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package week11;

/**
 *
 * @author kishore
 */
public class Octagon extends GeometricObject implements Comparable<Octagon>
{
            private double side = 1.0;
            protected native Object clone() throws CloneNotSupportedException;

                       
            public Octagon()
            {
            }
           
            public Octagon(double side)
            {
                        super();
                        this.side = side;
            }
           
                       
           
            public void setSide(double side)
            {
                        this.side = side;
            }         
           
            public double getSide(double side)
            {
                        return side;
            }
           
            public double getArea()
            {
                        return (2 + 4 / (Math.sqrt(2)) * side * side);
            }
           
            public double getPerimeter()
            {
                        return side * 8;
            }                     
           
            public String toString()
            {
                        return "The length of each side is: " + side;
            }
           
            public int compareTo(Octagon octagon1)
            {
                        if (getArea() >= octagon1.getArea())
                                    return 1;
                       
                        else if (getArea() < octagon1.getArea())
                                    return -1;
                       
                        else
                                    return 0;
            }                     
           
            public interface Cloneable
            {
            }
           
}


OctagonTester class:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package week11;

/**
 *
 * @author kishore
 */
public class OctagonTester
{
            public static void main(String[] args) throws CloneNotSupportedException

            {
                        Octagon octagon = new Octagon();
                        Octagon octagon1 = new Octagon(5.0);
                       
                        System.out.println("The area of the octagon is: " + octagon1.getArea());
                        System.out.println("The perimeter of the octagon is: " + octagon1.getPerimeter());
                        System.out.println("When you compare the two objects, they return: " + octagon.compareTo(octagon1));
            }
}
Output:
run:
The area of the octagon is: 72.71067811865474
The perimeter of the octagon is: 40.0
When you compare the two objects, they return: -1
BUILD SUCCESSFUL (total time: 0 seconds)



No comments:

Post a Comment