Results 1 to 3 of 3
- 02-24-2013, 05:32 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 2
- Rep Power
- 0
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);
*/
}
}
- 02-24-2013, 06:03 PM #2
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
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:
Now, if you want to create an object of type SomeClass, you simply use the new keyword like this:Java 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); } }
Hope this was helpful.Java 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(); }Last edited by AndrewM16921; 02-24-2013 at 06:06 PM.
- 02-24-2013, 06:33 PM #3
Member
- Join Date
- Feb 2013
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
call default constructor inside of parameterized constructor after conditional check
By Googol in forum New To JavaReplies: 5Last Post: 08-11-2012, 09:50 AM -
Java - Constructor Method versus Constructor
By brocksoffice in forum New To JavaReplies: 1Last Post: 08-01-2012, 09:17 AM -
How to call a master constructor from a minor constructor?
By b.m in forum New To JavaReplies: 5Last Post: 12-14-2011, 01:47 PM -
[SOLVED] How to pass information from child class to parent class
By pellebye in forum New To JavaReplies: 7Last Post: 05-06-2009, 12:42 PM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks