Results 1 to 3 of 3
  1. #1
    Ormolu611 is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default 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);
    */

    }




    }

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    261
    Rep Power
    6

    Default 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:

    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);
    	}
    }
    Now, if you want to create an object of type SomeClass, you simply use the new keyword like this:
    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();
    }
    Hope this was helpful.
    Last edited by AndrewM16921; 02-24-2013 at 06:06 PM.

  3. #3
    Ormolu611 is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default Re: How do I pass information to a constructor?

    Quote Originally Posted by AndrewM16921 View Post
    Hope this was helpful.
    Yes, I think so...thanks!

Similar Threads

  1. Replies: 5
    Last Post: 08-11-2012, 09:50 AM
  2. Java - Constructor Method versus Constructor
    By brocksoffice in forum New To Java
    Replies: 1
    Last Post: 08-01-2012, 09:17 AM
  3. Replies: 5
    Last Post: 12-14-2011, 01:47 PM
  4. Replies: 7
    Last Post: 05-06-2009, 12:42 PM
  5. Replies: 0
    Last Post: 12-19-2007, 09:10 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
  •