Pallavi Singh - 02 Java Programming Elements Level 1 Lab Practice (1)
Pallavi Singh - 02 Java Programming Elements Level 1 Lab Practice (1)
1. Sample Program 1 - Write a program to display Sam with Roll Number 1, Percent Marks
99.99, and the result ‘P’ indicates Pass(‘P’) or Fail (‘F’).
IMP => Follow Good Programming Practice demonstrated below in all Practice Programs
// Creating Class with name DisplayResult indicating the purpose is to display
// result. Notice the class name is a Noun.
class DisplayResult {
public static void main(String[] args) {
// Create a char variable result and assign value 'P' for pass
char result = 'P';
2. Sample Program 2 - Eric Travels from Chennai to Bangalore via Vellore. From Chennai to
Vellore distance is 156.6 km and the time taken is 4 Hours and 4 Mins and from Vellore to
1
Bangalore is 211.8 km and will take 4 Hours and 25 Mins. Compute the total distance and
total time from Chennai to Bangalore
// Create TravelComputation Class to compute the Distance and Travel Time
class TravelComputation {
2
}
3
Level 1 Practice Programs
1. Write a program to find the age of Harry if the birth year is 2000. Assume the Current Year is
2024
I/P => NONE
O/P => Harry's age in 2024 is ___
public class ComputeAge{
2. Sam’s mark in Maths is 94, Physics is 95 and Chemistry is 96 out of 100. Find the average
percent mark in PCM
I/P => NONE
O/P => Sam’s average mark in PCM is ___
public class ComputeAverageMarks{
4
Hint: 1 km = 1.6 miles
I/P => NONE
O/P => The distance ___ km in miles is ___
public class CalculateMiles{
double distanceInKilometers=10.8;
double distanceInMiles=distanceInKilometers*1.6;
}}
4. Create a program to calculate the profit and loss in number and percentage based on the
cost price of INR 129 and the selling price of INR 191.
Hint =>
a. Use a single print statement to display multiline text and variables.
b. Profit = selling price - cost price
c. Profit Percentage = profit / cost price * 100
I/P => NONE
O/P =>
The Cost Price is INR ___ and Selling Price is INR ___
The Profit is INR ___ and the Profit Percentage is ___
public class CalculateProfitLoss{
5
System.out.print("The Cost Price is INR "+cost_Price + " and Selling
Price is INR " +selling_Price+ "\n The Profit is INR " +profit+ "and
the Profit Percentage is "+profit_Percentage+" %");
}}
5. Suppose you have to divide 14 pens among 3 students equally. Write a program to find how
many pens each student will get if the pens must be divided equally. Also, find the remaining
non-distributed pens.
Hint =>
a. Use Modulus Operator (%) to find the reminder.
b. Use Division Operator to find the Quantity of pens
I/P => NONE
O/P => The Pen Per Student is ___ and the remaining pen not distributed is ___
6. The University is charging the student a fee of INR 125000 for the course. The University is
willing to offer a discount of 10%. Write a program to find the discounted amount and
discounted price the student will pay for the course.
Hint =>
a. Create a variable named fee and assign 125000 to it.
b. Create another variable discountPercent and assign 10 to it.
c. Compute discount and assign it to the discount variable.
d. Compute and print the fee you have to pay by subtracting the discount from the fee.
O/P => The discount amount is INR ___ and final discounted fee is INR ___
import java.util.Scanner;
6
int discountPercent = 10;// Discount percentage
7
8. Create a program to convert distance in kilometers to miles.
Hint =>
a. Create a variable km and assign type as double as in double km;
b. Create Scanner Object to take user input from Standard Input that is the Keyboard as in
Scanner input = new Scanner(System.in);
c. Use Scanner Object to take user input for km as in km = input.nextInt();
d. Use 1 mile = 1.6 km formulae to calculate miles and show the output
I/P => km
O/P => The total miles is ___ mile for the given ___ km
import java.util.Scanner;
double km = input.nextDouble();
9. Write a new program similar to the program # 6 but take user input for Student Fee and
University Discount
Hint =>
a. Create a variable named fee and take user input for fee.
b. Create another variable discountPercent and take user input.
c. Compute the discount and assign it to the discount variable.
d. Compute and print the fee you have to pay by subtracting the discount from the fee.
I/P => fee, discountPrecent
8
O/P => The discount amount is INR ___ and final discounted fee is INR ___
import java.util.Scanner;
10. Write a program that takes your height in centimeters and converts it into feet and inches
Hint => 1 foot = 12 inches and 1 inch = 2.54 cm
I/P => height
O/P => Your Height in cm is ___ while in feet is ___ and inches is ___
import java.util.Scanner;
9
public static void main(String[] args) {
11. Write a program to create a basic calculator that can perform addition, subtraction,
multiplication, and division. The program should ask for two numbers (floating point) and
perform all the operations
Hint =>
a. Create a variable number1 and number 2 and take user inputs.
b. Perform Arithmetic Operations of addition, subtraction, multiplication and division and
assign the result to a variable and finally print the result
I/P => number1, number2
O/P => The addition, subtraction, multiplication and division value of 2 numbers ___ and
___ is ___, ____, ____, and ___
import java.util.Scanner;
10
System.out.print("Enter first number: ");
10. Write a program that takes the base and height to find area of a triangle in square inches
and square centimeters
Hint => Area of a Triangle is ½ * base * height
I/P => base, height
O/P => Your Height in cm is ___ while in feet is ___ and inches is ___
import java.util.Scanner;
11
public static void main(String[] args) {
11. Write a program to find the side of the square whose parameter you read from user
Hint => Perimeter of Square is 4 times side
I/P => perimeter
O/P => The length of the side is ___ whose perimeter is ____
import java.util.Scanner;
12
double perimeter = input.nextDouble();
12. Write a program the find the distance in yards and miles for the distance provided by user in
feets
Hint => 1 mile = 1760 yards and 1 yard is 3 feet
I/P => distanceInFeet
O/P => Your Height in cm is ___ while in feet is ___ and inches is ___
import java.util.Scanner;
13
15. Write a program to input the unit price of an item and the quantity to be bought. Then,
calculate the total price.
Hint => NA
I/P => unitPrice, quantity
O/P => The total purchase price is INR ___ if the quantity ___ and unit price is INR ___
import java.util.Scanner;
16. Create a program to find the maximum number of handshakes among N number of
students.
Hint =>
a. Get integer input for numberOfStudents variable.
b. Use the combination = (n * (n - 1)) / 2 formula to calculate the maximum number of
possible handshakes.
14
c. Display the number of possible handshakes.
import java.util.Scanner;
15