0% found this document useful (0 votes)
110 views7 pages

Java Programming: Grade 10 - Einstein S.Y 2022-2023

This document describes two Java programs: 1. A basic calculator program that allows a user to enter two numbers and an operator to perform additions, subtractions, multiplications, and divisions, displaying the result. It can be restarted to continue calculating. 2. A truth or dare name picking program that allows users to enter names, then randomly selects a name and a random number, indicating if it's a "truth" or "dare" based on being odd or even. Both programs utilize Java concepts like classes, methods, conditionals, loops, and inputs/outputs to build interactive console applications.

Uploaded by

jayven
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views7 pages

Java Programming: Grade 10 - Einstein S.Y 2022-2023

This document describes two Java programs: 1. A basic calculator program that allows a user to enter two numbers and an operator to perform additions, subtractions, multiplications, and divisions, displaying the result. It can be restarted to continue calculating. 2. A truth or dare name picking program that allows users to enter names, then randomly selects a name and a random number, indicating if it's a "truth" or "dare" based on being odd or even. Both programs utilize Java concepts like classes, methods, conditionals, loops, and inputs/outputs to build interactive console applications.

Uploaded by

jayven
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

JAVA PROGRAMMING

BASIC CALCULATOR USING JAVA PROGRAMMING

GRADE 10 – EINSTEIN
S.Y 2022-2023

GROUP MEMBERS
Jenny Lee
Jaybrix Luis
Shekinah Mabazza
Peter John Savedra
Fiona Vega
Empress Karen Narciso
Jayreed Ivan Isaguirre
Dia Marie Roduta
Jamica Gangan
import java.util.Scanner;

public class Calculator {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean restart = true;
double result = 0;

while (restart) {
System.out.print("Enter the first number: ");
double firstNumber = scanner.nextDouble();

result = calculateResult(firstNumber, scanner);

System.out.println("Final result: " + result);

System.out.print("Enter 'x' to restart or any


other key to quit: ");
char choice = scanner.next().charAt(0);
restart = (choice == 'x');
}

scanner.close();
System.out.println("Calculator closed.");
}

private static double calculateResult(double result,


Scanner scanner) {
while (true) {
System.out.print("Enter the operator (+, -, *, /),
'e' to quit: ");
char operator = scanner.next().charAt(0);

if (operator == 'e') {
break;
}

System.out.print("Enter the next number: ");


double number = scanner.nextDouble();

switch (operator) {
case '+':
result += number;
System.out.println( result + " + " +
number);
break;
case '-':
result -= number;
System.out.println( result + " - " +
number);
break;
case '*':
result *= number;
System.out.println( result + " * " +
number);
break;
case '/':
if (number != 0) {
result /= number;
} else {
System.out.println("Cannot divide by
zero (0).");
}
System.out.println( result + " / " +
number);
break;
default:
System.out.println("Invalid operator.
Please try again.");
continue;
}

System.out.println("Current result: " + result);


}

return result;
}
}
JAVA PROGRAMMING

TRUTH OR DARE

GRADE 10 – EINSTEIN
S.Y 2022-2023

GROUP MEMBERS
Jenny Lee
Jaybrix Luis
Shekinah Mabazza
Peter John Savedra
Fiona Vega
Empress Karen Narciso
Jayreed Ivan Isaguirre
Dia Marie Roduta
Jamica Gangan
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class RandomNamePicker {


public static void main(String[] args) {
List<String> names = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter names (one per line, enter
'done' to finish):");
String name = scanner.nextLine();
while (!name.equalsIgnoreCase("done")) {
names.add(name);
name = scanner.nextLine();
}
if (names.isEmpty()) {
System.out.println("No names entered.
Exiting...");
return;
}

String randomName = getRandomName(names);


System.out.println("Randomly picked name: " +
randomName);

Random random = new Random();


int min = 1;
int max = 1000;

int randomnumber = random.nextInt(max - min) + min;

System.out.println("if the random number is odd(truth)


if even(dare)");

if (randomnumber % 2 !=0){
System.out.println( randomnumber + " is = odd =
truth");
}
else {
System.out.println( randomnumber + " is = even
= dare");
}
}

public static String getRandomName(List<String> names) {


Random random = new Random();
int index = random.nextInt(names.size());
return names.get(index);
}
}

You might also like