Labs
These labs have us working with Java's wrapper classes to demonstrate their utility, methods, and interaction with primitives.
- Write a program with two variables, one of type
doubleand oneDouble. Assign each of them the value0.0 / 0.0and print the results.
(Solution: NaNTest.java)
- This is
WrapperClasses/com.example.wrapperclasses.labs.AutoboxOverload:
package com.example.wrapperclasses.labs;
public class AutoboxOverload {
public static void method(Object o) { System.out.println("In Object method"); }
public static void method(Number n) { System.out.println("In Number method"); }
public static void method(Long l) { System.out.println("In Long method"); }
public static void main(String[] args) {
int var = 17;
method(var);
}
}
(Solution: _AutoboxOverloada.java_)
b. After running it, add another method that takes a parameter of type long (the primitive type). Which method will run?
(Solution: _AutoboxOverloadb.java_)
c. Add another method that takes a parameter of type Integer. Which method runs? Why?
(Solution: _AutoboxOverloadc.java_)
d. Add another method that takes a parameter of type int. Which method will run?
(Solution: _AutoboxOverloadd.java_)
- Write a program that prompts the user for two values: first an integer, second a boolean. If the boolean is
true, the program will print the numbers from0to the first argument. Iffalseit will print the numbers from the first argument to0.
Test your program with various strings passed on the command line.
(Solution: CountUpOrDown.java)
- Given the following code, what if anything will be printed by the
println?
public static void main(String[] args) {
Integer i = Integer.valueOf(1000);
increment(i);
System.out.println(i);
}
private static Integer increment(Integer i) {
i++;
return i;
}
- Write a program that reads a line of text as a
Stringfrom the keyboard (using aScannerandnextLine()to read in a string that may contain spaces). It should then print out each character of the string, along with whether or not that character is an uppercase letter, a lowercase, or a digit.
(Solution: TestLetters.java)