Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Beginner Problem

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Beginner Problem

    Hi, I'm a little confused with an assignment. I've try to figure it out for a while but I'm stuck.

    The instructions are as follows:

    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 have this:
    class WrapperShallow 
    {
    	private int[] a = new int[2];
     
     
     
    	public void setArray()
    	{
    		a[0] = 7;
    		a[1] = 17;
    		a[2] = 77;
    		for(int i = 0; i < 3; i++)
    		{
            	System.out.println("Inital shallow object contain the numbers:\n");
            	System.out.print(a[i] + " ");
            }
     
    	}
     
    	public WrapperShallow (WrapperShallow ws)
    	{
    		a = ws.a;	
    	}
     
    	public String toString()
    	{
    		return Integer.toString(a[0]); 	
    	}
     
    	public boolean equal(WrapperShallow ws2)
    	{
    		return a.equals(ws2);
    	}
     
     
    }
     
     
     
    class WrapperDeep
    {
     
    	private int[] a = new int[2];
     
    	public void setArray()
    	{
    		a[0] = 7;
            a[1] = 17;
            a[2] = 77;
     
    		for(int i = 0; i < 3; i++)
    		{
    			System.out.println("Inital shallow object contain the numbers:\n");
    			System.out.print(a[i] + " ");
    		}
    	}
    	public WrapperDeep(WrapperDeep wd)
    	{
    		a = new int[3];
     
    		for (int i = 0; i < 3; i++)
    			a[i] = wd.a[i];
    	}
     
    	public String toString()
     
    	{
    		return Integer.toString(a[0]); 
    	}
     
    	public boolean equal(WrapperDeep wd2)
    	{
    		return a.equals(wd2);
    	}
     
     
    }
     
     
     
     
     
    public class WrapperDemo {
     
     
    	public static void main(String[] args) 
    	{
    		WrapperShallow ws = new WrapperShallow();		
    		ws.setArray();
     
     
    	}
     
     
    }

    Just ignore the toString and Equals. I'll change those later.
    The error I'm trying to understand is in this line "WrapperShallow ws = new WrapperShallow();"
    I think I'm supposed to pass an array in the parentheses however I get an error every time I try to. I know I'm supposed to send something because it is supposed to receive something. Am I approaching this problem in the correct way? Any help would be greatly appreciated.

    Thank you!


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Beginner Problem

    Ok, in the WrapperShallow class, you have declared the following to be the constructor:
    public WrapperShallow (WrapperShallow ws)
        {
            a = ws.a;  
        }

    Since that is the only constructor that has been declared, that is the only one that you can use.

    So when you make the call:
    WrapperShallow ws = new WrapperShallow();

    That is not allowed because there is no constructor that looks like this:
    public WrapperShallow ()
    {
            ... 
    }

    Realistically, I can't see the above code really doing anything with the specific constructor that is now there. Since you need to send each WrapperShallow a WrapperShallow object in its constructor and there are no alternate constructors. Essentially, you would have to either endlessely create WrapperShallow objects for each WrapperShallow object, or you would have to send it null, which would defeat the entire purpose of the constructor.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner Problem

    thanks for the reply.

    Yeah that's what I am so confused about.

    The instructions say to include this:
    public WrapperShallow (WrapperShallow ws)
        {
            a = ws.a;  
        }

    But like you said it's expecting a WrapperShallow to be sent to it. Do you think the instructor made a mistake? I'm not really sure what to do at this point. I might just send him an email and see if he can help me.

    Thanks again.

Similar Threads

  1. Java Beginner Here!
    By j3nn42o in forum The Cafe
    Replies: 10
    Last Post: January 10th, 2011, 04:57 AM
  2. [SOLVED] While Loop Help (beginner)
    By Perplexing in forum Loops & Control Statements
    Replies: 4
    Last Post: October 23rd, 2010, 02:00 PM
  3. Programming beginner
    By LatinaC09 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 15th, 2010, 01:33 PM
  4. Java Beginner
    By rannoune in forum Java Theory & Questions
    Replies: 3
    Last Post: December 25th, 2009, 03:30 AM
  5. I need a help ! i am beginner
    By yinky in forum Java Theory & Questions
    Replies: 3
    Last Post: September 30th, 2009, 07:22 AM