Download Blue-Pelican Java Answers/Tests/Keys

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Answers 21-3
Key to Exercise on Lesson 21
1. The classes that convert primitives to objects are called
wrapper
classes.
2. Name the four primitive data types with which wrapper classes primarily deal.
int, double, boolean, char
3. Write code that will convert double dx into a wrapper class object. Call the object dd.
Double dd = dx; //pre Java 5.0, Double dd = new Double(dx);
4. Write code that will produce a Boolean type wrapper object called bj (“wrap” a true
inside it).
Boolean bj = true; //pre Java 5.0, Boolean bj = new Boolean(true);
5. Write code that will convert the integer ip into an Integer wrapper class object. Call the
object ozzie.
Integer ozzie = ip; //pre Java 5.0, Integer ozzie = new Integer(ip);
6. Assume you have object Character cw. Write code to convert this to a primitive
character.
char ch = cw; //pre Java 5.0, char ch = cw.charValue( );
7. Assume you have Double objects d1 and d2. Show how you would multiply the values
stored in these objects and then store the answer in primitive double dd.
double dd = d1 * d2;
//pre Java 5.0, double dd = d1.doubleValue( ) * d2.doubleValue( );
8. Assume you have Integer objects i1 and i2. Show how you would add the values stored
in these objects and then store the answer in a third Integer object called i3.
Integer i3 = i1 + i2;
// int j = i1.intValue( ) + i2.intValue( ); //pre Java 5.0,
//Integer i3 = new Integer(j);
9. Write code that will extract the boolean wrapped in the Boolean object, wnOh, and test it
with an if statement.
if(wnOh) //pre Java 5.0, if ( wnOh.booleanValue( ) )
10. Convert the object jj (of type Double) into a primitive float called ff.
float ff = jj.floatValue( );
Even with Java 5.0+, neither float ff = jj; nor float ff = (float)jj; works.
11. Convert the object pk (of type Double) into a primitive int called gurg. What is the
danger of doing this?
int gurg = pk.intValue( ); //We would lose precision
Even with Java 5.0+, neither int gurg=pk; nor int gurg=(int)pk; works.
12. What is the primary purpose of wrapper classes?
The conversion of primitive types to wrapper equivalents,
… we want these “equivalents” to be objects.
Key to Exercise on Lesson 22
Answers 22-3
1. Write code that will convert a String called rr into an int type called i.
int i = Integer.parseInt(rr);
2. The String s contains “123.456”. How would you convert this into a double type
variable?
double d = Double.parseDouble(s);
3. What evidence is there in the following statement that the method is static?
int v = Integer.parseInt(s);
…the fact that we use Integer, the class name, rather than an object.
4. How would you convert decimal equivalent String sd to String sh in hex form?
int i = Integer.parseInt(sd); //first convert to int
String sh = Integer.toHexString(i); //now convert to String
5. Suppose you have an int type stored in jj. How would you convert this into a String?
String s = Integer.toString(jj);//or… String s = “” + jj;
6. Suppose you must pass the Integer object equivalent of 1000 as a parameter to a
methodA; however, all you have is a String representation ss of that integer. Show how
you would manipulate ss and change it into an object called obj so that it could be used as
a parameter for methodA.
int i = Integer.parseInt(ss);
Integer obj = i; //preJava 5.0, Integer obj = new Integer(i);
another way 
Integer obj = Integer.valueOf(ss);
7. What is output by the following code?
String pdq = “-772.29”;
System.out.println( 3 + Double.parseDouble(pdq) ); //-769.29
8. Assume iObj is an Integer object “wrapping” the value -186. What is output by the
following code?
int ip = iObj; //pre Java 5.0, int ip = iObj.intValue( );
String mz = “3” + Integer.toString(ip) + “3”;
System.out.println(mz); // 3-1863
9. Write code that will convert “3pfh” (a String reprensation of a base 33 number) to int i.
int i = Integer.parseInt(“3pfh”, 33);
10. Write code that will convert int i into its String equivalent in base 6.
String s = Integer.toString(i, 6);