diff --git a/BasicCalculator.java b/BasicCalculator.java new file mode 100644 index 0000000..f4200eb --- /dev/null +++ b/BasicCalculator.java @@ -0,0 +1,88 @@ +import java.util.Scanner; + +public class BasicCalculator { + + // Method for addition + public static double add(double num1, double num2) { + return num1 + num2; + } + + // Method for subtraction + public static double subtract(double num1, double num2) { + return num1 - num2; + } + + // Method for multiplication + public static double multiply(double num1, double num2) { + return num1 * num2; + } + + // Method for division + public static double divide(double num1, double num2) { + if (num2 == 0) { + throw new ArithmeticException("Cannot divide by zero"); + } + return num1 / num2; + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + boolean continueCalculation = true; + + System.out.println("Basic Calculator"); + + while (continueCalculation) { + // Input for first number + System.out.print("Enter first number: "); + double num1 = scanner.nextDouble(); + + // Input for second number + System.out.print("Enter second number: "); + double num2 = scanner.nextDouble(); + + // Input for operation + System.out.print("Enter operation (+, -, *, /): "); + char operation = scanner.next().charAt(0); + + double result; + + // Switch case to handle different operations + switch (operation) { + case '+': + result = add(num1, num2); + break; + case '-': + result = subtract(num1, num2); + break; + case '*': + result = multiply(num1, num2); + break; + case '/': + try { + result = divide(num1, num2); + } catch (ArithmeticException e) { + System.out.println(e.getMessage()); + continue; + } + break; + default: + System.out.println("Invalid operation"); + continue; + } + + // Output the result + System.out.println("The result is: " + result); + + // Ask if the user wants to perform another calculation + System.out.print("Do you want to perform another calculation? (yes/no): "); + String continueInput = scanner.next(); + if (!continueInput.equalsIgnoreCase("yes")) { + continueCalculation = false; + } + } + + // Close the scanner + scanner.close(); + System.out.println("Calculator closed."); + } +} diff --git a/NumberGuessingGame.java b/NumberGuessingGame.java new file mode 100644 index 0000000..f9b6082 --- /dev/null +++ b/NumberGuessingGame.java @@ -0,0 +1,60 @@ +import java.util.Random; +import java.util.Scanner; + +public class NumberGuessingGame { + + public static void main(String[] args) { + // Create a Scanner object for reading input from the console + Scanner scanner = new Scanner(System.in); + + // Create a Random object for generating random numbers + Random random = new Random(); + + // Generate a random number between 1 and 100 + int numberToGuess = random.nextInt(100) + 1; + + // Set the maximum number of attempts + int maxAttempts = 10; + + // Initialize the number of attempts used + int attempts = 0; + + // Variable to track if the user has guessed the number + boolean hasGuessedCorrectly = false; + + // Game introduction + 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 it."); + + // Game loop + while (attempts < maxAttempts) { + // Increment the number of attempts + attempts++; + + // Prompt the user for a guess + System.out.print("Attempt " + attempts + ": Enter your guess: "); + int userGuess = scanner.nextInt(); + + // Check if the user's guess is correct + if (userGuess == numberToGuess) { + hasGuessedCorrectly = true; + break; + } else if (userGuess < numberToGuess) { + System.out.println("Your guess is too low."); + } else { + System.out.println("Your guess is too high."); + } + } + + // Check if the user guessed the number correctly + if (hasGuessedCorrectly) { + System.out.println("Congratulations! You've guessed the number correctly in " + attempts + " attempts."); + } else { + System.out.println("Sorry, you've used all " + maxAttempts + " attempts. The number was " + numberToGuess + "."); + } + + // Close the scanner + scanner.close(); + } +} diff --git a/TemperatureConverter.java b/TemperatureConverter.java new file mode 100644 index 0000000..bc063d2 --- /dev/null +++ b/TemperatureConverter.java @@ -0,0 +1,41 @@ +import java.util.Scanner; + +public class TemperatureConverter { + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + System.out.println("Enter the temperature to convert: "); + double temperature = scanner.nextDouble(); + + System.out.println("Enter the scale to convert from (C, F, or K): "); + String scaleFrom = scanner.next(); + + System.out.println("Enter the scale to convert to (C, F, or K): "); + String scaleTo = scanner.next(); + + double convertedTemperature = convertTemperature(temperature, scaleFrom, scaleTo); + + System.out.println("The converted temperature is: " + convertedTemperature); + } + + public static double convertTemperature(double temperature, String scaleFrom, String scaleTo) { + double convertedTemperature = 0; + + if (scaleFrom.equals("C") && scaleTo.equals("F")) { + convertedTemperature = (temperature * 9/5) + 32; + } else if (scaleFrom.equals("C") && scaleTo.equals("K")) { + convertedTemperature = temperature + 273.15; + } else if (scaleFrom.equals("F") && scaleTo.equals("C")) { + convertedTemperature = (temperature - 32) * 5/9; + } else if (scaleFrom.equals("F") && scaleTo.equals("K")) { + convertedTemperature = (temperature - 32) * 5/9 + 273.15; + } else if (scaleFrom.equals("K") && scaleTo.equals("C")) { + convertedTemperature = temperature - 273.15; + } else if (scaleFrom.equals("K") && scaleTo.equals("F")) { + convertedTemperature = (temperature - 273.15) * 9/5 + 32; + } + + return convertedTemperature; + } +} \ No newline at end of file diff --git a/ToDoList.java b/ToDoList.java new file mode 100644 index 0000000..f56fa8a --- /dev/null +++ b/ToDoList.java @@ -0,0 +1,113 @@ +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() { + this.isCompleted = true; + } + + @Override + public String toString() { + return (isCompleted ? "[X] " : "[ ] ") + description; + } +} + +public class ToDoList { + private ArrayList tasks; + + public ToDoList() { + 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 task number"); + } + } + + public void markTaskAsCompleted(int index) { + if (index >= 0 && index < tasks.size()) { + tasks.get(index).markAsCompleted(); + } else { + System.out.println("Invalid task number"); + } + } + + public void displayTasks() { + if (tasks.isEmpty()) { + System.out.println("No tasks in the list."); + } 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(); + boolean exit = false; + + while (!exit) { + System.out.println("\nTo-Do List:"); + toDoList.displayTasks(); + System.out.println("\nOptions:"); + System.out.println("1. Add Task"); + System.out.println("2. Delete Task"); + System.out.println("3. Mark Task as Completed"); + System.out.println("4. Exit"); + System.out.print("Choose an option: "); + + int choice = scanner.nextInt(); + scanner.nextLine(); // Consume the newline + + switch (choice) { + case 1: + System.out.print("Enter task description: "); + String description = scanner.nextLine(); + toDoList.addTask(description); + break; + case 2: + System.out.print("Enter task number to delete: "); + int deleteIndex = scanner.nextInt() - 1; + toDoList.deleteTask(deleteIndex); + break; + case 3: + System.out.print("Enter task number to mark as completed: "); + int completeIndex = scanner.nextInt() - 1; + toDoList.markTaskAsCompleted(completeIndex); + break; + case 4: + exit = true; + break; + default: + System.out.println("Invalid choice. Please try again."); + } + } + + scanner.close(); + System.out.println("To-Do List application closed."); + } +}