r/javahelp • • Sep 06 '15

I need help

For my assignment I need to do this....DECLARE AN INSTANCE VARIABLE CALLED NAME THAT CAN CONTAIN A VALUE SUCH AS "EGGS"....I have no idea where to start. If someone could help me get started hopefully I can finish the assignment. Thanks everyone

0 Upvotes

16 comments sorted by

View all comments

0

u/[deleted] Sep 06 '15

[deleted]

4

u/desrtfx Out of Coffee error - System halted Sep 06 '15

Now, you have a value "Eggs" that is encompassed in double quotes ". This already is a hint on the type of the variable.

In Java, Strings are encompassed by double quotes.

So, you need to declare a variable, called name, that can hold a String "Eggs".

What you need to do is:

String name;

You need another instance variable cost that can hold a value of 1.21. The decimal point again hints the data type: double.

So, the next instance variable is:

double cost;

Last, you need an instance variable aisleNum that can hold a value such as 5. The missing decimal point hints that you should use an int:

int aisleNum.

Now, you need the constructor that takes the parameters name, cost, aisleNum.

Constructors are methods that have the same name as the class:

public class GroceryItem {
    // here, you would put the instance variables (class fields) from above

    public GroceryItem(String name, double cost, int aisleNum) {
        // that is the constructor
        // here you will need some code
        // that assigns the values of the parameters passed
        // to the instance variables.
    }

    public String getName() {
        // This method should return the name of the 
        // GroceryItem
        // Since the name is a String, you will need to return a String
     }
}

2

u/schnoper Sep 06 '15

Generally speaking, it's considered poor to very poor practice to represent currency with a floating point number (in java either float or double ). It's much safer to use the BigDecimal class.

This is because once you get into fractional values, float's are often imprecise and this leads to situations where you have missing or additional value in your money. The bean counters hate this.

You should also learn about "banker's rounding"

For reference: http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency

2

u/desrtfx Out of Coffee error - System halted Sep 06 '15

This is technically absolutely correct.

Yet, since this seems to be a beginner's assignment, your suggestion goes way over the top and definitely outside the scope of the assignment.

If any number is presented with a decimal point, this type of assignment mostly calls for using a double. (I am also perfectly sure that most professors/teachers would mark an assignment using BigDecimal as wrong because they themselves lack the knowledge of this class and of proper usage of currency in programming.)

Proper practice (using BigDecimal) is one thing that needs to be learned when appropriate and an entry level class is mostly not the place for that.