Results 1 to 10 of 10
Thread: Double Variable Class
- 09-05-2009, 09:50 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 13
- Rep Power
- 0
Double Variable Class
How do you create a variable class to hold double values? My String works as I want it to, but I can't figure out how to instance all the double values.
Also, how do i create a constructor that takes the listofItems String and the (to be created) itemCosts double to initialize the instance variables and also uses get and set methods for each instance?Java Code:class Item { public static void main(String args[]) { //These are the items values which I want to call itemCosts Double itemCoffee = new Double("1.00"); //Coffe is $1.00 Double itemWater = new Double("2.00"); // Water is $2.00 Double itemMilk = new Double("1.50"); // Milk is $1.50 Double itemBagel = new Double("1.25"); // Bagel is $1.25 Double itemDonut = new Double("0.75"); //Donut is $0.75 //This mess here prints out each item System.out.println(itemCoffee); System.out.println(itemWater); System.out.println(itemMilk); System.out.println(itemBagel); System.out.println(itemDonut); //This String of items works as I want, it neatly lists //each item and its price and uses only 1 println String listofItems = "Coffee - $1.00 \n"+ "Water - $2.00 \n"+ "Milk - $1.50 \n"+ "Bagel - $1.25 \n"+ "Donut - $0.75 "; System.out.println(listofItems); } }
I know (read: i think) that get and set would be something like:
Many thanks in advance!Java Code:get itemPrices set itemPrices get itemNames set itemNames
-
First thing to do is to get rid of your main method and start thinking about true OOP classes.
- Create your class,
- give it a double field (not Double), called price
- give it a String field called itemName
- give it a constructor that takes a String and a double.
- give it getters and setter methods for the price and itemName fields.
- give it ...
Last edited by Fubarable; 09-05-2009 at 09:58 PM.
-
For example, to get you started:
Java Code:public class Item { private String itemName; private double price; // constructor(s) here public Item(String iName, double p) { // TODO: add code to finish this } // TODO: add getters and setters here // not a main to be seen anywhere in this code! }
- 09-05-2009, 10:24 PM #4
Member
- Join Date
- Aug 2009
- Posts
- 13
- Rep Power
- 0
Fubarable,
Thanks for the response. Do you mean something like this?
Java Code:public class Item { private String itemName; private double itemPrice; // constructor(s) here public Coffee() { String itemName = Coffee double = 1.00 } public Water() { String itemName = Water double = 2.00 } }
-
No, your constructor has to have the same name as your class. Please reread your chapter on constructors and such.
- 09-05-2009, 10:57 PM #6
Member
- Join Date
- Aug 2009
- Posts
- 13
- Rep Power
- 0
Yea, thanks. I have re-read almost the entire book, I am not grasping it as well as I had liked. Plus I grasp information better when I am able to ask specific questions as they come.
From your example and last comment, I come up with:
Is that correct?Java Code:public class Items { private String itemName; private double itemPrice; public Items(String itemName, double itemPrice) { itemName = Coffee; itemPrice = 1.00; } }
If that is correct, how do I tell it to grab the String from my original post? Do I enter an itemName and itemPrice for each?
-
You give your constructor the same name as your class, and that part is correct, but the rest of the constructor is wrong.
Let's have a closer look at some of your code:
In this code there are actually two distinct itemName variables -- the class field called itemName and declared at the top, and the first parameter in your constructor, also called itemName. Within the body of your constructor, the compiler has no way of knowing whcih itemName you are referring to, so it assumes that anything called itemName in the body of the constructor refers to the parameter, and that if you want to refer to the class field, you must use this.itemName.Java Code:// here are your class fields private String itemName; private double itemPrice; // here is your constructor declaration with its two parameters public Items(String itemName, double itemPrice) { // here is the body of your constructor itemName = Coffee; itemPrice = 1.00; }
Currently your constructor changes the values held by the parameter variables to some fixed constant Coffee (I suppose you're trying to use a string here but it's not in quotes -- but even if it were in quotes it would be wrong), and 1.00. But understand that since the constructor parameters disappear into nothingness once program flow leaves the constructor -- once all of its code has been called, this constructor does nothing useful.
Your constructor's goal is to take the values passed in via the parameters and use this to set the class fields. A simple example of what I mean is this:
here I take the value passed into the constructor's fooVar parameter and use it to set the class field. Your constructor should do something similar.Java Code:class Foo { private int fooVar; public Foo(int fooVar) { this.fooVar = fooVar; } }
I can then use this to create Foo objects:
Also, a minor nit-pick: I'd name my class "Item" not "Items" as this class will hold information about one item only. If you have an array of objects of this class, you could call the array variable items as it will hold one or more Item objects.Java Code:class SomeOtherClass { public static void main(String[] args) { Foo myFoo = new Foo(3); // passes 3 into the constructor } }
- 09-06-2009, 12:58 AM #8
Member
- Join Date
- Aug 2009
- Posts
- 13
- Rep Power
- 0
Thanks again. I will have to take a bit to read through this and grasp what you are explaining.
-
-
Cross-post: Double Variable Class (Beginning Java forum at JavaRanch)
Please read the link in your cross-post about being forthright when cross-posting. Thanks.
Similar Threads
-
variable double problem
By DarkoDrljaca in forum New To JavaReplies: 5Last Post: 03-02-2009, 08:00 AM -
Rediculous problem?! Assigning a fraction to a double variable
By Tzoshirzup in forum New To JavaReplies: 3Last Post: 11-24-2008, 07:01 PM -
Interface variable to class
By zill in forum Advanced JavaReplies: 6Last Post: 10-11-2008, 03:29 AM -
why does a variable be bracketted in double @val@!Thank you
By ibmzz in forum Advanced JavaReplies: 3Last Post: 01-19-2008, 08:38 AM -
Public class variable
By Java Tip in forum Java TipReplies: 0Last Post: 12-03-2007, 09:58 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks