Results 1 to 20 of 31
- 03-28-2009, 10:13 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 14
- Rep Power
- 0
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 10:24 PM.
- 03-28-2009, 10:15 PM #2
Member
- Join Date
- Mar 2009
- Posts
- 2
- Rep Power
- 0
OK i will ry to solve it
-
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.
- 03-29-2009, 02:50 AM #4
Member
- Join Date
- Mar 2009
- Posts
- 14
- Rep Power
- 0
: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? } }
- 03-29-2009, 04:35 AM #5
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
-
- 03-29-2009, 04:43 AM #7
Member
- Join Date
- Mar 2009
- Posts
- 14
- Rep Power
- 0
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!
-
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.
-
Alright here's a trivial example of what I mean:
Fubar1 class that holds some data
Fubar1Test class to create an array of Fubar1 and fill with user inputJava 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; } }
enjoyJava 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]); } } }
- 03-29-2009, 05:15 AM #10
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
to guess at what Fubarable means...
I think fubarable means that your Candy class should have a constructor in it, such as
you could then call this constructor in another class that accepts input to set the variables.Java Code:public Candy(string name, double weight, double cost, int IDNumber){ }
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!
-
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.
- 03-29-2009, 05:24 AM #12
Member
- Join Date
- Mar 2009
- Posts
- 14
- Rep Power
- 0
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
also teach says there should be 2 classes... i dont get it...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]); } } }
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.
thanksLast edited by katalyst; 03-29-2009 at 05:37 AM.
- 03-29-2009, 07:20 AM #13
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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).
- 03-29-2009, 07:57 AM #14
Member
- Join Date
- Mar 2009
- Posts
- 14
- Rep Power
- 0
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...
- 03-29-2009, 08:09 AM #15
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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.
- 03-29-2009, 05:43 PM #16
Toadaly... that chair example is great !!!
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 03-29-2009, 07:34 PM #17
Member
- Join Date
- Mar 2009
- Posts
- 14
- Rep Power
- 0
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
- 03-29-2009, 07:50 PM #18
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
As for get/set methods...
getters return the value of the variable you are getting
e.g.after using this method, if you used a system.out.println statement to display currentname and myCandy.name, they would be exactly the same.Java Code:String currentname = myCandy.getName()
setters set the value of the variable
e.g.after using this method, the name of myCandy would be "the new name for myCandy".Java Code:myCandy.setName("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!
- 03-29-2009, 08:54 PM #19
Member
- Join Date
- Mar 2009
- Posts
- 14
- Rep Power
- 0
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!
- 03-29-2009, 09:48 PM #20
Member
- Join Date
- Mar 2009
- Posts
- 14
- Rep Power
- 0
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:
only compile error i get is the red line "cannot find symbol method setNumber()"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); } }
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
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
Classes and Methods help
By border9 in forum New To JavaReplies: 5Last Post: 01-30-2009, 06:51 PM -
Arrays & Methods
By TheRocket in forum New To JavaReplies: 1Last Post: 12-10-2008, 07:37 PM -
How to call methods of different classes
By adeeb in forum New To JavaReplies: 2Last Post: 06-06-2008, 06:08 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks