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

Java Simulation Exercises Overview

The document contains five simulation exercises in Java: a Student Grading System that categorizes student grades based on input marks, a Number Pattern Generator that prints a sequence of numbers in a triangular format, a Calculator Simulation for basic arithmetic operations, an Employee Salary Slip Generator that calculates gross and net salary based on basic salary, and an Electricity Bill Calculator that computes the bill based on units consumed. Each exercise utilizes a loop for continuous input until the user decides to stop. The exercises demonstrate fundamental programming concepts such as conditionals, loops, and user input handling.

Uploaded by

mehulmeghan5
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)
8 views3 pages

Java Simulation Exercises Overview

The document contains five simulation exercises in Java: a Student Grading System that categorizes student grades based on input marks, a Number Pattern Generator that prints a sequence of numbers in a triangular format, a Calculator Simulation for basic arithmetic operations, an Employee Salary Slip Generator that calculates gross and net salary based on basic salary, and an Electricity Bill Calculator that computes the bill based on units consumed. Each exercise utilizes a loop for continuous input until the user decides to stop. The exercises demonstrate fundamental programming concepts such as conditionals, loops, and user input handling.

Uploaded by

mehulmeghan5
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

SIMULATION EXERCISE NO.

1 - Student Grading System

import [Link];

public class StudentGradingSystem {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int totalStudents = 0, aCount = 0, bCount = 0, cCount = 0, dCount = 0, fCount =
0;
char choice;

do {
[Link]("Enter marks: ");
int marks = [Link]();
totalStudents++;

if (marks >= 90 && marks <= 100) {


[Link]("Grade: A");
aCount++;
} else if (marks >= 80) {
[Link]("Grade: B");
bCount++;
} else if (marks >= 70) {
[Link]("Grade: C");
cCount++;
} else if (marks >= 50) {
[Link]("Grade: D");
dCount++;
} else {
[Link]("Grade: F");
fCount++;
}

[Link]("Do you want to continue? (y/n): ");


choice = [Link]().charAt(0);
} while (choice == 'y' || choice == 'Y');

[Link]("Total Students: " + totalStudents);


[Link]("Grade A: " + aCount);
[Link]("Grade B: " + bCount);
[Link]("Grade C: " + cCount);
[Link]("Grade D: " + dCount);
[Link]("Grade F: " + fCount);
}
}

SIMULATION EXERCISE NO. 2 - Number Pattern Generator

import [Link];

public class NumberPattern {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int rows = [Link]();
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
[Link](j + " ");
}
[Link]();
}
}
}

SIMULATION EXERCISE NO. 3 - Calculator Simulation

import [Link];

public class CalculatorSimulation {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int choice;
do {
[Link]("1. Add\n2. Subtract\n3. Multiply\n4. Divide\n5. Exit");
choice = [Link]();
if (choice >= 1 && choice <= 4) {
[Link]("Enter two numbers: ");
double a = [Link]();
double b = [Link]();
switch (choice) {
case 1:
[Link]("Result: " + (a + b));
break;
case 2:
[Link]("Result: " + (a - b));
break;
case 3:
[Link]("Result: " + (a * b));
break;
case 4:
if (b != 0)
[Link]("Result: " + (a / b));
else
[Link]("Cannot divide by zero");
break;
}
}
} while (choice != 5);
}
}

SIMULATION EXERCISE NO. 4 - Employee Salary Slip Generator

import [Link];

public class EmployeeSalarySlip {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
char ch;
do {
[Link]("Enter Employee Name: ");
String name = [Link]();
[Link]("Enter Basic Salary: ");
double basic = [Link]();
double hra = 0.2 * basic;
double da = 0.3 * basic;
double gross = basic + hra + da;
double tax = (gross >= 50000) ? 0.1 * gross : 0.05 * gross;
double net = gross - tax;
[Link]("Employee: " + name);
[Link]("Gross Salary: " + gross);
[Link]("Tax: " + tax);
[Link]("Net Salary: " + net);
[Link]("Do you want to continue (y/n)? ");
ch = [Link]().charAt(0);
} while (ch == 'y' || ch == 'Y');
}
}

SIMULATION EXERCISE NO. 5 - Electricity Bill Calculator

import [Link];

public class ElectricityBill {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
char ch;
do {
[Link]("Enter units consumed: ");
int units = [Link]();
double bill = 0;
if (units <= 100) {
bill = units * 2;
} else if (units <= 200) {
bill = 100 * 2 + (units - 100) * 3;
} else {
bill = 100 * 2 + 100 * 3 + (units - 200) * 5;
}
[Link]("Total Bill: Rs." + bill);
[Link]("Do you want to continue (y/n)? ");
ch = [Link]().charAt(0);
} while (ch == 'y' || ch == 'Y');
}
}

Common questions

Powered by AI

Advantages of a CLI include simplicity and low resource consumption, making it fast and efficient for repetitive tasks like data input/output or calculations without the overhead of a GUI framework. However, disadvantages are that it might be less intuitive for users unfamiliar with command lines and lacks the visual appeal or interactivity of graphical interfaces, potentially reducing usability for non-technical users .

The simulations (e.g., Student Grading System, Electricity Bill Calculator) use do-while loops to repeatedly execute tasks based on user decisions, responding to their input with prompts asking if they wish to continue. This design places control in the user's hands, facilitating on-the-fly interaction and iteration of tasks as long as the user deems necessary .

The Employee Salary Slip Generator calculates the net salary by first determining the gross salary through the sum of basic salary (provided by user), House Rent Allowance (HRA) which is 20% of the basic, and Dearness Allowance (DA) which is 30% of the basic. It then computes tax based on the gross salary: 10% for gross salary equal to or above 50,000, otherwise 5%. The net salary is gross salary minus the tax .

Enhancements to the Number Pattern Generator could include options for different pattern outputs, such as inverted triangles or pyramids, dynamically generated based on user selection. Integrating graphical output or expanding the program to allow custom symbols and size adjustments per row could also greatly enhance user engagement and program versatility .

Refactoring the Electricity Bill Calculator could involve encapsulating calculations into separate methods that accept units as parameters and return calculated bills. This modular approach would separate logic from user interaction, enhance readability, reduce redundant code, and make adjustments easier by isolating changes to specific parts of the code without affecting other functionality .

The Electricity Bill Calculator calculates the bill based on a tiered rate structure. For the first 100 units, it charges Rs. 2 per unit. For units between 101 and 200, it charges Rs. 3 per unit. Above 200 units, the rate is Rs. 5 per unit. The program adds costs at each tier incrementally to determine the total bill amount .

The use of a do-while loop in the Student Grading System allows the program to repeatedly prompt the user to enter marks and assign grades until the user chooses to stop. This loop guarantees that the block of code will execute at least once, ensuring initial user input is handled before checking if the user wishes to continue or exit the loop .

The Calculator Simulation has a switch statement that processes user choices for mathematical operations. For division, it checks whether the divisor 'b' is zero. If 'b' is zero, it outputs a message 'Cannot divide by zero' to prevent a runtime exception from dividing by zero. Otherwise, it performs the division and displays the result .

The Student Grading System uses conditional statements to determine grades based on marks entered by the user. If the marks are between 90 and 100, an 'A' is assigned. Marks between 80 and 89 receive a 'B', 70 to 79 get a 'C', 50 to 69 receive a 'D', and any mark below 50 results in an 'F' grade .

The Number Pattern Generator uses nested loops to print a pattern of numbers. It first reads an integer for the number of rows. For each row 'i', it prints numbers from 1 to 'i', incrementing the column number with each iteration. This creates a triangular number pattern where each row contains an increasing sequence of numbers starting from 1 .

You might also like