Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program runs as a naive programmer would expect, but it might surprise some

ID: 3680152 • Letter: T

Question

This program runs as a naive programmer would expect, but it might surprise someone who knows that the != operator in Java compares object identities, not their values. If you change the initial values to 1001 and 1010, the code behaves the way a naive programmer would expect while it would surprise a naive programmer.

Obviously the implementors of class Integer did some extra work to make it behave as a naive programmer would expect so long as the integer values were within a small range. Don't worry about finding the bounds of the range.

Question: It is not clear that you can write legal Java code to do what the Integer() constructor does, but if you replace new Integer() with calls to a newInteger() factory method, write legal Java code to do something like what the Integer class is doing to satisfy beginners. (Hint: If you stretch the code out and format it carefully, it's about 10 lines of code.)

Explanation / Answer

class Toy {
public static void main( String[] args ){
int i = 1;
int j = 10;
while (i != j) {
System.out.println( "" + i );
i = i + 1;
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote