Incorrect Constructor Values?

Program compiles but displays incorrect answer.

Page 1 of 1

7 Replies - 665 Views - Last Post: 16 November 2008 - 09:21 PM Rate Topic: -----

#1 Alleluia707  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-October 08

Incorrect Constructor Values?

Post icon  Posted 15 November 2008 - 11:47 PM

Ok, the class has a package that retails for $99. A discount is given according to the number of units sold. The class stores the number of units sold and is supposed to have a method that returns the total cost of the purchase.

Now, the program compiles but it produces a value of 0 no matter what amount is entered. Is it because of the constructor values or something else?

SoftwareSale.java

public class SoftwareSale
{

	private double units,	// Number of units sold
			   price,		// Price of software package
			   discount,	// Discount on software package
			   total;		// Total cost
	
	
	/**
	 * Constructor 
	 */
 	 
	public SoftwareSale()
	{
		units = 0;
		price = 0;
		discount = 0;
	}


	
	public SoftwareSale(double u, double d)
	{
		units = u;
		price = 0;
		discount = d;
	}

	

	public void setUnits(double u)
	{
		units = u;
	}


	
	public void setPrice(double p)
	{
		price = p;
	}


	
	public void setDiscount()
	{
		if (units >= 10)
		   discount = 0.20;
		else if (units >= 20)
		   discount = 0.30;
		else if (units >= 50)
		   discount = 0.40;
		else if (units >= 100)
		   discount = 0.50;
	}



	public double getUnits()
	{
		return units;
	}


	
	public double getPrice()
	{
		return price;
	}



	public double getDiscount()
	{
		return discount;
	}



	public double getTotal()
	{
		return price * discount;
	}
		
}



TestSoftwareSale.java

import java.util.Scanner;	// Needed for the Scanner class


/**********************************************
 * This program tests the SoftwareSale class.
 * 
 **********************************************/


public class TestSoftwareSale
{

   public static void main(String[] args)
   {

	SoftwareSale sf;		 // Reference to SoftwareSale object
	double units,		 // To hold amount of units sold
		   price,			 // To hold price of software package
		   total;			 // Total cost of purchase 	


	// Create a Scanner object to read input.
	Scanner keyboard = new Scanner(System.in);


	// Ask the user for number of units sold.
	System.out.print("Enter the number of units " 
			 + "sold: ");
	units = keyboard.nextDouble();
	 

	// Create a SoftwareSale object.
	sf = new SoftwareSale(); 

	
	// Store the data.
	sf.setUnits(units);
	sf.setPrice(99.0);
	

	// Display the total cost.
	System.out.println("Total cost of purchase: $"
			   		   + sf.getTotal());

   }

}


Is This A Good Question/Topic? 0
  • +

Replies To: Incorrect Constructor Values?

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Incorrect Constructor Values?

Posted 16 November 2008 - 12:01 AM

View PostAlleluia707, on 15 Nov, 2008 - 10:47 PM, said:

Ok, the class has a package that retails for $99. A discount is given according to the number of units sold. The class stores the number of units sold and is supposed to have a method that returns the total cost of the purchase.

Now, the program compiles but it produces a value of 0 no matter what amount is entered. Is it because of the constructor values or something else?

SoftwareSale.java

public class SoftwareSale
{

	private double units,	// Number of units sold
			   price,		// Price of software package
			   discount,	// Discount on software package
			   total;		// Total cost
	
	
	/**
	 * Constructor 
	 */
 	 
	public SoftwareSale()
	{
		units = 0;
		price = 0;
		discount = 0;
	}


	
	public SoftwareSale(double u, double d)
	{
		units = u;
		price = 0;
		discount = d;
	}

	

	public void setUnits(double u)
	{
		units = u;
	}


	
	public void setPrice(double p)
	{
		price = p;
	}


	
	public void setDiscount()
	{
		if (units >= 10)
		   discount = 0.20;
		else if (units >= 20)
		   discount = 0.30;
		else if (units >= 50)
		   discount = 0.40;
		else if (units >= 100)
		   discount = 0.50;
	}



	public double getUnits()
	{
		return units;
	}


	
	public double getPrice()
	{
		return price;
	}



	public double getDiscount()
	{
		return discount;
	}



	public double getTotal()
	{
		return price * discount;
	}
		
}



TestSoftwareSale.java

import java.util.Scanner;	// Needed for the Scanner class


/**********************************************
 * This program tests the SoftwareSale class.
 * 
 **********************************************/


public class TestSoftwareSale
{

   public static void main(String[] args)
   {

	SoftwareSale sf;		 // Reference to SoftwareSale object
	double units,		 // To hold amount of units sold
		   price,			 // To hold price of software package
		   total;			 // Total cost of purchase 	


	// Create a Scanner object to read input.
	Scanner keyboard = new Scanner(System.in);


	// Ask the user for number of units sold.
	System.out.print("Enter the number of units " 
			 + "sold: ");
	units = keyboard.nextDouble();
	 

	// Create a SoftwareSale object.
	sf = new SoftwareSale(); 

	
	// Store the data.
	sf.setUnits(units);
	sf.setPrice(99.0);
	

	// Display the total cost.
	System.out.println("Total cost of purchase: $"
			   		   + sf.getTotal());

   }

}

You never set the discount. Either call sf.setDiscount() after you set the units, or add a call to it in the setUnits() property. Or initialize the discount to 1 in the class default constructor. :)
And in getTotal() shouldn't you be including units as a multiplicative term?

This post has been edited by n8wxs: 16 November 2008 - 12:05 AM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Incorrect Constructor Values?

Posted 16 November 2008 - 12:04 AM

your getTotal() method returns:

price * discount

and discount == 0 so it will always return 0

Sorry n8wxs... cross post
Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Incorrect Constructor Values?

Posted 16 November 2008 - 12:06 AM

pbl: we type at different speeds! :)
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Incorrect Constructor Values?

Posted 16 November 2008 - 12:11 AM

View Postn8wxs, on 15 Nov, 2008 - 11:06 PM, said:

pbl: we type at different speeds! :)

question of age I guess :D
Was This Post Helpful? 0
  • +
  • -

#6 Alleluia707  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-October 08

Re: Incorrect Constructor Values?

Posted 16 November 2008 - 12:25 AM

I added sf.setDiscount() after sf.setUnits(units) and I changed my getTotal() method to (price * units) * discount. Oh, and I changed discount = 1. Now it compiles properly.

Thanks to both of you for your help! ^_^
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Incorrect Constructor Values?

Posted 16 November 2008 - 12:29 AM

View PostAlleluia707, on 15 Nov, 2008 - 11:25 PM, said:

Now it compiles properly.

Now it behaves properly you mean
It was already compiling properly
Was This Post Helpful? 0
  • +
  • -

#8 Alleluia707  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-October 08

Re: Incorrect Constructor Values?

Posted 16 November 2008 - 09:21 PM

View Postpbl, on 15 Nov, 2008 - 11:29 PM, said:

Now it behaves properly you mean
It was already compiling properly

Oh, you're right. My bad, hehe :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1