| By Phantom (Phantom) on Sunday, May 02, 2004 - 04:57 pm: Edit |
Please help me with the following AP practice questions:
1) IO.readInt() is a call to a method that reads an integer.
String tempSequence = "";
int x =IO.readInt();
int y =IO.readInt();
while (y>=x)
{
tempSequence += x;
x = y;
y = IO.readInt();
}
tempSequence +=x;
System.out.println(tempSequence);
***What is the output, if the input is 1,1,2,3,5,4,7,8? How did you do it?
-------------------------
2)
public class Point
{
private int x;
private int y;
Point(int x1, int y1)
{
x = x1;
y = y1;
}
public void int getX()
{
return x;
}
public int getY()
{
return y;
}
}
***Why wouldn't the statement: if(p1.equals(p2)) work to check whether 2 Points have the same x and y coordinates? I thought all object classes automatically inherited the equals method???
| By Phantom (Phantom) on Sunday, May 02, 2004 - 05:01 pm: Edit |
Another question:
public static int doTask(int [] a)
{
int index = 0;
int soFar = 1;
int count = 1;
for(int k = 1; ksoFar)
{
soFar = count;
index = k;
}
}
else
{
count = 1;
}
}
return a[index];
}
When the following is executed:
int [] arr = {1,2,3,3,3,3,4,2,2,2,2,2,2,2,5,6,6,6,6,6,6,4,7,8};
System.out.println(doTask(arr));
What is displayed?
| By Mr_Sanguine (Mr_Sanguine) on Sunday, May 02, 2004 - 05:06 pm: Edit |
Number 2)
You are correct in saying all classes automatically inherit the equals() method; the Object class is a parent to every other class.
However, the key point is that the equals() method inherited from the Object class is equivalent in function to ==. == will return true if p1 and p2 are references to the same object, not if the contents are the same.
To compare if the contents are the same, the equals() method would need to be overridden in the Point class to compare the contents of the objects, not the references.
| By Wishful_Thinker (Wishful_Thinker) on Sunday, May 02, 2004 - 05:07 pm: Edit |
1) is the answer "11235"? (if not...then i don't know how they did it. otherwise, i'll explain)
2) all classes DO inherit the equals method but you can't compare a class. you see, when you compare classes p1 and p2...you aren't comparing anything. you can compare
((p1.getX()).equals(p2.getX()))&& ((p1.getY()).equals(p2.getY()))
or
((p1.getX())==(p2.getX()))&& ((p1.getY())==(p2.getY()))
you see, here we compare integers...not classes.
you CAN compare integers, doubles, strings
| By Wishful_Thinker (Wishful_Thinker) on Sunday, May 02, 2004 - 05:10 pm: Edit |
for another question...your input doesn't make sense. you have an "else" statement out of nowhere and a weird for statement.
| By Ubercollegeman (Ubercollegeman) on Sunday, May 02, 2004 - 05:15 pm: Edit |
1)
You can concatenate ints on strings in Java? O.O Didn't know that until just now..
Anyway, the key is to realize that the loop will terminate after '4' is entered when x=5. (At this point, the string is 11235). The loop will terminate because 4 isn't bigger than 5, and it will add on one more 5. So it will print out 112355.
2) I'm not totally sure. Might have something to do with hash codes and stuff..
Anyway, I can tell you that "public void int" doesn't make any sense :D.
3) for(int k = 1; ksoFar) ? Fix that and then I'll try it :D.
| By Phantom (Phantom) on Sunday, May 02, 2004 - 06:07 pm: Edit |
Sorry! here it is:
for(int k = 1; ksoFar)
{
soFar = count;
index = k;
}
thanks for all your help.
| By Phantom (Phantom) on Sunday, May 02, 2004 - 06:10 pm: Edit |
argh! i don't get it, it's not letting me post the actual code.
forget #3 then
| By Ubercollegeman (Ubercollegeman) on Sunday, May 02, 2004 - 06:15 pm: Edit |
Wait-that's the same thing.
k < soFar? k <= soFar?
| By Phantom (Phantom) on Sunday, May 02, 2004 - 06:18 pm: Edit |
the answer IS 11235 for number one.
so for the equals method, for any class but String, x.equals(y) is the same as x == y?
but if
String x = "hello";
String y = "hello";
x.equals(y) would be true, right?
| By Wishful_Thinker (Wishful_Thinker) on Sunday, May 02, 2004 - 06:22 pm: Edit |
phantom, if you want, you can e-mail me the q and i can try it. i need to test my skills anyway.
and ubercollegeman. what do you mean "k < soFar? k <= soFar?" are the same thing? they can't be. in the first one, k has to be less than soFar and in the second, it has to be less or equal to.
| By Phantom (Phantom) on Sunday, May 02, 2004 - 06:24 pm: Edit |
I have no idea why, but when I try posting number 3, the computer just deletes everything I write. There's supposed to be 2 more "if" statements under the "for loop," but I can't post it. Just don't worry about number 3.
| By Theli0nheart (Theli0nheart) on Monday, May 03, 2004 - 01:23 pm: Edit |
The answer for 1) is 12355, not 112355.
I'll execute the program as the computer would
x=1
y=1 (note here that readInt() now gets the second integer in the string of integers, which is 1)
1>=1 -->
tempSequence+="1" (1)
x=1
y=2 (now reads the third integer)
2>=1 -->
tempSequence+="2" (12)
x=2
y=3
3>=2 -->
tempSequence+="3" (123)
x=3
y=5
5>=3 -->
tempSequence+="5" (1235)
x=5
y=4
4<5 so break out of the loop
tempSequence+="5" (12355)
and we're done.
Report an offensive message on this page
E-mail this page to a friend
| Posting is currently disabled in this topic. Contact your discussion moderator for more information. |
| Administrator's Control Panel -- Board Moderators Only Administer Page | Delete Conversation | Close Conversation | Move Conversation |