Files
JavaForBeginners/Basics/src/main/java/com/example/Basics.java
2024-09-12 19:50:18 +02:00

156 lines
4.9 KiB
Java

package com.example;
import java.util.Scanner; //We need to importthis class to get text from the command line
public class Basics {
public static void main(String[] args) {
System.out.println("Write once, run anywhere");
System.out.println("Write once, run anywhere!");
int a = 5;
int b = 3;
int z = a%b;
System.out.println(a-z);
Scanner sc = new Scanner(System.in); //After importing the Scanner class, we need to create a Scanner object
System.out.print("Gimme your name: ");
String name = sc.nextLine();
System.out.print("Gimme your age: ");
int age = sc.nextInt();
System.out.println("Your name is: " + name);
System.out.println("Your age is: " + age);
/*
Task: Take the bill amount as input and output the corresponding tip amount, which should be 15% of the amount.
*/
//System.out.print("Gimme the amount: ");
System.out.print("Payment: ");
if (!(age < 18)) {
double amount = sc.nextDouble();
System.out.println(amount*15/100);
}else{
System.out.println("You are not old enaugh to tip!");
}
/*
You are making a program for a water sensor that should check if the water is boiling.
Take the integer temperature in Celsius as input and output "Boiling" if the temperature is above or equal to 100.
Output "Not boiling" if it's not.
*/
System.out.print("What is the waters temperature? ");
int temp = sc.nextInt();
if (temp >= 100) {
System.out.println("Boiling");
}else{
System.out.println("Not boiling");
}
if (age < 18) {
System.out.print("Give me the number of the day: ");
int day = sc.nextInt();
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Some Other day, or whatever.");
break;
}
}
/*
You are making a robot that should categorize items by their color.
Each color corresponds to a box with a specific number.
For simplicity, our program will handle 3 colors:
red goes to box #1
green goes to box #2
black goes to box #3
Your program needs to take a color as input and output the corresponding box number.
Sample Input
green
Sample Output
2
*/
System.out.print("Gimme a color: ");
Scanner sci = new Scanner(System.in);
String color = sci.nextLine();
switch (color) {
case "red":
System.out.println(1);
break;
case "green":
System.out.println(2);
break;
case "black":
System.out.println(3);
break;
default:
System.out.println("Something else!");
break;
}
/*
Given the age of a person as an input, output their age group.
Here are the age groups you need to handle:
Child: 0 to 11
Teen: 12 to 17
Adult: 18 to 64
Sample Input
42
Sample Output
Adult
*/
int ages = sc.nextInt();
if (ages > 0 && ages <= 11) {
System.out.println("Child");
}else if(ages <= 17){
System.out.println("Teen");
} else if (ages <= 64) {
System.out.println("Adult");
}
/*
Your math teacher asked you to calculate the sum of the numbers 1 to N, where N is a given number.
Task: Take an integer N from input and output the sum of the numbers 1 to N, inclusive.
Sample Input:
10
Sample Output:
55
*/
int number = sc.nextInt();
int sum = 0;
while (number > 0) {
sum += number;
number--;
}
System.out.println(sum);
/*
The factorial of a number N is equal to 1 * 2 * 3 * ... * N
For example, the factorial of 5 is 1* 2 * 3 * 4 * 5 = 120.
Create a program that takes a number from input and output the factorial of that number.
*/
long fact = 1;
int num = sc.nextInt();
for (int i = 2; i <= num; i++) {
fact *= i;
}
System.out.println(fact);
sc.close();
sci.close();
}
}