Page 1 of 2 12 LastLast
Results 1 to 20 of 31
  1. #1
    katalyst is offline Member
    Join Date
    Mar 2009
    Posts
    14
    Rep Power
    0

    Default methods, classes, arrays.. oh my!

    wow. so im normally pretty quick about pickin' up a new programming language, but it's been a while since i've done it and i'm struggling with a java homework assignment. here is the code I have so far:

    import staugIO.StaugIO; //import StaugIO() class

    class GetInfo{
    //Declare private variables
    String itemNumber;
    String itemName;
    String itemWeight;
    String itemPrice;
    StaugIO io = new StaugIO();

    //Second Level of Refinement Methods
    public void setNumber(){
    itemNumber = io.readString("Please enter the item's number: ");
    }
    public void setName(){
    itemNumber = io.readString("Please enter the item's name: ");
    }

    public void setWeight(){
    itemNumber = io.readString("Please enter the item's weight: ");
    }

    public void setPrice(){
    itemNumber = io.readString("Please enter the item's price: ");
    }
    }//End Candy



    public class MainProgram {

    StaugIO io = new StaugIO();

    public static void main(String[] args) {
    }
    }


    [/CODE]

    Here is what the program SHOULD do:

    Code a program that will store candy information. Each candy's data will be stored in an array as an object. To do so the array will use a class as a data type. Let the user determine the size of the array. Prompt the user to enter the item number, item name, weight, and price. Once the array has been populated with candy information, display the contents of the array one at a time in separate GUI windows.

    seems pretty simple, i just cannot for the life of me get my head around the classes, methods, and how to call each one.. the public/private/static/void has got me so confused it's not even funny...

    can someone help me get the structure correct so that i can start coding this thing? apparently it's supposed to have a main class, a class to set/get the info, a class to create teh array and display the info and then the main method... what goes where?
    Last edited by katalyst; 03-28-2009 at 11:24 PM.

  2. #2
    gargoosh88 is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default

    OK i will ry to solve it

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

    Default

    I would first create a Candy class and have this class hold the data for a type of candy -- item number, name, price, and weight all stored in the appropriate type. I'd have a constructor that accepted these same bits of data as parameters and with the parameters, I'd initialize the object's fields. I would not have Candy concern itself with directly interacting with the user, rather it would only be used to hold the Candy data and to display this data with a toString() method if desired, that's it.

    Then another class would hold the array of Candy[] and would interact with the user to get the data that would eventually be used to create Candy objects and place them in the array.

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

    Default

    :o

    code examples?

    the jargon throws me off...

    Java Code:
    import staugIO.StaugIO; //import StaugIO() class
    
    class Candy{
    //Declare private variables
    String itemNumber;
    String itemName;
    String itemWeight;
    String itemPrice;
    
    //are these methods correct or do i even need methods?
    
    public void setNumber(){
    itemNumber = io.readString("Please enter the item's number: ");
    }
    public void setName(){
    itemNumber = io.readString("Please enter the item's name: ");
    }
    
    public void setWeight(){
    itemNumber = io.readString("Please enter the item's weight: ");
    }
    
    public void setPrice(){
    itemNumber = io.readString("Please enter the item's price: ");
    }
    }//End Candy
    
    class HoldArray{
    //what goes here?
    
    }
    
    public class MainProgram {
    
    StaugIO io = new StaugIO();
    
    public static void main(String[] args) {
    //does anything actually happen in main?
    
    }
    }

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

    Default

    one thing I notice immediately: you only ever set itemNumber, instead of setting itemNumber, itemName, itemWeight, AND itemPrice.

    also, what is the code for StaugIO? do you have it?

    if you don't have the StaugIO code, what is it supposed to do?
    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!

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

    Default

    Quote Originally Posted by katalyst View Post
    code examples?
    the jargon throws me off...
    The code examples should largely come from you as it is your assignment, and I"ll try to take a look at what you posted. As for the "jargon", better learn it now, else you're in for a long hard ride.

    Best of luck.

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

    Default

    the staugio package just allows output.. similar to system.out.println, but it outputs it to gui windows (ok/cancel boxes)

    answer me this..

    when i create a new project, it creates the main class and the main() method. would these self-created classes go INSIDE that class or outside and how would they be declared (pubic/private/ just class)

    im not getting the set/get crap.. why do i need two different methods... ugh this is so confusing...

    need to see it in code...

    thx for helping so far!

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

    Default

    Again, your Candy class is trying to get user input, and it shouldn't be doing that. I would get all io out of Candy. Instead (and again) give it a constructor that accepts a String for the Candy name, a double for its weight, as well as other parameters for its cost and ID number. Use these parameters to set your Candy fields.

    Then later you will create a class that should fill the Candy[] array, and this class will use i/o to get the user's input.

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

    Default

    Alright here's a trivial example of what I mean:

    Fubar1 class that holds some data
    Java Code:
    public class Fubar1
    {
      private String name;
      private int age;
    
      // Fubar1 constructor 
      public Fubar1(String name, int age)
      {
        this.name = name;
        this.age = age;
      }
      
      // to display a Fubar1 object's data
      public String toString()
      {
        return name + ", " + age;
      }
    }
    Fubar1Test class to create an array of Fubar1 and fill with user input
    Java Code:
    import java.util.Scanner;
    
    public class Fubar1Test
    {
      public static void main(String[] args)
      {
        // scanner to get user input
        Scanner scanner = new Scanner(System.in);
        
        // Fubar1 array 
        Fubar1[] fubars = new Fubar1[4];
        
        // get user input and put into array
        for (int i = 0; i < fubars.length; i++)
        {
          System.out.print("Enter a name: " );
          String name = scanner.nextLine();
          System.out.print("Enter an age: " );
          int age = scanner.nextInt();
          scanner.nextLine(); // to swallow the next line symbol
          fubars[i] = new Fubar1(name, age);
        }
        
        // show data in array 
        System.out.println("Fubars:");
        for (int i = 0; i < fubars.length; i++)
        {
          // this calls the Fubar1 toString method
          System.out.println(fubars[i]);
        }
      }
    }
    enjoy

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

    Default to guess at what Fubarable means...

    I think fubarable means that your Candy class should have a constructor in it, such as
    Java Code:
    public Candy(string name, double weight, double cost, int IDNumber){
    }
    you could then call this constructor in another class that accepts input to set the variables.

    Fubarable, correct me if the doubles should be something else, not too familiar with them.

    You may also want additionally methods to set the name, weight, cost, and IDNumber after using the constructor, but leave those for later.
    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!

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

    Default

    Boyo, I'd use a double for the weight and a String for the IDNumber since ID numbers are for identification, not for doing math. As for the cost, (sorry fishtoprecords), but I'd use a double here for that in this simple program, but some here would shoot me for saying this, and if this were a real-life program, they'd be right, but for instructional purposes, I don't see much wrong with this.

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

    Default

    ok adapted what you had a bit. i need this to output to gui popups instead of command line so im using writeInfo in place of the system.out.println statements. however on the for loop output it wont let me for some reason...

    here's what i got.. im having trouble outputting it to gui instead of command line

    Java Code:
    import staugIO.StaugIO;
    
    class Inventory
    {
      private String id, name, weight, price;
      StaugIO io = new StaugIO();
    
      // Fubar1 constructor 
      public Inventory(String id, String name, String weight, String price)
      {
        this.name = name;
        this.id = id;
        this.weight = weight;
        this.price = price;
      }
      
      // to display a Fubar1 object's data
      public String toString()
      {
      	StaugIO io = new StaugIO();
      	return io.writeInfo("Item Id: " + id + "\nItem Name: " + name + "\n...");
      }
    }
    
    public class ProgramName
    {
      public static void main(String[] args)
      {
        // scanner to get user input
        StaugIO io = new StaugIO();
        int size = io.readInt("How many items would you like to select?");
        
        // Fubar1 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 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);
        }
        
        // show data in array 
        for (int i = 0; i < candy.length; i++)
        {
          // this calls the Fubar1 toString method
          System.out.println(candy[i]);
        }
      }
    }
    also teach says there should be 2 classes... i dont get it...
    does she mean to move all the code from the main above to a class by itself?

    your help is greatly appreciated btw! ive been stressin' over this one for weeks and i have a test monday in 2 different classes i still need to study for.

    thanks
    Last edited by katalyst; 03-29-2009 at 06:37 AM.

  13. #13
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Quote Originally Posted by katalyst View Post
    also teach says there should be 2 classes... i dont get it...
    does she mean to move all the code from the main above to a class by itself?
    Well, probably not. Probably, she means you should have a Candy class, and another class that contains an array of Candy...as fubarable originally suggested.

    You have a class called 'ProgramName'. Why? Why not call it 'CandyStore' or something meaningful?

    Think of classes as 'types of things'. For example, a person is a type of thing. If you were going to write code to represent a person, you would create a class (short for classification) called 'Person'. People have different colors of eyes right? So you might have a member variable called 'eyeColor', that represents the eye color.

    But how do you know what color someone's eye's are? You look at them. Well, code doesn't have eyes to look, so it has to discover this information the way you would over AIM. You would ask them. in code-speek, you would 'get' their eye color. If you were creating babies and could control their eye color, you would 'set' it. That's the concept behind 'getter' and 'setter' methods, as well as classes, and member variables.

    Methods are actions you can take on a class. 'getEyeColor' is an example of a method (if you are familiar with functions, methods are functions, but their scope is limited rather than global).

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

    Default

    well ProgramName is the name of the project (homework assignment), the one created when i created the project.

    seems like the 'get' and 'set' are doing the same thing? if i 'get' the eyecolor, couldnt i 'set' it in the same method? or maybe im misinterpreting the 'get'.. im thinking something like

    method name(
    int number = readInt("Select a number");
    )

    wouldn't that be 'getting' the number and 'setting' equal to "number" all in the same method? or is this array-specific stuff here?

    I actually have some clarity with fubar's code, but the fact that there should be two classes is confusing to me.. 'cause it seems likes it's perfectly doable with one class... i don't get what belongs in the "missing" class here...

  15. #15
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Anything you can do in 2 classes, you can do in 1. But the idea behind Object Oriented programming, is that it's less complex. That means that not only can you be more productive and make fewer errors, but the code you write can be more easily modified by someone else later on if necessary.

    If you were going to make a chair, would you try to carve it from a single of piece of wood, or would you assemble it from parts?

    As far as 'getter' and 'setter' go, I think this will make more sense once you really appreciate what a member variable is.

  16. #16
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Toadaly... that chair example is great !!!

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

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

    Default

    well i fully understand the modularity aspect of OOP, but maybe it's the simplicity of the example at hand that seems overkill to split into multiple classes?

    i have to dedicate my whole sunday to learning this...

    thanks guys

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

    Default

    As for get/set methods...

    getters return the value of the variable you are getting
    e.g.
    Java Code:
    String currentname = myCandy.getName()
    after using this method, if you used a system.out.println statement to display currentname and myCandy.name, they would be exactly the same.

    setters set the value of the variable
    e.g.
    Java Code:
    myCandy.setName("the new name for myCandy")
    after using this method, the name of myCandy would be "the new name for myCandy".

    I rarely use getters unless I use a method that sets a variable according to parameters I don't control. For example, if I need to use the width of a frame in an equation, but I set the size of the frame using frame.pack(), I would need to use frameWidth = frame.getWidth() to have a number I can use in an equation.

    Hope this helps!

    P.S. note that to use methods like setName and getName, you would need to define them yourself. They were simply examples of methods you might need that relate to your program.
    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!

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

    Default

    makes a bit more sense. (what a difference a fresh set of eyes, a rock star, some pringles and the fine coders at jforums.com make!)

    couple more questions before i can claim to "understand" it clearly :)

    for every class i create, do i need a constructor for that class (i know one will be created for me if i dont make my own, but is it good practice to write my own?)

    and, i have the program working properly (minus output to gui instead of command line but.. ). how would i adapt it to create the second class that im supposed to have? I assume i would take the contents of my main method and throw it into another class but not real sure how to set that up (what gets declared where and such)

    really, i appreciate all the help so far! as this is what i do in my free time and will hopefully make a living off of in the future, you are helping more than just a "guy tryin to get an assignment done"

    thanks!

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

    Default

    ok so after some looking, i realize that i am not using get and set methods in the program, which i think is one of the requirements of the assignment.. so im trying to redo it... here is what i have:

    Java Code:
     
    import staugIO.StaugIO;
    
    class Inventory{
    	//Declare private variables
    	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;
    	  }
    	
    	StaugIO io = new StaugIO();
    	//are these methods correct or do i even need methods?
    	public String getNumber(String id){
    		return id;
    	}
    	
    	public void setNumber(){
    		id = io.readString("Please enter the item's number: ");
    	}
    	
    	public String getName(String name){
    		return name;
    	}
    	
    	public void setName(){
    		name = io.readString("Please enter the item's name: ");
    	}
    	
    	public String getWeight(String weight){
    		return weight;
    	}
    	
    	public void setWeight(){
    		weight = io.readString("Please enter the item's weight: ");
    	}
    	
    	public String getPrice(String price){
    		return price;
    	}
    	
    	public void setPrice(){
    		price = io.readString("Please enter the item's price: ");
    	}
    }//End Candy
    
    
    public class CanyProgram
    {
      public static void main(String[] args)
      {
        StaugIO io = new StaugIO();
        int size = io.readInt("How many items would you like to select?");
        
        // Fubar1 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: ");
          [COLOR="Red"]String id = candy.setNumber();[/COLOR]
          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++)
        {
        	System.out.println(candy[i]);
        }
        System.exit(0);
      }
    }
    only compile error i get is the red line "cannot find symbol method setNumber()"

    not sure i am calling it correctly.. but this is how i understand (correct me please if im wrong)

    ill have a class with a constructor, 4 set methods and 4 get methods

    ill have a class that has a method that contains a for loop that will print the records.

    ill have main method with will call the set and get methods of the first class

    is this correct? if my class is Inventory and i instantiated it with the 'candy' variable, do i call it with candy.getMethod() or Inventory.getMethod()?

    thanks

Page 1 of 2 12 LastLast

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
  •