Skip to content
This repository was archived by the owner on Jun 29, 2024. It is now read-only.

Sudharshan V - easy level tasks #35

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions NumberGuessing.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
100 changes: 100 additions & 0 deletions Temp.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
116 changes: 116 additions & 0 deletions ToDoList.java
Original file line number Diff line number Diff line change
@@ -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<Task> 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();
}
}
52 changes: 52 additions & 0 deletions cal.java
Original file line number Diff line number Diff line change
@@ -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();
}
}