Page 2 of 2 FirstFirst 12
Results 21 to 31 of 31
  1. #21
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You're again mixing your user input out put with the Candy class. for instance:
    Java Code:
    	public void setNumber(){
    		id = io.readString("Please enter the item's number: ");
    	}
    This is not a setter method. Your setter method here should take a String parameter and should not interact directly with the user. i.e.,
    Java Code:
    public void setFoo(String fooString)
    {
       this.fooString = fooString;
    }
    One of the keys of OOP design is to separate functionality of your program into separate parts. I don't know how I can tell you so you will believe me, but you really really want to not have user IO in the Candy class. Do you believe me?

  2. #22
    katalyst is offline Member
    Join Date
    Mar 2009
    Posts
    14
    Rep Power
    0

    Default

    actually, after rereading the directions, there's nothing that says i have to use 2 classes or get/set methods (although i still would like to get 110% clarity on the subject matter..)

    ill go back to the previous revision of the code. all that i have left is to get the output to be in gui windows one at a time, as opposed to system output.

    Thanks guys/girls

  3. #23
    katalyst is offline Member
    Join Date
    Mar 2009
    Posts
    14
    Rep Power
    0

    Default

    no i totally believe you!
    it just seems like you're setting a variable equal to itself? what's the point?

    here's how im understanding it (which is probably the problem)...

    I call a setter method: public void setVariable(String s)

    inside that method is a way to ... bahhhhh... i just dont get it... how do you know what to set it to if you havent asked the user for input yet?? should i be asking for input in a separate class and then calling the setter method? they just seem so pointless? if i set it equal to a variable when i ask the user, what's the point of "setting" it somewhere else too?...

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

    Default

    it just seems like you're setting a variable equal to itself? what's the point?
    but I'm not and that's the whole point.

    this.fooString is the class field, the String that's held by the object and fooString here without the this is the parameter. We can even change the parameter name to make it more easily understood:

    Java Code:
    public class Fubar
    {
      private String fooString = "";
      
      public void setFooString(String f)
      {
        // fooString is the class field
        // and f is the parameter
        fooString = f;
      }
    }
    Note that in this second example since the class's String variable's name, fooString is different from the parameter, f, "this." is not needed to distinguish the two.

  5. #25
    katalyst is offline Member
    Join Date
    Mar 2009
    Posts
    14
    Rep Power
    0

    Default

    ahhhh ok that makes sense!

    here's another dumb question. do you always have to code your own set/get methods or are there some "built-in" if you don't, kind of like the constructor if you don't code your own

    and it seems im having trouble calling a method within the Inventory class from within main..
    im calling candy.getDisplay(); and inside the Inventory class i have public void getDisplay(){ some output; } and i keep getting invalid method call errors...

    *offtopic* is there like a "rewards" thing on this forum or some way i can thank all you guys?
    Last edited by katalyst; 03-29-2009 at 11:52 PM.

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

    Default

    1) There's no built-in that I know of for getters/setters (although many IDEs such as Eclipse can do it for you).
    2) as for "rewards", tips in small bills are always appreciated.

    But seriously, just saying "thanks" is more than most do here and is much appreciated.

  7. #27
    katalyst is offline Member
    Join Date
    Mar 2009
    Posts
    14
    Rep Power
    0

    Default

    instead of passing it to my own toString() method, which is only giving me console output, im trying to call my own display() method from within main to output the 4 fields to an io.writeInfo() gui window.

    in the Inventory class i have :
    Java Code:
    public void getDisplay(Inventory[] candy) 
      {
        io.writeInfo("well you made it here ok");
      }
    in main, i have the call:
    Java Code:
    for (int i = 0; i < candy.length; i++)
        {
        	//io.writeInfo("CANDY NO. " + i);
        	candy.getDisplay(candy);
        	//System.out.println(candy[i]);
        }
    when i compile, the error keeps saying "cannot find symbol method getDisplay()"

  8. #28
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    built-in getters/setters are there for many java classes, but as Fubarable said, there is no built-in way to create getters/setters for your own code. For IDE-generate methods, you have to rename many of the methods, as the IDE's use the variable name for the method name after the set/get, so it turns out as set"Variable Name", and for variables that are abbreviations, this is rarely what you want.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  9. #29
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    hmm, my last comment came too late.

    As for your error, it means you have not defined the getDisplay() method. If/Once you have tried to, if it still does not work, post your code and we'll see what we can do to help.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  10. #30
    katalyst is offline Member
    Join Date
    Mar 2009
    Posts
    14
    Rep Power
    0

    Default

    in all its glory!

    Java Code:
    import staugIO.StaugIO;
    
    class Inventory
    {
    
      StaugIO io = new StaugIO();
      private String id, name, weight, price;
    
      // Inventory constructor 
      public Inventory(String id, String name, String weight, String price)
      {
        this.name = name;
        this.id = id;
        this.weight = weight;
        this.price = price;
      }
      
      public void getDisplay(Inventory[] candy) 
      {
        io.writeInfo("well you made it here ok"); //test to see if the method is actually getting called
      }
      
      // to display a full record
      /**public String toString()
      {
      	output = io.writeInfo("\n\nITEM NO: " + id + "\nITEM: " + name + "\nWT: " + weight + "\nPRICE: " + price);
      	return output.toString();
      }*/
    }
    
    
    public class CandyProgram
    {
      public static void main(String[] args)
      {
        StaugIO io = new StaugIO();
        int size = io.readInt("How many items would you like to select?");
        
        // create candy array 
        Inventory[] candy = new Inventory[size];
        // get user input and put into array
        for (int i = 0; i < candy.length; i++)
        {
          String id = io.readString("Enter item id: ");
          //String id = candy.setNumber(id);
          String name = io.readString("Enter item name: ");
          String weight = io.readString("Enter item weight: ");
          String price = io.readString("Enter item price: ");
          candy[i] = new Inventory(id, name, weight, price);
        }
        for (int i = 0; i < candy.length; i++)
        {
        	//io.writeInfo("CANDY NO. " + i);
        	candy.getDisplay(candy);
        	//System.out.println(candy[i]);
        }
        
        System.exit(0);
      }
    }

    it works just like i need it to, only the output needs to be a msgBox gui with the contents of each array element, one at a time (4 fields of each record printed), instead of system output.

  11. #31
    katalyst is offline Member
    Join Date
    Mar 2009
    Posts
    14
    Rep Power
    0

    Default

    holy **** do i feel dumb....

    sooo ummm i changed the System.out.println line to System.out.print and got my output in a info popup just like i need!!!!!!

    wow.. i feel happy... relieved that i got the assignment completed, but more relieved that i get to learn this without pressure now!

    wanted to thank all of you again and i look forward to becoming an active member here on java-forums!

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. Classes and Methods help
    By border9 in forum New To Java
    Replies: 5
    Last Post: 01-30-2009, 07:51 PM
  3. Arrays & Methods
    By TheRocket in forum New To Java
    Replies: 1
    Last Post: 12-10-2008, 08:37 PM
  4. How to call methods of different classes
    By adeeb in forum New To Java
    Replies: 2
    Last Post: 06-06-2008, 07:08 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
  •