From 5927ba278868148d251d0632c35f258495ff565c Mon Sep 17 00:00:00 2001 From: BathalaDimpul Date: Tue, 4 Jun 2024 20:47:39 +0530 Subject: [PATCH] easy level --- BasicCalculator.java | 46 ++++++++++++++++ NumberGuessingGame.java | 41 ++++++++++++++ TaskManager.java | 111 ++++++++++++++++++++++++++++++++++++++ TemperatureConverter.java | 99 ++++++++++++++++++++++++++++++++++ 4 files changed, 297 insertions(+) create mode 100644 BasicCalculator.java create mode 100644 NumberGuessingGame.java create mode 100644 TaskManager.java create mode 100644 TemperatureConverter.java diff --git a/BasicCalculator.java b/BasicCalculator.java new file mode 100644 index 0000000..a122195 --- /dev/null +++ b/BasicCalculator.java @@ -0,0 +1,46 @@ +import java.util.Scanner; + +public class BasicCalculator { + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + System.out.println("Basic Calculator"); + System.out.println("----------------"); + System.out.print("Enter first number: "); + double num1 = scanner.nextDouble(); + + System.out.print("Enter an operator (+, -, *, /): "); + char operator = scanner.next().charAt(0); + + System.out.print("Enter second number: "); + double num2 = scanner.nextDouble(); + + double result; + + switch (operator) { + 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 by zero."); + return; + } + break; + default: + System.out.println("Error! Invalid operator."); + return; + } + + System.out.printf("The result of %.2f %c %.2f = %.2f", num1, operator, num2, result); + } +} \ No newline at end of file diff --git a/NumberGuessingGame.java b/NumberGuessingGame.java new file mode 100644 index 0000000..1166138 --- /dev/null +++ b/NumberGuessingGame.java @@ -0,0 +1,41 @@ +import java.util.Random; +import java.util.Scanner; + +public class NumberGuessingGame { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + Random random = new Random(); + + int maxAttempts = 5; + int numberToGuess = random.nextInt(100) + 1; // Random number between 1 and 100 + int attempts = 0; + boolean hasGuessedCorrectly = false; + + System.out.println("Welcome to the Number Guessing Game!"); + System.out.println("I have selected a number between 1 and 100."); + System.out.println("You have " + maxAttempts + " attempts to guess the number."); + + while (attempts < maxAttempts) { + System.out.print("Enter your guess: "); + int playerGuess = scanner.nextInt(); + attempts++; + + if (playerGuess < numberToGuess) { + System.out.println("Too low! Try again."); + } else if (playerGuess > numberToGuess) { + System.out.println("Too high! Try again."); + } else { + hasGuessedCorrectly = true; + break; + } + } + + if (hasGuessedCorrectly) { + System.out.println("Congratulations! You guessed the number in " + attempts + " attempts."); + } else { + System.out.println("Sorry! You've used all your attempts. The number was " + numberToGuess + "."); + } + + scanner.close(); + } +} \ No newline at end of file diff --git a/TaskManager.java b/TaskManager.java new file mode 100644 index 0000000..af7e43c --- /dev/null +++ b/TaskManager.java @@ -0,0 +1,111 @@ +import java.util.ArrayList; +import java.util.Scanner; + +class Task { + private String description; + private boolean isCompleted; + + public Task(String description) { + this.description = description; + this.isCompleted = false; + } + + public void markAsCompleted() { + this.isCompleted = true; + } + + public boolean isCompleted() { + return isCompleted; + } + + public String getDescription() { + return description; + } + + @Override + public String toString() { + return (isCompleted ? "[Completed] " : "[Pending] ") + description; + } +} + +public class TaskManager { + private static ArrayList tasks = new ArrayList<>(); + private static Scanner scanner = new Scanner(System.in); + + public static void main(String[] args) { + int choice; + do { + System.out.println("\nTask Manager"); + System.out.println("1. Add Task"); + System.out.println("2. Delete Task"); + System.out.println("3. Mark Task as Completed"); + System.out.println("4. List All Tasks"); + System.out.println("5. Exit"); + System.out.print("Enter your choice: "); + choice = scanner.nextInt(); + scanner.nextLine(); // Consume newline + + switch (choice) { + case 1: + addTask(); + break; + case 2: + deleteTask(); + break; + case 3: + markTaskAsCompleted(); + break; + case 4: + listAllTasks(); + break; + case 5: + System.out.println("Exiting..."); + break; + default: + System.out.println("Invalid choice. Please try again."); + } + } while (choice != 5); + } + + private static void addTask() { + System.out.print("Enter task description: "); + String description = scanner.nextLine(); + tasks.add(new Task(description)); + System.out.println("Task added successfully."); + } + + private static void deleteTask() { + listAllTasks(); + System.out.print("Enter task number to delete: "); + int taskNumber = scanner.nextInt(); + if (taskNumber > 0 && taskNumber <= tasks.size()) { + tasks.remove(taskNumber - 1); + System.out.println("Task deleted successfully."); + } else { + System.out.println("Invalid task number."); + } + } + + private static void markTaskAsCompleted() { + listAllTasks(); + System.out.print("Enter task number to mark as completed: "); + int taskNumber = scanner.nextInt(); + if (taskNumber > 0 && taskNumber <= tasks.size()) { + tasks.get(taskNumber - 1).markAsCompleted(); + System.out.println("Task marked as completed."); + } else { + System.out.println("Invalid task number."); + } + } + + private static void listAllTasks() { + if (tasks.isEmpty()) { + System.out.println("No tasks available."); + } else { + for (int i = 0; i < tasks.size(); i++) { + System.out.println((i + 1) + ". " + tasks.get(i)); + } + } + } +} +} diff --git a/TemperatureConverter.java b/TemperatureConverter.java new file mode 100644 index 0000000..cfb1547 --- /dev/null +++ b/TemperatureConverter.java @@ -0,0 +1,99 @@ +import java.util.Scanner; + +public class TemperatureConverter { + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + System.out.println("Temperature Converter"); + System.out.println("----------------------"); + System.out.println("Select the input temperature scale:"); + System.out.println("1. Celsius"); + System.out.println("2. Fahrenheit"); + System.out.println("3. Kelvin"); + System.out.print("Enter your choice (1-3): "); + int inputChoice = scanner.nextInt(); + + System.out.print("Enter the temperature to convert: "); + double inputTemperature = scanner.nextDouble(); + + System.out.println("Select the output temperature scale:"); + System.out.println("1. Celsius"); + System.out.println("2. Fahrenheit"); + System.out.println("3. Kelvin"); + System.out.print("Enter your choice (1-3): "); + int outputChoice = scanner.nextInt(); + + double convertedTemperature = 0; + switch (inputChoice) { + case 1: // Celsius + convertedTemperature = convertFromCelsius(inputTemperature, outputChoice); + break; + case 2: // Fahrenheit + convertedTemperature = convertFromFahrenheit(inputTemperature, outputChoice); + break; + case 3: // Kelvin + convertedTemperature = convertFromKelvin(inputTemperature, outputChoice); + break; + default: + System.out.println("Invalid input scale choice."); + return; + } + + String inputScale = getScaleName(inputChoice); + String outputScale = getScaleName(outputChoice); + System.out.printf("The temperature %.2f %s is equal to %.2f %s.\n", inputTemperature, inputScale, convertedTemperature, outputScale); + } + + private static double convertFromCelsius(double temp, int outputChoice) { + switch (outputChoice) { + case 1: // Celsius + return temp; + case 2: // Fahrenheit + return (temp * 9/5) + 32; + case 3: // Kelvin + return temp + 273.15; + default: + throw new IllegalArgumentException("Invalid output scale choice."); + } + } + + private static double convertFromFahrenheit(double temp, int outputChoice) { + switch (outputChoice) { + case 1: // Celsius + return (temp - 32) * 5/9; + case 2: // Fahrenheit + return temp; + case 3: // Kelvin + return (temp - 32) * 5/9 + 273.15; + default: + throw new IllegalArgumentException("Invalid output scale choice."); + } + } + + private static double convertFromKelvin(double temp, int outputChoice) { + switch (outputChoice) { + case 1: // Celsius + return temp - 273.15; + case 2: // Fahrenheit + return (temp - 273.15) * 9/5 + 32; + case 3: // Kelvin + return temp; + default: + throw new IllegalArgumentException("Invalid output scale choice."); + } + } + + private static String getScaleName(int choice) { + switch (choice) { + case 1: + return "Celsius"; + case 2: + return "Fahrenheit"; + case 3: + return "Kelvin"; + default: + return ""; + } + } +}