How do I pass information to a constructor?
How do I pass information to a constructor?
Help! I am trying to complete my homework, but since I am taking an online course, I do not have the same resources for help that I might otherwise. I have been trying to do this for hours with no success. I know that the answer is probably simple – any hints available?
Here are the two existing classes that I am working with:
package c2;
public class Item {
String itemNum, itemName, itemUPC, itemQty, itemUM;
public Item(String iNum, String iName, String iUPC, String iQty, String iUM) {
itemNum = iNum;
itemName = iName;
itemUPC = iUPC;
itemQty = iQty;
itemUM = iUM;
}
public void show() {
System.out.println(itemNum);
System.out.println(itemName);
System.out.println(itemUPC);
System.out.println(itemQty);
System.out.println(itemUM);
}
public static void main(String []args) {
}
}
package c2;
public class ItemApp {
}
public static void main(String[] args) {
String itemNum = new String("99");
String itemName = new String("Cheerios 32oz");
String itemUPC = new String("4321");
String itemQty = new String("1");
String itemUM = new String("Gross");
System.out.println(itemNum);
System.out.println(itemName);
System.out.println(itemUPC);
System.out.println(itemQty);
System.out.println(itemUM);
}
}
I am asked to Comment out the code in ItemApp’s main method and then to add code that will:
1. Create an Item object and assign it to a variable called ItemObj
2. Pass the following 5 bits of information to the new Item object’s constructor:
44
Crest
6543
1
Half dozen
(I am told that these parameters must be separated by commas.
3. Finally, I am asked to invoke the Item object’s show method.
I am lost! The following is as far as I can get…and it is WRONG…any help please?
package c2;
public class ItemApp {
String Item;
public ItemApp(String itemObj) {
Item = itemObj;
}
public ItemApp() {
Item("44","Crest Tartar Ctrl 17 oz","5432","1","Dozen");
Item = new Item();
Item.show();
}
public static void main(String[] args) {
/* String itemNum = new String("99");
String itemName = new String("Cheerios 32oz");
String itemUPC = new String("4321");
String itemQty = new String("1");
String itemUM = new String("Gross");
System.out.println(itemNum);
System.out.println(itemName);
System.out.println(itemUPC);
System.out.println(itemQty);
System.out.println(itemUM);
*/
}
}
Re: How do I pass information to a constructor?
First, please wrap all java code in code tags. It's very difficult to read otherwise.
Now, I'll explain some of these concepts in a general sense so that you can apply them to your assignment.
A constructor does just that - it constructs the object. They are declared like this in their classes. Similarly, we'll define a simple show() method as well:
Code:
public class SomeClass
{
//We'll save the values we get from the
//constructor in variables called a and b
private int a, b;
//This is the constructor
public SomeClass(int param1, int param2)
{
a = param1;
b = param2;
}
//The show method simply prints the values
public void show()
{
System.out.println(a + ", " + b);
}
}
Now, if you want to create an object of type SomeClass, you simply use the new keyword like this:
Code:
public static void main(String[] args)
{
//notice the parameters 1 and 2 (both int)
//match the parameter types from the
//constructor we defined earlier.
SomeClass some = new SomeClass(1, 2);
//Now, to invoke the show() method, simply
//use dot notation like so:
some.show();
}
Hope this was helpful.
Re: How do I pass information to a constructor?
Quote:
Originally Posted by
AndrewM16921
Hope this was helpful.
Yes, I think so...thanks!