JarantillaRA LE3 1
JarantillaRA LE3 1
1. Discount Calculator
Program Description:
This program calculates the discount and discounted price of shoes based on user input. It
prompts the user to input the shoe’s cash price and the percentage discount (between 10% to
50%). After validating the discount (%), the program calculates the discount amount and
discounted price. Finally, it displays both the discount amount and the discounted price.
Program Requirements:
Input/s: Cash price of the shoes (numeric input) and Percent discount (numeric input within the
range of 10% to 50%)
Outputs/s: Discount amount (numeric value) and Discounted price (numeric value)
Source Code
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
br.close();
}
}
1|P ag e
Output/s
2. Simple Calculator
Program Description:
This simple calculator program allows users to perform basic arithmetic operations (+, -, *, /, %).
It displays a menu allowing the user to select an operation and then prompts the user to enter
the values for which the operation should be performed. After receiving the inputs, the program
performs the intended operation and displays the outcome.
Program Requirements:
Input/s: Arithmetic operation (+, -, *, /, %) and Input values (numeric input)
Output/s: Result of the arithmetic operation (numeric value)
Source Code
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
try {
System.out.print( "Choose an arithmetic operation (+, -, *, /, %): ");
String ops = br.readLine();
2|P ag e
double result = 0;
switch (ops) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
if (num2 != 0) {
result = num1 / num2;
} else {
System.out.println("Error: Division error - denominator is zero");
return;
}
break;
case "%":
result = num1 % num2;
break;
default:
System.out.println("Error: Operation is invalid");
return;
}
Output/s
* When choosing the ‘/’ operator, and the input for the second number is zero
3|P ag e
*If the input for the arithmetic operation is not one of the available choices
Source Code
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
try {
System.out.print( "Enter a year: ");
String input = br.readLine();
int year = Integer.parseInt(input);
if (isLeapYear(year)) {
4|P ag e
System.out.printf("%d is a leap year.\n", year);
} else {
System.out.printf("%d is not a leap year.\n", year);
}
br.close();
} catch (NumberFormatException e) {
System.out.println( "Invalid input. Please enter a valid year.");
}
}
Output/s
4. Sum of Integers
Program Description:
This program demonstrates the use of three loop structures: for, while, and do-while. It
continuously prompts the user to input integer numbers until the user enters 0 (zero). Once the
input ends, the program computes the sum of all input integers. It has separate sections for each
loop structure to showcase their implementations. After each loop iteration, the program displays
the sum of the input integers.
Program Requirements:
Input/s: Integer numbers (numeric input)
Output/s: Sum of all input numbers (numeric value)
5|P ag e
Source Code
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
int sumFor = 0;
int sumWhile = 0;
int sumDoWhile = 0;
int input;
br.close();
}
6|P ag e
System.out.println("NOTE: Just enter '0' to stop");
int sum = 0;
int input;
do {
System.out.print("Enter an integer: ");
input = Integer.parseInt(reader.readLine());
sum += input;
} while (input != 0);
return sum;
}
}
Output/s
7|P ag e