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

7

u/zifyoip Sep 06 '15

Read the posting rules in the sidebar.

  • All post titles must be descriptive.

    • Bad title: I'm new to Java and need help.

    • Good title: I'm having trouble sorting values without loops.

-1

u/king_of_the_universe Sep 06 '15

Well. Suppose there's a class called "Eggs". Then you would have to do this:

Eggs name;

That's the variable declaration.

-1

u/jbristow I'm no hero, son. Sep 06 '15

And the variable "name" (of type Eggs) is declared, but not instantiated.

0

u/[deleted] Sep 06 '15

[deleted]

2

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

Your Constructor is also wrong:

//Constructors
public GroceryItem(String name, double cost, int aisle) 
{
    name = name;
    cost = cost;
    aisle = aisle;

}

First, you are not using the proper variable name for aisleNum and seconds, which is much worse than the first one, you are assigning the fields wrong:

    name = name;

Actually does nothing.

You need to use this to assign the value to the field.

this.name = name;

And similarly for the rest of the variables.

Had you made your constructor parameters final, your Constructor wouldn't even have compiled.

Good practice:

//Constructors
public GroceryItem(final String name, final double cost, final int aisleNum)

The final keyword prevents accidental manipulation of the parameters.

1

u/Seder777 Sep 07 '15

Thank you so much for your help, sorry I am a rookie at this. I really appreciate it.

3

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

private int getAisle; private double getCost;

They are wrong.

Your assignment clearly calls them aisleNum and cost.

The get prefix is used for the method that returns the value of the respective variable.

so: Variable: aisleNum -> Methods: setAisleNum and getAisleNum

You have to learn to read and follow the assignments to the point. This is also an important skill for a programmer.

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.

1

u/yodawg32 Sep 06 '15

Doing this.name = name etc. What it specifically do? How does it effect those variables in other parts of the program?

1

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

this.name = name

What it specifically do?

The statement as such does nothing but assign the parameter that was passed in the constructor (or in any method for that matter) to the class-internal variable (field).

This step is necessary so that the class gets it's data that it needs to work with.

Think about a simple Employee class. Each Employee has a name, employeeId, a baseSalary, and more.

In the constructor you give meaningful values to those variables so that later in the program you can calculate the salary based on the baseSalary of the specific employee and the hoursWorked (that can come from a different source, like a spreadsheet or similar).

1

u/yodawg32 Sep 06 '15

Okay so can't any method do this? What is the difference between a constructor and any normal method doing it?

1

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

The constructor performs some additional functions behind the scenes that initialize the object itself.

Constructors are called as soon as you use the new keyword.

You can use "normal" methods after the object is initialized by the constructor.

1

u/yodawg32 Sep 06 '15

So,you're saying the actual (this) variables get turned into Objects? I understand the new keyword and how you pass arguments into it so it matches the constructor. What is the significance of turning those variables into Objects? You can still use the variables fully without initialising them right?

1

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

Yes, you can say that this represents the current instance of the class, i.e. the current object

There can only be one this per class as it refers to the object itself.

You are not turning the variables inside the class into objects. These variables already belong to the object and are properties of the object that describe it.

You can still use the variables fully without initialising them right?

Depends: primitive types (boolean, byte, char, int, short, long, double, float) always are initialized by default (numeric types to 0, boolean to false). Object types (like String) must be initialized otherwise they contain null which will lead to NullPointerExceptions.