Tuesday, 27 August 2013

Program that accepts a 2D array of 4 by 5 by numbers and display the sum of all the rows, columns and the entire matrix ?

import java.util.Scanner;

public class TwoD {

    public static void main(String[] args) {
        float sum = 0, isum = 0;
        int a[][] = new int[4][5];
        Scanner in = new Scanner(System.in);
        for (int i = 0; i <= 3; i++) {
            for (int j = 0; j <= 4; j++) {
                System.out.println("Enter element no. " + i + j);
                a[i][j] = in.nextInt();
            }
        }
        System.out.println("Entered matrix is:");
        for (int i = 0; i <= 3; i++) {
            for (int j = 0; j <= 4; j++) {
                System.out.print(a[i][j] + "    ");
            }
            System.out.println();
        }
        for (int i = 0; i <= 3; i++) {
            for (int j = 0; j <= 4; j++) {
                sum = sum + a[i][j];
            }
            isum = isum + sum;
            System.out.println("Sum of " + (i + 1) + " row is:" + sum);
            sum = 0;
        }

        for (int i = 0; i <= 4; i++) {
            for (int j = 0; j <= 3; j++) {
                sum = sum + a[j][i];
            }
            System.out.println("Sum of " + (i + 1) + " coloum is:" + sum);
            sum=0;

        }
        System.out.println("Sum of intire matrix is: " + isum);

    }
}

Program that accepts an array by 10 numbers and display them in ascending order

import java.util.Scanner;

public class Ascending {

    public static void main(String[] args) {
        int temp;
        Scanner in = new Scanner(System.in);
        int a[] = new int[10];
        for (int i = 0; i <= 9; i++) {
            System.out.println("Enter element no. " + (i + 1));
            a[i] = in.nextInt();
        }
        for (int i = 0; i <= 9; i++) {
            for (int j = i + 1; j <= 9; j++) {
                if (a[i] > a[j]) {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
        for (int i = 0; i <= 9; i++) {
            System.out.print(a[i] + " ");
        }
    }
}

Saturday, 11 August 2012

TempEmp CLASS QUESTION

TEMPEMP CLASS QUESTION

import java.util.Scanner;


public class TempEmp extends Employee {
protected String grade;
    protected int days;
public TempEmp() {
    }
public TempEmp(int x, String y, String grade, int days) {
super(x, y);
this.grade = grade;
this.days = days;
    }
public TempEmp(String grade) {
super(20, "Nirmal");
this.grade = grade;
days = 10;
    }
public TempEmp(int days) {
super(20, "Vinod");
this.days = days;
grade = "A";

}public int getDays() {
        return days;
    }


public String getGrade() {
        return grade;
    }

    @Override
    public void getData() {
        super.getData();
Scanner in = new Scanner(System.in);
System.out.println("Enter grade:");
grade = in.nextLine();
System.out.println("Enter work of days:");
days = in.nextInt();
    }

    @Override
    public void showData() {

super.showData();
System.out.println("Grade is:" + grade);

System.out.println("days of working is:" + days);
    }
public static void main(String[] args) {
TempEmp t = new TempEmp(1234, "Manoj", "B", 60);
t.showData();
        TempEmp t1 = new TempEmp("A");
t1.showData();
        TempEmp t2 = new TempEmp(60);

t2.showData();
        TempEmp t3 = new TempEmp();
t3.getData();
        t3.showData();
}

}

POINT CLASS FOR RECTANGLE

POINT CLASS FOR RECTANGLE




import java.util.Scanner;


public class Point {
protected int x_co;
    protected int y_co;

    public Point() {
    }


public Point(int x, int y) {
        x_co = x;
        y_co = y;
    }
public int getX_co() {
        return x_co;
    }
public void setX_co(int x_co) {
        this.x_co = x_co;
    }


public int getY_co() {
        return y_co;
    }
public void setY_co(int y_co) {
this.y_co = y_co;
    }


public void getData() {
Scanner in = new Scanner(System.in);
System.out.println("Enter X_CO:");
        x_co = in.nextInt();
        System.out.println("Enter Y_CO:");
        y_co = in.nextInt();
    }
public void showData() {
System.out.println("X_CO=" + x_co);
        System.out.println("Y_CO=" + y_co);
    }
public static void main(String[] args) {

Point p = new Point();

Point p1 = new Point();
p1.getData();
p.showData();
p1.showData();
}

}

Create a class called Student that stores its name, class, section, and marks in four subjects. Write functions to accept, display and get/set of the data.

Create a class called Student that stores its name, class, section, and marks in four subjects. Write
functions to accept, display and get/set of the data.




import java.util.Scanner;


public class Student {


protected String name;

protected int Class;

protected char section;

protected int s1;

protected int s2;

protected int s3;

protected int s4;


public Student() {

}


public Student(String name, int Class, char section, int s1, int s2, int s3, int s4) {

this.name = name;

this.Class = Class;

this.section = section;

this.s1 = s1;

this.s2 = s2;

this.s3 = s3;

this.s4 = s4;
    }


public String getName() {

return name;
    }


public void setName(String name) {

this.name = name;
    }


public int get_Class() {

return Class;
    }


public void setClass(int Class) {

this.Class = Class;
    }


public char getSection() {

return section;
    }


public void setSection(char section) {

this.section = section;
    }


public int getS1() {

return s1;
    }


public void setS1(int s1) {

this.s1 = s1;
    }


public int getS2() {

return s2;
    }


public void setS2(int s2) {

this.s2 = s2;
    }


public int getS3() {

return s3;
    }


public void setS3(int s3) {

this.s3 = s3;
    }


public int getS4() {

return s4;
    }


public void setS4(int s4) {

this.s4 = s4;
    }


public void getData() {

Scanner in = new Scanner(System.in);

System.out.println("Enter name:");

name = in.nextLine();

System.out.println("Enter class:");

Class = in.nextInt();

System.out.println("Enter Section:");

section = in.next().charAt(0);

System.out.println("Enter marks in 4 subjects:");

s1 = in.nextInt();
        s2 = in.nextInt();
        s3 = in.nextInt();
        s4 = in.nextInt();


    }


public void showData() {

System.out.println("Name is: " + name + "\nClass is: " + Class + "\nSection is: " + section + "\nMarks in 4 subjects are: " + s1 + " " + s2 + " " + s3 + " " + s4);

}


public int getTotal() {

return (s1 + s2 + s3 + s4);

}


public float getPercentage() {

return (float) (getTotal() / 4);

}


public static void main(String[] args) {
Student s1 = new Student("Gourav", 12, 'A', 89, 95, 75, 81);

 s1.showData();

System.out.println("Total is: " + s1.getTotal());
System.out.println("Percentage is: " + s1.getPercentage());
Student s2 = new Student();

s2.getData();
s2.showData();
System.out.println("Total is: " + s2.getTotal());

System.out.println("Percentage is: " + s2.getPercentage());

   
}

}

Create a class called Employee that stores its code and name. Create two derive classes from Employee named TempEmp and PerEmp.

Create a class called Employee that stores its code and name. Create two derive classes from
Employee named TempEmp and PerEmp.




import java.util.Scanner;


public class Employee {
protected int code;
protected String name;
public Employee() {
    }
public Employee(int code) {
        this.code = code;
    }


public Employee(String name) {
        this.name = name;
    }
public Employee(int code, String name) {
        this.code = code;
        this.name = name;
    }
public int getCode() {
        return code;
    }
public void setCode(int code) {
        this.code = code;
    }


public String getName() {
        return name;
    }
public void setName(String name) {
        this.name = name;
    }

public void getData() {
        Scanner in = new Scanner(System.in);
System.out.println("Enter name");
        name = in.nextLine();
System.out.println("Enter code");
        code = in.nextInt();
    }
public void showData() {
        System.out.println("Code is:" + code);
System.out.println("Name is:" + name);
}
public static void main(String[] args) {

Employee e = new Employee(1234, "hemant");
e.showData();

}

}
 

Create a class called Employee that stores its name, department, designation and basic salary.

Create a class called Employee that stores its name, department, designation and basic salary. Write
functions to accept, display and get/set of the data.
The incentives includes HRA, DA , CA.
import java.util.Scanner;


public class Employe {

    protected String name;
    protected String department;
    protected String designation;
    protected float bs;

    public Employe() {

}
public Employe(String name, String department, String designation, float bs) {

this.name = name;
        this.department = department;
        this.designation = designation;
        this.bs = bs;

}

    public String getName() {

return name;
}

    public void setName(String name) {

this.name = name;
}
public String getDepartment() {
return department;

}


public void setDepartment(String department) {
this.department = department;
}
public String getDesignation() {
return designation;
}

    public void setDesignation(String designation) {
this.designation = designation;
}

    public void getData() {
Scanner in = new Scanner(System.in);
System.out.println("Enter name:");
name = in.nextLine();
System.out.println("Enter department:");
department = in.nextLine();
System.out.println("Enter designation:");
designation = in.nextLine();
System.out.println("Enter Basic salary:");
    bs = in.nextInt();
}
public void showData() {
System.out.println("Name is:" + name);

System.out.println("Department is:" + department);
        System.out.println("Designation is:" + designation);
System.out.println("Basic salary is:" + bs);
}


public float getBs() {

return bs;
}


public void setBs(float bs) {
this.bs = bs;

}
public float getHRA() {
        return bs * 20 / 100;
    }


public float getDA() {

return bs * 10 / 100;

}
public float getCA() {
        return bs * 10 / 100;
    }

    public static void main(String[] args) {

        Employe e = new Employe("Vikas", "management", "ceo", 60000);
        e.showData();
        System.out.println("HRA is:" + e.getHRA());
        System.out.println("DA is:" + e.getDA());
System.out.println("CA is:" + e.getCA());
        Employe e1 = new Employe();
        e1.getData();
       e1.showData();
System.out.println("HRA is:" + e1.getHRA());
System.out.println("DA is:" + e1.getDA());
System.out.println("CA is:" + e1.getCA());

}
}