Constructor and Method differences in use.

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 6779 Views - Last Post: 24 January 2013 - 11:21 AM Rate Topic: -----

#1 DtP1337  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 15-October 12

Constructor and Method differences in use.

Posted 17 January 2013 - 01:36 PM

This is copying an object with a constructor?


Inventory i3 = new Inventory(i2);
	System.out.println(i3);




What would copying using a method look like if thats true?

Can you explain and give me an example of what to do between creating objects that copy using either the copy constructor or copy method.
Is This A Good Question/Topic? 0
  • +

Replies To: Constructor and Method differences in use.

#2 DtP1337  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 15-October 12

Re: Constructor and Method differences in use.

Posted 17 January 2013 - 01:49 PM

Just a quick thought.

Would this be using the product of my copy method in the class like this.

Just a shot in the dark.

Inventory i4 = new Inventory(copyInv);
System.out.println(i4);


Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10771
  • View blog
  • Posts: 40,104
  • Joined: 27-December 08

Re: Constructor and Method differences in use.

Posted 17 January 2013 - 01:50 PM

A copy constructor takes a different instance of the class and copies it's state. It's pretty straight-forward:
class Point{
   
    private int x, y;

    public Point(int x, int y){
       this.x = x;
       this.y = y;
    }

    public Point(Point other){
       this.x = other.x;
       this.y = other.y;
    }
}

Point one = new Point(3,3);
Point two = new Point(one); //two.x = 3, two.y = 3


Was This Post Helpful? 1
  • +
  • -

#4 DtP1337  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 15-October 12

Re: Constructor and Method differences in use.

Posted 17 January 2013 - 01:53 PM

Essentially i4 is a copy using a method?
i3 is constuctor?



public class InventoryDemo {

	public static void main(String[] args) {
	
		Inventory total;
		double num = 0.0;
		
		Inventory i1 = new Inventory();
		System.out.println(i1);
		
		Inventory i2 = new Inventory(10,2,3.25,Regular);
		System.out.println(i2);
		
		Inventory i3 = new Inventory(i2);
		System.out.println(i3);
		
		Inventory i4 = new Inventory(copyInv);
		System.out.println(i4);
		
________________________________________________________________________

public class Inventory {
	private int quantity; // Stores quantity.
	private double width; // Stores width.
	private double cost; // Stores cost.
	private String name; // Stores name.
	
public Inventory() {
		setQuantity(0);
		setWidth(0.0);
		setCost(0.0);
		setName(" ");
		
	}
	
public Inventory(Inventory items) {
		quantity = items.quantity;
		width = items.width;
		cost = items.cost;
		name = items.name;
	}
	
public Inventory(int q, double w, double c, String n) {
		quantity = q;
		width = w;
		cost = c;
		name = n;
	}
	
public boolean equals(Inventory items) {
	boolean status;
		if (width == (items.width) && cost == (items.cost))
				status = true;
			else
				status = false;
				
			return status;
}
	
public void setQuantity(int q) {
		quantity = q;
	}
	
public void setWidth(double w) {
		width = w;
	}

public void setCost(double c) {
		cost = c;
	}
	
public void setName(String n) {
		name = n;
	}

public int getQuantity() {
		return quantity;
	}

public double getWidth() {
		return width;
	}

public double getCost() {
		return cost;
	}

public String getName() {
		return name;
	}

public double getTotalCost() {
		return quantity * cost;
	}

public String toString() {

	String result = "Quantity:" +getQuantity()+
			"\n Width:" +getWidth()+
			"\n Cost:" +getCost()+
			"\n Name:" +getName();
	return result;
	}

public Inventory copy() {
	Inventory copyInv = new Inventory(quantity,width,cost,name);
	return copyInv;
	}


}



This post has been edited by DtP1337: 17 January 2013 - 01:55 PM

Was This Post Helpful? 0
  • +
  • -

#5 DtP1337  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 15-October 12

Re: Constructor and Method differences in use.

Posted 17 January 2013 - 03:08 PM

Create an inventory item I3 which is a copy of I2. Use the copy constructor.
Use toString to display the results.
Create an inventory item I4 which is a copy of I2. Use the copy method.
Double the cost of I4.
Change the name to Double.
Use toString to display the results.


I don't understand the difference of using method or constuctor in these instructions.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10771
  • View blog
  • Posts: 40,104
  • Joined: 27-December 08

Re: Constructor and Method differences in use.

Posted 17 January 2013 - 03:10 PM

There really isn't a clear difference. One is a constructor, and the other isn't.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Constructor and Method differences in use.

Posted 17 January 2013 - 03:24 PM

View PostDtP1337, on 17 January 2013 - 06:08 PM, said:

1) Create an inventory item I3 which is a copy of I2. Use the copy constructor.
2) Use toString to display the results.
3) Create an inventory item I4 which is a copy of I2. Use the copy method.
4) Double the cost of I4.
5) Change the name to Double.
6) Use toString to display the results.

1) Constructor
2) Method
3) Constructor
4) Method
5) Method
6) Method
Was This Post Helpful? 1
  • +
  • -

#8 DtP1337  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 15-October 12

Re: Constructor and Method differences in use.

Posted 17 January 2013 - 03:50 PM

So for i4 would be?

Inventory i4 = new Inventory(i2);  

i4.setCost = cost*2;
i4.setName = "Double";

System.out.println(i4);  


This post has been edited by DtP1337: 17 January 2013 - 03:54 PM

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Constructor and Method differences in use.

Posted 17 January 2013 - 07:33 PM

Getter() and Setter(xxx) are basic OOP concepts
   i4.setCost(i4.getCost() * 2);  
   i4.setName("Double");  


Was This Post Helpful? 1
  • +
  • -

#10 DtP1337  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 15-October 12

Re: Constructor and Method differences in use.

Posted 18 January 2013 - 11:22 AM

Doing that for i4, would that would satisfy the use copy method situation in the instructions?
Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2813
  • View blog
  • Posts: 11,909
  • Joined: 20-September 08

Re: Constructor and Method differences in use.

Posted 18 January 2013 - 03:28 PM

Quote

would that would satisfy the use copy method situation in the instructions
Seems to me that they expect you to have a method actually named 'copy'. Check API docs for Object.clone()
Was This Post Helpful? 0
  • +
  • -

#12 DtP1337  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 15-October 12

Re: Constructor and Method differences in use.

Posted 19 January 2013 - 09:09 AM

The very last code in my class I believe is the method. I'm not sure how to access it in the Demo.

This post has been edited by DtP1337: 19 January 2013 - 09:10 AM

Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Constructor and Method differences in use.

Posted 19 January 2013 - 10:10 AM

Inventory i4 = otherInventoryObject.copy();
Was This Post Helpful? 1
  • +
  • -

#14 DtP1337  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 15-October 12

Re: Constructor and Method differences in use.

Posted 19 January 2013 - 10:31 AM

Just a guess, but where you have otherInventoryObject, should I change the name of the copy method?
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Constructor and Method differences in use.

Posted 19 January 2013 - 10:37 AM

it is the name of the Inventory object from which you want to access its inventory method so maybe

Inventory i3 = new Inventory(...
Inventory i4 = i3.copy();

based on what your copy method does

public Inventory copy() {
     // create a new Inventory Object based on quantity,width,cost and name
     // hold in the instance variables of i3  
     Inventory copyInv = new Inventory(quantity,width,cost,name);
     // return that object that will be stored in i4  
     return copyInv;  
     }  


Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2