Results 1 to 10 of 10
  1. #1
    hypes057 is offline Member
    Join Date
    Aug 2009
    Posts
    13
    Rep Power
    0

    Default 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.

    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);
    	}
    }
    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?

    I know (read: i think) that get and set would be something like:

    Java Code:
    get itemPrices
    set itemPrices
    
    get itemNames
    set itemNames
    Many thanks in advance!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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!
    }

  4. #4
    hypes057 is offline Member
    Join Date
    Aug 2009
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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!
    }
    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
       }
       
     }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    No, your constructor has to have the same name as your class. Please reread your chapter on constructors and such.

  6. #6
    hypes057 is offline Member
    Join Date
    Aug 2009
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    No, your constructor has to have the same name as your class. Please reread your chapter on constructors and such.
    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:

    Java Code:
    public class Items
    {
       private String itemName;
       private double itemPrice;
       
       public Items(String itemName, double itemPrice) 
       {
       itemName = Coffee;
       itemPrice = 1.00;
       }
       
       
          
    }
    Is that correct?
    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?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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:

    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;
       }
    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.

    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:

    Java Code:
    class Foo {
      private int fooVar;
    
      public Foo(int fooVar) {
        this.fooVar = fooVar;
      }
    
    }
    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.

    I can then use this to create Foo objects:
    Java Code:
    class SomeOtherClass {
      public static void main(String[] args) {
        Foo myFoo = new Foo(3);  // passes 3 into the constructor
      }
    }
    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.

  8. #8
    hypes057 is offline Member
    Join Date
    Aug 2009
    Posts
    13
    Rep Power
    0

    Default

    Thanks again. I will have to take a bit to read through this and grasp what you are explaining.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. variable double problem
    By DarkoDrljaca in forum New To Java
    Replies: 5
    Last Post: 03-02-2009, 08:00 AM
  2. Replies: 3
    Last Post: 11-24-2008, 07:01 PM
  3. Interface variable to class
    By zill in forum Advanced Java
    Replies: 6
    Last Post: 10-11-2008, 03:29 AM
  4. Replies: 3
    Last Post: 01-19-2008, 08:38 AM
  5. Public class variable
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 09:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •