diff --git a/Calculator.java b/Calculator.java new file mode 100644 index 0000000..b68eb7d --- /dev/null +++ b/Calculator.java @@ -0,0 +1,48 @@ +import java.util.Scanner; + +public class Calculator { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + // Prompt user for numbers + System.out.print("Enter first number: "); + double num1 = scanner.nextDouble(); + + System.out.print("Enter second number: "); + double num2 = scanner.nextDouble(); + + // Prompt user for operation + System.out.print("Enter operation (+, -, *, /): "); + char operator = scanner.next().charAt(0); + + double result; + + // Perform operation based on operator + switch (operator) { + case '+': + result = num1 + num2; + System.out.println("Result: " + result); + break; + case '-': + result = num1 - num2; + System.out.println("Result: " + result); + break; + case '*': + result = num1 * num2; + System.out.println("Result: " + result); + break; + case '/': + if (num2 != 0) { + result = num1 / num2; + System.out.println("Result: " + result); + } else { + System.out.println("Error: Division by zero"); + } + break; + default: + System.out.println("Invalid operator"); + } + + scanner.close(); + } +} diff --git a/NumberGuessingGame.java b/NumberGuessingGame.java new file mode 100644 index 0000000..c9f1751 --- /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 minNumber = 1; + int maxNumber = 100; + int targetNumber = random.nextInt(maxNumber - minNumber + 1) + minNumber; + int maxAttempts = 5; + int attempts = 0; + boolean guessedCorrectly = false; + + System.out.println("Welcome to the Number Guessing Game!"); + System.out.println("I've picked a number between " + minNumber + " and " + maxNumber + ". Can you guess it?"); + + while (attempts < maxAttempts) { + System.out.print("Enter your guess: "); + int guess = scanner.nextInt(); + attempts++; + + if (guess == targetNumber) { + System.out.println("Congratulations! You've guessed the correct number in " + attempts + " attempts!"); + guessedCorrectly = true; + break; + } else if (guess < targetNumber) { + System.out.println("Too low! Try again."); + } else { + System.out.println("Too high! Try again."); + } + } + + if (!guessedCorrectly) { + System.out.println("Sorry, you've used all your attempts. The correct number was: " + targetNumber); + } + + scanner.close(); + } +} diff --git a/TemperatureConverter.java b/TemperatureConverter.java new file mode 100644 index 0000000..22460fd --- /dev/null +++ b/TemperatureConverter.java @@ -0,0 +1,85 @@ +import java.util.Scanner; + +public class TemperatureConverter { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + while (true) { + System.out.println("Temperature Converter"); + System.out.println("1. Celsius to Fahrenheit"); + System.out.println("2. Fahrenheit to Celsius"); + System.out.println("3. Celsius to Kelvin"); + System.out.println("4. Kelvin to Celsius"); + System.out.println("5. Fahrenheit to Kelvin"); + System.out.println("6. Kelvin to Fahrenheit"); + System.out.println("7. Exit"); + System.out.print("Enter your choice: "); + + int choice = scanner.nextInt(); + double temperature; + + switch (choice) { + case 1: + System.out.print("Enter temperature in Celsius: "); + temperature = scanner.nextDouble(); + System.out.println("Temperature in Fahrenheit: " + celsiusToFahrenheit(temperature)); + break; + case 2: + System.out.print("Enter temperature in Fahrenheit: "); + temperature = scanner.nextDouble(); + System.out.println("Temperature in Celsius: " + fahrenheitToCelsius(temperature)); + break; + case 3: + System.out.print("Enter temperature in Celsius: "); + temperature = scanner.nextDouble(); + System.out.println("Temperature in Kelvin: " + celsiusToKelvin(temperature)); + break; + case 4: + System.out.print("Enter temperature in Kelvin: "); + temperature = scanner.nextDouble(); + System.out.println("Temperature in Celsius: " + kelvinToCelsius(temperature)); + break; + case 5: + System.out.print("Enter temperature in Fahrenheit: "); + temperature = scanner.nextDouble(); + System.out.println("Temperature in Kelvin: " + fahrenheitToKelvin(temperature)); + break; + case 6: + System.out.print("Enter temperature in Kelvin: "); + temperature = scanner.nextDouble(); + System.out.println("Temperature in Fahrenheit: " + kelvinToFahrenheit(temperature)); + break; + case 7: + System.out.println("Exiting..."); + scanner.close(); + System.exit(0); + default: + System.out.println("Invalid choice"); + } + } + } + + public static double celsiusToFahrenheit(double celsius) { + return (celsius * 9 / 5) + 32; + } + + public static double fahrenheitToCelsius(double fahrenheit) { + return (fahrenheit - 32) * 5 / 9; + } + + public static double celsiusToKelvin(double celsius) { + return celsius + 273.15; + } + + public static double kelvinToCelsius(double kelvin) { + return kelvin - 273.15; + } + + public static double fahrenheitToKelvin(double fahrenheit) { + return (fahrenheit - 32) * 5 / 9 + 273.15; + } + + public static double kelvinToFahrenheit(double kelvin) { + return (kelvin - 273.15) * 9 / 5 + 32; + } +} diff --git a/ToDoList.java b/ToDoList.java new file mode 100644 index 0000000..2295978 --- /dev/null +++ b/ToDoList.java @@ -0,0 +1,113 @@ +import java.util.ArrayList; +import java.util.Scanner; + +class Task { + private String description; + private boolean completed; + + public Task(String description) { + this.description = description; + this.completed = false; + } + + public String getDescription() { + return description; + } + + public boolean isCompleted() { + return completed; + } + + public void markAsCompleted() { + this.completed = true; + } + + @Override + public String toString() { + return (completed ? "[X] " : "[ ] ") + description; + } +} + +public class ToDoList { + private ArrayList tasks; + + public ToDoList() { + this.tasks = new ArrayList<>(); + } + + public void addTask(String description) { + tasks.add(new Task(description)); + } + + public void deleteTask(int index) { + if (index >= 0 && index < tasks.size()) { + tasks.remove(index); + } else { + System.out.println("Invalid index"); + } + } + + public void markTaskAsCompleted(int index) { + if (index >= 0 && index < tasks.size()) { + tasks.get(index).markAsCompleted(); + } else { + System.out.println("Invalid index"); + } + } + + public void displayTasks() { + if (tasks.isEmpty()) { + System.out.println("No tasks"); + } else { + for (int i = 0; i < tasks.size(); i++) { + System.out.println((i + 1) + ". " + tasks.get(i)); + } + } + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + ToDoList todoList = new ToDoList(); + + while (true) { + System.out.println("\n---- ToDo List Menu ----"); + System.out.println("1. Add Task"); + System.out.println("2. Delete Task"); + System.out.println("3. Mark Task as Completed"); + System.out.println("4. Display Tasks"); + System.out.println("5. Exit"); + System.out.print("Enter your choice: "); + + int choice = scanner.nextInt(); + scanner.nextLine(); // Consume newline character + + switch (choice) { + case 1: + System.out.print("Enter task description: "); + String description = scanner.nextLine(); + todoList.addTask(description); + break; + case 2: + System.out.print("Enter index of task to delete: "); + int deleteIndex = scanner.nextInt(); + todoList.deleteTask(deleteIndex - 1); + break; + case 3: + System.out.print("Enter index of task to mark as completed: "); + int completeIndex = scanner.nextInt(); + todoList.markTaskAsCompleted(completeIndex - 1); + break; + case 4: + System.out.println("\n---- Tasks ----"); + todoList.displayTasks(); + break; + case 5: + System.out.println("Exiting..."); + scanner.close(); + System.exit(0); + default: + System.out.println("Invalid choice"); + } + } + } +}