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

Lab 1

Uploaded by

nihithakonda63
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Lab 1

Uploaded by

nihithakonda63
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

23CS3353 − Object Oriented Programming through Java Lab Reg. No.

Lab Session 01
Apply fundamentals of Java Data types, Variables, Operators, and Control Statements to a
given application
Date of the Session: Time of the Session:

Pre-Lab Tasks:
1. Mention eight primitive types of data.

2. char in Java is not as same as char in C or C++. Justify

3. What is a variable? Write the basic form of a variable declaration.

4. What are the two conditions to be met for an automatic type conversion to take place?

5. What is a narrowing conversion?


B. Tech − Computer Science and Engineering (IIIrd Semester) Page No.

23CS3353 − Object Oriented Programming through Java Lab Reg. No.


In-Lab Tasks:
1. Write the Steps involved in Verification and Installation of a JDK and JRE Software, demonstrate the
setting up environment variables path and class path to JDK and JRE in a system. Develop a simple
“Hello World” program and test the compilation and execution of the program.
Test Cases:
Test Case 1: Input: Enter the commands “java and javac” in command prompt environment to check
whether system is able to recognize the commands and run the appropriate executable components.
Output:
Successful for running java and javac for compiling and executing java files.
Test Case 2:
Save, compile and run a sample java programs to check whether programs able to compile and run
java files.

Steps for Installing JDK:


Step 1: Click the below link

downloads.html
• Then scroll your page and click the link, jdk-
14.0.2_windows-x64_bin.exe
• Once you click, a pop will appear which shown below

https://www.oracle.com/java/technologies/javase-jdk14-

Step 2: Go to Downloads and Double on jdk-14.0.2_windows-x64_bin

•A pop up will appear as Do you want to allow this app to make


changes to this device?

B. Tech − Computer Science and Engineering (IIIrd Semester) Page No.


23CS3353 − Object Oriented Programming through Java Lab Reg. No.

• Give Yes
• Then installation process will begin
• Once installation is done click the button close

Step 3: Go to location where Java is installed C:\Program


Files\Java\jdk-14.0.2\bin
• Copy
the path
as shown
in the
image

Step 4: Go to This PC, Right Click


and Click on Properties

Step 5: Once you click properties, a window will appear as shown below
• Click Advanced system settings

B. Tech − Computer Science and Engineering (IIIrd Semester) Page No.


23CS3353 − Object Oriented Programming through Java Lab Reg. No.

Step 6: Once you click Advanced system settings, a window will appear
as shown below

• ClickEnvironment Variables, a window will appear as shown


below
B. Tech − Computer Science and Engineering (IIIrd Semester) Page No.

23CS3353 − Object Oriented Programming through Java Lab Reg. No.


Click new, a window will appear as shown below
• In that Enter Variable name as Path and now paste the path in
Variable value which you copied in step 3 (C:\Program
Files\Java\jdk-14.0.2\bin)
• Click Ok button

Step 7: Press Windows Key and type cmd, a window will appear like this

Step 8: Then Javac or Java, you may find that it displays various
possible options. It means your path has been set successfully.
SIMPLE PROGRAM :
class HelloWorld
{public static void main(String[] args {
System.out.println("Hello world");}}
OUTPUT :

B. Tech − Computer Science and Engineering (IIIrd Semester) Page No.


23CS3353 − Object Oriented Programming through Java Lab Reg. No.

2. Given the meal price (base cost of a meal), tip percent (the percentage of the meal price being added as
tip), and tax percent (the percentage of the meal price being added as tax) for a meal, find and print the
meal's total cost.
Note: Be sure to use precise values for your calculations, or you may end up with an incorrectly rounded
result!
Input Format
There are 3 lines of numeric input:
The first line has a double, mealCost (the cost of the meal before tax and tip).
The second line has an integer,tipPercent (the percentage of mealCost being added as tip). The
third line has an integer, taxPercent (the percentage of mealCost being added as tax).
Output Format
Print the total meal cost, where totalCost is the rounded integer result of the entire bill (mealCost with
added tax and tip).
Example:
Given: mealCost = 45 , tipPercent = 20 ,
taxPercent = 8
Calculations: tip = 45 X 20/100 = 9 tax = 45 X
8/100 = 3.6 totalCost = mealCost + tip + tax = 45 + 9
+ 3.6 = 57.6 round(totalCost) = 57
Test Cases:
Test Case 1:
Input:
Enter the details: 45 20 8 Output:
Total cost of the meal: 57
Aim:
To write a java program for finding the total meal cost.
Algorithm:
1. Start
2. Read the inputs a)mealcost as double b)tippercent as integer
c)taxpercent as integer

3. Calculate tip by tip=mealcost*tip percent*0.01


4. Calculate tax by tax=mealcost*tax percent*0.01
5. Calculate total=mealcost+tip+tax
6. Calculate total1=int(total)
7. Print total1
8. Stop

Source Code: import


java.util.*;
class Meal
{
B. Tech − Computer Science and Engineering (IIIrd Semester) Page No.

23CS3353 − Object Oriented Programming through Java Lab Reg. No.


public static void main(String args[])
{
Scanner input=new Scanner(System.in);
System.out.println("Enter the details :");
double mealcost=input.nextDouble(); int
tippercent=input.nextInt(); int
taxpercent=input.nextInt(); double
tip=mealcost*tippercent*0.01; double
tax=mealcost*taxpercent*0.01; double
total=mealcost+tip+tax;
int total1=(int)total;
System.out.println("The Total cost of the meal :" +total1);
}
}
Output:

Result:
Thus the java program for finding the total meal cost is executed successfully and got verified.

3. Consider a vending machine that offers the following options:


[1] Get gum
[2] Get chocolate
[3] Get popcorn

B. Tech − Computer Science and Engineering (IIIrd Semester) Page No.


23CS3353 − Object Oriented Programming through Java Lab Reg. No.

[4] Get juice


[5] Display total sold so far [6] Quit
Design and implement a program that continuously allows users to select from these options.
When options 1–4 are selected an appropriate message is to be displayed acknowledging their
choice. For example, when option 3 is selected the following message could be displayed: Here is
your popcorn

When option 5 is selected, the number of each type of item sold is displayed. For example:
3 items of gum sold
2 items of chocolate sold
6 items of popcorn sold
9 items of juice sold
When option 6 is chosen the program terminates. If an option other than 1–6 is entered an appropriate
error message should be displayed, such as:
Error, options 1-6 only!

Aim:
To provide an interactive and user-friendly interface for selecting
items from a vending machine.
Algorithm:
1. Start
2. Initialize counters for each item type (gum, chocolate,
popcorn, juice) to zero.
3. Use a loop to repeatedly display the menu and process user input
until the user chooses to quit.
4. Display menu [1] Get gum [2] Get chocolate [3] Get popcorn [4]
Get juice [5] Display total sold so far [6] Quit 5. Read the
user's choice (an integer between 1 and 6).
6. use a switch statement to handle each option and update the
counters accordingly.
7. When option 5 is chosen, the totals for each item are
displayed.
8. When option 6 is selected, the loop ends, and the program
terminates.
9. Stop Source Code : import java.util.*; public class
VendingMachine
{ public static void main(String[]
args)
{
Scanner scan = new Scanner(System.in);
int key,c1,c2,c3,c4;
c1=c2=c3=c4=0;
System.out.println("[1] Get gum\n[2] Get chocolate\n[3] Get
popcorn\n[4] Get juice\n[5] Display total sold so far\n[6] Quit");
while(true)
{
System.out.println("Enter your Choice
:"); key = scan.nextInt();
switch (key){ case 1:{
System.out.println("Here is your
gum"); c1++; break; }
case 2:{
System.out.println("Here is your
chocolate"); c2++;
break;
}
case 3:
{
System.out.println("Here is your
popcorn"); c3++;
break;
}
case 4:
{
System.out.println("Here is your
juice"); c4++;
break;
}
case 5:
{

B. Tech − Computer Science and Engineering (IIIrd Semester) Page No.


23CS3353 − Object Oriented Programming through Java Lab Reg. No.

System.out.println(c1+"items of gum sold");


System.out.println(c2+"items of chocolate sold");
System.out.println(c3+" items of popcorn sold");
System.out.println(c4+"items of juice sold");
break;
}
case 6: {
scan.close();
return;
}
default:
{
System.out.println("Error, options 1-6 only!");
break;
}}}}}

Output:
Post-Lab Tasks:
1. Develop a text-based game that asks the user to guess a number! The system then outputs whether the
guess was right or wrong! Let’s make the program give some help to the user! i.e. output whether the
guess was higher or lower than the secret number!
// Hint :Use (Math.random() * ((max - min) + 1)) + min from lang package
Number Entered Message to Display
3 Too Low
5 You Win!
7 Too High
8 Too High
Test Cases:
Test Case 1:
Input:
Enter the number you guess: 3 Output:
The number is too Low Test
Case 2:
Input:
Enter the number you guess: 5 Output:
You Win!
Algorithm:
1. Start
2. Define the minimum (min) and maximum (max) values for the range of
numbers. Generate a random number within this range to be the secret
number.
3. Initialize a loop to repeatedly ask the user for their guess until they
guess correctly.
4. Read the user's input (the guess).
5. Compare the user's guess to the secret number
6. If userGuess == secretNumber, print "Congratulations! You guessed the
right number!". If userGuess < secretNumber, print "Too low! Try again."
If userGuess > secretNumber, print "Too high! Try again."
7. Continue looping until the user’s guess matches secretNumber.
8. Stop

Source Code :

import java.util.Scanner; public


class Game
{ public static void main(String[]
args)
{
Scanner scanner = new
Scanner(System.in); int min = 1;
int max = 10;
int secretNumber = (int) (Math.random() * ((max - min) + 1)) +
min;
System.out.print("Enter the number you guess:
"); int userGuess = scanner.nextInt();
if (userGuess < secretNumber) {
System.out.println("The number is too Low");
} else if (userGuess > secretNumber) {
System.out.println("The number is too High");

B. Tech − Computer Science and Engineering (IIIrd Semester) Page No.


23CS3353 − Object Oriented Programming through Java Lab Reg. No.
} else {
System.out.println("You

Win!");} scanner.close();}} Output:

2. A plumber opens a savings account with $100 000 at the beginning of January. He then makes a deposit
of $1000 at the end of each month for the next 12 months (starting at the end of January). Interest is
calculated and added to his account at the end of each month (before the $1000 deposit is made). The
monthly interest rate depends on the amount A in his account at the time when interest is calculated, in the
following way: A ≤110000: 1 percent 110000 < A ≤125000: 1.5 percent
A >125000: 2 percent

Write a program which displays, for each of the 12 months, under suitable headings, the situation at the
end of the month as follows: the number of the month, the interest rate, the amount of interest and the
new balance. (Answer: values in the last row of output should be 12, 0.02, 2534.58, and 130263.78).
Algorithm:
1. Start
2. Declare initialbalance, monthly deposit, interest. initial_balance =
100000, monthly_deposit = 1000.
3. Define the monthly interest rates based on account balance.
4. For each month from 1 to 12: calculate intrest and update the balance.
5. Print the details for each month in a tabular format.
6. Stop

Source Code :
public class SavingsAccount
{ public static void main(String[]
args)
{ double initialBalance =
100000; double monthlyDeposit =
1000; int months = 12;
System.out.printf("%-10s %-12s %-15s %-15s%n", "Month",
"Interest Rate", "Interest", "New Balance");
double balance = initialBalance; for (int
month = 1; month <= months; month++)
B. Tech − Computer Science and Engineering (IIIrd Semester) Page No.
23CS3353 − Object Oriented Programming through Java Lab Reg. No.
double interestRate; if (balance <= 110000) {
interestRate = 0.01; } else if (balance <= 125000) {
interestRate = 0.015;
} else { interestRate =
0.02;} double interest = balance *
interestRate; balance += interest;
System.out.printf("%-10d %-12.2f %-15.2f %-15.2f%n",
month, interestRate, interest, balance); balance +=
monthlyDeposit;}}} OUTPUT :

Student’s Signature

(For Evaluator’s use only)


Marks Secured: _________ out of __________

Faculty Signature

You might also like