PROBLEM:
1. A department store gives discounts to all its shoes for sale. Prompt the user
to input the cash price (the price of the shoes) of a shoe and the percent
discount (discount range is from 10% - 50% only) and output the discount and
the discounted price. Use the appropriate data type.
Source code:
import java.util.*;
public class ProblemOne {
public static void main(String[] args) {
Scanner UserInput = new Scanner(System.in);
double Price, Discount, Deduction, TotalPrice;
System.out.println("PT1 - Prelims LE3: Basic Java Programming - Problem 1 \n");
System.out.print("Please enter price: ");
Price = UserInput.nextDouble();
System.out.println(" ");
System.out.print("Please enter discount: ");
Discount = UserInput.nextDouble() / 100;
if (Discount < 0.1) {
System.out.println("Discount too low. Please enter a valid discount.");
return;
else if (Discount > 0.5) {
System.out.println("Discount too high. Please enter a valid discount.");
return;
System.out.println(" ");
Deduction = Discount * Price;
TotalPrice = Price - Deduction;
System.out.println("Your total is P" + TotalPrice + "." + "You saved a total of P" + Deduction + ".");
}
Outputs:
2. You are tasked to create a program that will simulate a simple calculator.
Prompt the user to choose what arithmetic operation (i.e. +, -, *, /, or %)
he/she wants to perform, the input values and finally, display the result based
on the chosen arithmetic operation.
Source code:
import java.util.*;
public class ProblemTwo {
public static void main(String[] args) {
Scanner UserInput = new Scanner(System.in);
String Operation;
System.out.println("Please choose an operation by inputting a symbol.");
System.out.println("'+' for Addition");
System.out.println("'-' for Subtraction");
System.out.println("'*' for Multiplication");
System.out.println("'/' for Division");
System.out.println("'%' for Modular Division" + "\n");
Operation = UserInput.nextLine();
double operandOne, operandTwo, result;
switch(Operation) {
case "+":
System.out.println("ADDITION");
System.out.print("Enter first operand: ");
operandOne = UserInput.nextDouble();
System.out.print("Enter second operand: ");
operandTwo = UserInput.nextDouble();
result = operandOne + operandTwo;
System.out.println("Result = " + result);
break;
case "-":
System.out.println("SUBTRACTION");
System.out.print("Enter first operand: ");
operandOne = UserInput.nextDouble();
System.out.print("Enter second operand: ");
operandTwo = UserInput.nextDouble();
result = operandOne - operandTwo;
System.out.println("Result = " + result);
break;
case "*":
System.out.println("MULTIPLICATION");
System.out.print("Enter first operand: ");
operandOne = UserInput.nextDouble();
System.out.print("Enter second operand: ");
operandTwo = UserInput.nextDouble();
result = operandOne * operandTwo;
System.out.println("Result = " + result);
break;
case "/":
System.out.println("DIVISION");
System.out.print("Enter first operand: ");
operandOne = UserInput.nextDouble();
System.out.print("Enter second operand: ");
operandTwo = UserInput.nextDouble();
result = operandOne / operandTwo;
System.out.println("Result = " + result);
break;
case "%":
System.out.println("MODULAR DIVISION");
System.out.print("Enter first operand: ");
operandOne = UserInput.nextDouble();
System.out.print("Enter second operand: ");
operandTwo = UserInput.nextDouble();
result = operandOne % operandTwo;
System.out.println("Result = " + result);
break;
default:
System.out.println("Invalid symbol. Exiting program.");
}
Outputs:
3. Simulate a Leap Year Calculator with the following description:
A year will be a leap year if it is divisible by 4 but not by 100. If a year is
divisible by 4 and by 100, it is not a leap year unless it is also divisible by 400.
Thus, years such as 1996, 1992, 1988 and so on are leap years because
they are divisible by 4 but not by 100. For century years, the 400 rule is
important. Thus, century years 1900, 1800 and 1700 while all still divisible by
4 are also exactly divisible by 100. As they are not further divisible by 400,
they are not leap years.
Source code:
import java.util.*;
public class ProblemThree {
static boolean isLeapYear(int year) {
if (year % 4 == 0) {
if (year % 100 == 0) {
return year % 400 == 0;
} else {
return true;
}
} else {
return false;
public static void main(String[] args) {
Scanner UserInput = new Scanner(System.in);
System.out.print("Enter a year: ");
int Year = UserInput.nextInt();
if (isLeapYear(Year)) {
System.out.println(Year + " is a leap year.");
} else {
System.out.println(Year + " is not a leap year.");
}
Outputs:
4. Using the 3 loop structures (for, while and do-while), write a JAVA program
that will prompt the user to continuously input an integer number. The
program will stop accepting inputs if the user entered 0 (zero) and will then
output the sum of all input numbers. (For this example, display on your
document only one 1 screenshot per loop structure)
Source Code:
import java.util.*;
public class ProblemFour {
public static void main(String[] args) {
Scanner UserInput = new Scanner(System.in);
String Choice;
System.out.println("Choose one of the three loops to simulate by entering a respective letter.");
System.out.println("A - for Loop");
System.out.println("B - while Loop");
System.out.println("C - do-while Loop");
Choice = UserInput.nextLine();
int Sum = 0;
switch (Choice) {
case "A":
System.out.println("Enter numbers || Enter 0 to stop:");
for (int x = UserInput.nextInt(); x != 0; x = UserInput.nextInt()) {
Sum += x;
System.out.println("Sum: " + Sum);
break;
case "B":
System.out.println("Enter numbers || Enter 0 to stop:");
int x = UserInput.nextInt();
while (x != 0) {
Sum += x;
x = UserInput.nextInt();
System.out.println("Sum: " + Sum);
break;
case "C":
System.out.println("Enter numbers || Enter 0 to stop:");
do {
x = UserInput.nextInt();
Sum += x;
} while (x != 0);
System.out.println("Sum: " + Sum);
break;
default:
System.out.println("Invalid input. Exiting program.");
Outputs:
for Loop
while Loop
do-while Loop
Default: