LPU Java Unit 2 Lecture 12 RB | LPUCOLAB

LPU Java Unit 2 Lecture 12 RB | LPUCOLAB

LPU_Java_Unit 2_Lecture 12_RB

1. Problem Statement

Preethi is working on a project to automate sales tax calculations for items in a store. She wants to create a program that takes the price of an item and the sales tax rate as input and calculates the final price of the item after applying the sales tax.

Write a program that handles both integer and double inputs using an overloaded method named calculateFinalPrice and print the final price of the item.

Formula Used:
Final price = price + ((price * sales tax rate) / 100)

Input format:
The first two lines of input consist of two integers a and b, representing the price of the item and the sales tax rate.
The third and fourth lines consist of two double values m and n, representing the price of the item and the sales tax rate.

Output format:
The first line of output prints an integer, representing the final price of the item after applying the sales tax for integer inputs (a and b).
The second line prints a double value, representing the final price of the item after applying the sales tax for double-value inputs (m and n), rounded to two decimal places.

Solution:

class SalesTaxCalculator {
    public static int calculateFinalPrice(int price, int taxRate) {
        return (int)(price + ((price * taxRate) / 100.0));
    }

    public static double calculateFinalPrice(double price, double taxRate) {
        return price + ((price * taxRate) / 100.0);
    }
}

2. Problem Statement

Bob has been tasked with creating a program to calculate and display the circumference and area of the circle.

The program should allow Bob to input the radius of a circle as both an integer and a double and compute both the circumference and area of the circle using separate overloaded methods:

  • calculateCircumference - To calculate the circumference using the formula 2 * 3.14 * radius
  • calculateArea - To calculate the area 3.14 * radius * radius

Write a program to help Bob.

Input format:
The first line of input consists of an integer m, representing the radius of the circle as a whole number.
The second line consists of a double value n, representing the radius of the circle as a decimal number.

Output format:
The first line of output displays two space-separated double values, rounded to two decimal places, representing the circumference of the circle with the integer radius and the double radius, respectively.
The second line displays two space-separated double values, rounded to two decimal places, representing the area of the circle with the integer radius and the double radius, respectively.

Solution:

class CircleUtils {
    public static double calculateCircumference(int radius) {
        return 2 * 3.14 * radius;
    }

    public static double calculateCircumference(double radius) {
        return 2 * 3.14 * radius;
    }

    public static double calculateArea(int radius) {
        return 3.14 * radius * radius;
    }

    public static double calculateArea(double radius) {
        return 3.14 * radius * radius;
    }
}

3. Problem Statement

Anu is tasked with creating a program to determine whether a given number or a range of numbers falls into one of two categories: prime numbers or Fibonacci numbers. There are two constructors to achieve this task:

Single Number Checker

  • Constructor: PrimeFibonacciChecker(int number, String type)
  • Input: A single integer number and a type ("prime" or "fibonacci").
  • Output: Determine if the provided number is a prime number or part of the Fibonacci series.

Range Checker

  • Constructor: PrimeFibonacciChecker(int start, int end, String type)
  • Input: A range of integers defined by a start and end value, along with a type ("prime" or "fibonacci").
  • Output: Find all numbers within the given range that are prime numbers or part of the Fibonacci series.

Input format:
The first line contains either a single integer x (for a single number check) or two integers start and end separated by a space (for a range check).
The second line contains a string type, which can be either "prime" or "fibonacci," indicating the type of check to perform.

Output format:
For a single number check: - If the type is "prime," the program outputs whether the given number is a prime number or not. - If the type is "fibonacci," the program outputs whether the given number is part of the Fibonacci series or not.

For a range check: - If the type is "prime," the program outputs all prime numbers within the specified range. - If the type is "fibonacci," the program outputs all Fibonacci numbers within the specified range.

If an invalid type is specified, the program outputs Invalid.

Code constraints:
1 ≤ x ≤ 100

Solution:

class PrimeFibonacciChecker {
    public PrimeFibonacciChecker(int number, String type) {
        if (type.equals("prime")) {
            if (isPrime(number)) {
                System.out.println(number + " is a prime number.");
            } else {
                System.out.println(number + " is not a prime number.");
            }
        } else if (type.equals("fibonacci")) {
            if (isFibonacci(number)) {
                System.out.println(number + " is part of the Fibonacci series.");
            } else {
                System.out.println(number + " is not part of the Fibonacci series.");
            }
        } else {
            System.out.println("Invalid.");
        }
    }

    public PrimeFibonacciChecker(int start, int end, String type) {
        if (type.equals("prime")) {
            List<Integer> primes = new ArrayList<>();
            for (int i = start; i <= end; i++) {
                if (isPrime(i)) {
                    primes.add(i);
                }
            }
            System.out.println("Prime numbers in the range " + start + " to " + end + ": " + primes);
        } else if (type.equals("fibonacci")) {
            List<Integer> fibonacciNumbers = new ArrayList<>();
            for (int i = start; i <= end; i++) {
                if (isFibonacci(i)) {
                    fibonacciNumbers.add(i);
                }
            }
            System.out.println("Fibonacci numbers in the range " + start + " to " + end + ": " + fibonacciNumbers);
        } else {
            System.out.println("Invalid.");
        }
    }

    private boolean isPrime(int number) {
        if (number <= 1) {
            return false;
        }
        for (int i = 2; i * i <= number; i++) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }

    private boolean isFibonacci(int number) {
        int a = 0, b = 1;
        while (b < number) {
            int temp = a + b;
            a = b;
            b = temp;
        }
        return b == number || number == 0;
    }
}