public class Main {
public static void main(String[] args) {
// printf() = is a method used to format output
// %[flags][width][precision][specifier-character]
String name = "Spongebob";
char firstLetter = 'S';
int age = 30;
double height = 60.5;
boolean isEmployed = true;
System.out.printf("Hello %s\n", name );
System.out.printf("Your name starts with a %c\n", firstLetter);
System.out.printf("You are %d years old\n", age);
System.out.printf("You are %f inches tall\n", height);
System.out.printf("Emloyed: %b\n", isEmployed);
System.out.printf("%s is %d years old", name, age);
}
}Forwarded from Webstrom_Tech Family🔥
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Compound interest calculator
Scanner scanner = new Scanner(System.in);
double principal;
double rate;
int timesCompounded;
int years;
double amount;
System.out.print("Enter the principal amount: ");
principal = scanner.nextDouble();
System.out.print("Enter the interest rate (in %): ");
rate = scanner.nextDouble() / 100;
System.out.print("Enter the # of times compounded per years: ");
timesCompounded = scanner.nextInt();
System.out.print("Enter the # of years: ");
years = scanner.nextInt();
amount = principal * Math.pow(1 + rate / timesCompounded, timesCompounded * years);
System.out.printf("The amount after %d years is $%.2f", years, amount);
scanner.close();
}
}
public class Main {
public static void main(String[] args) {
String name = "Password";
// int length = name.length();
// char letter = name.charAt(0);
// int index = name.indexOf(" ");
// int lastIndex = name.lastIndexOf("o");
// name = name.toUpperCase();
// name = name.toUpperCase();
// name = name.trim();
// name = name.replace("o", "a");
// if(name.isEmpty()) {
// System.out.println("Your name is Empty");
// }else {
//
// System.out.println(" Hello" + name);
// }
// if(name.contains(" ")) {
//
// System.out.println("Your name contains a space");
// }else {
// System.out.println("Your name doesn't contain any spaces");
// }
if (name.equalsIgnoreCase("password")) {
System.out.println("Your name can't be password");
}else {
System.out.println("Hello " + name);
}
}
}public class Main {
public static void main(String[] args) {
String txt = "Hello World";
System.out.println(txt.toUpperCase()); // HELLO WORLD
System.out.println(txt.toLowerCase()); // hello world
System.out.println(txt.length()); // 11
String text = "Please locate where locate occurs";
System.out.println(text.indexOf("locate")); // 7
String sample = "Hello";
System.out.println(sample.charAt(0)); // H
System.out.println(sample.charAt(4)); // o
String a = "Hello";
String b = "Hello";
System.out.println(a.equals(b)); // true
System.out.println(a.equalsIgnoreCase(b)); // true
String lyrc = " Hello World ";
System.out.println(lyrc.trim());
String txt1 = "Java is hard";
System.out.println(txt1.replace("hard", "easy"));
String txt2 = "backendDeveloper";
System.out.println(txt2.startsWith("back")); // true
System.out.println(txt2.endsWith("per")); // true
String txt3 = "";
System.out.println(txt3.isEmpty()); // true
String txt4 = "Microservice";
System.out.println(txt4.substring(0, 6)); // Micros
}
}import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// .substring() = A mehtod used to extract a portion of a string
// string.substring(start, end)
// .Substring() - method olib tashlaydi (0, 6) nishonovafotima24@gmail.com -> nishon
Scanner scanner = new Scanner(System.in);
String email;
String username;
String domain;
System.out.println("Enter your email: ");
email = scanner.nextLine();
if (email.contains("@")) {
username = email.substring(0, email.indexOf("@"));
domain = email.substring(email.indexOf("@") + 1);
System.out.println(username);
System.out.println(domain);
}else {
System.out.println("Emails must contains @");
}
scanner.close();
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// WEIGHT CONVERSION PROGRAM
Scanner scanner = new Scanner(System.in);
double weight ;
double newWeight;
int choice;
System.out.println("Weight Conversion Program");
System.out.println("1: Convert lbs to kgs");
System.out.println("2: Convert kgs to lbs");
System.out.print("Choose an option: ");
choice = scanner.nextInt();
if (choice == 1){
System.out.print("Enter the wight in lbs: ");
weight = scanner.nextDouble();
newWeight = weight * 0.453592;
System.out.printf("The new weight in kgs is: %.2f", newWeight);
}
else if (choice == 2){
System.out.print("Enter the wight in kgs: ");
weight = scanner.nextDouble();
newWeight = weight * 2.20462;
System.out.printf("The new weight in lbs is: %.2f", newWeight);
}else {
System.out.println("That was in lbs a valid choice");
}
scanner.close();
}
}
public class Main {
public static void main(String[] args) {
// ternary operator ? = Return 1 of 2 values if a condition is true
// variables = (condition) ? ifTrue : ifFalse
int income = 30000;
double textRate = (income >= 40000) ? 0.25 : 0.15;
System.out.println(textRate);
}
}import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double temp;
double newTemp;
String unit;
System.out.print("Enter the temprature: ");
temp = scanner.nextDouble();
System.out.print("Convert to Celsius to Fahrenheit? (C or F): ");
unit = scanner.next().toUpperCase();
// (condition) ? true : false
newTemp = (unit.equals("C")) ? (temp - 32) * 5 / 9 : (temp * 5 / 9) + 25;
System.out.printf("%.1f°%s", newTemp, unit);
scanner.close();
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = scanner.nextInt();
System.out.print("Do you have VIP pass? (true/false): ");
boolean hasVIPPass = scanner.nextBoolean();
System.out.print("Do you want 3D? (true/false): ");
boolean wants3D = scanner.nextBoolean();
System.out.println("\n--- Result ---");
if (age >= 18) {
if (hasVIPPass) {
if (wants3D) {
System.out.println("You can watch VIP 3D movie!");
} else {
System.out.println("You can watch VIP Standard movie!");
}
} else {
System.out.println("You can watch Standard movie only.");
}
} else {
System.out.println("No movie for you!");
}
scanner.close();
}
}
public class ControlFlowIf {
public static void main(String[] args) {
int score = 85;
System.out.println("Score: " + score);
// Simple If-Else
if (score >= 50) {
System.out.println("Result: Pass");
} else {
System.out.println("Result: Fail");
}
// Else-If Ladder (Grading System)
char grade;
if (score >= 90) {
grade = 'A';
} else if (score >= 80) {
grade = 'B';
} else if (score >= 70) {
grade = 'C';
} else if (score >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade: " + grade);
// Nested If
boolean hasLicense = true;
boolean hasCar = false;
if (hasLicense) {
if (hasCar) {
System.out.println("You can drive your car.");
} else {
System.out.println("You need to rent a car.");
}
} else {
System.out.println("You need a license used.");
}
}
}import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Enhanced switch = A replacement to many else if statements
// (Java14 features)
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the day of the Week: ");
String day = scanner.nextLine();
switch (day) {
case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" ->
System.out.println("It is a weekday!");
case "Saturday", "Sunday" ->
System.out.println("It is the weekend!");
default -> System.out.println(day + " is not a day!");
}
scanner.close();
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Calculator program
Scanner scanner = new Scanner(System.in);
double num1;
double num2;
char operator;
double result = 0;
boolean validOperation = true;
System.out.print("Enter the first number: ");
num1 = scanner.nextDouble();
System.out.print("Enter an operator (+, -,/, ^): ");
operator = scanner.next().charAt(0);
System.out.print("Enter the second number: ");
num2 = scanner.nextDouble();
switch (operator){
case '+' -> result = num1 + num2;
case '-' -> result = num1 - num2;
case '*' -> result = num1 * num2;
case '/' -> {
if (num2 == 0) {
System.out.println("Cannot divide by zero!");
validOperation = false;
} else {
result = num1 / num2;
}
}
case '^' -> result = Math.pow(num1, num2);
default -> {
System.out.println("Invalid operator!");
validOperation = false;
}
}
if(validOperation) {
System.out.println(result);
}
scanner.close();
}
}
public class Main {
public static void main(String[] args) {
// LOGICAL OPERATORS
// && = AND
// || = OR
// ! = NOT
double temp = 35;
boolean isSunny = true;
if (temp <= 30 && temp >= 0 && isSunny){
System.out.println("The weather is GOOD ");
System.out.println("It is SUNNY outside ");
} else if (temp <= 30 && temp >= 0 && !isSunny ) {
System.out.println("The weather is GOOD! ");
System.out.println("It is CLOUDY outside! ");
} else if (temp > 30 || temp < 0) {
System.out.println("The weather is bad! ");
}
}
}