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

Bathala dimpul #43

Open
wants to merge 2 commits 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
46 changes: 46 additions & 0 deletions BasicCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import java.util.Scanner;

public class BasicCalculator {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Basic Calculator");
System.out.println("----------------");
System.out.print("Enter first number: ");
double num1 = scanner.nextDouble();

System.out.print("Enter an operator (+, -, *, /): ");
char operator = scanner.next().charAt(0);

System.out.print("Enter second number: ");
double num2 = scanner.nextDouble();

double result;

switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
System.out.println("Error! Division by zero.");
return;
}
break;
default:
System.out.println("Error! Invalid operator.");
return;
}

System.out.printf("The result of %.2f %c %.2f = %.2f", num1, operator, num2, result);
}
}
41 changes: 41 additions & 0 deletions NumberGuessingGame.java
Original file line number Diff line number Diff line change
@@ -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 maxAttempts = 5;
int numberToGuess = random.nextInt(100) + 1; // Random number between 1 and 100
int attempts = 0;
boolean hasGuessedCorrectly = false;

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 the number.");

while (attempts < maxAttempts) {
System.out.print("Enter your guess: ");
int playerGuess = scanner.nextInt();
attempts++;

if (playerGuess < numberToGuess) {
System.out.println("Too low! Try again.");
} else if (playerGuess > numberToGuess) {
System.out.println("Too high! Try again.");
} else {
hasGuessedCorrectly = true;
break;
}
}

if (hasGuessedCorrectly) {
System.out.println("Congratulations! You guessed the number in " + attempts + " attempts.");
} else {
System.out.println("Sorry! You've used all your attempts. The number was " + numberToGuess + ".");
}

scanner.close();
}
}
111 changes: 111 additions & 0 deletions TaskManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
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 void markAsCompleted() {
this.isCompleted = true;
}

public boolean isCompleted() {
return isCompleted;
}

public String getDescription() {
return description;
}

@Override
public String toString() {
return (isCompleted ? "[Completed] " : "[Pending] ") + description;
}
}

public class TaskManager {
private static ArrayList<Task> tasks = new ArrayList<>();
private static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
int choice;
do {
System.out.println("\nTask 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. List All Tasks");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
scanner.nextLine(); // Consume newline

switch (choice) {
case 1:
addTask();
break;
case 2:
deleteTask();
break;
case 3:
markTaskAsCompleted();
break;
case 4:
listAllTasks();
break;
case 5:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 5);
}

private static void addTask() {
System.out.print("Enter task description: ");
String description = scanner.nextLine();
tasks.add(new Task(description));
System.out.println("Task added successfully.");
}

private static void deleteTask() {
listAllTasks();
System.out.print("Enter 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.");
}
}

private static void markTaskAsCompleted() {
listAllTasks();
System.out.print("Enter 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.");
}
}

private static void listAllTasks() {
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));
}
}
}
}
}
99 changes: 99 additions & 0 deletions TemperatureConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import java.util.Scanner;

public class TemperatureConverter {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Temperature Converter");
System.out.println("----------------------");
System.out.println("Select the input temperature scale:");
System.out.println("1. Celsius");
System.out.println("2. Fahrenheit");
System.out.println("3. Kelvin");
System.out.print("Enter your choice (1-3): ");
int inputChoice = scanner.nextInt();

System.out.print("Enter the temperature to convert: ");
double inputTemperature = scanner.nextDouble();

System.out.println("Select the output temperature scale:");
System.out.println("1. Celsius");
System.out.println("2. Fahrenheit");
System.out.println("3. Kelvin");
System.out.print("Enter your choice (1-3): ");
int outputChoice = scanner.nextInt();

double convertedTemperature = 0;
switch (inputChoice) {
case 1: // Celsius
convertedTemperature = convertFromCelsius(inputTemperature, outputChoice);
break;
case 2: // Fahrenheit
convertedTemperature = convertFromFahrenheit(inputTemperature, outputChoice);
break;
case 3: // Kelvin
convertedTemperature = convertFromKelvin(inputTemperature, outputChoice);
break;
default:
System.out.println("Invalid input scale choice.");
return;
}

String inputScale = getScaleName(inputChoice);
String outputScale = getScaleName(outputChoice);
System.out.printf("The temperature %.2f %s is equal to %.2f %s.\n", inputTemperature, inputScale, convertedTemperature, outputScale);
}

private static double convertFromCelsius(double temp, int outputChoice) {
switch (outputChoice) {
case 1: // Celsius
return temp;
case 2: // Fahrenheit
return (temp * 9/5) + 32;
case 3: // Kelvin
return temp + 273.15;
default:
throw new IllegalArgumentException("Invalid output scale choice.");
}
}

private static double convertFromFahrenheit(double temp, int outputChoice) {
switch (outputChoice) {
case 1: // Celsius
return (temp - 32) * 5/9;
case 2: // Fahrenheit
return temp;
case 3: // Kelvin
return (temp - 32) * 5/9 + 273.15;
default:
throw new IllegalArgumentException("Invalid output scale choice.");
}
}

private static double convertFromKelvin(double temp, int outputChoice) {
switch (outputChoice) {
case 1: // Celsius
return temp - 273.15;
case 2: // Fahrenheit
return (temp - 273.15) * 9/5 + 32;
case 3: // Kelvin
return temp;
default:
throw new IllegalArgumentException("Invalid output scale choice.");
}
}

private static String getScaleName(int choice) {
switch (choice) {
case 1:
return "Celsius";
case 2:
return "Fahrenheit";
case 3:
return "Kelvin";
default:
return "";
}
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy