Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Question 1: Use the API documentation to find the answers to the following questions:Question 1a: WhatQuestion 2: What is the value of the following expression, and why?Integer
method can you use to convert anint
into a string that expresses the number in hexadecimal? For example, what method converts the integer 65 into the string "41"?
Answer 1a:toHexString
Question 1b: What two
Integer
methods would you use to convert a string expressed in base 5 into the equivalentint
? For example, how would you convert the string "230" into the integer value 65? Show the code you would use to accomplish this task.
Answer 1b:valueOf
andintValue
. Here's how:String base5String = "230"; int result = Integer.valueOf(base5String, 5).intValue();Question 1c: What Double method can you use to detect whether a floating-point number has the special value Not a Number (
NaN
)?
Answer 1c:isNaN
Answer 2: False. The two objects (thenew Integer(1).equals(new Long(1))Integer
and theLong
) have different types.
Exercise 1: ChangeMaxVariablesDemo
to show minimum values instead of maximum values. You can delete all code related to the variablesaChar
andaBoolean
. What is the output?
Answer 1: SeeMinVariablesDemo
. Here is the output:The smallest byte value is -128 The smallest short value is -32768 The smallest integer value is -2147483648 The smallest long value is -9223372036854775808 The smallest float value is 1.4E-45 The smallest double value is 4.9E-324
Exercise 2: Create a program that reads an unspecified number of integer arguments from the command line and adds them together. For example, suppose that you enter the following:The program should displayjava Adder 1 3 2 1016
and then exit. The program should display an error message if the user enters only one argument. You can base your program onValueOfDemo.java
.
Answer 2: SeeAdder
.
Exercise 3: Create a program that is similar to the previous one but has the following differences:For example, suppose that you enter the following:
- Instead of reading integer arguments, it reads floating-point arguments.
It displays the sum of the arguments, using exactly two digits to the right of the decimal point.The program would displayjava FPAdder 1 1e2 3.0 4.754108.75
. Depending on your locale, the decimal point might be a comma (,
) instead of a period (.
).
Answer 3: SeeFPAdder
.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.