Java pgm 10-18
Java pgm 10-18
OUTPUT:
PROGRAM NUMBER 12
AIM: Write a program in java to print sum of even intergers from 1 to 10
CODE:
import java.io.*;
public class Main
{
public static void main(String[] args)
{
int sum=0;
for(int i=2;i<=10;i+=2)
{
sum+=i;
}
System.out.println("Sum of even numbers from 1 to 10 is "+sum);
}
}
OUTPUT:
PROGRAM NUMBER 13
AIM: Write a program in java to print average of n numbers
CODE:
import java.util.*;
import java.io.*;
}
}
OUTPUT:
PROGRAM NUMBER 14
AIM: Write a program in java to store and print 5 numbers in an array
CODE:
import java.util.*;
import java.io.*;
CODE:
class Employee {
String empName, empId, address, mailId;
long mobileNo;
// Constructor
Employee(String empName, String empId, String address, String mailId, long mobileNo) {
this.empName = empName;
this.empId = empId;
this.address = address;
this.mailId = mailId;
this.mobileNo = mobileNo;
}
// Method to display basic details
void displayDetails() {
System.out.println("Employee Name: " + empName);
System.out.println("Employee ID: " + empId);
System.out.println("Address: " + address);
System.out.println("Mail ID: " + mailId);
System.out.println("Mobile No: " + mobileNo);
}
}
class Programmer extends Employee {
double basicPay;
Programmer(String empName, String empId, String address, String mailId, long mobileNo, double
basicPay) {
super(empName, empId, address, mailId, mobileNo);
this.basicPay = basicPay;
}
void generatePaySlip() {
double da = 0.97 * basicPay;
double hra = 0.10 * basicPay;
double pf = 0.12 * basicPay;
double staffClubFund = 0.001 * basicPay;
double grossSalary = basicPay + da + hra;
double netSalary = grossSalary - (pf + staffClubFund);
AssistantProfessor(String empName, String empId, String address, String mailId, long mobileNo,
double basicPay) {
super(empName, empId, address, mailId, mobileNo);
this.basicPay = basicPay;
}
void generatePaySlip() {
double da = 0.97 * basicPay;
double hra = 0.10 * basicPay;
double pf = 0.12 * basicPay;
double staffClubFund = 0.001 * basicPay;
double grossSalary = basicPay + da + hra;
double netSalary = grossSalary - (pf + staffClubFund);
AssociateProfessor(String empName, String empId, String address, String mailId, long mobileNo,
double basicPay) {
super(empName, empId, address, mailId, mobileNo);
this.basicPay = basicPay;
}
void generatePaySlip() {
double da = 0.97 * basicPay;
double hra = 0.10 * basicPay;
double pf = 0.12 * basicPay;
double staffClubFund = 0.001 * basicPay;
double grossSalary = basicPay + da + hra;
double netSalary = grossSalary - (pf + staffClubFund);
Professor(String empName, String empId, String address, String mailId, long mobileNo, double
basicPay) {
super(empName, empId, address, mailId, mobileNo);
this.basicPay = basicPay;
}
void generatePaySlip() {
double da = 0.97 * basicPay;
double hra = 0.10 * basicPay;
double pf = 0.12 * basicPay;
double staffClubFund = 0.001 * basicPay;
double grossSalary = basicPay + da + hra;
double netSalary = grossSalary - (pf + staffClubFund);
System.out.println("\nPay Slip for Professor:");
displayDetails();
System.out.println("Basic Pay: " + basicPay);
System.out.println("DA: " + da);
System.out.println("HRA: " + hra);
System.out.println("PF: " + pf);
System.out.println("Staff Club Fund: " + staffClubFund);
System.out.println("Gross Salary: " + grossSalary);
System.out.println("Net Salary: " + netSalary);
}
}
public class Main {
public static void main(String[] args) {
Programmer programmer = new Programmer("Alice", "P001", "123 Main St",
"[email protected]", 9876543210L, 50000);
AssistantProfessor assistantProfessor = new AssistantProfessor("Bob", "A002", "456 Elm St",
"[email protected]", 8765432109L, 60000);
AssociateProfessor associateProfessor = new AssociateProfessor("Charlie", "AP003", "789 Pine
St", "[email protected]", 7654321098L, 70000);
Professor professor = new Professor("Diana", "PR004", "321 Oak St", "[email protected]",
6543210987L, 80000);
programmer.generatePaySlip();
System.out.println();
assistantProfessor.generatePaySlip();
System.out.println();
associateProfessor.generatePaySlip();
System.out.println();
professor.generatePaySlip();
// patient, pathology,doctor,nurse
}
}
OUTPUT:
PROGRAM NUMBER 16
AIM: Declare a class called item having data members item_code, item_name,
cost and discount. Derive two classes from class item, namely employee and
customer. The class employee has data members like employee_code,
employee_name and amount. The class customer has data members like
customer_name and amount. Define following functions for initializing data
members. displaying the values of data members. computing amount to be paid
for a purchased item. Also define function main to create objects of both derived
classes and to show usage of above functions.
CODE:
class Item
{
private int item_code;
private String item_name;
private int cost;
private int discount;
void setData(int code,String itName,int c,int d)
{
item_code=code;
item_name=itName;
cost=c;
discount=d;
}
public void display()
{
System.out.println("ITEM CODE: "+item_code);
System.out.println("ITEM NAME: "+item_name);
System.out.println("COST OF ITEM: "+cost);
System.out.println("DISCOUNT: "+discount);
}
}
class Employee extends Item
{
private int employee_code;
private String emp_name;
private int amount;
public void setData(int item_code,String item_name,int item_cost,int item_disc,int
emp_code, String name, int salary)
{
super.setData(item_code,item_name,item_cost,item_disc);
employee_code = emp_code;
emp_name = name;
amount = salary;
}
public void displayData()
{
System.out.println("Employee Code: " + employee_code);
System.out.println("Employee Name: " + emp_name);
System.out.println("Amount: " + amount);
}
}
class Costumer extends Item
{
private String costumer_name;
private int amount;
public void setData(int item_code,String item_name,int item_cost,int item_disc, String
name, int salary)
{
super.setData(item_code,item_name,item_cost,item_disc);
costumer_name = name;
amount = salary;
}
public void displayData() {
System.out.println("Costumer Name: " + costumer_name);
System.out.println("Amount: " + amount);
}
}
class Main
{
public static void main(String[] args)
{
Employee obj1=new Employee();
Costumer obj2=new Costumer();
obj1.setData(2204,"biscuits",100,5,55012,"Aditya",1000);
obj2.setData(2205,"chips",200,20,"Sujal",300);
obj1.display();
obj1.displayData();
System.out.println();
obj2.display();
obj2.displayData();
}
}
OUTPUT:
PROGRAM NUMBER 17
AIM:.Create a class student that stores roll_no, name. Create a class test that stores marks
obtained in five subjects. Class result derived from student and test contains the total marks
and percentage obtained in test. Input and display information of a student.
CODE:
class Student {
protected int roll_no;
protected String name;
OUTPUT:
PROGRAM NUMBER 18
AIM: Assume that Circle is defined using radius and Cylinder is defined using radius and
height. Write a Circle class as base class and inherit the Cylinder class from it. Develop
classes such that user can compute the area of Circle objects and volume of Cylinder objects.
Area of Circle is pie radius*radius, while volume of Cylinder is pie(radius * radius)*height
CODE:
class Circle
{
protected double radius;
public Circle(double radius) {
this.radius = radius;
}
public double calculateArea() {
return Math.PI * radius * radius;
}
}
OUTPUT: