public class Main {
public static void main(String[] args) {
boolean b = (1 > 2);
System.out.println(b);
if(true){
System.out.println("Welcome to IS147");
}
}
}
Sample code on Github boolean code
//boolean expression returns true or false
if (boolean-expression) {
statement(s);
}
int radius=5;
if (radius >= 0) {
area = radius * radius * PI;
System.out.println("The area"
+ " for the circle of radius "
+ radius + " is " + area);
}
public class Main {
public static void main(String[] args) {
int x = 20;
int y = 18;
if (x > y) {
System.out.println("x is greater than y");
}
}
}
public class Ifelse {
int number = 4;
public static void main(String[] args) {
if (boolean-expression) {
statement(s)-for-the-true-case;
}
else {
statement(s)-for-the-false-case;
}
}
}
Sample code on Github if else code
public class Main{
public static void main(String[] args) {
int radius = 0;
double area;
if (radius > 0) {
area = radius * radius * 3.14159;
System.out.println("The area for the"
+ "circle of radius " + radius +
" is " + area);
}
else {
System.out.println("Negative input");
}
}
}
public static void main(String[] args) {
public class Main {
public static void main(String[] args) {
int i = 1;
int j = 2;
int k = 3;
if (i > j)
if (i > k)
System.out.println("A");
else
System.out.println("B");
}
}
}
//Solution
public class Main {
public static void main(String[] args) {
int i = 1;
int j = 2;
int k = 3;
if (i > j) {
if (i > k)
System.out.println("A");
}
else
System.out.println("B");
}
}
| # | Operator | Name |
|---|---|---|
| 1 | ! | not |
| 2 | && | and |
| 3 | || | or |
| 4 | ^ | exclusive or |
switch (status) {
case 0: compute taxes for single filers;
break;
case 1: compute taxes for married file jointly;
break;
case 2: compute taxes for married file separately;
break;
case 3: compute taxes for head of household;
break;
default: System.out.println("Errors: invalid status");
System.exit(1);
}
public class Main {
public static void main(String[] args) {
String dayOfWeek = "Tuesday";
// Using lambda expressions with switch
switch (dayOfWeek) {
case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"
-> System.out.println("Work day");
case "Saturday", "Sunday" -> System.out.println("Weekend");
default -> System.out.println("Invalid day");
}
}
}
(boolean-expression) ? exp1 : exp2
| Specifier | Output | Example |
|---|---|---|
| %b | a boolean | true or false |
| %c | a character | 'a' |
| %d | a decimal integer | 200 |
| %f | a floating-point number | 45.4600 |
| %e | a number in standard scientific notation | 4.555e+01 |
| %s | a string | "IS 147" |
public class UnaryOperators {
public static void main(String[] args) {
int a = 10;
// Unary plus and minus
System.out.println(+a); // 10
System.out.println(-a); // -10
// Increment and Decrement Operators
System.out.println(++a); // Pre-increment: 11
System.out.println(a++); // Post-increment: 11 (prints before incrementing)
System.out.println(a); // 12 (after post-increment)
System.out.println(--a); // Pre-decrement: 11
System.out.println(a--); // Post-decrement: 11 (prints before decrementing)
System.out.println(a); // 10 (after post-decrement)
// Logical NOT
boolean flag = true;
System.out.println(!flag); // false
}
}
public class BinaryOperators {
public static void main(String[] args) {
int x = 10, y = 5;
// Arithmetic Operators
System.out.println("Addition: " + (x + y)); // 15
System.out.println("Subtraction: " + (x - y));// 5
System.out.println("Multiplication: " + (x * y)); // 50
System.out.println("Division: " + (x / y)); // 2
System.out.println("Modulus: " + (x % y)); // 0
// Relational Operators
System.out.println(x > y); // true
System.out.println(x < y); // false
System.out.println(x == y); // false
System.out.println(x != y); // true
// Logical Operators
System.out.println((x > y) && (x > 0)); // true
System.out.println((x < y) || (x > 0)); // true
// Bitwise Operators
System.out.println(x & y); // 0 (Bitwise AND)
System.out.println(x | y); // 15 (Bitwise OR)
System.out.println(x ^ y); // 15 (Bitwise XOR)
// Assignment Operators
x += y; // x = x + y;
System.out.println(x); // 15
}
}
import java.util.Random
//Using Math class
//If you want to generate a number from 0 to 100 then your code would look like this:
int number1 = (int)(Math.random() * 101);
System.out.println (number1);
//Using Random class
Random rand = new Random ();
int dice = rand.nextInt(6)+1; (0-5) + 1 = 1-6
int dice = rand.nextFloat()*6 +1; (0.0-0.999)*6+1
int bottles = rand.nextInt(100); (0-99)
value = Math.cos(90) + Math.sqrt(delta);
double circumference = diameter*Math.PI;
int dice= (int) ( Math.random()*6+1);
Logic errors are called bugs. The process of finding and correcting errors is called debugging. A common approach to debugging is to use a combination of methods to narrow down to the part of the program where the bug is located. You can hand-trace the program (i.e., catch errors by reading the program), or you can insert print statements in order to show the values of the variables or the execution flow of the program. This approach might work for a short, simple program. But for a large, complex program, the most effective approach for debugging is to use a debugger utility.
Debugger is a program that facilitates debugging. You can use a debugger to