Wednesday, 10 February 2016

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)


No comments:

Post a Comment