Note that there are some explanatory texts on larger screens.

plurals
  1. POWorking with classes and and if-else statements. Wondering if someone can explain certain parts better.
    primarykey
    data
    text
    <p>Just as the title states. I have been working with classes for a couple of classes in school now but I don't really understand the why portion of what I am doing. My code may not be perfect but I have commented out some of the questions I have pertaining to this and other programs I wrote like it. This particular program takes the number of software packages sold in a software class with a discount for different levels of purchases. Then in another class it will calculate or should i say, display the total based on the entered number of packages sold. My questions are commented out in the code. Thank you everyone so much. I know that this stuff is very elementary as far as java goes but I am having trouble understanding the why's of it all. </p> <pre><code>public class SoftwareSaleClass { private double quantity; // Should this really be private? This is the way my teacher suggested when creating classes. public SoftwareSaleClass (double q ) { quantity = q; // why do I have to put this here? I never use the q for anything but Eclipse says I need a constructor } public double getQuantity() { return quantity; // Does this need to be here? If so why? I never use it in the tester class or do I? } public double getYourTotal() {double total; if (quantity &gt;= 10 &amp;&amp; quantity &lt;= 19) { total = (99 * quantity * .80); } else if (quantity &gt;= 20 &amp;&amp; quantity &lt;= 49) { total = (99 * quantity * .70); } else if (quantity &gt;= 50 &amp;&amp; quantity &lt;= 99) { total = (99 * quantity * .60); } else if (quantity &gt;= 100) { total = (99 * quantity * .50); } else total = (99 * quantity); return total; } // This part I totally understand the why, although I think my math isn't quite right. } import java.util.Scanner; public class SoftwareSalesTester { public static void main(String[] args) { double quantity; // Does this above double point to the other class or is it possible i could have named it something else? Scanner keyboard = new Scanner(System.in); System.out.print("Enter the amount of packages being sold: "); quantity = keyboard.nextDouble(); SoftwareSaleClass grandTotal = new SoftwareSaleClass(quantity); System.out.println("Your grand total today is: " + grandTotal.getYourTotal() + "\nWe Gladly Accept VISA, MASTERCARD, AND CASH MONEY!"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload