Small Code Exercises
4-5 Exercises
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);
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);
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);
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);
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);
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);
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);
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);