Constructor and Method differences in use.

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 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:39 AM

I love you in a non-relationship way.
Was This Post Helpful? 0
  • +
  • -

#17 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 22 January 2013 - 11:18 AM

Figured I'd post the finished result. Its just a refresher for this new semester. Thanks again dreamers.


public class InventoryDemo {

	public static void main(String[] args) {
	
		
		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 = i2.copy();
		
		i4.setCost(i4.getCost() * 2);
		i4.setName("Double");
		
		System.out.println(i4);

		if(i1.equals(i2)) {
			System.out.println("i1 equals i2");
		}
		else {
			System.out.println("i1 equals i2");
		}

		for(int i = 10; i>=1; i--) {
			i2.setQuantity(i);
			
		if(i2.getTotalCost() >= 40) {
			System.out.println("Reduce inventory");
		}
		else {
			System.out.println(i2);
		}
			}
		}
	
	}

--------------------------------------------------------------------------------------

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;
	}


}

Was This Post Helpful? 0
  • +
  • -

#18 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: Constructor and Method differences in use.

Posted 22 January 2013 - 01:11 PM

just fyi your equals method does not override Object's .equals, which accepts an Object as a parameter.

  Inventory i1 = new Inventory();
  Inventory i2 = new Inventory(i1);

  System.out.println(i1.equals((Object)i2));
 System.out.println(i1.equals(i2));


print out different things! You should either
1.Use the @Override if you were intending to override .equals, this way the compiler will catch your error and alert you.
2.Name your method something other then equals.

If you go with 1, oracle suggests you also override hashcode such that if i1.equals(i2), i1.hashCode() == i2.hashCode()

This post has been edited by ianian112: 22 January 2013 - 01:15 PM

Was This Post Helpful? 1
  • +
  • -

#19 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 24 January 2013 - 11:21 AM

?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2