0% found this document useful (0 votes)
8 views

Java pgm 10-18

The document contains a series of Java programs, each with a specific aim such as printing patterns, generating multiplication tables, calculating sums and averages, and implementing classes for employee management and geometric shapes. Each program includes a code snippet and an output example. The programs demonstrate various programming concepts including loops, arrays, inheritance, and method overriding.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Java pgm 10-18

The document contains a series of Java programs, each with a specific aim such as printing patterns, generating multiplication tables, calculating sums and averages, and implementing classes for employee management and geometric shapes. Each program includes a code snippet and an output example. The programs demonstrate various programming concepts including loops, arrays, inheritance, and method overriding.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

PROGRAM NUMBER 10

AIM: Write a program in java to print the following pattern


*
**
***
****
*****
******
CODE:
// Java Program to print pattern
// Number triangle pattern
import java.util.*;
class Pattern {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
System.out.println("pattern:");
// outer loop to handle number of rows
for (i = 1; i <= n; i++) {
// inner loop to print space
for (j = 1; j <= n - i; j++) {
System.out.print(" ");
}
// inner loop to print star
for (j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
class Main
{
public static void main(String args[])
{
Pattern obj=new Pattern();
obj.printPattern(6);
}
}
OUTPUT:
PROGRAM NUMBER 11
AIM: Write a program in java to print a multiplication table of any number
CODE:
import java.util.*;
import java.io.*;
class Multiply
{
void multiplicationTable(int n)
{
for(int i=1;i<=10;i++)
{
System.out.println(+n+" * " +i+" = " +(n*i));
}
}
}
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number whose multiplication table is required");
int n=sc.nextInt();
Multiply obj=new Multiply();
obj.multiplicationTable(n);
}
}

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.*;

public class Main


{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of n");
int n=sc.nextInt();
int num,sum=0;
for(int i=1;i<=n;i++)
{
if(i%10==1 && i!=11)
{
System.out.println("Enter the "+i+"st number");
}
else if(i%10==2 && i!=12)
{
System.out.println("Enter the "+i+"nd number");
}
else if(i%10==3 && i!=13)
{
System.out.println("Enter the "+i+"rd number");
}
else{
System.out.println("Enter the "+i+"th number");
}
num=sc.nextInt();
sum+=num;
}
System.out.println("Average of "+n+" numbers is "+(sum/n));

}
}

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.*;

public class Main


{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int[] ar=new int[5];
for(int i=0;i<5;i++)
{
System.out.println("Enter the number at array index"+i);
ar[i]=sc.nextInt();
}
System.out.println("Array ELEMENTS:");
for(int i=0;i<5;i++)
{
System.out.print(ar[i]+" ");
}
}
}
OUTPUT:
PROGRAM NUMBER 15
AIM: Write a program in java to print the elements of an array in reverse order
CODE:
import java.util.*;
import java.io.*;

public class Main


{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int[] ar=new int[5];
for(int i=0;i<5;i++)
{
System.out.println("Enter the number at array index "+i);
ar[i]=sc.nextInt();
}
System.out.println("Array Elements in reverse order :");
for(int i=4;i>=0;i--)
{
System.out.print(ar[i]+" ");
}
}
}
OUTPUT:
PROGRAM NUMBER 16
AIM: Develop a java application with Employee class with Emp_name, Emp_id, Address,
Mail id, Mobile no as members. Inherit the classes, Programmer, Assistant Professor,
Associate Professor and Professor from employee class. Add Basic Pay (BP) as the member
of all the inherited classes with 97% of BP as DA, 10% of BP as HRA, 12% of BP as PF,
0.1% of BP for staff club fund, Generate pay slips for the employees with their gross
and net salary.

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);

System.out.println("\nPay Slip for Programmer:");


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);
}
}

class AssistantProfessor extends Employee {


double basicPay;

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);

System.out.println("\nPay Slip for Assistant 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);
}
}
class AssociateProfessor extends Employee {
double basicPay;

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);

System.out.println("\nPay Slip for Associate 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);
}
}

class Professor extends Employee {


double basicPay;

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;

public void setStudentDetails(int roll_no, String name) {


this.roll_no = roll_no;
this.name = name;
}
}

class Test extends Student {


protected int[] marks = new int[5];

public void setMarks(int[] marks) {


this.marks = marks;
}
}

class Result extends Test {


private int totalMarks;
private double percentage;

public void calculateResult() {


totalMarks = 0;
for (int mark : marks) {
totalMarks += mark;
}
percentage = (double) totalMarks / 5;
}

public void displayResult() {


System.out.println("Roll No: " + roll_no);
System.out.println("Name: " + name);
System.out.println("Total Marks: " + totalMarks);
System.out.println("Percentage: " + percentage + "%");
}
}

public class Main {


public static void main(String[] args) {
Result student = new Result();
student.setStudentDetails(32, "Aryan");
student.setMarks(new int[]{85, 90, 78, 88, 92});
student.calculateResult();
student.displayResult();
}
}

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;
}
}

class Cylinder extends Circle


{
private double height;
public Cylinder(double radius, double height)
{
super(radius);
this.height = height;
}
public double calculateVolume() {
return Math.PI * radius * radius * height;
}
}

public class Main {


public static void main(String[] args)
{
Circle circle = new Circle(5.0);
System.out.println("Area of Circle: " + circle.calculateArea());
Cylinder cylinder = new Cylinder(5.0, 10.0);
System.out.println("Volume of Cylinder: " + cylinder.calculateVolume());
}
}

OUTPUT:

You might also like