• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays Classes with Copy Constructor  RSS feed

 
Danny Treart
Ranch Hand
Posts: 37
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm working on an assignment for my Java class, and I've hit a point where I can't really figure out the issue.

Here is the exact instruction for the assignment...
Make 2 classes WrapperShallow and WrapperDeep.
Each class is simply a wrapper class to hold a private array variable. int [] a;
The default constructor for each class should initialize “a”.
Each class should have a toString() and equals().
Each class should have a setArray method that allows you to set the “a” variable.
WrapperShallow should have an invalid copy constructor.
public WrapperShallow(WrapperShallow ws){
a = ws.a;
}
WrapperDeep should have a properly functioning copy constructor.
public WrapperDeep(WrapperDeep ws){
a = new int[3];
for(int i = 0; i < 3; i++)
a[i]=ws.a[i];
}
Think about why shallow is wrong and deep is correct! What happens to the old “a” in the WrapperDeep copy
constructor? (think garbage collection)
Example Output:
--------------------Configuration: <Default>--------------------
**** TESTING SHALLOW OBJECTS ****
inital shallow object contains
7 17 77
copy shallow object contains
7 17 77
inital shallow object changed to
13 14 15
copy shallow object not changed contains
13 14 15
WOOPS! ws.equals(ws2) is true
**** TESTING DEEP OBJECTS ****
inital deep object contains
2 3 4
copy deep object contains
2 3 4
inital deep object changed to
7 6 -5
copy deep object not changed contains
2 3 4
RIGHT! wd.equals(wd2) is false
Process completed.



So far I've only finished part of the WrapperDeep class. I can't get my equals method to work properly, I keep getting the error:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - missing return statement
at WrapperDeep.equals(WrapperDeep.java:27)
at WrapperTest.main(WrapperTest.java:18)
Java Result: 1


My class looks like this (I'm a newb):

My main class is:

Any ideas why this won't work?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check your braces. Then check them again.

Also think on this: does it make sense to have an unconditional return in a for loop?
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at lines 20 and 21. What's wrong with them? Are your braces matched? What does the method return and when?
 
Danny Treart
Ranch Hand
Posts: 37
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm.. I'm not sure how to compare two arrays with several different values, without using a loop to check each one..
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not the for loop that's the problem. Read Bear's and my posts carefully.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
put it another way...

what happens the first time through your loop:

 
Danny Treart
Ranch Hand
Posts: 37
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm.. So the actual method needs a return statement outside of the for loop? Would it make sense just to set it to false?



So if all else fails, the method will return false. Also, does the return inside the for loop break out of the method as soon as 1 of the values in the array checks out as equal to the value in the incoming array?
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try writing out on paper what that loop would do one iteration at a time. Also, is it okay to return out of a for loop?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Danny Treart wrote:Hmm.. So the actual method needs a return statement outside of the for loop?

Actually, no. That isn't the problem.


let's walk through this.
i = 0.
you compare this.a[0] with wd.a[0]. you get either true or false. what do you do with that value? (hint - what is the FIRST word on line 2 above?)
 
Danny Treart
Ranch Hand
Posts: 37
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't this comparing the values that are in the i index, for both instances of a? Or is this checking the same value? Then returns true or false if they are the same/different value? That's what I'm expecting.

Ugh, maybe this field just isn't for me. :/
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Danny Treart wrote:Isn't this comparing the values that are in the i index, for both instances of a? Or is this checking the same value? Then returns true or false if they are the same/different value? That's what I'm expecting.

And that is what it does. It compares this.a[0] and wd.a[0], and RETURNS THAT true/false value.

"return" means "LEAVE THIS METHOD RIGHT NOW". So it never gets to the point where it checks this.a[1] and wd.a[1], or this.a[2] and wd.a[2], because once it compares the 0th, it exits the method


Danny Treart wrote:Ugh, maybe this field just isn't for me. :/

Don't give up. This stuff is HARD. You have to re-train (or possibly just train) your brain how to think like the computer. If it was easy, everybody would do it, and we'd not be paid nearly as well.
 
Danny Treart
Ranch Hand
Posts: 37
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just when I start feeling like "I'm getting it", it just gets more complicated. Hahaha. Does it ever level off? I've read each line of code like 100 times, and followed the order. Does reading/writing code ever become second nature?

Good thing is, I think this one is figured out. I'm going to go back over the chapters and read up on it again so I can grasp it entirely.

Thanks so much for the help guys.
 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Danny Treart wrote:Just when I start feeling like "I'm getting it", it just gets more complicated. Hahaha. Does it ever level off? I've read each line of code like 100 times, and followed the order. Does reading/writing code ever become second nature?


Parts of it do.
But then someone comes along and writes a new framework or invents a new development paradigm.


It's a constantly evolving industry.
There will always be something that will make you scratch your head.
Look on the bright side, it keeps your brain active...;)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!