[Go to site: main page, start]

0% found this document useful (0 votes)
8 views13 pages

Java Practice Lab: Coding Exercises

The document contains a series of Java programming exercises, each with a problem statement, solution code, and example output. The exercises cover various topics such as calculating sums, user input handling, eligibility checks, grading systems, and data structures. Each program is designed to demonstrate specific programming concepts and techniques in Java.
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)
8 views13 pages

Java Practice Lab: Coding Exercises

The document contains a series of Java programming exercises, each with a problem statement, solution code, and example output. The exercises cover various topics such as calculating sums, user input handling, eligibility checks, grading systems, and data structures. Each program is designed to demonstrate specific programming concepts and techniques in Java.
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

JAVA PRACTICE LAB

1. Write a program to find sum of all integers greater than 100 and less than 200 that
are divisible by 7.

SOL: package PracticeLab;


public class I {
public static void main(String[] args) {
int sum = 0;
for (int i = 101; i < 200; i++) {
if (i % 7 == 0) {
sum += i;
}
}
[Link]("Sum of integers divisible by 7 between 101 and 199 is: "
+sum);
}
}

OUTPUT: Sum of integers divisible by 7 between 101 and 199 is: 2107

2. Write a program to ask 5 numbers from user and display count of even number and odd
number.

SOL: package PracticeLab;


import [Link];
public class II {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int evenCount = 0, oddCount = 0;
[Link]("Enter 5 integers:");
for (int i = 1; i <= 5; i++) {
[Link]("Number " + i + ": ");
int num = [Link]();
if (num % 2 == 0) {
evenCount++;
} else {
oddCount++;
}
}
[Link]("Total Even Numbers: " + evenCount);
[Link]("Total Odd Numbers: " + oddCount);
[Link]();
}
}
OUTPUT: Enter 5 integers:
Number 1: 10
Number 2: 11
Number 3: 12
Number 4: 13
Number 5: 14
Total Even Numbers: 3
Total Odd Numbers: 2

3. Write a program to ask name ,age and salary of a employee and check if his salary is less
than 30000 print salary after adding 20% along with other details.

SOL: package PracticeLab;


import [Link];
public class III{
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter employee name: ");
String name = [Link]();
[Link]("Enter employee age: ");
int age = [Link]();
[Link]("Enter employee salary: ");
double salary = [Link]();
if (salary < 30000) {
double increasedSalary = salary + (salary * 0.20);
[Link]("\n--- Employee Details ---");
[Link]("Name: " + name);
[Link]("Age: " + age);
[Link]("Original Salary: " + salary);
[Link]("Updated Salary (after 20% increase): " + increasedSalary);
} else {
[Link]("\n--- Employee Details ---");
[Link]("Name: " + name);
[Link]("Age: " + age);
[Link]("Salary: " + salary);
}
[Link]();
}
}

OUTPUT: Enter employee name: ABCD


Enter employee age: 25
Enter employee salary: 25000
--- Employee Details ---
Name: ABCD
Age: 25
Original Salary: 25000.0
Updated Salary (after 20% increase): 30000.0

4. You are tasked with writing a Java program for a library that checks if a person is eligible
to borrow a book based on their age and the book category. The rules for borrowing are as
follows:

 Children under 12 years old can only borrow children's books (category "Children").

 Teenagers between 12 and 17 years old can borrow both children's and teen's books
(categories "Children" and "Teen").

 Adults (18 years and older) can borrow books from any category.

Write a Java program that prompts the user to enter their age and the category of the book
they want to borrow. Based on this input, the program should print whether the person is
eligible to borrow the book or not.

SOL: package PracticeLab;


import [Link];
public class IV {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter your age: ");
int age = [Link]();
[Link]();
[Link]("Enter book category (Children / Teen / Adult): ");
String category = [Link]().trim();
boolean eligible = false;
if (age < 12) {
if ([Link]("Children")) {
eligible = true;
}
} else if (age >= 12 && age <= 17) {
if ([Link]("Children") || [Link]("Teen")) {
eligible = true;
}
} else if (age >= 18) {
eligible = true;
}
if (eligible) {
[Link]("You are eligible to borrow a \"" + category + "\" book.");
} else {
[Link]("You are NOT eligible to borrow a \"" + category + "\" book.");
}
[Link]();
}
}

OUTPUT: Enter your age: 22


Enter book category (Children / Teen / Adult): TEEN
You are eligible to borrow a "TEEN" book.

5. You are developing a Java program for a grading system. The program should accept a
student's score (out of 100) and determine their letter grade based on the following criteria:
 A score of 90 or above gets an "A".

 A score between 80 and 89 gets a "B".

 A score between 70 and 79 gets a "C".

 A score between 60 and 69 gets a "D".

 A score below 60 gets an "F".

Write a Java program that prompts the user to enter a score and then prints the
corresponding letter grade based on the input score.

SOL: package PracticeLab;


import [Link];
public class V {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter student's score (0 to 100): ");
int score = [Link]();
String grade;
if (score >= 90 && score <= 100) {
grade = "A";
} else if (score >= 80) {
grade = "B";
} else if (score >= 70) {
grade = "C";
} else if (score >= 60) {
grade = "D";
} else if (score >= 0) {
grade = "F";
} else {
grade = "Invalid";
}
if ([Link]("Invalid") || score > 100) {
[Link]("Invalid score. Please enter a value between 0 and 100.");
} else {
[Link]("Grade: " + grade);
}
[Link]();
}
}

OUTPUT: Enter student's score (0 to 100): 90


Grade: A

6. Write a program to print out all Armstrong numbers between 1 and 500. If sum of cubes
of each digit of the number is equal to the number itself, then the number is called an
Armstrong number.
For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 )

SOL: package PracticeLab;


public class VI{
public static void main(String[] args) {
[Link]("Armstrong numbers between 1 and 500 are:");
for (int num = 1; num <= 500; num++) {
int originalNum = num;
int sum = 0;
while (originalNum != 0) {
int digit = originalNum % 10;
sum += digit * digit * digit; // Cube of each digit
originalNum /= 10;
}
if (sum == num) {
[Link](num);
}
}
}
}

OUTPUT: Armstrong numbers between 1 and 500 are:

153
370

371

407

7. Write an algorithm to determine if a number n is happy.A is a number defined by the


following process:

 Starting with any positive integer, replace the number by the sum of the squares of
its digits.

 Repeat the process until the number equals 1 (where it will stay), or it which does
not include 1.

 Those numbers for which this process are happy.

Return true and false.

SOL: package PracticeLab;


import [Link];
public class VII {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number to check if it's happy: ");
int n = [Link]();
int original = n;
while (n != 1 && n != 4) {
n = sumOfSquares(n);
}
if (n == 1) {
[Link](original + " is a Happy Number");
} else {
[Link](original + " is NOT a Happy Number");
}
[Link]();
}
public static int sumOfSquares(int num) {
int sum = 0;
while (num > 0) {
int digit = num % 10;
sum += digit * digit;
num /= 10;
}
return sum;
}
}

OUTPUT: Enter a number to check if it's happy: 44

44 is a Happy Number

8. Given an array nums. We define a running sum of an array as runningSum[i] =


sum(nums[0]…nums[i]).Return the running sum of nums.
nums = [1,2,3,4]
[1,3,6,10]
Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
SOL: package PracticeLab;
import [Link];
public class VIII {
public static void main(String[] args) {
int[] nums = {1, 2, 3, 4};
int[] runningSum = new int[[Link]];
runningSum[0] = nums[0];
for (int i = 1; i < [Link]; i++) {
runningSum[i] = runningSum[i - 1] + nums[i];
}
[Link]("Running Sum: " + [Link](runningSum));
}
}

OUTPUT: Running Sum: [1, 3, 6, 10]

9. Wap to define a sorted array of size N and an integer K, find the position at which K is
present in the array using binary search.
Example 1:
Input:
N=5
arr[] = {1 2 3 4 5}
K=4
Output: 3
Explanation: 4 appears at index 3.
SOL: package PracticeLab;
import [Link];
public class IX {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter size of array (N): ");
int n = [Link]();
int[] arr = new int[n];
[Link]("Enter " + n + " sorted elements:");
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
}
[Link]("Enter the value of K to search: ");
int k = [Link]();
int low = 0, high = n - 1;
int position = -1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == k) {
position = mid;
break;
} else if (arr[mid] < k) {
low = mid + 1;
} else {
high = mid - 1;
}
}
if (position != -1) {
[Link]("K is present at index: " + position);
} else {
[Link]("K is not present in the array.");
}
[Link]();
}
}

OUTPUT: Enter size of array (N): 5

Enter 5 sorted elements:

10

20

30

40

50
Enter the value of K to search: 20

K is present at index: 1

10. Problem Statement: Given an array, print all the elements which are leaders. A Leader is
an element that is greater than all of the elements on its right side in the array.
Examples:
Example 1:
Input:
arr = [4, 7, 1, 0]
Output:
710
Explanation:
Rightmost element is always a leader. 7 and 1 are greater than the elements in their right
side.

SOL: package PracticeLab;


public class X {
public static void main(String[] args) {
int[] arr = {4, 7, 1, 0};
int n = [Link];
[Link]("Leader elements in the array:");
int maxFromRight = arr[n - 1];
[Link](maxFromRight + " ");
for (int i = n - 2; i >= 0; i--) {
if (arr[i] > maxFromRight) {
maxFromRight = arr[i];
[Link](maxFromRight + " ");
}
}
}
}

OUTPUT: Leader elements in the array:


017
11. Write a program to arrange a set of integer numbers in a ascending order where
input will be taken through command line argument.

SOL: package PracticeLab;


public class XI {
public static void main(String[] args) {
if ([Link] == 0) {
[Link]("Please provide integers as command line arguments.");
return;
}
int[] nums = new int[[Link]];
for (int i = 0; i < [Link]; i++) {
nums[i] = [Link](args[i]);
}
for (int i = 0; i < [Link] - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < [Link]; j++) {
if (nums[j] < nums[minIndex]) {
minIndex = j;
}
}
int temp = nums[i];
nums[i] = nums[minIndex];
nums[minIndex] = temp;
}
[Link]("Sorted numbers in ascending order:");
for (int num : nums) {
[Link](num + " ");
}
}
}

OUTPUT: Sorted numbers in ascending order:


10 12 25 32 69 74 88
12. Write a program to maintain the office database using single inheritance. Superclassis
Employee that contain the information as follows- Emp_code, Emp_name,Address, Ph_no,
Da-10%, Hra-20%. Create three subclass of Manager, Typist, officer each class
having their own basic pay & da,hra remain same. Create a menu driven application in main
and depending on choice ask details and printy them on screen .

SOL: package PracticeLab;


import [Link];
class Employee {
String empCode;
String empName;
String address;
String phNo;
double da;
double hra;
double basicPay;
double totalSalary;
void inputDetails(Scanner sc) {
[Link]("Enter Employee Code: ");
empCode = [Link]();
[Link]("Enter Employee Name: ");
empName = [Link]();
[Link]("Enter Address: ");
address = [Link]();
[Link]("Enter Phone Number: ");
phNo = [Link]();
}
void calculateSalary() {
da = 0.10 * basicPay;
hra = 0.20 * basicPay;
totalSalary = basicPay + da + hra;
}
void displayDetails(String role) {
[Link]("\n--- " + role + " Details ---");
[Link]("Employee Code: " + empCode);
[Link]("Employee Name: " + empName);
[Link]("Address: " + address);
[Link]("Phone Number: " + phNo);
[Link]("Basic Pay: " + basicPay);
[Link]("DA (10%): " + da);
[Link]("HRA (20%): " + hra);
[Link]("Total Salary: " + totalSalary);
}
}
class Manager extends Employee {
void getManagerDetails(Scanner sc) {
inputDetails(sc);
[Link]("Enter Basic Pay for Manager: ");
basicPay = [Link]([Link]());
calculateSalary();
displayDetails("Manager");
}
}
class Typist extends Employee {
void getTypistDetails(Scanner sc) {
inputDetails(sc);
[Link]("Enter Basic Pay for Typist: ");
basicPay = [Link]([Link]());
calculateSalary();
displayDetails("Typist");
}
}
class Officer extends Employee {
void getOfficerDetails(Scanner sc) {
inputDetails(sc);
[Link]("Enter Basic Pay for Officer: ");
basicPay = [Link]([Link]());
calculateSalary();
displayDetails("Officer");
}
}
public class XII{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
while (true) {
[Link]("\n1. Manager");
[Link]("2. Typist");
[Link]("3. Officer");
[Link]("4. Exit");
[Link]("Enter your choice: ");
String choice = [Link]();
switch (choice) {
case "1":
Manager m = new Manager();
[Link](sc);
break;
case "2":
Typist t = new Typist();
[Link](sc);
break;
case "3":
Officer o = new Officer();
[Link](sc);
break;
case "4":
[Link]("Exiting...");
[Link]();
return;
default:
[Link]("Invalid choice.");
}
}
}
}

OUTPUT: 1. Manager
2. Typist
3. Officer
4. Exit
Enter your choice: 1
Enter Employee Code: 123
Enter Employee Name: ABCD
Enter Address: 12PQRS
Enter Phone Number: 1234567899
Enter Basic Pay for Manager: 25000

--- Manager Details ---


Employee Code: 123
Employee Name: ABCD
Address: 12PQRS
Phone Number: 1234567899
Basic Pay: 25000.0
DA (10%): 2500.0
HRA (20%): 5000.0
Total Salary: 32500.0

You might also like