0% found this document useful (0 votes)
51 views50 pages

Computer - Project 1 (1) Isc

Uploaded by

tanuskapadhy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views50 pages

Computer - Project 1 (1) Isc

Uploaded by

tanuskapadhy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 50

1) Write a program in java to display the second smallest

number of the three numbers input from the keyboard.

A) Algorithm:

1) Accept three Numbers and store it in three variables namely “a, b


and c”.
2) Use “If else if” condition to find the greatest number.
3) Compare a and b:
4) If a ≤ b, then a is the smallest, and b is the second smallest (so far).
5) If b < a, then b is the smallest, and a is the second smallest (so far).
6) Compare the second smallest number (from step 1) with c:
If c < second smallest number, then c becomes the second smallest
number.
7) Return the second smallest number.
B) Program:

import java.util.Scanner;

public class SecondSmallest {


public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter first number:");
int n1 = sc.nextInt();
System.out.println("Enter second number:");
int n2 = sc.nextInt();
System.out.println("Enter third number:");
int n3 = sc.nextInt();

int secondSmallest = findSecondSmallest(n1, n2, n3);


// this is used to find second smallest number

System.out.println("Second smallest no. is: " + secondSmallest);


}
public static int findSecondSmallest(int n1, int n2, int n3) {
if (n1 <= n2 && n1 <= n3) {
if (n2 <= n3) {
return n2;
} else {
return n3;
}
} else if (n2 <= n1 && n2 <= n3) {
if (n1 <= n3) {
return n1;
} else {
return n3;
}
} else {
if (n1 <= n2) {
return n1;
} else {
return n2;
}}}}
C) Variable Description:

Variable Datatype Description


n1 int To store value of n1
n2 int To store value of n2
n3 int To store value of n3

D) Terminal Window:

Enter first number:


23
Enter second number:
25
Enter third number:
19
Second smallest no. is:
23
2) Write a program in Java to input a number (greater than zero)
and if
a) It is odd positive number then print its square and cube
b) It is an even positive number then print sum of its square and
square root

A) Algorithm:

1. Read a number from the user

2. Check if the number is greater than zero


- If not, print an error message and exit

3. Check if the number is even or odd


- If even, calculate:
- Square of the number
- Square root of the number
- Sum of square and square root
- If odd, calculate:
- Square of the number
- Cube of the number

4. Print the calculated values.


B) Program:

import java.util.Scanner;

public class NumberOperations {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter a positive number: ");


int number = scanner.nextInt();

while (number <= 0) {


System.out.println("Please enter a number greater than
zero.");
number = scanner.nextInt();
}

if (number % 2 == 0) {
System.out.println("Sum of square and square root: " +
(number * number + Math.sqrt(number)));
} else {
System.out.println("Square: " + number * number);
System.out.println("Cube: " + number * number * number);
}
}
}
C) Variable Description:

Variable Datatype Description


number int To store a number

D) Terminal window:

Enter a positive number:


4
Sum of square and square root: 18

Enter a positive number:


3
Square: 9
Cube: 27
3) Write a program in Java to find the volume, total surface area and the
diagonal of a cuboid as per the user’s choice. Take length (l), breadth
(b) and height (h) as inputs.
Volume=l*b*h
Total surface area=2*(l*b + b*h + l*h)

A) Algorithm:

1. Read input values (l, b, h) from the user.


2. Display menu for user to choose calculation.
3. Read user's choice.
4. Perform calculation based on user's choice:
- If choice == 1: Calculate Volume = l * b * h.
- If choice == 2: Calculate Total Surface Area = 2 * (l * b + b * h + l
* h).
- If choice == 3: Calculate Diagonal = √ (l² + b² + h²).
- If choice == 4: Calculate and display all (Volume, Total
Surface Area, and Diagonal).
5. Display result(s) to the user.
B) Program:

import java.util.Scanner;

public class CuboidCalculator {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

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


double length = scanner.nextDouble();
System.out.println("Enter breadth: ");
double breadth = scanner.nextDouble();
System.out.println("Enter height: ");
double height = scanner.nextDouble();
System.out.println("Volume: " + (length * breadth * height));
System.out.println("Total Surface Area: " + (2 * (length * breadth +
breadth * height + length * height)));

System.out.println ("Diagonal: " + Math.sqrt (Math.pow (length, 2)


+Math.pow (breadth, 2) + Math.pow (height, 2)));
}
}
C) Variable Description:

Variable Datatype Description


length int To so\tore length of a
cuboid.
breadth int To store breadth of a
cuboid.
height int To store height of a
cuboid.
4) Write a program in Java to accept radius and height and print the
volume of cone, cylinder and sphere as per the user’s choice. (Using
switch case)
Volume of cone= 𝜋𝑟^3/3
Volume cylinder=𝑟2ℎ
Volume of sphere= 𝛱𝑟

A) Algorithm:

1. Input radius (r) and height (h) from the user.


2. Display menu for user to choose shape (cone, cylinder, sphere).
3. Read user's choice.
4. Use switch-case statement to calculate volume based on choice:
- Case 1: Cone - Volume = πr²h/3
- Case 2: Cylinder - Volume = πr²h
- Case 3: Sphere - Volume = (4/3) πr³
5. Display calculated volume.
B) Program:

import java.util.Scanner;

public class VolumeCalculator {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter radius (r): ");


double radius = scanner.nextDouble();

System.out.println("Enter height (h): ");


double height = scanner.nextDouble();
System.out.println("Choose shape:");
System.out.println("1. Cone");
System.out.println("2. Cylinder");
System.out.println("3. Sphere");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("Volume of Cone: " + (Math.PI *
Math.pow(radius, 2) * height / 3));
break;
case 2:
System.out.println("Volume of Cylinder: " + (Math.PI *
Math.pow(radius, 2) * height));
break;
case 3:
System.out.println("Volume of Sphere: " + ((4/3.0) * Math.PI
* Math.pow(radius, 3)));
break;
default:
System.out.println("Invalid choice");
}
scanner.close();
}
}
C) Variable Description:

Variable Datatype Description


radius int To store the radius of
an object
height int To store the height of
an object
choice int To check the kind of
object
5) Write a java program to input a number and determine the
number of digits. Now form an integer that uses number of digits
and the significant digit at the one’s place. Display the number.
Sample input: 2136
Sample output: 46

A) Algorithm:

1. Accept a number from the user.


2. Now use while loop to calculate the number of digits in the input
number.
3. Extract the last digit (one's place) of the input number.
4. Form a new integer using the number of digits and the last digit.
5. Display the new integer.
B) Program

import java.util.Scanner;

public class DigitFormatter {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter a number: ");


int number = scanner.nextInt();

// Calculate number of digits


int numDigits = String.valueOf(Math.abs(number)).length();

// Extract last digit


int lastDigit = number % 10;

// Form new integer


int newNumber = numDigits * 10 + lastDigit;

System.out.println("New Number: " + newNumber);

scanner.close();
}
}
C) Variable Description:

Variable Datatype Description


number int To store a number
numDigits int To store the number of
digits
lastDigits int To store the last digit
of a number
6) Write a program in Java to accept a three single digit number
and display all the combinations of the three digit. No digit
should be repeated more than once.

A) Algorithm:

1. Accept three numbers. Check if all numbers are single digit numbers or
not. If yes, then continue, else end the program stating invalid number.
2. Generate combinations using nested loops:
- Loop through each digit (d1, d2, d3) for the hundreds place.
- Loop through remaining digits for the tens place.
- Loop through remaining digit for the ones place.
3. Print each generated combination.

B) Program:

import java.util.Scanner;
public class DigitPermutations {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter three single digits separated by spaces:");


int d1 = scanner.nextInt();
int d2 = scanner.nextInt();
int d3 = scanner.nextInt();
// validate input
if (d1 < 0 || d1 > 9 || d2 < 0 || d2 > 9 || d3 < 0 || d3 > 9) {
System.out.println("Invalid input. Please enter single digits.");
return;
}
String str = "" + d1 + d2 + d3;
System.out.println("Permutations:");
permute(str, 0, str.length() - 1);
scanner.close();
}

public static void permute(String str, int l, int r) {


if (l == r) {
System.out.println(str);
} else {
for (int i = l; i <= r; i++) {
String swapped = swap(str, l, i);
permute(swapped, l + 1, r);
}}}
public static String swap(String str, int i, int j) {
char[] arr = str.toCharArray();
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return new String(arr);
}
}
C) Variable Description:

Variable Datatype Description


d1 int To store a number
d2 int To store a number
d3 int To store a number
i int Outer loop
7) Write a program in java to accept n value and print first n
prime numbers.

A) Algorithm:

1. Accept a number.
2. Initialize count = 0 and num = 2.
3. While count < n:
a. Check if num is prime using IsPrime() function.
b. If num is prime:
i. Print num.
ii. Increment count.
c. Increment num.
4. End.
C) Program:
import java.util.Scanner;
public class PrimeNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of prime numbers:");
int n = scanner.nextInt();
int count = 0;
int num = 2;
System.out.println("Prime numbers:");
while (count < n) {
if (isPrime(num)) {
System.out.println(num); count+
+;
}
num++;
}
scanner.close();
}
public static boolean isPrime(int num) {
if (num <= 1) return false;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) return false;
}
return true;
}
}
C) Variable Description:

Variable Datatype Description


num int To store a number
n int To store n value
count int To count the prime
nos.
i int Loop variable

D) Terminal Window:

Enter the number of prime numbers:


5
Prime numbers:
2
3
5
7
11
8) Write a Java program to print the following sum series up to n terms:
S=1+2
1+2+3
1+2+3+⋯+n

A) Algorithm:

1. Accept a number n.
2. Initialize i = 1.
3. While i ≤ n:
a. Initialize sum = 0 and j = 1.
b. While j ≤ i:
i. Print j.
ii. If j < i, print "+".
iii. sum += j.
iv. j++.
c. Print " = " and sum.
d. i++.

B) Program:
import java.util.Scanner;

public class SumSeries {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter the value of n:");


int n = scanner.nextInt();

// Validate
input if (n <=
0) {
System.out.println("Invalid input. Please enter a positive integer.");
return;
}

System.out.println("Sum Series:");
printSeries(n);
printSeries(n, true);

scanner.close();
}

// Method to print series without equals sum


public static void printSeries(int n) {
for (int i = 1; i <= n; i++) {
printRow(i);
System.out.println();
}
}

// Method to print series with equals sum


public static void printSeries(int n, boolean showSum) {
System.out.println();
for (int i = 1; i <= n; i++) {
printRow(i);
if (showSum) {
System.out.print(" = " + calculateSum(i));
}
System.out.println();
}
}

// Method to print a row of the series


public static void printRow(int n) {
for (int i = 1; i <= n; i++) {
System.out.print(i);
if (i < n) System.out.print("+");
}
}

// Method to calculate the sum of a row


public static int calculateSum(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
return sum;
}
}

C) Variable Description:
Variable Datatype Description
n int To store value of n
i int Loop variable
j int Loop variable

D) Terminal window:

Enter a number:
2

1
1+2

Sum=3
9) Write a program in Java to input two numbers m and n. Find
and print: s=𝑚 !(𝑛𝑛−! 𝑚)!. Use a method that accepts
numbers and return its factorial.

A) Algorithm:

1. Calculate factorial(m) and store in factM.


2. Calculate factorial(n-1) and store in factNMinusOne.
3. Calculate factorial(m) and store in factM.
4. Calculate expression: s = factM / (factNMinusOne * factM).
5. Return s.

B) Program:
import java.util.Scanner;
public class FactorialExpression {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the value of m:");
int m = scanner.nextInt();
System.out.println("Enter the value of n:");
int n = scanner.nextInt();
// Validate input
if (m < 0 || n < 0) {
System.out.println("Invalid input. Please enter non-negative
integers.");
return;
}
long result = calculateExpression(m, n);
System.out.println("s = " + result);
scanner.close();
}
// Method to calculate the expression
public static long calculateExpression(int m, int n) {
return factorial(m) / (factorial(n - 1) * factorial(m));
}
// Method to calculate factorial
public static long factorial(int num) {
long fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
return fact;
}
}
C) Variable Description:

Variable Datatype Description


m int To store m value
n int To store n value
i int Loop variable
fact long To store factorial of a
number
10) Write a Java program to find the roots of a quadratic
equation:𝑎𝑥2 + 𝑏𝑥 + 𝑐 = 0 with the following class
specifications: Class name: Quad Data members:
float a,b,c,d - (a,b are co efficients , constant c and d is discriminant)
x1 and x2 are the roots of the equation
Member Methods:
Quad(float x, float y, float z) - to initialize a=x, b=y, c=z, d=0 void
calculate() – Find d=𝑏2 − 4𝑎𝑐 if d<0 then print “Roots not possible”
otherwise find and display x1=(- )/2a and
x2= ( )/2a

A) Algorithm:

1. Calculate discriminant d = b^2 - 4ac.


2. If d > 0:
a. Calculate roots x1 = (-b + √d) / 2a, x2 = (-b - √d) / 2a.
b. Display real and distinct roots.
3. Else if d == 0:
a. Calculate root x = -b / 2a.
b. Display real and equal roots.
4. Else (d < 0):
a. Calculate real part x = -b / 2a.
b. Calculate imaginary part y = √(-d) / 2a.
c. Display complex roots.
B) Program:

public class Quad {


float a, b, c, d;
double x1, x2;

// Constructor to initialize coefficients


public Quad(float x, float y, float z) {
this.a = x;
this.b = y;
this.c = z;
this.d = 0;
}

// Method to calculate discriminant


public void calculateDiscriminant() {
d = b * b - 4 * a * c;
}

// Method to calculate roots


public void calculateRoots() {
calculateDiscriminant();
if (d > 0) {
x1 = (-b + Math.sqrt(d)) / (2 * a);
x2 = (-b - Math.sqrt(d)) / (2 * a);
System.out.println("Real and distinct roots: " + x1 + ", " + x2);
} else if (d == 0) {
x1 = x2 = -b / (2 * a);
System.out.println("Real and equal roots: " + x1);
} else {
x1 = -b / (2 * a);
x2 = -b / (2 * a);
double imaginary = Math.sqrt(-d) / (2 * a);
System.out.println("Complex roots: " + x1 + " + " + imaginary +
"i, " + x2 + " - " + imaginary + "i");
}
}
// Method to display equation
public void displayEquation() {
System.out.println("Quadratic Equation: " + a + "x^2 + " + b + "x + "
+ c + " = 0");
}
public static void main(String[] args) {
Quad quad = new Quad(1, -5, 6); // ax^2 + bx + c = 0
quad.displayEquation();
quad.calculateRoots();
}
}

C) Variable Description:
Variable Data Type Description
a float Coefficient of x^2
term
b float Coefficient of x term
c float Constant term
d float Discriminant
(b^2 - 4ac)
x1 double Root 1 of the quadratic
equation
x2 double Root 2 of the
quadratic equation

D) Terminal Window:

Quadratic Equation:
1.0x^2 + -5.0x + 6.0 = 0
Real and distinct roots:
2.0, 3.0
11) Class name: Discount
Data Members:
Int cost – to store the list price of the article
String name – to store name
Double dc – to store discount
Double amt – to store amount to be paid

Member Functions:
Void input()- to accept name of the customer and cost of the article
purchased
Void cal()- to calculate the discount and the amount
Void display()- to display the name of the customer, cost, discount
and amount to be paid .
Write a program to compute the discount according to given conditions
and displays the output as per given format:
List price Rate of
Discount
Upto ₨ 5000 no discount
From ₨ 5001 to ₨ 10000 10% on list
price
From ₨ 10001 to ₨15000 15% on list
price
Above ₨15000 20% on list
price
Output:
Name of Customer Discount Amount to be paid
……………….. ……… ..………….

A) Algorithm:
1. Input customer name and list price.
2. Calculate discount based on list price:
- ≤ 5000: 0%
- 5001-10000: 10%
- 10001-15000: 15%
- ≥ 15001: 20%
3. Calculate amount to be paid.
4. Display customer name, list price, discount rate, and amount to be paid.
B) Program:

import java.util.*;
public class Discount {
int cost;
String name;
double dc;
double amt;
void input() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter customer name: ");
name = scanner.nextLine();
System.out.print("Enter list price: ");
cost = scanner.nextInt();
scanner.close();
}
void cal() {
if (cost <= 5000) {
dc = 0;
} else if (cost >= 5001 && cost <= 10000) {
dc = 10;
} else if (cost >= 10001 && cost <= 15000) {
dc = 15;
} else {
dc = 20;
}
amt = cost - (cost * dc / 100);
}
void display() {
System.out.println(“Name of Customer ” + “\t” + “ Discount”+ “\ t”
+ “Amount to be paid”);
System.out.println(“……………”+ “\t” + “…......” + “\t” +
“…………….”);
}
public static void main(String[] args) {
Discount discount = new Discount();
discount.input();
discount.cal();
discount.display();
}
}

C) Variable Description:
Variable Datatype Description
Cost int To store the MRP cost
of an article.
Name String To store the name of
customer.
Dc double To store the discount
on the article.
Amt double To store the final price
of the article.
12) A bank charges interest for the vehicle loan as per the following
tariff:
Number of Rate of Interest
Up to 5 years 15%
More than 5 years and up to 10 years 12%
Above 10 years 10%

Write a program to model the class with the specifications given


below:
Class name: Loan

Data Members:
int time- time for which loan is sanctioned
double principal- amount sanctioned
double rate- rate of interest
double interest- to store interest
Interest=(principal*rate*time)/100
double amt- amount to be paid after given time

Member Functions:
Void getdata()- to accept principal and time
Void calculate()- to find interest and amount
Void display()- to display interest and amount
B) Program:

import java.util.Scanner;

class Loan {
int time; // time for which loan is sanctioned
double principal; // amount sanctioned
double rate; // rate of interest
double interest; // to store interest
double amt; // amount to be paid after given time

// Method to accept principal and time


public void getData() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the principal amount: ");
principal = scanner.nextDouble();
System.out.print("Enter the time (in years): ");
time = scanner.nextInt();

if (time <= 5) {
rate = 15.0;
} else if (time <= 10) {
rate = 12.0;
} else {
rate = 10.0;
}
}

// Method to find interest and amount


public void calculate() {
interest = (principal * rate * time) / 100;
amt = principal + interest;
}

// Method to display interest and amount


public void display () {
System.out.println("Interest: " + interest);
System.out.println ("Amount to be paid after " + time + " years: " +
amt);
}
public static void main(String[] args) {
Loan loan = new Loan();
loan.getData();
loan.calculate();
loan.display();
}
}
13. Define a class called Student to check whether student is eligible
for taking admission in Std XI with the following specifications:

Class name: Student

Data Members:
String name- to store name of the student
Int mm- to store marks in maths
Int scm- to store marks in science
Int comp- to store marks in computer

Member Methods:
Student()- parameterized constructor to initialize data members by
accepting details check()- to check the eligiblity for the course with
the following conditions:
Marks Course
90% or more in all subjects Science with Computer
Average marks 90% or more Bio- science
Average marks 80% or more and less than 90%
Commerce display()- to display eligibility

Write a main() method to create an object of the class and call all the
above member methods.
A) Algorithm:
1. Initialize the Student Object:
Accept the student’s name and marks in Maths, Science, and Computer.
Check Eligibility:

If marks in all subjects are 90% or more, the student is eligible for
Science with Computer.

If the average marks are 90% or more, the student is eligible for Bio-
Science.

If the average marks are between 80% and 89.99%, the student is
eligible for Commerce.

Display Eligibility:
Print the student’s name and the eligible course based on the conditions
checked.
B) Program:
class Student {
// Data members
private String name;
private int mm; // Marks in Maths
private int scm; // Marks in Science
private int comp; // Marks in Computer

// Parameterized constructor
public Student(String n, int m, int sc, int c) {
name = na;
mm = m;
scm = sc;
comp = c;
}

// Method to check eligibility


public String check() {
if (mm >= 90 && scm >= 90 && comp >= 90) {
return "Eligible for Science with Computer";
} else {
double average = (mm + scm + comp) / 3.0;
if (average >= 90) {
return "Eligible for Bio-Science";
} else if (average >= 80 && average < 90) {
return "Eligible for Commerce";
} else {
return "Not eligible for any course";
}
}
}

// Method to display eligibility


public void display() {
System.out.println("Student Name: " + name);
System.out.println("Eligibility: " + check());
}
public static void main(String args[]) {

Scanner in = new Scanner(System.in);


System.out.print("Enter Name: ");
String n = in.nextLine();
System.out.print("Enter Marks in Maths: ");
int m = in.nextInt();
System.out.print("Enter Marks in Science: ");
int sc = in.nextInt();
System.out.print("Enter Marks in Computer: ");
int c = in.nextInt();}

// Main method to create an object and call methods


public static void main(String[] args) {
Student student = new Student("John Doe", 92, 88, 91);
student.display();
}
}

C) Variable Descrition:

1. name:

Type: String

Description: Stores the name of the student.

1. mm:

Type: int

Description: Stores the marks obtained by the student in


Mathematics.

2. scm:

Type: int

Description: Stores the marks obtained by the student in Science.

3. comp:
Type: int

Description: Stores the marks obtained by the student in


Computer Science.
14. Define a class Salary described as below:
Class name: Salary

Data Members:
Name, address, phone, subject, specialization, monthly salary,
incometax

Member Methods:
(i) To accept details of the teacher including monthly salary
(ii) To display details of the teacher
(iii) To compute annual income tax at 5% of annual salary above
Rs. 175000
Write a main method to call the above member methods.

A) Algorithm:
1. Initialize the Salary Object:

Accept the teacher’s name, address, phone number, subject, specialization,


and monthly salary.

2. Compute Annual Income Tax:

Calculate the annual salary by multiplying the monthly salary by 12.

If the annual salary exceeds Rs. 175,000, compute the income tax as 5% of
the amount exceeding Rs. 175,000.

Otherwise, set the income tax to 0.

3. Display Teacher’s Details:

Print the teacher’s name, address, phone number, subject, specialization,


monthly salary, and computed annual income tax.

4. Main Method:

Create an instance of the Salary class.


Call the method to accept details of the teacher.

Call the method to compute the annual income tax.

B) Program:
class Salary {
// Data members
private String name;
private String address;
private String phone;
private String subject;
private String specialization;
private double monthlySalary;
private double incomeTax;

// Method to accept details of the teacher


public void acceptDetails(String n, String add, String ph, String sub,
String spec, double sal) {
name = n;

address = add ;

phone = ph;

subject = sub;
specialization = spec;
monthlySalary = sal;
}
// Method to display details of the teacher
public void displayDetails() {
System.out.println("Name: " + name);
System.out.println("Address: " + address);
System.out.println("Phone: " + phone);
System.out.println("Subject: " + subject);
System.out.println("Specialization: " + specialization);
System.out.println("Monthly Salary: " + monthlySalary);
System.out.println("Annual Income Tax: " + incomeTax);
}

// Method to compute annual income tax


public void computeIncomeTax() {
double annualSalary = monthlySalary * 12;
if (annualSalary > 175000) {
incomeTax = (annualSalary - 175000) * 0.05;
} else {
incomeTax = 0;
}
}

// Main method to create an object and call methods


public static void main(String[] args) {
Salary teacher = new Salary();
teacher.acceptDetails("Alice", "123 Main St", "555-1234",
"Mathematics", "Algebra", 20000);
teacher.computeIncomeTax();
teacher.displayDetails();
}
}
15. Define a class Pay with the following specifications:

Data Members:
String name, address , city
Float salary

Member Functions:
Pay(String n, String add, String cy, float s)
Void outputdata()- to display all
initialized values Void
calculate()- to find and print the
salary da=15% of salary
hra=10% of salary
Pf=12% of salary
gross= salary+da+hra
net=gross-pf
void display()-to display all details of the employee

A) Algorithm:

1. Initialize Data Members: Create a constructor to initialize


the name, address, city, and salary data members.
2. Output Data: Define a method outputData() to print the initialized values.
3. Calculate Salary Components: Define a method calculate() to compute
DA, HRA, PF, gross salary, and net salary.
4. Display Details: Define a method display() to
call outputData() and calculate() methods.
5. Main Method: Create an instance of the Pay class and call
the display() method.

B) Program:

import java.awt.Font;

import javax.swing.JFrame;

import javax.swing.JLabel;
import javax.swing.JPanel;

public class Pay {

private String name;

private String address;

private String city;

private float salary;

// Constructor to initialize data members

public Pay(String n, String add, String cy, float s) {

name = n;

address = add;

city = cy;

salary = s;

// Function to display all initialized values

public void outputData() {

System.out.println("Name: " + name);

System.out.println("Address: " + address);

System.out.println("City: " + city);

System.out.println("Salary: " + salary);

}
// Function to calculate and print the salary details

public void calculate() {

float da = 0.15f * salary;

float hra = 0.10f * salary;

float pf = 0.12f * salary;

float gross = salary + da + hra;

float net = gross - pf;

System.out.println("Dearness Allowance (DA): " + da);

System.out.println("House Rent Allowance (HRA): " + hra);

System.out.println("Provident Fund (PF): " + pf);

System.out.println("Gross Salary: " + gross);

System.out.println("Net Salary: " + net);

// Function to display all details of the employee

public void display() {

outputData();

calculate();

public static void main(String[] args) {


Pay employee = new Pay("John Doe", "123 Main St", "New York",
50000.0f);

employee.display();

C) Variable Description:

1. name

Datatype: String

Description: To store name of the employee.

2. address

Datatype: String

Description: To store address of the worker.

3. city

Datatype: String

Description: To store city name of the worker.

4. salary

Datatype: float

Description: to store salary of the worker.


D) Terminal window:

Name:

Aizen

Address:

A 303, Shrinand City 3, New Maninagar.

City:

Ahmedabad

Salary:

20000000 Inr

Dearness Allowance (DA):

3000000 Inr

House Rent Allowance (HRA):

1500000 Inr

Provident Fund (PF):

1800000 Inr

Gross Salary:

24500000 Inr

Net Salary:

18200000 Inr

You might also like