Lab 1
Lab 1
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.
4. What are the two conditions to be met for an automatic type conversion to take place?
https://www.oracle.com/java/technologies/javase-jdk14-
• Give Yes
• Then installation process will begin
• Once installation is done click the button close
Step 5: Once you click properties, a window will appear as shown below
• Click Advanced system settings
Step 6: Once you click Advanced system settings, a window will appear
as shown below
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 :
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
Result:
Thus the java program for finding the total meal cost is executed successfully and got verified.
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:
{
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 :
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
Faculty Signature