Results 1 to 15 of 15
Like Tree2Likes
  • 1 Post By gimbal2
  • 1 Post By sehudson

Thread: empty constructors

  1. #1
    Join Date
    Nov 2013
    Posts
    29
    Rep Power
    0

    Default empty constructors

    Java Code:
    package collectionsHw_netid;
    import java.util.ArrayList;
    
    public class ShoppingCart {
    	//field items
    	static ArrayList <String> food = new ArrayList<String>(); 
    	static ArrayList <Double> price = new ArrayList<Double>(); 
    	
    	// Empty Constructor 
    	void emptyCart ()
    	{
    
    	}
    	public static void food (String items [])
    	{
    		//initialize
    		food.add("Milk");
    		food.add("Cookies");
    
    		price.add(2.80);
    		price.add(3.90);
    
    
    
    
    	}
    	public void print(){
    		for (String name : food){
    			System.out.println(name);
    		}
    		for (String money : food){
    			System.out.println(money);
    		}
    
    	}
    	public static void main (String[] args){
    
    
    	}
    
    
    }
    instructions
    Create a new class, ShoppingCart, with a single field, items, that can hold any number of items (Hint: use an ArrayList)
    Create a default constructor that creates an empty cart
    Create the following methods. After you write each method, test it by running main.
    insertItem which adds an Item to the end of the cart (use only 1 parameter)
    print which prints the name and price of each item on a separate line using a loop
    removeItem which removes an item at a given index
    Last edited by shad0wblade890; 02-26-2014 at 05:31 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default Re: empty constructors

    I'm not certain what your prof means by an empty constructor. The default constructor for a class MyClass is smply

    Java Code:
    MyClass() {
    }
    It may or may not contain code to initialize the instance. In your case, I presume it must contain what ever code is required to
    satisfy the definition of an empty cart.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    354
    Rep Power
    5

    Default Re: empty constructors

    Can you post some code or instructions as to what exactly you are trying to program?

  4. #4
    Join Date
    Nov 2013
    Posts
    29
    Rep Power
    0

    Default Re: empty constructors

    updated my post

  5. #5
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    161
    Rep Power
    0

    Default Re: empty constructors

    Ok, so, the format for constructors is basically always the same

    Java Code:
    public classname(parameters) {
    do something with parameters here;
    }
    Then to create an instance
    Java Code:
    classname instancename = new classname(parameters for constructor);
    NOTICE: a constructor has NO void. It doesn't simply return nothing. It builds a class. All you use for a constructor is "public classname()", without the "void"
    You can read more about this here: http://docs.oracle.com/javase/tutori...structors.html

    It's good that you realized that your constructor and other methods (except for the main method there) need to be non-static. That is correct. Since they depend on a specific instance, they shouldn't be static.


    I don't see anything else that you need help with. Once you take out the void and add a public, you should be on the right track.
    Last edited by AlexGraal; 02-26-2014 at 05:49 AM.

  6. #6
    Join Date
    Nov 2013
    Posts
    29
    Rep Power
    0

    Default Re: empty constructors

    whitch lines are you suggesting me changing??

  7. #7
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,675
    Rep Power
    5

    Default Re: empty constructors

    "Please spoon-feed me the answer so I don't have to think". NO. Read Alex's reply and try to understand it - you have to actually learn something here, not be told what to change.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    Join Date
    Nov 2013
    Posts
    29
    Rep Power
    0

    Default Re: empty constructors

    gimbal2, please dont say stuff that can hurt people. you don't know that person. if you have nothing good to say just dont say anything! ill admit i have a birth disable called cerebral palsy (mild) and i may have a hard time learning this stuff but i am learning.

  9. #9
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,675
    Rep Power
    5

    Default Re: empty constructors

    I certainly hope you choose not the be hurt. Notice how I pass the ball back to you? I suggest you get used to this, the internet is full of plonkers like me who are out for your best interest in stead of trying to just win points by providing you with easy answers. If you get angry at me, I'll take the hit. A few years from now you'll think fondly of me.

    If there is anything in Alex's very helpful response that you don't understand, feel free to ask a more specific question. But the only way you leave this thread any better is if you're going to have a EUREKA! moment and you then go change your code for the better yourself based on your understanding of this material.

    Agreed or no? Either way I'll get out of your hair.
    SurfMan likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    Join Date
    Nov 2013
    Posts
    29
    Rep Power
    0

    Default Re: empty constructors

    Can any one explain what does static mean or does? and you are right there are many different people on the net and can be a mean place if you dont know how to handle it. thanks for the quick reply. and i would like for you to stick around because coders need coders thats what i believe in. and i got much to learn still about programming lol

  11. #11
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,675
    Rep Power
    5

    Default Re: empty constructors

    Thank you!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    918
    Rep Power
    2

    Default Re: empty constructors

    Quote Originally Posted by gimbal2 View Post
    I certainly hope you choose not the be hurt
    That is the best touché I have read this year.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  13. #13
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    354
    Rep Power
    5

    Default Re: empty constructors

    Quote Originally Posted by shad0wblade890 View Post
    Can any one explain what does static mean or does?
    In JAVA, you have the idea of class vs instance. When something is static, it belongs to the class instead of a specific instance. Usually, things like utility methods are made static, because they are not tied to a particular instance of the class, so you don't have to create an instance of that class in order to use the method. A good example of this is the pow() method in the Math class:

    Java Code:
    int power = Math.pow(2,5);
    Notice that you don't create an INSTANCE of the Math class in order to use the pow() method, I simply use the name of the CLASS to call the method.

    On the other hand, your ShoppingCart has a print method. That is a non static method, because it is very much tied to a particular instance of a ShoppingCart.

    Java Code:
    ShoppingCart myGroceryCart = new ShoppingCart();
    //add some food
    myGroceryCart.print();
    
    ShoppingCart yourGroceryCart = new ShoppingCart();
    //add some food
    yourGroceryCart.print();
    When we call myGroceryCart.print(), the print method is acting upon myGroceryCart. Similarly, when we call yourGroceryCart.print(), the print method is acting upon yourGroceryCart, so print would be a non static method.
    Last edited by sehudson; 02-26-2014 at 04:43 PM.
    gimbal2 likes this.

  14. #14
    Join Date
    Nov 2013
    Posts
    29
    Rep Power
    0

    Default Re: empty constructors

    Java Code:
    package collectionsHw_netid;
    import java.text.DecimalFormat;
    import java.util.ArrayList;
    
    public class ShoppingCart {
    	//field items
    	static ArrayList <String> food = new ArrayList<String>(); 
    	static ArrayList <Double> price = new ArrayList<Double>(); 
    	
    	// Empty Constructor 
    	ShoppingCart (){
    
    	}
    	
    	public static void food (String items []){
    		//initialize
    		food.add("Milk");
    		food.add("Cookies");
    
    		price.add(2.80);
    		price.add(3.90);
    		
    		food.remove(1);
    	}
    	// print in voice
    	public ArrayList<String> printinvoice(double gettotal){
    		if (food.add("Milk") && food.add("Cookies"))
    			System.out.println(food);
    			System.out.println(gettotal);
    		return food;
    	}
    	// insert unique item. NEED HELP WITH THIS!
    	public boolean insertuniqueitem(boolean food){
    		
    		
    	}
    	// get the max price item
    	public double maxprice(){
    		return 3.90;
    		
    	}
    	// toString method
    			public void tostring(){
    				DecimalFormat df = new DecimalFormat("#.##");
    			}
    	// get total
    	public double gettotal(boolean food){
    		double total = 0.0;
    		   if (food=true)
    		     total = total + 2.80 ;
    		   if (food=true)
    			     total = total + 3.90;
    		   else 
    			   System.out.println(total+total);
    		return total;
    
    		}
    	// print
    	public void print(){
    		for (String name : food){
    			System.out.println(name);
    	}
    		for (String money : food){
    			System.out.println(money);
    	}
    
    	}
    	public void main (String[] args){
    
    
    	}
    
    
    }

    part 2 for the shopping cart class that i forgot.

    Create the following methods. After you write each method, test it by running main.
    getTotal which returns the total price of all items in the cart using a loop
    printInvoice which prints the details of each item on a separate line using a loop, followed by the total
    insertUniqueItem which adds an item only if no other items in the shopping cart have the same name
    getMaximumPricedItem which returns the most expensive item in the cart
    Have Item.toString() return price to 2 decimal places

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default Re: empty constructors

    I will say that Java is case sensitive, so if the requirements say "getTotal" then "gettotal" is not correct.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Help with constructors
    By Modulus in forum New To Java
    Replies: 2
    Last Post: 02-11-2013, 01:12 AM
  2. Constructors
    By zhider in forum New To Java
    Replies: 5
    Last Post: 11-13-2012, 03:43 AM
  3. Constructors
    By cups in forum New To Java
    Replies: 1
    Last Post: 02-15-2012, 11:55 AM
  4. Need help with constructors
    By tpfaff in forum New To Java
    Replies: 10
    Last Post: 10-22-2010, 04:33 AM
  5. Constructors?
    By annna in forum New To Java
    Replies: 3
    Last Post: 01-27-2010, 10:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •