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 [Link].*;
public class ProblemOne {
public static void main(String[] args) {
Scanner UserInput = new Scanner([Link]);
double Price, Discount, Deduction, TotalPrice;
[Link]("PT1 - Prelims LE3: Basic Java Programming - Problem 1 \n");
[Link]("Please enter price: ");
Price = [Link]();
[Link](" ");
[Link]("Please enter discount: ");
Discount = [Link]() / 100;
if (Discount < 0.1) {
[Link]("Discount too low. Please enter a valid discount.");
return;
else if (Discount > 0.5) {
[Link]("Discount too high. Please enter a valid discount.");
return;
[Link](" ");
Deduction = Discount * Price;
TotalPrice = Price - Deduction;
[Link]("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 [Link].*;
public class ProblemTwo {
public static void main(String[] args) {
Scanner UserInput = new Scanner([Link]);
String Operation;
[Link]("Please choose an operation by inputting a symbol.");
[Link]("'+' for Addition");
[Link]("'-' for Subtraction");
[Link]("'*' for Multiplication");
[Link]("'/' for Division");
[Link]("'%' for Modular Division" + "\n");
Operation = [Link]();
double operandOne, operandTwo, result;
switch(Operation) {
case "+":
[Link]("ADDITION");
[Link]("Enter first operand: ");
operandOne = [Link]();
[Link]("Enter second operand: ");
operandTwo = [Link]();
result = operandOne + operandTwo;
[Link]("Result = " + result);
break;
case "-":
[Link]("SUBTRACTION");
[Link]("Enter first operand: ");
operandOne = [Link]();
[Link]("Enter second operand: ");
operandTwo = [Link]();
result = operandOne - operandTwo;
[Link]("Result = " + result);
break;
case "*":
[Link]("MULTIPLICATION");
[Link]("Enter first operand: ");
operandOne = [Link]();
[Link]("Enter second operand: ");
operandTwo = [Link]();
result = operandOne * operandTwo;
[Link]("Result = " + result);
break;
case "/":
[Link]("DIVISION");
[Link]("Enter first operand: ");
operandOne = [Link]();
[Link]("Enter second operand: ");
operandTwo = [Link]();
result = operandOne / operandTwo;
[Link]("Result = " + result);
break;
case "%":
[Link]("MODULAR DIVISION");
[Link]("Enter first operand: ");
operandOne = [Link]();
[Link]("Enter second operand: ");
operandTwo = [Link]();
result = operandOne % operandTwo;
[Link]("Result = " + result);
break;
default:
[Link]("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 [Link].*;
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([Link]);
[Link]("Enter a year: ");
int Year = [Link]();
if (isLeapYear(Year)) {
[Link](Year + " is a leap year.");
} else {
[Link](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 [Link].*;
public class ProblemFour {
public static void main(String[] args) {
Scanner UserInput = new Scanner([Link]);
String Choice;
[Link]("Choose one of the three loops to simulate by entering a respective letter.");
[Link]("A - for Loop");
[Link]("B - while Loop");
[Link]("C - do-while Loop");
Choice = [Link]();
int Sum = 0;
switch (Choice) {
case "A":
[Link]("Enter numbers || Enter 0 to stop:");
for (int x = [Link](); x != 0; x = [Link]()) {
Sum += x;
[Link]("Sum: " + Sum);
break;
case "B":
[Link]("Enter numbers || Enter 0 to stop:");
int x = [Link]();
while (x != 0) {
Sum += x;
x = [Link]();
[Link]("Sum: " + Sum);
break;
case "C":
[Link]("Enter numbers || Enter 0 to stop:");
do {
x = [Link]();
Sum += x;
} while (x != 0);
[Link]("Sum: " + Sum);
break;
default:
[Link]("Invalid input. Exiting program.");
Outputs:
for Loop
while Loop
do-while Loop
Default: