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;
Example:
// 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.

Recommended Answers

All 3 Replies

Where did you get stuck? what did you answer so far?

I'm learning about programming.
I get these from my professor.
I dont understand but I'm trying.
I want anyone to teach me.

Ok, show us what you have tried so far and what you don't understand.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.