Small Code Exercises for Primitive Types

Integer

import java.util.Random;
public class Example {
    public static void main(String[] args) {
        
        int a = (int)(Math.random() * 100);

      System.out.println(a);
    }
}
Example.main(null);
98

Double

import java.util.Random;
public class Example {
    double a = 0.0;
    public static void main(String[] args) {
        double a = Math.random() * 100;
        System.out.println(a); 

        String b = String.valueOf(a);
        System.out.println(b);
        for (int i = 0; i < 5; i++) {
            if (b.substring(i,i+1) != "1") {
            System.out.println(b.substring(i,i+1));
            }
        }
    }
}
Example.main(null);
32.14245474233138
32.14245474233138
3
2
.
1
4

Boolean

public class Example {
    public static void main(String[] args) {
        boolean x = true;
        boolean y = false;

        if (x && y) {
            System.out.println("both true");
        }
        else if (x || y) {
            System.out.println("one true");
        }
        else {
            System.out.println("none true");
        }
    }
}
Example.main(null);
one true

Char

public class Example {
    public static void main(String[] args) {
        char[] charArray = new char[5];
        charArray[0] = 'A';
        charArray[1] = 'B';
        charArray[2] = 'C';
        charArray[3] = 'D';
        charArray[4] = 'E';
        
        for (int i = 0; i < 5; i++) {
        System.out.println("Element at index " + i + ": " + charArray[i]);
        }
    }
}

Example.main(null);
Element at index 0: A
Element at index 1: B
Element at index 2: C
Element at index 3: D
Element at index 4: E

Small Code Exercises for Primitive Types (Wrapper Classes)

Integer

import java.util.Random;
import java.util.ArrayList;
public class Example {
    public static void main(String[] args) {
        
        int a = (int)(Math.random() * 100);
        int b = (int)(Math.random() * 100);
        int c = (int)(Math.random() * 100);

        System.out.println("int values: " + a + " " + b + " " + c);

      Integer d = Integer.valueOf(a);
      Integer e = Integer.valueOf(b);
      Integer f = Integer.valueOf(c);

      ArrayList<Integer> myNumbers = new ArrayList<Integer>();
      myNumbers.add(d);
      myNumbers.add(e);
      myNumbers.add(f);
      for (int i : myNumbers) {
        System.out.println(i);
      }
      System.out.println(myNumbers);
    }
}
Example.main(null);
int values: 13 79 7
13
79
7
[13, 79, 7]

Double

import java.util.Random;
public class Example {
    double a = 0.0;
    public static void main(String[] args) {
        double a = Math.random() * 100;
        double b = Math.random() * 100;
        double c = Math.random() * 100;

      Double d = Double.valueOf(a);
      Double e = Double.valueOf(b);
      Double f = Double.valueOf(c);

      List<Double> myDoubles = new ArrayList<Double>();
      myDoubles.add(d);
      myDoubles.add(e);
      myDoubles.add(f);
      for (double i : myDoubles) {
        System.out.println(i);
      }
      System.out.println(myDoubles);
    }
}
Example.main(null);
24.821008262495514
68.52454237527138
92.77791987859953
[24.821008262495514, 68.52454237527138, 92.77791987859953]

Boolean

public class Example {
    public static void main(String[] args) {
        boolean x = true;
        boolean y = false;

        if (x && y) {
            System.out.println("both true");
        }
        else if (x || y) {
            System.out.println("one true");
        }
        else {
            System.out.println("none true");
        }

        Boolean a = Boolean.valueOf(x);
        Boolean b = Boolean.valueOf(y);

        List<Boolean> myBooleans = new ArrayList<Boolean>();   
        myBooleans.add(a);
        myBooleans.add(b);
        System.out.println(myBooleans);
    }
}
Example.main(null);
one true
[true, false]

Char

public class Example {
    public static void main(String[] args) {
        char[] charArray = new char[5];
        charArray[0] = 'A';
        charArray[1] = 'B';
        charArray[2] = 'C';
        charArray[3] = 'D';
        charArray[4] = 'E';
        
        for (int i = 0; i < 5; i++) {
        System.out.println("Element at index " + i + ": " + charArray[i]);
        }

        Character f = Character.valueOf(charArray[0]);
        Character g = Character.valueOf(charArray[1]);
        Character h = Character.valueOf(charArray[2]);
        Character i = Character.valueOf(charArray[3]);
        Character j = Character.valueOf(charArray[4]);

        List<Character> myCharacters = new ArrayList<Character>();
        myCharacters.add(f);
        myCharacters.add(g);
        myCharacters.add(h);
        myCharacters.add(i);
        myCharacters.add(j);

        System.out.println(myCharacters);
    }
}

Example.main(null);
Element at index 0: A
Element at index 1: B
Element at index 2: C
Element at index 3: D
Element at index 4: E
[A, B, C, D, E]