Session 2
TypesDemo.java
public class TypesDemo {
    public static void main(String[] args) {
        // 1. byte: 8-bit signed integer
        
        // 2. short: 16-bit signed integer
        
        // 3. int: 32-bit signed integer
        
        // 4. long: 64-bit signed integer (needs 'L' at the end for large values)
        
        // 5. float: 32-bit floating point (needs 'f' at the end)
        
        // 6. double: 64-bit floating point
        
        // 7. char: single 16-bit Unicode character
        
        // 8. boolean: true or false
        
        // Print them
        
        // String
        
        
    }
}ArithmeticDemo.java
public class ArithmeticDemo {
    public static void main(String[] args) {
        // Integer arithmetic
        int a = 15;
        int b = 4;
        
        // Double arithmetic
        double x = 15.0;
        double y = 4.0;
    }
}
ScannerExample.java
// import all the classes and interfaces from the java.util package into your program.
import java.util.*;
public class ScannerExample {
    public static void main(String[] args) {
        System.out.println("Scanner Example Code");
        // 1. Print a message to the user
        
        // 2. Initialize the Scanner. Declare a variable named in
        // that reads from System.in
        
        // 3. Create a variable and assign it to the value returned by nextLine
        // The Scanner class provides a method called nextLine that reads a
        // line of input from the keyboard and returns a String.
        
        // 4. Print the value of the variable
        
        
        // 5. Close the Scanner
        
    }
}ScannerExample2.java
import java.util.*;
public class ScannerExample2 {
    public static void main(String[] args) {
        System.out.println("Scanner Example 2 Code");
        Scanner in = new Scanner(System.in);
        System.out.print("What's your age? ");
        int age = in.nextInt();
        // add code to fix the "Scanner bug"
        System.out.print("What's your name? ");
        String line = in.nextLine();
        System.out.println("Name: " + line);
        System.out.println("Age: " + age);
        in.close();
    }
}S2Problem1.java
public class S2Problem1 {
    public static void main(String[] args) {
        System.out.println("Problem 1");
    }
}S2Problem2.java
public class S2Problem2 {
    public static void main(String[] args) {
        System.out.println("Problem 2");
    }
}S2Problem3.java
public class S2Problem3 {
    public static void main(String[] args) {
        System.out.println("Problem 3");
        byte num1 = 200;           // Error 1
        short num2 = 50000;        // Error 2
        long num3 = 5000000000;    // Error 3
        float num4 = 3.14;         // Error 4
        char letter = "B";         // Error 5
        boolean flag = yes;        // Error 6
    }
}S2Problem4.java
public class S2Problem4 {
    public static void main(String[] args) {
        System.out.println("Problem 4");
    }
}S2Problem5.java
public class S2Problem5 {
    public static void main(String[] args) {
        System.out.println("Problem 5");
    }
}S2Problem6.java
public class S2Problem6 {
    public static void main(String[] args) {
        System.out.println("Problem 6");
    }
}TipCalculator.java
import java.util.*;
public class TipCalculator {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the total bill amount: ");
        System.out.println("Bill Amount: $");
        System.out.println("Tip (15%): $");
        System.out.println("Total Amount: $");
    }
}
CurrencyConverter.java
import java.util.*;
public class CurrencyConverter {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter amount in USD: ");
        System.out.println("Amount in EUR £: ");
        System.out.println("Amount in GBP €: ");
        System.out.println("Amount in JPY ¥: ");
    }
}
CompoundInterestCalculator.java
import java.util.*;
public class CompoundInterestCalculator {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the principal amount: ");
        
        System.out.print("Enter the annual interest rate (in decimal): ");
        
        System.out.print("Enter the number of years: ");
        System.out.println("The amount after " + " years is: ");
    }
}Last updated
Was this helpful?