Friday, October 28, 2011

Strings And Objects


Strings
Equal

1.  Write an expression that evaluates to true if and only if the string variable s equals the string "end".
Example:
Suppose that the value of s were "friend".  Then the value of your expression should be false.

On the other hand, suppose that the value of s were "end".  Then the value of your expression should be true.

s.equals("end")


Inequality

1.  Write an expression that evaluates to true if the value of the string variable s1 is greater than the value of string variable s2.
Example:
Suppose that the value of s1 were "ABC" and that the value of s2 were "GHI".  Then the value of your expression should be false because s1 is not greater than s2 (in the sense that s1 does not come after s2 alphabetically).

Even if the value of s1 and s2 were identical the value of your expression should be false because s1 would not be greater than s2 (in the sense that s1 does not come after s2 alphabetically).

s1.compareTo(s2)>0


2.  Write an expression that evaluates to true if the value of variable lastName is greater than the string Dexter.

lastName.compareTo("Dexter")>0


3.  Write an expression that evaluates to true if and only if the string variable s does not equal the string end.

!(s.equals("end"))


MaxMin

1.  Given the String variables name1 and name2, write a fragment of code that assigns the larger of the two to the variable first (assume that all three are already declared and that name1 and name2 have been assigned values).

(NOTE: "larger" here means alphabetically larger, not "longer".  Thus, "mouse" is larger than "elephant" because "mouse" comes later in the dictionary than "elephant"!)

if(name1.compareTo(name2)>0)
{
first=name1;
}
else
{
first=name2;
}


2.  Given the string variables name1, name2, and name3, write a fragment of code that assigns the largest value to the variable max (assume all three have already been declared and have been assigned values).

max=(name1.compareTo(name2)>0)?name1:name2;
if(name3.compareTo(max)>0)
max=name3;




Objects
Creating_Objects

1.  Suppose a reference variable of type Integer called myInt is already declared.  Create an object of type Integer with the initial value of 1 and assign it to the reference variable myInt.
Example:  So, after your code executes, myInt refers to an Integer object whose value is 1.

myInt = new Integer(1) ;


2.  Suppose a reference variable of type Long called myLong is already declared.  Create an object of type Long with the initial value of two billion and assign it to the reference variable myLong.

myLong = new Long(2000000000) ;


3.  Suppose a reference variable of type Double called myDouble is already declared.  Create an object of type Double with the initial value of 1.5 and assign it to the reference variable myDouble.

myDouble = new Double(1.5) ;


4.  Suppose a reference variable of type Double called myDouble has already been declared.  There is also a double variable x that has been declared and initialized.  Create an object of type Double with the initial value of  x and assign it to the reference variable myDouble.

myDouble = new Double(x) ;


5.  Suppose a reference variable of type String called myString has already been declared.  Create an object of type String and assign it to the reference variable myString.

myString = new String() ;


6.  Suppose a reference variable of type File called myFile has already been declared.  Create an object of type File with the initial file name input.dat and assign it to the reference variable myFile.

myFile = new File("input.dat") ;








No comments:

Post a Comment