0% found this document useful (0 votes)
25 views7 pages

JarantillaRA LE3 1

This document contains details of four Java programs: a discount calculator, simple calculator, leap year calculator, and a program to calculate the sum of integers using different loop structures. It provides the purpose, requirements and source code for each program.

Uploaded by

League of Ròn
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)
25 views7 pages

JarantillaRA LE3 1

This document contains details of four Java programs: a discount calculator, simple calculator, leap year calculator, and a program to calculate the sum of integers using different loop structures. It provides the purpose, requirements and source code for each program.

Uploaded by

League of Ròn
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
You are on page 1/ 7

ITCC202

Data Structures and Algorithms

NAME: Ron Ace Jarantilla DATE: February 7, 2024


COURSE CODE: ITC C202 SECTION: 203I

PT1 – Prelims LE3: Basic Java Programming

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;

public class DiscountCalculator {

public static void main(String[] args) throws IOException {


BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the cash price of the shoe: ");


float shoePrice = Float.parseFloat(br.readLine());

System.out.print("Enter the discount percentage (between 10 and 50): ");


int discountPercentage = Integer.parseInt(br.readLine());

if (discountPercentage < 10 || discountPercentage > 50) {


System.out.println("The discount percentage you entered is out of range. It should
be between 10 and 50.");
return;
}

float discountPercent = discountPercentage / 100.0f;


float discount = shoePrice * discountPercent;
float discountedPrice = shoePrice - discount;

System.out.printf("Discount: %.2f %n", discount);


System.out.printf("Discounted price: %.2f %n", discountedPrice);

br.close();
}
}

1|P ag e
Output/s

* If the discount percentage is out of the range of 10% to 50%

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;

public class SimpleCalculator {


public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

try {
System.out.print( "Choose an arithmetic operation (+, -, *, /, %): ");
String ops = br.readLine();

System.out.print("Enter the first number: ");


String input = br.readLine();
double num1 = Double.parseDouble(input);

System.out.print("Enter the second number: ");


input = br.readLine();
double num2 = Double.parseDouble(input);

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;
}

System.out.printf("Result: %.2f %s %.2f = %.2f%n", num1, ops, num2, result);


} catch (IOException | NumberFormatException e) {
System.out.println("Error: Invalid input format");
}
}
}

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

*If the input in the first or second number is not in numericform

3. Leap Year Calculator


 Program Description:
This program determines if a particular year is a leap year or not. It prompts the user to input a
year, then applies the leap year logic to determine if the input year meets the conditions for being
a leap year. If the year is divisible by 4 but not by 100, or if it is divisible by 400, it is considered a
leap year; otherwise, it is not. Finally, the program displays a message whether the input year is
a leap year or not.
 Program Requirements:
Input/s: Year (numeric input)
Output/s: Message indicating whether the input year is a leap year or not (message output)

Source Code
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class LeapYearCalculator {


public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

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.");
}
}

public static boolean isLeapYear(int year) {


return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
}

Output/s

*If the input value cannot be parsed as a valid integer

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;

public class LoopSumCalcu {

public static void main(String[] args) throws IOException {


BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int sumFor = 0;
int sumWhile = 0;
int sumDoWhile = 0;
int input;

// Using for loop


System.out.println("Using for loop:");
sumFor = calculateSumUsingForLoop(br);
System.out.printf("Sum of all input numbers (for loop): %d%n", sumFor);

// Using while loop


System.out.println("\nUsing while loop:");
sumWhile = calculateSumUsingWhileLoop(br);
System.out.printf("Sum of all input numbers (while loop): %d%n", sumWhile);

// Using do-while loop


System.out.println("\nUsing do-while loop:");
sumDoWhile = calculateSumUsingDoWhileLoop(br);
System.out.printf("Sum of all input numbers (do-while loop): %d%n", sumDoWhile);

br.close();
}

public static int calculateSumUsingForLoop(BufferedReader reader) throws IOException {


System.out.println("NOTE: Just enter '0' to stop");
int sum = 0;
for (;;) {
System.out.print("Enter an integer: ");
int input = Integer.parseInt(reader.readLine());
if (input == 0) {
break;
}
sum += input;
}
return sum;
}

public static int calculateSumUsingWhileLoop(BufferedReader reader) throws IOException {


System.out.println("NOTE: Just enter '0' to stop");
int sum = 0;
while (true) {
System.out.print("Enter an integer: ");
int input = Integer.parseInt(reader.readLine());
if (input == 0) {
break;
}
sum += input;
}
return sum;
}

public static int calculateSumUsingDoWhileLoop(BufferedReader reader) throws IOException {

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

You might also like