473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

NEED HELP ASAP: logical if statement?

I have been working on this simple project for a week, and come to the point where I feel like I need outside help. For the life of me I can't figure out what is wrong with this "sellIceCream(...)" method. I think theres something wrong with my if statement, because even if i have enough "ingredients", it still prints my "not enough" statement instead of selling.

The error that the online tester for this project gave me was:

"Test results indicate that your code still contains bugs. Your code appears to cover only 56% of the behavior required for this assignment.

Double check that you have carefully followed all initial conditions requested in the assignment in setting up your solution, and that you have also met all requirements for a complete solution in the final state of your program.

The following hint(s) may help you locate some ways in which your solution may be improved:

*

sell ice cream
*

multi sell I ice cream
*

sell ice cream no ice cream

(only 3 of 4 hints shown)"

I think I set all the initial conditions correctly. In anycase, I don't know what's wrong. This is my whole program:


Expand|Select|Wrap|Line Numbers
  1. public class IceCreamStation
  2. {
  3.     /** instance variables.*/
  4.     /**amount of cash in station.*/
  5.     private double numCash;
  6.     /**number of cones.*/
  7.     public int numCones;
  8.     /**amount of cash used to buy cones.*/
  9.     public double cashBuyCones;
  10.     /**amount of cash used to buy ice cream.*/
  11.     public double cashBuyIceCream;
  12.     /**number of cones sold.*/
  13.     public int numSold;
  14.     /**amount of ice cream in pints.*/
  15.     public double numIceCream;
  16.     /**price per pint of ice cream.*/
  17.     public static final double PRICEPERPINT = 3.25;
  18.     /**price per pint box of cones.*/
  19.     public static final double PRICEPERBOX = 2.50;
  20.     /**amount of cones in box.*/
  21.     public static final int CONESPERBOX = 10;
  22.     /**amount of pints to make ice cream cone.*/
  23.     public static final double QUARTERPINT = .25;
  24.  
  25.     /**
  26.      * Constructor for objects of class IceCreamStation.
  27.      * @version 2008.02.14
  28.      */
  29.     public IceCreamStation()
  30.     {
  31.         // initialise instance variables
  32.         numCones = 0;
  33.         numIceCream = 0;
  34.         cashBuyCones = 0; 
  35.         cashBuyIceCream = 0;
  36.         numSold = 0;
  37.         numCash = 100.00;
  38.     }
  39.  
  40.     /**
  41.      * Constructor for input start cash.
  42.      * 
  43.      * @param startCash   starting cash
  44.      */
  45.     public IceCreamStation(double startCash)
  46.     {
  47.         // initialise instance variables
  48.         numCones = 0;
  49.         numIceCream = 0;
  50.         cashBuyCones = 0; 
  51.         cashBuyIceCream = 0;
  52.         numSold = 0;
  53.         numCash = startCash;
  54.  
  55.  
  56.     }
  57.  
  58.     /**
  59.      * buys cones by the box.
  60.      * 
  61.      * @param buyNumBox  number of boxes
  62.      * */
  63.     public void buyCones(int buyNumBox) 
  64.     {
  65.         cashBuyCones = buyNumBox * PRICEPERBOX;
  66.  
  67.         if (numCash >= cashBuyCones)
  68.         {
  69.             numCones = numCones + buyNumBox * CONESPERBOX;
  70.             numCash = numCash -  cashBuyCones;
  71.         }
  72.         else
  73.         {
  74.             System.out.println("Not enough cash to buy cones");
  75.         }
  76.     }
  77.  
  78.     /**
  79.      * buys ice cream by the pint.
  80.      * 
  81.      * @param numPints  number of pints
  82.      * */
  83.     public void buyIceCream(double numPints)
  84.     {
  85.         cashBuyIceCream = numPints * PRICEPERPINT;
  86.         if (numCash >= cashBuyIceCream)
  87.         {
  88.             numCash = numCash - cashBuyIceCream;
  89.             numIceCream = numIceCream + numPints;
  90.         }
  91.         else
  92.         {
  93.             System.out.println("Not enough cash to buy ice cream");
  94.         }
  95.     }
  96.  
  97.     /**
  98.      * sells ice cream cones.
  99.      * 
  100.      * @param numToSell  number of ice cream cones 
  101.      * @param unitPrice   cost per cone
  102.      * */
  103.     public void sellIceCreamCones(int numToSell, double unitPrice)
  104.     {
  105.         if (numCones < numToSell || (numIceCream * QUARTERPINT) < numToSell)
  106.         {
  107.             System.out.println("not enough cones or ice cream");
  108.         }
  109.         else
  110.         {
  111.             numSold = numSold + numToSell;
  112.             numCones = numCones - numToSell;
  113.             numIceCream = numIceCream - (numToSell * QUARTERPINT);            
  114.             numCash = numCash + (numToSell * unitPrice);         
  115.         }
  116.     } 
  117.     /**
  118.      * returns number of cones in station.
  119.      * */
  120.     public int getConesLeft()
  121.     {
  122.         return numCones;
  123.     }
  124.  
  125.      /**
  126.      * returns how many pints of ice cream left.
  127.      * */
  128.     public double getIceCreamLeft()
  129.     {
  130.         return numIceCream;
  131.     }
  132.  
  133.      /**
  134.      * returns number of cones sold.
  135.      * */
  136.     public int getNumIceCreamConesSold()
  137.     {
  138.         return numSold;
  139.     }
  140.  
  141.      /**
  142.      * returns amount of cash in station.
  143.      * */
  144.     public double getCashOnHand()
  145.     {
  146.         return numCash;
  147.     }
  148. }
  149.  
Mar 9 '08 #1
4 1634
sukatoa
539 512MB
Expand|Select|Wrap|Line Numbers
  1. public void sellIceCreamCones(int numToSell, double unitPrice)
  2.     {
  3.         if (numCones < numToSell || (numIceCream * QUARTERPINT) <numToSell)
  4.         {
  5.             System.out.println("not enough cones or ice cream");
  6.         }
  7.         else
  8.         {
  9.             numSold = numSold + numToSell;
  10.             numCones = numCones - numToSell;
  11.             numIceCream = numIceCream - (numToSell * QUARTERPINT);            
  12.             numCash = numCash + (numToSell * unitPrice);         
  13.         }
  14.     } 
You are comparing Double type value to an integer type value...

Expand|Select|Wrap|Line Numbers
  1. if (numCones < numToSell || (numIceCream * QUARTERPINT) <numToSell)
Update us,
Sukatoa
Mar 9 '08 #2
If I changed my int to a double, I would be losing percision wouldn't I? Could casting the int as a double work? What would this look like, and where should it go if this is the solution?
Mar 9 '08 #3
i think it would also mess up the equations because the int and double types won't multiply out.
Mar 9 '08 #4
BigDaddyLH
1,216 Expert 1GB
Using doubles for currency is asking for trouble, because of floating point rounding errors. A better solution is to use int and work with values in cents. If you want to display values like "3.25", you can handle that explicitly.
Mar 10 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

34
by: yensao | last post by:
Hi, I have a hard time to understand difference and similarities between Relational database model and the Object-Oriented model. Can somebody help me with this? Thank you in advance. ...
1
by: Massimo Bonanni | last post by:
Hi, I try to implement ASAP protocol in my web service, but I find a very hard problem. I define my SOAP Header: public class Request : SoapHeader {
3
by: serge | last post by:
How do I determine which method I should use if I want to optimize the performance of a database. I took Northwind's database to run my example. My query is I want to retrieve the Employees'...
2
by: clinttoris | last post by:
Hello, If someone could help me it would be appreciated as I am not having much luck. I'm struggling with my asp code and have some questions relating to asp and oracle database. First...
14
by: Pep | last post by:
I have a method in a class like this class myClass { ... ctros, dtor, etc ... string myClassMethod() { string myString = "";
0
by: Roald | last post by:
I am working on an application that needs to implement logical deletes instead of removing the records permanently. The logical delete works by setting 3 fields (deleted flag, date and user). I...
1
by: jgill | last post by:
Have problems with the recordset…if the value of tempPremium is more than > 10,000 than I get no results after this statement executes: set rsDownPayment =...
24
by: user923005 | last post by:
The purpose is for a shellsort. I am experimenting with the increment for shellsort using a modified version of Pete's driver and this bit of code (shell_ratio is a floating point number for the...
11
by: Dominic Vella | last post by:
I am using MS-Access2000. I can't seem to set the default values for Logical type fields. I start with Dim dbsTmp As Object ' I think it's DAO.Database Set dbsTmp =...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.