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)



cs java programs


Question 1:-
/*
 * 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 clshm4;

/**
 *
 * @author kishore
 */
import java.util.*;
public class Employees {

   
public static void main(String[] args) {

int[] Employee0 = {2, 4, 3, 4, 5, 8, 8};
int[] Employee1 = {7, 3, 4, 3, 3, 4, 4};
int[] Employee2 = {3, 3, 4, 3, 3, 2, 2};
int[] Employee3 = {9, 3, 4, 7, 3, 4, 1};
int[] Employee4 = {3, 5, 4, 3, 6, 3, 8};
int[] Employee5 = {3, 4, 4, 6, 3, 4, 4};
int[] Employee6 = {3, 7, 4, 8, 3, 8, 4};
int[] Employee7 = {6, 3, 5, 9, 2, 7, 9};

int EmployeeTotalHours0 = AddingHours(Employee0);
int EmployeeTotalHours1 = AddingHours(Employee1);
int EmployeeTotalHours2 = AddingHours(Employee2);
int EmployeeTotalHours3 = AddingHours(Employee3);
int EmployeeTotalHours4 = AddingHours(Employee4);
int EmployeeTotalHours5 = AddingHours(Employee5);
int EmployeeTotalHours6 = AddingHours(Employee6);
int EmployeeTotalHours7= AddingHours(Employee7);

int[] total = {EmployeeTotalHours0, EmployeeTotalHours1,EmployeeTotalHours2, EmployeeTotalHours3, EmployeeTotalHours4, EmployeeTotalHours5, EmployeeTotalHours6, EmployeeTotalHours7};

//java.util.Arrays.sort(total);
                
  for (int i = 0; i < total.length; i++) { 
              
System.out.println("Employee  "+i+" "+total[i] + " "); 

}
  for (int i = 0; i < total.length; i++) {
java.util.Arrays.sort(total);
System.out.println(total[i]);
  }
}

public static int AddingHours(int[] array){
int total = 0;
for (int i = 0; i < array.length; i++) {
total += array[i];
}
return total;
}

}
        
Output:-
run:
Employee  0 34
Employee  1 28
Employee  2 20
Employee  3 31
Employee  4 32
Employee  5 28
Employee  6 37
Employee  7 41
20
28
28
31
32
34
37
41
BUILD SUCCESSFUL (total time: 0 seconds)







Question 2:-

/*
 * 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 clshm4;

/**
 *
 * @author kishore
 */
public class CircleWithPrivateDataFields {
                  
private double radius = 1;                         
private static int numberOfObjects = 0;                  

public CircleWithPrivateDataFields() {                
numberOfObjects++;
}
public CircleWithPrivateDataFields(double newRadius) {
radius = newRadius;
numberOfObjects++;
}
                  
public double getRadius() {        
return radius;
}
public static int getNumberOfObjects() {
return numberOfObjects;
}
public void setRadius(double newRadius){ 
radius = (newRadius>= 0) ? newRadius : 0;
}
public double getArea() {
return radius*radius*Math.PI;
}
}

/*
 * 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 clshm4;

/**
 *
 * @author kishore
 */

public class TestCircleArray {
   
  public static void main(String[] args) {
   
    CircleWithPrivateDataFields[] circleArray;

   circleArray = createCircleArray();

   printCircleArray(circleArray);
  }

  public static CircleWithPrivateDataFields[] createCircleArray() {
    CircleWithPrivateDataFields[] circleArray =
      new CircleWithPrivateDataFields[5];

    for (int i = 0; i < circleArray.length; i++) {
      circleArray[i] =
        new CircleWithPrivateDataFields(Math.random() * 100);
    }

     return circleArray;
      }
    public static void printCircleArray(CircleWithPrivateDataFields[] circleArray)
    {
        System.out.printf("%-30s%-15s\n", "Radius", "Area");
   
    for (int i = 0; i < circleArray.length; i++) {
     
        System.out.printf("%-30f%-15f\n", circleArray[i].getRadius(),
        circleArray[i].getArea());
    }
    System.out.println("-----------------------------------------");

   
    System.out.printf("%-30s%-15f\n", "The total areas of circles is",
      sum(circleArray));
  }
 
   public static double sum(
      CircleWithPrivateDataFields[] circleArray) {
      double sum = 0;

      for (int i = 0; i < circleArray.length; i++)
      sum += circleArray[i].getArea();

       return sum;
  }
  
} 
    Output :
run:
Radius                        Area          
73.021368                     16751.349642  
36.823880                     4259.993727   
87.138576                     23854.525802  
72.842942                     16669.586734  
5.400311                      91.619393     
-----------------------------------------
The total areas of circles is 61627.075298  
BUILD SUCCESSFUL (total time: 0 seconds)

 Question 3:-
/*
 * 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 clshm4;

/**
 *
 * @author kishore
 */
 public class Rectangle {

     double width=1;
     double height=1;
     double area;
     double parimeter;
     private static int numberOfObjects = 0;
     public Rectangle()
     {
        this.width=width;
        this.height=height;
        area=width*height;
        parimeter = 2*(width+height);
 }
    public Rectangle(double newwidth,double newheight)
    {

         width=newwidth;
         height=newheight;
        area=width*height; 
        parimeter = 2*(width+height);
        numberOfObjects++;
    }   
   
    public double getArea(){
       
        return area;
    }
    public double getParimeter(){
       
        return parimeter;
    }

 }


/*
 * 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 clshm4;

/**
 *
 * @author kishore
 */
public class TestRectangle {
   
    public static void main(String ar[]){
         System.out.println("Width\t Height\t Area\t parimeter  ");
     System.out.print("1\t1");   
    Rectangle re=new Rectangle();
    System.out.println("\t"+re.getArea()+"\t"+re.getParimeter());
    Rectangle r=new Rectangle(4,40);
    System.out.print("4\t40");
     System.out.println("\t"+r.getArea()+"\t"+r.getParimeter());
   
    Rectangle r2=new Rectangle(3.5,3.59);
    System.out.print("3.5\t3.59");
  
    System.out.println("\t"+r2.getArea()+"\t"+r2.getParimeter());
      }
    }




Output:-
run:
Width        Height      Area         parimeter 
1       1       1.0    4.0
4       40     160.0         88.0
3.5    3.59  12.565       14.18
BUILD SUCCESSFUL (total time: 0 seconds)