d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Quick Java Question
Add Reply New Topic New Poll
Member
Posts: 17,396
Joined: Aug 8 2008
Gold: 246.08
May 23 2012 06:45pm
Please review my Java worksheet, it should be pretty simple but I'm never confident enough to answer these graded worksheets with 100% sureness.

For questions #1-#3 an operation is performed with an array ary. Write equivalent code that performs the same operation on the ArrayList object a. Assume that the array list object has been previously created with the generic: List a = new ArrayList();

1. int x=19;
ary[5]=x;

My answer:
a[5]=19;

2. int sz = ary.length;

My answer:
int sz=a.size();

3. int gh=ary[22];

My answer:
int gh=a.get(22);

4. What is the simplest way to completely empty the array list a? (Write the method call).

My answer:
a=null;


5. In the GridWorld case study, which two classes implement the interface Grid<E>? <-- Not sure if you guys can answer that.

My answer:
Actor and...world?


Thanks in advance guys :)
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 23 2012 06:52pm
1. wrong
2. right
3. wrong
4. wrong
5. read the classes and see which ones say they implement the interface

you can correct 1 and 3 in a compiler so i won't explain them.
4 is not emptying the arraylist, you're simply removing one reference. the arraylist object still has everything inside.

i assume this is AP computer science in high school? This is almost the end of may. if the exam is already over, you're prolly not gonna score very well.

This post was edited by carteblanche on May 23 2012 06:53pm
Member
Posts: 17,396
Joined: Aug 8 2008
Gold: 246.08
May 23 2012 06:58pm
Quote (carteblanche @ May 23 2012 07:52pm)
1. wrong
2. right
3. wrong
4. wrong
5. read the classes and see which ones say they implement the interface

you can correct 1 and 3 in a compiler so i won't explain them.
4 is not emptying the arraylist, you're simply removing one reference. the arraylist object still has everything inside.

i assume this is AP computer science in high school? This is almost the end of may. if the exam is already over, you're prolly not gonna score very well.


Oh I know I did awful on the actual AP exam. Any help with 1 and 3 and 4 lol? >_< Shit's got me stumped, I don't understand how the ArrayList properties work.

I also don't have a compiler on this computer, these are written review sheets that we have to complete :S Only really program on the school computers.

e/ Shit, sorry I didn't see your signature before I pm'd you >_<

e^2:

Thanks for the help with #5, I found the appendix for the Gridworld case study and figured it out ^_^

e^3:

newly discovered that the ArrayList class has a .clear() method, so #4 I'm assuming is a.clear(); :O

This post was edited by xcharliex on May 23 2012 07:13pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 23 2012 07:14pm
Quote
Assume that the array list object has been previously created with the generic: List a = new ArrayList();


your variable a has a reference/compile type of List. that means you only have access to List's methods unless you cast. I recommend looking at the api and seeing which methods make the most sense. Pay close attention to the return type.

http://docs.oracle.com/javase/6/docs/api/java/util/List.html
Member
Posts: 17,396
Joined: Aug 8 2008
Gold: 246.08
May 23 2012 07:27pm
Quote (carteblanche @ May 23 2012 08:14pm)
your variable a has a reference/compile type of List. that means you only have access to List's methods unless you cast. I recommend looking at the api and seeing which methods make the most sense. Pay close attention to the return type.

http://docs.oracle.com/javase/6/docs/api/java/util/List.html


So for question 1

Quote

int x=19;
ary[5]=x;


I can't assign it a value using a simple "=" statement, but I can use the add method to replace a[5] with x?

Taken from the website:
"add(int index, E element)
Inserts the specified element at the specified position in this list (optional operation)."

So #1's answer should be (correct if I'm wrong please):

a.add(5,x);

Also, would #2 be:

int gh=a.get(Integer(22));

"get(int index)
Returns the element at the specified position in this list."
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 23 2012 07:31pm
You're making progress but you're still not there.

Quote
Also, would #2 be:

int gh=a.get(Integer(22));

"get(int index)
Returns the element at the specified position in this list."


Quote (carteblanche @ May 23 2012 09:14pm)
your variable a has a reference/compile type of List. that means you only have access to List's methods unless you cast. I recommend looking at the api and seeing which methods make the most sense. Pay close attention to the return type.

http://docs.oracle.com/javase/6/docs/api/java/util/List.html



Remember that high school java is basically "be the compiler". So use a compiler.

This post was edited by carteblanche on May 23 2012 07:32pm
Member
Posts: 17,396
Joined: Aug 8 2008
Gold: 246.08
May 23 2012 07:53pm
Quote (carteblanche @ May 23 2012 08:31pm)
You're making progress but you're still not there.


Remember that high school java is basically "be the compiler". So use a compiler.


I don't have a compiler on this computer and my disk space left is terrible, sorry :S I'd use the school computer's compiler but this is due first thing when I walk in the door so I don't really know how to get hold of a compiler by then. What would be the error displayed by the compiler if you don't mind showing me (assuming you have one)?

So the get method returns the element at the specified position in the list meaning that if I had an ArrayList where a[3] contained the integer 5 a call such as "a.get(3);" would return 5? And because of that would I wouldn't need to cast the returned object as an Integer because it is an integer as indicated in the header, but wouldn't I still need to instantiate the variable gh as an int? So it would start off as "int gh =" and then it would be followed by "a.get(22);"?

I know you said it was wrong at the beginning but I just walked right back into the same statement I originally got for #2 :S

int gh=a.get(22);

Or would it be

int gh.equals(a.get(22));


Also, would my new answer to #1 be correct?

Quote
a.add(5,x);


I feel as though I'm over-complicating a lot of this :unsure:

This post was edited by xcharliex on May 23 2012 07:54pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 23 2012 08:09pm
Quote (xcharliex @ May 23 2012 09:53pm)
I don't have a compiler on this computer and my disk space left is terrible, sorry :S I'd use the school computer's compiler but this is due first thing when I walk in the door so I don't really know how to get hold of a compiler by then. What would be the error displayed by the compiler if you don't mind showing me (assuming you have one)?

So the get method returns the element at the specified position in the list meaning that if I had an ArrayList where a[3] contained the integer 5 a call such as "a.get(3);" would return 5? And because of that would I wouldn't need to cast the returned object as an Integer because it is an integer as indicated in the header, but wouldn't I still need to instantiate the variable gh as an int? So it would start off as "int gh =" and then it would be followed by "a.get(22);"?


it would return the Integer object representing 5. however, that's the object type, not the compile-time type. read the return type in the api.

public E get(int index)

Imo your teacher failed when he used the word generic to describe how it was declared, since generics is a feature added to 1.5 and that does not use generics.

So figure out what your E represents if you're not using generics, then ask yourself does the compiler know how to do that?
Quote
Also, would my new answer to #1 be correct?



I feel as though I'm over-complicating a lot of this  :unsure:


That would be my guess. however, i dont know off hand. ArrayList internally uses an array. but I don't know if that will put it into the 5th index internally when there are no other objects in there. I noticed your teacher did not use Generics either. In 1.5 java added autoboxing where it promotes primitives to object wrappers automatically. If your book is restricted to java 1.4, then you'll have to box it yourself

a.add(5, new Integer(x));

This post was edited by carteblanche on May 23 2012 08:12pm
Member
Posts: 17,396
Joined: Aug 8 2008
Gold: 246.08
May 23 2012 08:15pm
Quote (carteblanche @ May 23 2012 09:09pm)
it would return the Integer object representing 5. however, that's the object type, not the compile-time type. read the return type in the api.

public E get(int index)

Imo your teacher failed when he used the word generic to describe how it was declared, since generics is a feature added to 1.5 and that does not use generics.

So figure out what your E represents if you're not using generics, then ask yourself does the compiler know how to do that?


That would be my guess. however, i dont know off hand. ArrayList internally uses an array. but I don't know if that will put it into the 5th index internally when there are no other objects in there.


Thanks for all of your help :) I'm gonna try and go through this a few more times by tomorrow morning and ask some classmates about it. Take care ^_^
Member
Posts: 61,418
Joined: Nov 21 2006
Gold: Locked
May 23 2012 09:53pm
5 is UnboundedGrid<E> and BoundedGrid<E> iirc
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll