1.In the Java API, the String class and all numeric wrapper classes (Number, Integer, Float...) are immutable. Name 3 other classes in the Java API whose objects are immutable. Give an example of creating a new object from each class.
2. Name 3 classes in the Java API whose objects are mutable and give an example of how you can change the attributes of an object after you create it. You cannot use "Date" because it is given here:
Mutable type: java.util.Date;
// create a Date for today.
Date now = new Date( );
// change the date to 1 January 2000
now.setDate( 1 );
now.setMonth( Calendar.JANUARY );
now.setYear( 100 );
// print the Date
System.out.println( "Today is " + now );
3. (a) Explain the difference between == and equals( ).
(b) Compare this example using primitive and object types:
int n = 10;
int m = 10;
if (n == m) System.out.println("primitives are same");
Long x = new Long( 10 );
Long y = new Long( 10 );
if (x == y) System.out.println("objects are same");
Explain the difference between the answers.
4. Sometimes we want to ask the user for a yes/no answer, like this:
Scanner console = new Scanner( System.in );
System.out.print("Do you like Java (input yes or no)? ");
String reply = console.next( );
if ( reply == "yes" ) likesJava++;
else if ( reply == "no" ) likesJava--;
else System.out.println( "Sorry, fumble fingers" );
(a) This code always prints "Sorry, fumble fingers" even if user inputs yes or no! Why?
(b) Write a correct code. Also write code so that the user can type "yes", "YES", "Yes", etc., and his answer will be recognized.