0% found this document useful (0 votes)
56 views58 pages

Computer Project 5

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

Computer Project 5

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

1.

Write a program in java to display the second smallest number of the three
numbers input from the keyboard.

➢ Algorithm:
1. Start the program.
2. Read three numbers from the user using the Scanner class and store them in
variables a, b, and c.
3. Check the following conditions: a. If ((a > b && b > c) || (c > b && b > a)),
then b is the second smallest number. Print "Second smallest is " + b. b. Else if
((b > c && c > a) || (a > c && c > b)), then c is the second smallest number.
Print "Second smallest is " + c. c. Else, a is the second smallest number. Print
"Second smallest is " + a.
4. End the program.

import java.util.*;

class second_smallest

public static void main (String args [])

Scanner sc = new Scanner (System.in);

System.out.println("Enter any three numbers:");

// To store three numbers

int a = sc.nextInt();

int b = sc.nextInt();

int c = sc.nextInt();

if ((a>b && b>c) || (c>b && b>a))


//To check whether the number is second smallest

System.out.println("Second smallest is " + b);

else if ((b>c && c>a) || (a>c && c>b))

System.out.println("Second smallest is " + c);

else

System.out.println("Second smallest is " + a);

// To print the second smallest

➢ Variable Description:
Variable Datatype Description
a int To read and store the value of a
b int To read and store the value of b
c int To read and store the value of c

➢ Output:
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.

➢ Algorithm:
1. Start the program.
2. Read a number greater than zero from the user using the Scanner class and store
it in variable n.
3. Check the following conditions:
a. If (n % 2 == 1), then the number is odd positive.
- Calculate the square of the number by multiplying n by itself and store the
result in a variable called square.
- Calculate the cube of the number by multiplying n by itself twice and store
the result in a variable called cube.
- Print "Square = " followed by the value of square.
- Print "Cube = " followed by the value of cube.
b. If (n % 2 == 0), then the number is even positive.
- Calculate the sum of the square of the number and the square root of the
number by using the Math.sqrt(n) function and store the result in a variable called
sum.
- Print the value of sum.
4. The given algorithm assumes that the input number is a positive number greater
than zero. If negative numbers or zero are allowed, additional checks and
conditions need to be added to handle those cases.
5. End the program.
import java.util.*;

class OPSC_EPSSSR

public static void main (String args[])

Scanner sc = new Scanner (System.in);

System.out.println("Enter any number greater than zero:");

double n = sc.nextDouble();

//To store number

if (n%2==1)

//To check whether the number is odd positive

System.out.println("Square = "+(n*n));

System.out.println("Cube = "+(n*n*n));

if (n%2==0)

//To check whether the number is even positive

double sum = (n*n) + (Math.sqrt(n));

System.out.println(sum);

}
➢ Variable Description:
Variable Datatype Description
n double To read and store the value of n
sum double To calculate the sum

➢ Output:
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)
Diagonal = √𝑙 2 + 𝑏 2 + ℎ2

➢ Algorithm:

1. Start the program.

2. Read the length, breadth, and height of a cuboid from the user using the Scanner
class and store them in variables l, b, and h respectively.

3. Calculate the volume of the cuboid by multiplying the length, breadth, and height
and store it in the variable volume.

4. Calculate the total surface area of the cuboid using the formula 2*(l*b + b*h + l*h)
and store it in the variable tsa.

5. Calculate the diagonal of the cuboid using the formula Math.sqrt(l*l + b*b + h*h)
and store it in the variable diagonal.

6. Print the value of the volume of the cuboid using the message "Volume of a cuboid
= " + volume.

7. Print the value of the total surface area of the cuboid using the message "Total
surface area of a cuboid = " + tsa.

8. Print the value of the diagonal of the cuboid using the message "Diagonal of a
cuboid = " + diagonal.

9. End the program.


import java.util.*;
class V_TSA_D
{
public static void main (String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter length, breadth, and height of a cuboid");
//To store length, breadth and height
double l = sc.nextDouble();
double b = sc.nextDouble();
double h = sc.nextDouble();
//To calculate volume, total surface area and diagonal of cuboid
double volume = l*b*h;
double tsa = 2*(l*b + b*h + l*h);
double diagonal = Math.sqrt(l*l + b*b + h*h);
//To print value of volume, total surface area and diagonal of cuboid
System.out.println("Volume of a cuboid = "+volume);
System.out.println("Total surface area of a cuboid = "+tsa);
System.out.println("Diagonal of a cuboid = "+diagonal);
}
}

➢ Variable Description:
Variable Datatype Description
l double To read and store the length
b double To read and store the breadth
h double To read and store the height
volume double To calculate and display the volume
tsa double To calculate and display the total surface area
diagonal double To calculate and display the diagonal
➢ Output:
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)
1
Volume of cone = 𝜋𝑟 2 ℎ
3

Volume of cylinder = 𝜋𝑟 2 ℎ
4
Volume of sphere = 𝜋𝑟 3
3

➢ Algorithm:

1. Start the program.

2. Read the radius and height of the cone, cylinder, or sphere from the user using the
Scanner class and store them in variables r and h respectively.

3. Display the message "Enter 1 for volume of cone, 2 for volume of cylinder, and 3
for volume of sphere:".

4. Read the choice of the user (1, 2, or 3) and store it in the variable ch.

5. Use a switch statement with ch as the control variable to calculate and print the
volume based on the user's choice:

- If ch is equal to 1, calculate the volume of the cone using the formula (1/3 * 3.14 *
r * r * h) and print the result.

- If ch is equal to 2, calculate the volume of the cylinder using the formula (3.14 * r
* r * h) and print the result.

- If ch is equal to 3, calculate the volume of the sphere using the formula (4/3 * 3.14
* r * r * r) and print the result.

6. End the switch statement.

7. End the program.


import java.util.*;
class Volume_CCS
{
public static void main (String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter radius and height");
//To store radius and height
double r = sc.nextDouble();
double h = sc.nextDouble();
System.out.println("Enter 1 for volume of cone, 2 for volume of cylinder and
3 for volume of sphere:");
int ch = sc.nextInt();
switch (ch)
//To calculate and print volume of cone, cylinder and sphere
{
case 1:
System.out.println("Volume of cone = "+((3.14*r*r*h)/3));
break;
case 2:
System.out.println("Volume of cylinder = "+(3.14*r*r*h));
break;
case 3:
System.out.println("Volume of sphere = "+((4*3.14*r*r*r)/3));
break;
default:
System.out.println("Please enter 1, 2 or 3 only");
break;
}
}
}
➢ Variable Description:
Variable Datatype Description
r double To read and store radius
h double To read and store height
ch int To read and store the condition

➢ Output:
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

➢ Algorithm:

1. Start the program.

2. Read an integer number from the user using the Scanner class and store it in the
variable n.

3. Calculate the number of digits in the input number by converting it to a string using
the `String.valueOf() method and then getting the length of the resulting string. Store
the result in the variable numberOfDigits.

4. Calculate the significant digit at the one's place of the input number by taking the
modulus of n with 10. Store the result in the variable significantDigit.

5. Form the target number by multiplying the numberOfDigits by 10 and adding the
significantDigit. Store the result in the variable formedNumber.

6. Display the formedNumber as the output, along with an appropriate message.

7. End the program.

import java.util.*;

public class formed_integer


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
int numberOfDigits = String.valueOf(n).length();
// Determine the number of digits
int significantDigit = n%10;
// Get the significant digit at the one's place
int formedNumber = (numberOfDigits * 10) + significantDigit;
// Form the integer
System.out.println("Formed number: " + formedNumber);
}
}

➢ Variable Description:
Variable Datatype Description
n int To read and store the value of n
numberOfDigits int To read and store the number of digits
significantDigit int To read and store the significant digit
formedNumber int To calculate the formed number

➢ Output:
6. Write a program in Java to accept a three-digit number and display all the
combinations of the three-digit. No digit should be repeated more than once.

➢ Algorithm:

1. Start the program.

2. Prompt the user to enter a three-digit number.

3. Read and store the user input in the variable 'n'.

4. Check if the entered number is less than 100 or greater than 999.

- If true, display "Invalid input. Please enter a three-digit number."

- If false, continue to the next steps.

5. Extract the first digit of the number by dividing 'n' by 100 and store it in the
variable 'digit1'.

6. Extract the second digit of the number by getting the remainder of 'n' divided by
100, then dividing the result by 10. Store it in the variable 'digit2'.

7. Extract the third digit of the number by getting the remainder of 'n' divided by 10.
Store it in the variable 'digit3'.

8. Create an integer array 'a' with elements 'digit1', 'digit2', and 'digit3' in that order.

9. Check if 'digit1', 'digit2', and 'digit3' are all different (not repeated).

- If true, continue to the next steps.

- If false, display "Invalid input. Digits should not be repeated."

10. Iterate over 'i' from 0 to 2 (inclusive) using a loop.

- Iterate over 'j' from 0 to 2 (inclusive) using a nested loop.

- Iterate over 'k' from 0 to 2 (inclusive) using another nested loop.

- Check if 'i', 'j', and 'k' are all different.


- If true, display the combination of 'a[i]', 'a[j]', and 'a[k]', without any
spaces between them.

11. End the program.

import java.util.*;
class three_digit_combinations
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a three digit number:");
int n=sc.nextInt();
if (n<100 || n>999)
//To check whether entered number is a three digit number or not
{
System.out.println("Invalid input. Please enter a three-digit number.");
}
else
{
int digit1 = n/100;
int digit2 = (n%100)/10;
int digit3 = n%10;
//Separating the number in three-digits
int a[] = {digit1, digit2, digit3};
if (digit1 != digit2 && digit1 != digit3 && digit2 != digit3)
//To check whether digits are repeated or not
{
for (int i=0; i<3; i++)
//Usage of loops to make different possible combinations
{
for (int j=0; j<3; j++)
{
for (int k=0; k<3; k++)
{
if (i!=j && j!=k && k!=i)
{
System.out.println(a[i] + "" + a[j] + "" + a[k]);
//To print the combinations
}
}
}
}
}
else
{
System.out.println("Invalid input. Digits should not be repeated.");
}
}
}
}
➢ Variable Description:
Variable Datatype Description
n int To read and store three-digit number
digit1 int To store first digit of the number
digit2 int To store second digit of the number
digit3 int To store third digit of the number
a int To store the three digits in an array
i int Loop variable
j int Loop variable
k int Loop variable

➢ Output:
7. Write a program in java to accept n value and print first n prime numbers.

➢ Algorithm:

1. Start the program.

2. Prompt the user to enter the value of 'n'.

3. Read and store the user input in the variable 'n'.

4. Display "Prime numbers between 1 to [n] are:".

5. Start a loop from 'i' = 2 to 'n' (inclusive).

- Set a counter variable 'count' to 0 for each iteration of 'i'.

- Start a nested loop from 'j' = 1 to 'i' (inclusive).

- Check if 'i' modulo 'j' is equal to 0 (i.e., 'j' is a divisor of 'i').

- If true, increment 'count' by 1.

- Check if 'count' is equal to 2.

- If true, 'i' is a prime number.

- Print 'i' followed by a space.

6. End the program.


import java.util.*;

class one_to_n_prime_number
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of n");
int n=sc.nextInt();
System.out.println("Prime numbers between 1 to " +n + " are:");
//Start checking from 2
for (int i=2; i<=n; i++)
{
int count=0;
for (int j=1; j<=i; j++)
{
//To check whether number is prime
if (i % j==0)
{
count++;
}
}
if(count==2)
System.out.print(i + " ");
//To print prime numbers till n
}
}
}
➢ Variable Description:
Variable Datatype Description
i int Loop variable
j int Loop variable
n int To print prime numbers till n
count int To count the prime number

➢ Output:
8. Write a program in Java to print the following sum series:
𝟏∗𝟐 𝟏∗𝟐∗𝟑 𝟏∗𝟐∗𝟑…∗𝒏
S= + +⋯
𝟏+𝟐 𝟏+𝟐+𝟑 𝟏+𝟐+𝟑…+𝒏

➢ Algorithm:

1. Start the program.

2. Prompt the user to enter the value of 'n'.

3. Read and store the user input in the variable 'n'.

4. Declare and initialize a variable 'sum' to 0 to keep track of the sum of the series.

5. Declare and initialize a variable 'factorial' to 2 to hold the factorial value.

6. Declare and initialize a variable 'denominator' to 2 to hold the denominator value.

7. Start a loop from 'i' = 1 to 'n' (inclusive) to calculate the sum of the series.

- Update 'factorial' by multiplying it with 'i' for each iteration.

- Update 'denominator' by adding 'i' for each iteration.

- Calculate the partial sum by dividing 'factorial' by 'denominator' and casting the
result to a double.

- Add the partial sum to 'sum'.

8. Display "Sum of the series: [sum]".

9. End the program.


import java.util.*;
public class sum_series
{
public static void main (String args[])
{
Scanner sc = new Scanner (System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
double sum = 0;
int factorial = 2;
//As the numerator ascends with increasing factorial
int denominator = 2;
//As the denominator starts with 2
for (int i=1; i<=n; i++)
//To calculate sum till n
{
factorial *= i;
denominator += i;
sum += factorial / (double) denominator;
}
System.out.println("Sum of the series: " + sum);
}
}
➢ Variable Description:
Variable Datatype Description
i int Loop variable
n int To read and store the value of n
sum int To calculate and display the sum
factorial int To calculate factorial in the numerator
denominator int To calculate sum in denominator
➢ Output:
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.

➢ Algorithm:

1. Start the program.

2. Create a new instance of the Scanner class.

3. Prompt the user to enter the value of m.

4. Read and store the value of m from the user.

5. Prompt the user to enter the value of n.

6. Read and store the value of n from the user.

7. Calculate the factorial of m using the factorial() method and store it in the variable
mFactorial.

8. Calculate the factorial of n using the factorial() method and store it in the variable
nFactorial.

9. Calculate the factorial of (n - m) using the factorial() method and store it in the
variable nMinusMFactorial.

10. Calculate the value of s by dividing nFactorial by the product of mFactorial and
nMinusMFactorial, and store it in the variable s.

11. Print the value of s.

12. End the program.


import java.util.*;
class factorial_nm
{
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of m: ");
int m = sc.nextInt();
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
int mFactorial = factorial(m);
int nFactorial = factorial(n);
int nMinusMFactorial = factorial(n - m);
double s = nFactorial / (mFactorial * nMinusMFactorial);
System.out.println("The value of s is: " + s);
}
//Method to calculate the factorial of a number using recursion
public static int factorial (int num)
{
//Base case: factorial of 0 and 1 is 1
if (num == 0 || num == 1)
{
return 1;
}
else
{
//Recursive case: multiply the number with factorial of (num - 1)
return num * factorial (num - 1);
}
}
}
➢ Variable Description:
Variable Datatype Description
m int To read and store m value
n int To read and store n value
mFactorial int To calculate and store value of factorial of m
nFactorial int To calculate and store value of factorial of n
nMinusMFactorial int To calculate and store value of (n-m) factorial
s double To calculate and store the value of s
num int To calculate the factorial of a number using
recursion

➢ Output:
FUNCTION OVERLOADING

1. Write a program in Java to find the roots of a quadratic equation


ax2+bx+c=0 with the following specifications:
Class name — Quad
Data Members — float a, b, c, d (a, b, c is the coefficients & d is the
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=b2-4ac
If d < 0 then print "Roots not possible" otherwise find and print:
x1 = (-b + √d) / 2a
x2 = (-b - √d) / 2a

➢ Algorithm:

1. Start the program.

2. Declare a class named "Quad".

3. Inside the class, declare variables "a", "b", "c", "d", "x1", and "x2" as floats.

4. Create a constructor for the "Quad" class that accepts three float parameters "x",
"y", and "z".

- Initialize the "a" variable with the value of "x".

- Initialize the "b" variable with the value of "y".

- Initialize the "c" variable with the value of "z".

- Initialize the "d" variable with 0.

5. Create a method named "calculate" without any parameters.

- Calculate the discriminant "d" using the formula: d = (b * b) - (4 * a * c).

- Check if "d" is less than 0.


- If true, print "Roots not possible".

- If false, calculate the roots using the quadratic formula:

- x1 = (-b + sqrt(d)) / (2 * a)

- x2 = (-b - sqrt(d)) / (2 * a)

- Print the values of "x1" and "x2".

6. Create the main method (public static void main) with the parameter "String args[]".

- Create a Scanner object to read user input.

- Prompt the user to enter the value of "a" and read the float value into variable "x".

- Prompt the user to enter the value of "b" and read the float value into variable "y".

- Prompt the user to enter the value of "c" and read the float value into variable "z".

- Create an object of the "Quad" class using the values of "x", "y", and "z".

- Call the "calculate" method on the "obj" object.

7. End the program.

import java.util.*;
class Quad
{
float a, b, c, d, x1, x2;
public Quad (float x, float y, float z)
{
//to initialize a=x, b=y, c=z, d=0
a = x;
b = y;
c = z;
d = 0;
}
public void calculate()
{
//To calculate the roots of the equation
d= (b * b) - (4 * a * c);
if (d < 0)
System.out.println("Roots not possible");
else
{
x1 = (float)((-b + Math.sqrt(d)) / (2 * a));
x2 = (float)((-b - Math.sqrt(d)) / (2 * a));
System.out.println("x1=" + x1);
System.out.println("x2=" + x2);
}
}
public static void main(String args[])
{
//Main method to accept values of a, b, c
Scanner sc = new Scanner(System.in);
System.out.print("Enter a: ");
float x = sc.nextFloat();
System.out.print("Enter b: ");
float y = sc.nextFloat();
System.out.print("Enter c: ");
float z = sc.nextFloat();
Quad obj = new Quad(x, y, z);
obj.calculate();
}
}
➢ Variable Description:
Variable Datatype Description
a float To read and store the value of a from the equation
b float To read and store the value of b from the equation
c float To read and store the value of c from the equation
d float To calculate the value of discriminant using a, b, c
x1 float To calculate the root1 from discriminant
x2 float To calculate the root2 from discriminant

➢ Output:
2. Define a class Discount having the following description:
Class name: Discount
Data Members:
int cost - to store the price of an article
String name - to store the customer's name
double dc - to store the discount
double amt - to store the amount to be paid
Member methods:
void input() - Stores the cost of the article and name of the customer
void cal() - Calculates the discount and amount to be paid
void display() - Displays the name of the customer, cost, discount and amount
to be paid
Write a program to compute the discount according to the given
conditions and display the output as per the given format.
List Price Rate of discount

Up to ₹5,000 No discount

From ₹5,001 to ₹10,000 10% on the list price

From ₹10,001 to ₹15,000 15% on the list price

Above ₹15,000 20% on the list price

Output:
Name of the customer Discount Amount to be paid
.................... ........ .................
.................... ........ .................
➢ Algorithm:

1. Start the program.

2. Declare a class named "Discount".

3. Inside the class, declare variables "cost" (integer), "name" (String), "dc" (double),
and "amt" (double).

4. Create a method named "input" without any parameters.

- Create a Scanner object to read user input.

- Prompt the user to enter the customer name and read the string value into the "name"
variable.

- Prompt the user to enter the article cost and read the integer value into the "cost"
variable.

5. Create a method named "cal" without any parameters.

- Check the value of "cost" using if-else statements to determine the discount amount:

- If "cost" is less than or equal to 5000, set "dc" to 0.

- If "cost" is greater than 5000 and less than or equal to 10000, calculate the
discount by multiplying "cost" by 0.1 and store the result in "dc".

- If "cost" is greater than 10000 and less than or equal to 15000, calculate the
discount by multiplying "cost" by 0.15 and store the result in "dc".

- If "cost" is greater than 15000, calculate the discount by multiplying "cost" by


0.2 and store the result in "dc".

- Calculate the amount to be paid by subtracting "dc" from "cost" and store the result
in "amt".

6. Create a method named "display" without any parameters.

- Print the following headers: "Name of the customer", "Discount", "Amount to be


paid".
- Print the values of "name", "dc", and "amt" separated by tabs.

7. Create the main method (public static void main) with the parameter "String args[]".

- Create an object of the "Discount" class named "obj".

- Call the "input" method on the "obj" object to accept customer details.

- Call the "cal" method on the "obj" object to calculate the discount and amount.

- Call the "display" method on the "obj" object to display the customer details,
discount, and amount.

8. End the program.


import java.util.*;
class Discount
{
int cost;
String name;
double dc;
double amt;
public void input()
{
//To accept details of the customer
Scanner sc = new Scanner(System.in);
System.out.print("Enter customer name: ");
name = sc.nextLine();
System.out.print("Enter article cost: ");
cost = sc.nextInt();
}
public void cal()
{
//To calculate the discount
if (cost <= 5000)
dc = 0;
else if (cost <= 10000)
dc = cost * 0.1;
else if (cost <= 15000)
dc = cost * 0.15;
else
dc = cost * 0.2;
amt = cost - dc;
}
public void display()
{
//To display the following
System.out.println("Name of the customer\tDiscount\tAmount to be
paid");
System.out.println(name + "\t" + dc + "\t" + amt);
}
public static void main(String args[])
{
Discount obj = new Discount();
obj.input();
obj.cal();
obj.display();
}
}

➢ Variable Description
Variable Datatype Description
cost int To read and store the price of an article
name String To read and store the customer’s name
dc double To calculate and store the discount
amt double To calculate and display the amount

➢ Output:
3. Bank charges interest for the vehicle loan as given below:
Number of years Rate of interest

Up to 5 years 15%

More than 5 and up to 10 years 12%

Above 10 years 10%

Write a program to model a 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 the interest
double amt - Amount to pay after given time
Member Methods:
void getdata() - To accept principal and time
void calculate() - To find interest and amount.
Interest = (Principal*Rate*Time)/100
Amount = Principal + Interest
void display() - To display interest and amount

➢ Algorithm:

1. Start the program.

2. Declare a class named "Loan".

3. Inside the class, declare variables "time" (integer), "principal" (double), "rate"
(double), "interest" (double), and "amt" (double).

4. Create a method named "getdata" without any parameters.

- Create a Scanner object to read user input.


- Prompt the user to enter the principal amount and read the double value into the
"principal" variable.

- Prompt the user to enter the time and read the integer value into the "time" variable.

5. Create a method named "calculate" without any parameters.

- Check the value of "time" using if-else statements to determine the interest rate:

- If "time" is less than or equal to 5, set "rate" to 15.0.

- If "time" is greater than 5 and less than or equal to 10, set "rate" to 12.0.

- If "time" is greater than 10, set "rate" to 10.0.

- Calculate the interest by multiplying "principal", "rate", and "time" and dividing the
result by 100.0. Store the result in "interest".

- Calculate the total amount payable by adding "principal" and "interest" and store the
result in "amt".

6. Create a method named "display" without any parameters.

- Print "Interest = " followed by the value of "interest".

- Print "Amount Payable = " followed by the value of "amt".

7. Create the main method (public static void main) with the parameter "String args[]".

- Create an object of the "Loan" class named "obj".

- Call the "getdata" method on the "obj" object to accept the loan details.

- Call the "calculate" method on the "obj" object to calculate the interest and amount
payable.

- Call the "display" method on the "obj" object to display the interest and amount
payable.

8. End the program.


import java.util.*;
class Loan
{
//To initialize the variables
int time;
double principal;
double rate;
double interest;
double amt;
public void getdata()
{
//To accept the details
Scanner sc = new Scanner(System.in);
System.out.print("Enter principal: ");
principal = sc.nextInt();
System.out.print("Enter time: ");
time = sc.nextInt();
}
public void calculate()
{
//To calculate interest and amount
if (time <= 5)
rate = 15.0;
else if (time <= 10)
rate = 12.0;
else
rate = 10.0;
interest = (principal * rate * time) / 100.0;
amt = principal + interest;
}
public void display()
{
//To display the data
System.out.println("Interest = " + interest);
System.out.println("Amount Payable = " + amt);
}
public static void main(String args[])
{
Loan obj = new Loan();
obj.getdata();
obj.calculate();
obj.display();
}
}
➢ Variable Description:
Variable Datatype Description
time int To read and store the time period
principal double To read and store the principal amount
rate double To read and store the rate of interest
interest double To calculate and display the interest
amt double To calculate and display the amount

➢ Output:
4. Define a class called Student to check whether a student is eligible for
taking admission in class XI with the following specifications:
Data Members:
String name - to store name
int mm - to store marks secured in Maths
int scm - to store marks secured in Science
double comp - to store marks secured in Computer
Member Methods:
Student( ) - parameterised constructor to initialize the data members by
accepting the details of a student
check( ) - to check the eligibility for course based on the table given below
display() - to print the eligibility by using check() function in nested form
Marks Eligibility

90% or more in all the subjects Science with Computer

Average marks 90% or more Bio-Science

Average marks 80% or more and less than 90% Science with Hindi

Write the main method to create an object of the class and call all the
member methods.

➢ Algorithm:

1. Start the program.

2. Declare a class named "Student".

3. Inside the class, declare variables "name" (String), "mm" (integer), "scm" (integer),
and "comp" (integer).

4. Create a parameterized constructor for the "Student" class that accepts four
parameters: "n" (String), "m" (integer), "sc" (integer), and "c" (integer).

- Assign the value of the "n" parameter to the "name" variable.


- Assign the value of the "m" parameter to the "mm" variable.

- Assign the value of the "sc" parameter to the "scm" variable.

- Assign the value of the "c" parameter to the "comp" variable.

5. Create a method named "check" that returns a String.

- Declare a variable "course" and initialize it with the value "Not Eligible".

- Calculate the average of the marks by adding "mm", "scm", and "comp" and
dividing the sum by 3.0.

- Check the following conditions:

- If "mm" is greater than or equal to 90, "scm" is greater than or equal to 90,
and "comp" is greater than or equal to 90, set "course" to "Science with Computer".

- Else if the average is greater than or equal to 90, set "course" to "Bio-
Science".

- Else if the average is greater than or equal to 80, set "course" to "Science with
Hindi".

- Return the value of "course".

6. Create a method named "display" that does not return anything.

- Call the "check" method and assign the returned value to a variable named
"eligibility".

- Print "Eligibility: " followed by the value of "eligibility".

7. Create the main method (public static void main) with the parameter "String args[]".

- Create a Scanner object to read user input.

- Prompt the user to enter the name and read the input into a variable named "n".

- Prompt the user to enter the marks in Maths and read the input into a variable named
"m".
- Prompt the user to enter the marks in Science and read the input into a variable
named "sc".

- Prompt the user to enter the marks in Computer and read the input into a variable
named "c".

- Create an object of the "Student" class named "obj" by passing the values of "n",
"m", "sc", and "c" to the constructor.

- Call the "display" method on the "obj" object to display the eligibility.

8. End the program.

import java.util.*;
class Student
{
String name;
int mm;
int scm;
int comp;
Student(String n, int m, int sc, int c)
{
//Parameterized constructor to initialize data members by accepting details
name = n;
mm = m;
scm = sc;
comp = c;
}
String check()
{
//To check the eligibility for the course with the following conditions:
String course = "Not Eligible";
double avg = (mm + scm + comp) / 3.0;
if (mm >= 90 && scm >= 90 && comp >= 90)
course = "Science with Computer";
else if (avg >= 90)
course = "Bio-Science";
else if (avg >= 80)
course = "Science with Hindi";
return course;
}
void display()
{
String eligibility = check();
System.out.println("Eligibility: " + eligibility);
}
public static void main(String args[])
{
//To accept the details of the student
Scanner sc = new Scanner(System.in);
System.out.print("Enter Name: ");
String n = sc.nextLine();
System.out.print("Enter Marks in Maths: ");
int m = sc.nextInt();
System.out.print("Enter Marks in Science: ");
int sc = sc.nextInt();
System.out.print("Enter Marks in Computer: ");
int c = sc.nextInt();
Student obj = new Student(n, m, sc, c);
obj.display();
}
}
➢ Variable Description:
Variable Datatype Description
name String To read and store the name of the student
mm int To read and store the marks secured in maths
scm int To read and store the marks secured in science
comp int To read and store the marks in computer
avg double To calculate and store the average marks of all subjects
course String To select course as per the average marks
eligibility String To check whether the student is eligible for respective
course or not

➢ Output:
5. 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.

➢ Algorithm:

1. Start the program.

2. Declare a class named "Salary".

3. Inside the class, declare variables "name" (String), "address" (String), "subject"
(String), "specialization" (String), "salary" (double), "incometax" (double), and
"phone" (long).

4. Create a method named "accept" that does not return anything.

- Create a Scanner object to read user input.

- Prompt the user to enter the name and read the input into the "name" variable.

- Prompt the user to enter the address and read the input into the "address" variable.

- Prompt the user to enter the subject and read the input into the "subject" variable.

- Prompt the user to enter the specialization and read the input into the "specialization"
variable.

- Prompt the user to enter the phone number and read the input into the "phone"
variable.
- Prompt the user to enter the salary and read the input into the "salary" variable.

5. Create a method named "display" that does not return anything.

- Print "Name: " followed by the value of the "name" variable.

- Print "Address: " followed by the value of the "address" variable.

- Print "Subject: " followed by the value of the "subject" variable.

- Print "Phone number: " followed by the value of the "phone" variable.

- Print "Specialization: " followed by the value of the "specialization" variable.

- Print "Monthly salary: " followed by the value of the "salary" variable.

6. Create a method named "compute" that does not return anything.

- Check if the annual salary (salary * 12) is greater than 175,000.

- If true, calculate the income tax by multiplying the annual salary by 0.05 and
assign it to the "incometax" variable.

- If false, set the "incometax" variable to 0.0.

- Print "Income Tax: " followed by the value of the "incometax" variable.

7. Create the main method (public static void main) with the parameter "String args[]".

- Create an object of the "Salary" class named "obj".

- Call the "accept" method on the "obj" object to accept the details of the teacher.

- Call the "display" method on the "obj" object to display the details of the teacher.

- Call the "compute" method on the "obj" object to calculate the income tax.

8. End the program.


import java.util.*;

class Salary

String name, address, subject, specialization;

double salary, incometax;

long phone;

void accept()

//To accept the details of the teacher

Scanner sc = new Scanner(System.in);

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

name = sc.nextLine();

System.out.print("Enter address: ");

address = sc.nextLine();

System.out.print("Enter subject: ");

subject = sc.nextLine();

System.out.print("Enter specialization: ");

specialization = sc.nextLine();

System.out.print("Enter phone number: ");

phone = sc.nextLong();

System.out.print("Enter salary: ");

salary = sc.nextDouble();

System.out.println();
}

void display()

//To display the details of the teacher

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

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

System.out.println("Subject: "+subject);

System.out.println("Phone number: "+phone);

System.out.println("Specialization: "+specialization);

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

void compute()

//To calculate income tax

if ((salary*12) > 175000)

incometax = salary* 12 * 0.05;

else

incometax = 0.0;

System.out.println("Income Tax: "+incometax);


}

public static void main(String args[])

//Main method to call the above functions

Salary obj = new Salary();

obj.accept();

obj.display();

obj.compute();

➢ Variable Description
Variable Datatype Description
name String To read and store the name of the teacher
address String To read and store the address of the teacher
phone long To read and store the phone number of the teacher
subject String To read and store the subject he/she teaches
specialization String To read and store in which field he/she is specialized
salary double To read and store the monthly salary of the teacher
incometax double To calculate and store the income tax
➢ Output:
6. 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 calculate the following salary components:
Dearness Allowance = 15% of salary
House Rent Allowance = 10% of salary
Provident Fund = 12% of salary
Gross Salary = Salary + Dearness Allowance + House Rent
Allowance
Net Salary = Gross Salary - Provident Fund
void display() - to display all details of the employee
Write a main method to create object of the class and call the member
methods.

➢ Algorithm:

1. Start the program.

2. Import the necessary packages.

3. Declare a class named "Pay".

4. Inside the class, declare variables "name" (String), "address" (String), "city"
(String), "salary" (float), "da" (double), "hra" (double), "pf" (double), "gross"
(double), and "net" (double).

5. Create a constructor for the class with parameters "n" (String), "add" (String), "cy"
(String), and "s" (float).

- Initialize the "name" variable with the value of the "n" parameter.

- Initialize the "address" variable with the value of the "add" parameter.
- Initialize the "city" variable with the value of the "cy" parameter.

- Initialize the "salary" variable with the value of the "s" parameter.

6. Create a method named "outputdata" that does not return anything.

- Print "Name: " followed by the value of the "name" variable.

- Print "Address: " followed by the value of the "address" variable.

- Print "City: " followed by the value of the "city" variable.

- Print "Salary: " followed by the value of the "salary" variable.

7. Create a method named "calculate" that does not return anything.

- Calculate the dearness allowance (da) by multiplying the salary by 0.15.

- Calculate the house rent allowance (hra) by multiplying the salary by 0.1.

- Calculate the provident fund (pf) by multiplying the salary by 0.12.

- Calculate the gross salary by adding the salary, da, and hra.

- Calculate the net salary by subtracting the pf from the gross salary.

8. Create a method named "display" that does not return anything.

- Print "Employee Name: " followed by the value of the "name" variable.

- Print "Salary: " followed by the value of the "salary" variable.

- Print "Dearness Allowance: " followed by the value of the "da" variable.

- Print "House Rent Allowance: " followed by the value of the "hra" variable.

- Print "Provident Fund: " followed by the value of the "pf" variable.

- Print "Gross Salary: " followed by the value of the "gross" variable.

- Print "Net Salary: " followed by the value of the "net" variable.

9. Create the main method (public static void main) with the parameter "String args[]".

- Create a Scanner object to read user input.


- Prompt the user to enter the name and read the input into the "n" variable.

- Prompt the user to enter the address and read the input into the "add" variable.

- Prompt the user to enter the city and read the input into the "cy" variable.

- Prompt the user to enter the salary and read the input into the "s" variable.

- Create an object of the "Pay" class named "obj" by calling the constructor with the
"n", "add", "cy", and "s" variables.

- Call the "outputdata" method on the "obj" object to display the initialized values.

- Call the "calculate" method on the "obj" object to calculate the salary components.

- Call the "display" method on the "obj" object to display all the details of the
employee.

10. End the program.

import java.util.*;
class Pay
{
String name, address, city;
float salary;
double da, hra, pf, gross, net;
public Pay(String n, String add, String cy, float s)
{
name = n;
address = add;
city = cy;
salary = s;
}
void outputdata()
{
//To display all initialized values
System.out.println("Name: "+name);
System.out.println("Address: "+address);
System.out.println("City: "+city);
System.out.println("Salary: "+salary);
System.out.println();
}
void calculate()
{
//To calculate the following salary components:
da = salary * 0.15;
hra = salary * 0.1;
pf = salary * 0.12;
gross = salary + da + hra;
net = gross - pf;
}
void display()
{
//To display all details of the employee
System.out.println("Employee Name: " + name);
System.out.println("Salary: " + salary);
System.out.println("Dearness Allowance: " + da);
System.out.println("House Rent Allowance: " + hra);
System.out.println("Provident Fund: " + pf);
System.out.println("Gross Salary: " + gross);
System.out.println("Net Salary: " + net);
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter Name: ");
String n = sc.nextLine();
System.out.print("Enter Address: ");
String add = sc.nextLine();
System.out.print("Enter City: ");
String cy = sc.nextLine();
System.out.println("Enter Salary: ");
float s = sc.nextFloat();
System.out.println();
Pay obj = new Pay(n, add, cy, s);
obj.outputdata();
obj.calculate();
obj.display();
}
}
➢ Variable Description:
Variable Datatype Description
name String To read and store the name of the employee
address String To read and store the address of the employee
city String To read and store the city where the employee lives
salary double To read and store the monthly salary of the employee
da double To calculate and store the dearness allowance
hra double To calculate and store the house rent allowance
pf double To calculate and store the provident fund
gross double To calculate and store the gross salary
net double To calculate and store the net salary
➢ Output:

You might also like