diff --git a/NumberGuessingGame.java b/NumberGuessingGame.java new file mode 100644 index 0000000..f952baa --- /dev/null +++ b/NumberGuessingGame.java @@ -0,0 +1,47 @@ +import java.util.Random; +import java.util.Scanner; + +public class NumberGuessingGame { + public static void main(String[] args) { + // Create a Random object to generate random numbers + Random random = new Random(); + + // Generate a random number between 1 and 100 + int targetNumber = random.nextInt(100) + 1; + + // Maximum number of attempts + int maxAttempts = 10; + + // Create a Scanner object for user input + Scanner scanner = new Scanner(System.in); + + System.out.println("Welcome to the Number Guessing Game!"); + System.out.println("I have generated a random number between 1 and 100."); + System.out.println("You have " + maxAttempts + " attempts to guess it."); + + // Variable to track the number of attempts used + int attempts = 0; + + while (attempts < maxAttempts) { + System.out.print("Enter your guess: "); + int guess = scanner.nextInt(); + attempts++; + + if (guess == targetNumber) { + System.out.println("Congratulations! You guessed the number in " + attempts + " attempts."); + break; + } else if (guess < targetNumber) { + System.out.println("Too low! Try again."); + } else { + System.out.println("Too high! Try again."); + } + + if (attempts == maxAttempts) { + System.out.println("Sorry, you've used all " + maxAttempts + " attempts. The correct number was " + targetNumber + "."); + } + } + + // Close the scanner + scanner.close(); + } +} diff --git a/Simple_calci.java b/Simple_calci.java new file mode 100644 index 0000000..fc497f2 --- /dev/null +++ b/Simple_calci.java @@ -0,0 +1,71 @@ +import java.util.Scanner; + +public class Simple_calci { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + boolean exit = false; + + while (!exit) { + showMenu(); + int choice = scanner.nextInt(); + switch (choice) { + case 1: + performOperation(scanner, "add"); + break; + case 2: + performOperation(scanner, "subtract"); + break; + case 3: + performOperation(scanner, "multiply"); + break; + case 4: + performOperation(scanner, "divide"); + break; + case 5: + exit = true; + System.out.println("Exiting the calculator. Goodbye!"); + break; + default: + System.out.println("Invalid choice. Please try again."); + } + } + + scanner.close(); + } + + private static void showMenu() { + System.out.println("\n--- Basic Calculator ---"); + System.out.println("1. Addition"); + System.out.println("2. Subtraction"); + System.out.println("3. Multiplication"); + System.out.println("4. Division"); + System.out.println("5. Exit"); + System.out.print("Enter your choice: "); + } + + private static void performOperation(Scanner scanner, String operation) { + System.out.print("Enter the first number: "); + double num1 = scanner.nextDouble(); + System.out.print("Enter the second number: "); + double num2 = scanner.nextDouble(); + + switch (operation) { + case "add": + System.out.printf("Result: %.2f + %.2f = %.2f\n", num1, num2, num1 + num2); + break; + case "subtract": + System.out.printf("Result: %.2f - %.2f = %.2f\n", num1, num2, num1 - num2); + break; + case "multiply": + System.out.printf("Result: %.2f * %.2f = %.2f\n", num1, num2, num1 * num2); + break; + case "divide": + if (num2 != 0) { + System.out.printf("Result: %.2f / %.2f = %.2f\n", num1, num2, num1 / num2); + } else { + System.out.println("Error: Division by zero is not allowed."); + } + break; + } + } +} diff --git a/Temp_converter.java b/Temp_converter.java new file mode 100644 index 0000000..d097426 --- /dev/null +++ b/Temp_converter.java @@ -0,0 +1,87 @@ +import java.util.Scanner; + +public class Temp_converter { + + // Method to convert Celsius to Fahrenheit + public static double celsiusToFahrenheit(double celsius) { + return (celsius * 9/5) + 32; + } + + // Method to convert Celsius to Kelvin + public static double celsiusToKelvin(double celsius) { + return celsius + 273.15; + } + + // Method to convert Fahrenheit to Celsius + public static double fahrenheitToCelsius(double fahrenheit) { + return (fahrenheit - 32) * 5/9; + } + + // Method to convert Fahrenheit to Kelvin + public static double fahrenheitToKelvin(double fahrenheit) { + return (fahrenheit - 32) * 5/9 + 273.15; + } + + // Method to convert Kelvin to Celsius + public static double kelvinToCelsius(double kelvin) { + return kelvin - 273.15; + } + + // Method to convert Kelvin to Fahrenheit + public static double kelvinToFahrenheit(double kelvin) { + return (kelvin - 273.15) * 9/5 + 32; + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + System.out.println("Temperature Converter"); + System.out.println("====================="); + System.out.println("Choose the conversion type:"); + System.out.println("1. Celsius to Fahrenheit"); + System.out.println("2. Celsius to Kelvin"); + System.out.println("3. Fahrenheit to Celsius"); + System.out.println("4. Fahrenheit to Kelvin"); + System.out.println("5. Kelvin to Celsius"); + System.out.println("6. Kelvin to Fahrenheit"); + + int choice = scanner.nextInt(); + + System.out.print("Enter the temperature value: "); + double tempValue = scanner.nextDouble(); + + double convertedValue = 0; + + switch (choice) { + case 1: + convertedValue = celsiusToFahrenheit(tempValue); + System.out.println(tempValue + " Celsius is equal to " + convertedValue + " Fahrenheit."); + break; + case 2: + convertedValue = celsiusToKelvin(tempValue); + System.out.println(tempValue + " Celsius is equal to " + convertedValue + " Kelvin."); + break; + case 3: + convertedValue = fahrenheitToCelsius(tempValue); + System.out.println(tempValue + " Fahrenheit is equal to " + convertedValue + " Celsius."); + break; + case 4: + convertedValue = fahrenheitToKelvin(tempValue); + System.out.println(tempValue + " Fahrenheit is equal to " + convertedValue + " Kelvin."); + break; + case 5: + convertedValue = kelvinToCelsius(tempValue); + System.out.println(tempValue + " Kelvin is equal to " + convertedValue + " Celsius."); + break; + case 6: + convertedValue = kelvinToFahrenheit(tempValue); + System.out.println(tempValue + " Kelvin is equal to " + convertedValue + " Fahrenheit."); + break; + default: + System.out.println("Invalid choice."); + break; + } + + scanner.close(); + } +} diff --git a/Todo_list.java b/Todo_list.java new file mode 100644 index 0000000..17b33e2 --- /dev/null +++ b/Todo_list.java @@ -0,0 +1,118 @@ +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 String getDescription() { + return description; + } + + public boolean isCompleted() { + return isCompleted; + } + + public void markAsCompleted() { + isCompleted = true; + } + + @Override + public String toString() { + return (isCompleted ? "[X] " : "[ ] ") + description; + } +} + +public class Todo_list { + private static ArrayList tasks = new ArrayList<>(); + private static Scanner scanner = new Scanner(System.in); + + public static void main(String[] args) { + boolean exit = false; + + while (!exit) { + showMenu(); + int choice = scanner.nextInt(); + scanner.nextLine(); // consume newline + switch (choice) { + case 1: + addTask(); + break; + case 2: + deleteTask(); + break; + case 3: + markTaskAsCompleted(); + break; + case 4: + viewTasks(); + break; + case 5: + exit = true; + System.out.println("Exiting the task manager. Goodbye!"); + break; + default: + System.out.println("Invalid choice. Please try again."); + } + } + + scanner.close(); + } + + private static void showMenu() { + System.out.println("\n--- Task 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. View Tasks"); + System.out.println("5. Exit"); + System.out.print("Enter your choice: "); + } + + private static void addTask() { + System.out.print("Enter the task description: "); + String description = scanner.nextLine(); + tasks.add(new Task(description)); + System.out.println("Task added successfully."); + } + + private static void deleteTask() { + System.out.print("Enter the 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. Please try again."); + } + } + + private static void markTaskAsCompleted() { + System.out.print("Enter the 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. Please try again."); + } + } + + private static void viewTasks() { + System.out.println("\n--- Your Tasks ---"); + 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)); + } + } + } +}