From f8ecb21af7c1665316b95651a3b8d77eab571104 Mon Sep 17 00:00:00 2001 From: Sudharshan2706 Date: Tue, 28 May 2024 22:22:12 +0530 Subject: [PATCH] Add files via upload --- NumberGuessing.java | 35 +++++++++++++ Temp.java | 100 ++++++++++++++++++++++++++++++++++++++ ToDoList.java | 116 ++++++++++++++++++++++++++++++++++++++++++++ cal.java | 52 ++++++++++++++++++++ 4 files changed, 303 insertions(+) create mode 100644 NumberGuessing.java create mode 100644 Temp.java create mode 100644 ToDoList.java create mode 100644 cal.java diff --git a/NumberGuessing.java b/NumberGuessing.java new file mode 100644 index 0000000..fb24495 --- /dev/null +++ b/NumberGuessing.java @@ -0,0 +1,35 @@ +import java.util.*; +import java.util.Random; +public class NumberGuessing { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + Random random = new Random(); + System.out.println("Welcome to the Number Guessing Game!"); + System.out.println("I have selected a number between 1 and 100. Try to guess it!"); + int maxAttempts = 10; + int secretNumber = random.nextInt(100) + 1; + int attempts = 0; + boolean guessedCorrectly = false; + while (attempts < maxAttempts) { + System.out.print("Enter your guess (between 1 and 100): "); + int guess = scanner.nextInt(); + attempts++; + if (guess == secretNumber) { + guessedCorrectly = true; + break; + } else if (guess < secretNumber) { + System.out.println("Too low! Try again."); + } else { + System.out.println("Too high! Try again."); + } + } + + if (guessedCorrectly) { + System.out.println("Congratulations! You've guessed the number " + secretNumber + " correctly in " + attempts + " attempts!"); + } else { + System.out.println("Sorry, you've exceeded the maximum number of attempts. The correct number was: " + secretNumber); + } + + scanner.close(); + } +} diff --git a/Temp.java b/Temp.java new file mode 100644 index 0000000..7669569 --- /dev/null +++ b/Temp.java @@ -0,0 +1,100 @@ +import java.util.Scanner; + +public class Temp { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + System.out.println("Welcome to Temperature Converter!"); + System.out.println("Enter the temperature scale you want to convert from:"); + System.out.println("1. Celsius"); + System.out.println("2. Fahrenheit"); + System.out.println("3. Kelvin"); + + System.out.print("Enter your choice (1/2/3): "); + int choiceFrom = scanner.nextInt(); + + System.out.print("Enter the temperature value: "); + double temperature = scanner.nextDouble(); + + double convertedTemperature = 0; + + switch (choiceFrom) { + case 1: + System.out.println("Convert Celsius to:"); + System.out.println("1. Fahrenheit"); + System.out.println("2. Kelvin"); + System.out.print("Enter your choice (1/2): "); + int choiceToCelsius = scanner.nextInt(); + if (choiceToCelsius == 1) { + convertedTemperature = celsiusToFahrenheit(temperature); + System.out.println(temperature + " Celsius = " + convertedTemperature + " Fahrenheit"); + } else if (choiceToCelsius == 2) { + convertedTemperature = celsiusToKelvin(temperature); + System.out.println(temperature + " Celsius = " + convertedTemperature + " Kelvin"); + } else { + System.out.println("Invalid choice!"); + } + break; + case 2: + System.out.println("Convert Fahrenheit to:"); + System.out.println("1. Celsius"); + System.out.println("2. Kelvin"); + System.out.print("Enter your choice (1/2): "); + int choiceToFahrenheit = scanner.nextInt(); + if (choiceToFahrenheit == 1) { + convertedTemperature = fahrenheitToCelsius(temperature); + System.out.println(temperature + " Fahrenheit = " + convertedTemperature + " Celsius"); + } else if (choiceToFahrenheit == 2) { + convertedTemperature = fahrenheitToKelvin(temperature); + System.out.println(temperature + " Fahrenheit = " + convertedTemperature + " Kelvin"); + } else { + System.out.println("Invalid choice!"); + } + break; + case 3: + System.out.println("Convert Kelvin to:"); + System.out.println("1. Celsius"); + System.out.println("2. Fahrenheit"); + System.out.print("Enter your choice (1/2): "); + int choiceToKelvin = scanner.nextInt(); + if (choiceToKelvin == 1) { + convertedTemperature = kelvinToCelsius(temperature); + System.out.println(temperature + " Kelvin = " + convertedTemperature + " Celsius"); + } else if (choiceToKelvin == 2) { + convertedTemperature = kelvinToFahrenheit(temperature); + System.out.println(temperature + " Kelvin = " + convertedTemperature + " Fahrenheit"); + } else { + System.out.println("Invalid choice!"); + } + break; + default: + System.out.println("Invalid choice!"); + } + + scanner.close(); + } + + public static double celsiusToFahrenheit(double celsius) { + return (celsius * 9 / 5) + 32; + } + + public static double celsiusToKelvin(double celsius) { + return celsius + 273.15; + } + + public static double fahrenheitToCelsius(double fahrenheit) { + return (fahrenheit - 32) * 5 / 9; + } + + public static double fahrenheitToKelvin(double fahrenheit) { + return (fahrenheit + 459.67) * 5 / 9; + } + + public static double kelvinToCelsius(double kelvin) { + return kelvin - 273.15; + } + + public static double kelvinToFahrenheit(double kelvin) { + return (kelvin * 9 / 5) - 459.67; + } +} diff --git a/ToDoList.java b/ToDoList.java new file mode 100644 index 0000000..b29f03f --- /dev/null +++ b/ToDoList.java @@ -0,0 +1,116 @@ +import java.util.ArrayList; +import java.util.Scanner; + +class Task { + private String name; + private boolean completed; + + public Task(String name) { + this.name = name; + this.completed = false; + } + + public String getName() { + return name; + } + + public boolean isCompleted() { + return completed; + } + + public void markCompleted() { + this.completed = true; + } +} + +public class TaskManager { + private ArrayList tasks; + private Scanner scanner; + + public TaskManager() { + tasks = new ArrayList<>(); + scanner = new Scanner(System.in); + } + + public void displayMenu() { + System.out.println("Task Manager 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. View Tasks"); + System.out.println("5. Exit"); + System.out.print("Enter your choice: "); + } + + public void addTask(String taskName) { + Task task = new Task(taskName); + tasks.add(task); + System.out.println("Task added successfully!"); + } + + public void deleteTask(int index) { + if (index >= 0 && index < tasks.size()) { + tasks.remove(index); + System.out.println("Task deleted successfully!"); + } else { + System.out.println("Invalid task index!"); + } + } + + public void markTaskCompleted(int index) { + if (index >= 0 && index < tasks.size()) { + tasks.get(index).markCompleted(); + System.out.println("Task marked as completed!"); + } else { + System.out.println("Invalid task index!"); + } + } + + public void viewTasks() { + System.out.println("Tasks:"); + for (int i = 0; i < tasks.size(); i++) { + Task task = tasks.get(i); + System.out.println((i + 1) + ". " + task.getName() + " - Completed: " + task.isCompleted()); + } + } + + public static void main(String[] args) { + TaskManager taskManager = new TaskManager(); + Scanner scanner = new Scanner(System.in); + int choice; + + do { + taskManager.displayMenu(); + choice = scanner.nextInt(); + scanner.nextLine(); // Consume newline + + switch (choice) { + case 1: + System.out.print("Enter task name: "); + String taskName = scanner.nextLine(); + taskManager.addTask(taskName); + break; + case 2: + System.out.print("Enter index of task to delete: "); + int deleteIndex = scanner.nextInt(); + taskManager.deleteTask(deleteIndex - 1); + break; + case 3: + System.out.print("Enter index of task to mark as completed: "); + int completeIndex = scanner.nextInt(); + taskManager.markTaskCompleted(completeIndex - 1); + break; + case 4: + taskManager.viewTasks(); + break; + case 5: + System.out.println("Exiting..."); + break; + default: + System.out.println("Invalid choice. Please enter a number between 1 and 5."); + } + } while (choice != 5); + + scanner.close(); + } +} diff --git a/cal.java b/cal.java new file mode 100644 index 0000000..4275280 --- /dev/null +++ b/cal.java @@ -0,0 +1,52 @@ +import java.util.Scanner; + +public class Cal { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + System.out.println("Welcome to Basic Calculator!"); + System.out.println("Enter the operation you want to perform:"); + System.out.println("1. Addition (+)"); + System.out.println("2. Subtraction (-)"); + System.out.println("3. Multiplication (*)"); + System.out.println("4. Division (/)"); + + System.out.print("Enter your choice (1/2/3/4): "); + int choice = scanner.nextInt(); + + System.out.print("Enter the first number: "); + double num1 = scanner.nextDouble(); + + System.out.print("Enter the second number: "); + double num2 = scanner.nextDouble(); + + double result = 0; + + switch (choice) { + case 1: + result = num1 + num2; + System.out.println("Result: " + num1 + " + " + num2 + " = " + result); + break; + case 2: + result = num1 - num2; + System.out.println("Result: " + num1 + " - " + num2 + " = " + result); + break; + case 3: + result = num1 * num2; + System.out.println("Result: " + num1 + " * " + num2 + " = " + result); + break; + case 4: + if (num2 != 0) { + result = num1 / num2; + System.out.println("Result: " + num1 + " / " + num2 + " = " + result); + } else { + System.out.println("Error: Division by zero!"); + } + break; + default: + System.out.println("Invalid choice!"); + } + + scanner.close(); + } +}