• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Need help figuring out what I'm doing wrong (Shopping Cart Assignment)

 
Greenhorn
Posts: 11
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following is my code for the following assignment however, I do not know what in the world I did wrong or what I'm doing wrong. Help please!
This is my Code for ItemsToPurchase.java;;;

thanks
Pic-1.Jpeg
[Thumbnail for Pic-1.Jpeg]
 
Bartender
Posts: 10979
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see you tried to use Code tags but they ended up in the wrong place. In the future highlight your code first before clicking on the Code button. I fixed it for you this time.
 
Carey Brown
Bartender
Posts: 10979
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your class stuff needs to be inside a class definition


Your code needs to be inside a method inside a class definition


A main() method would start like this
 
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deadly King wrote:This is my Code for ItemsToPurchase


Where did you define such class? Please show us the exact line of code. Or specify line number in the code.

In lines 8, 14, 20 you can't assign that way and expect to something good. You need to refer one variable to an instance one (by using keyword 'this' <-- google it).
 
Marshal
Posts: 80230
424
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

You have already got some good advice. I would suggest you only need one Scanner pointing to System.in, and you shou‍ld never close it. I recommend you create a utility class that does all your reading from the keyboard and hides that solitary Scanner instance inside itself, but you probably haven't got the time to do that now. So I shall suggest you a cut‑down version of such a class:-Possible usage:-
int i = KeyboardInputs.getScanner().nextInt();
Because the constructor is private you cannot make subclasses of that class. You also cannot make instances, but you never need an instance.of that class anyway. Simply call the method the same way you would call any static methods.
Beware of calling nextLine on a Scanner after nextAnythingElse; there is a potential pitfall.
 
Dante King
Greenhorn
Posts: 11
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys/gals,
I figured it out. I also learned some new things that will useful in my future coding experience. I will also post my solutions in my next few post replies.
Thanks again! I'm really liking this community.
 
Dante King
Greenhorn
Posts: 11
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Dante King
Greenhorn
Posts: 11
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dante King wrote:

Please delete the comments which don't tell us anything new. We know you have private fields, so a comment saying private fields is no use to anybody.
The constructor is incorrect. It isn't a default constructor, and if somebody has told you that, they don't know what a default constructor is. It allows you to set up the object without a price or with an empty name. There is no guarantee that the set methods will ever be called, so it is quite possible to have a price and no description. Get rid of that constructor and replace it with one which requires a parameter for each field. You can do validation of those fields in the constructor, but that is probably too advanced a topic for the current discussion.
If you have arguments in the constructor, do you still need those set methods?
I am not sure that quantity is an attribute of an item. (Actually the word item means one thing.) I think the quantity belongs somewhere else. Imagine you are buying a box of DVDs price £10. You would call that
new Item("DVDs", 10)
and then you would have a quantity in the shopping cart.
Why have you got two total methods? What do they do that is different from each other? That is where comments would have been useful, explaining those two methods.
 
Campbell Ritchie
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see your instructions say the quantity shou‍ld be in the Item class. I don't like that design, nor do I like assignments which are so prescriptive, but I think you are stuck with it.
 
Liutauras Vilda
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After all good comments by Campbell, we don't know anything about your progress as you just posted code without any questions. And what shall we do with it? Is there anything you still don't understand? Somehow is lacking an input from your side. We expect a bit more from you.

Get rid of the habit to do comments styling (in case you decide to ignore advise not to write them as such at all), as it suggests that you don't know what to write, so spending time on painting and styling, which is not the right place (*.java source file) for that.

Concentrate on getting your code indentation and formatting correct - braces somehow aren't aligned properly. If you think it isn't important - that's incorrect, same as about calling that constructor a default. Default constructor is the one which Java adds for you when you don't define one. What you have here is no arguments constructor, which Campbell told you already was a bad idea to define such.

Do you have any instructions given to you, so you'd know what have you accomplished so far and what else is needed?
 
Dante King
Greenhorn
Posts: 11
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi thanks for the responses. That's exactly what I am having trouble with understanding where I mess up and what exactly I am doing wrong is hard to figure out. The Assignments we have done so far are currently very strict on how we write our code. If we write it and do not follow the instructions to be exactly how it told us to write the code it tells us that we cannot turn in the assignment and it also doesn't allow us to try anything that may help us in the long run. However, I wrote the comments in my code to help me understand what I was doing step by step. As for the basic constructor that is how we were told was a proper way to write it and use it. I am literally just starting out in java and learning new things everyday and so far this is probably the only helpful community that I have found to be helpful so far. This class is probably the worst possible way for me to learn java but I have to stick with it because I am learning the basics. Criticizing me for my errors doesn't necessarily help my situation and I understand clearly what you are telling me Liutauras but, this is how the previous assignments was teaching  us to do these things. Also I understand that you do not seem to be too fond of comments in code but not everyone understands exactly what is going on and this is what we were taught to do. I'm just implementing the things I was taught so far. As for the constructor I understand that not giving it a name is not correct because no one can assure that it ever gets called into use but when I was nearing the end of getting my constructor to work properly my assignments turn in area would not accept my assignment due to the constructor being name/identified.
 
Liutauras Vilda
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, follow instructions is a must. So what these instructions are? We haven't seen them, so pointing out things that aren't considered as good.
 
Liutauras Vilda
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dante King wrote:Criticizing me for my errors


Oh, I missed that, don't get us wrong, we don't criticise you, but your code, and you're learning, same as I and many others here - so every time somebody sees a bad thing - points out, even though it isn't a main discussion point. So, no worries about that

Can you post your instructions, so we could all have a look? You probably understand, that without seeing them not much we can help you, just to criticise code parts we see without knowing the context what is asked.
 
Campbell Ritchie
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dante King wrote:. . . probably the worst possible way for me to learn java

I am sure there are even worse places.

but I have to stick with it because I am learning the basics. . . .

No, you don't. You can always look for a different course.

I don't understand what you mean about naming constructors. Do you mean giving them parameters? I still think that constructor needs parameters and it would be incorrect to teach otherwise.
 
Dante King
Greenhorn
Posts: 11
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Dante King wrote:Criticizing me for my errors


Oh, I missed that, don't get us wrong, we don't criticise you, but your code, and you're learning, same as I and many others here - so every time somebody sees a bad thing - points out, even though it isn't a main discussion point. So, no worries about that

Can you post your instructions, so we could all have a look? You probably understand, that without seeing them not much we can help you, just to criticise code parts we see without knowing the context what is asked.



Ok, it's just hard to tell how things are said when reading it in text. My apologies if it sounded rude. However, the instructions for this assignment are in the picture in my post. Those are the only instructions they give us. Thats why I have a hard time understanding exactly what they are asking me to do. However, the second post where I posted my new code was the correct way to do the assignment. All the input you all gave me is great because it shows me that there is always a better and easier way to write code. I have completed my assignment. Thank you all for the input.
 
Dante King
Greenhorn
Posts: 11
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand what you mean about naming constructors. Do you mean giving them parameters? I still think that constructor needs parameters and it would be incorrect to teach otherwise.

yes giving it parameters. but my instructor said that I cannot place the constructor in a main method because it would not function correctly. She said to just create a new project and write the code for my constructor in that manner. It just pretty much confused me like crazy because the instructions are so vague and so is the learning material for the java course I am taking. However, I have created the correct code for this assignment and have completed my assignment correctly with code I posted with the comments. Also thank you for your input it cleared up what my professor was trying to say about the constructor.
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, here's one thing about those instructions: It got something very basic wrong.

  • String itemName - initialized to "none" in default constructor
  • ...
  • Default constructor

  • The dictionary definition of "default" that most closely applies here is: "through or in the lack or absence of." That is, a default constructor is one that the Java compiler provides by default when a class has no explicitly defined constructors.

    The default constructor that Java provides has no arguments. That is, it has an empty parameter list. Many call an explicitly defined constructor that takes no arguments a "default constructor" but that is just incorrect.

    The Foo class defined below DOES NOT have a default constructor. The Bar class DOES.

    Given the above class definitions, the following lines of code are both valid:

    Do you get the difference? Do you get why specifying that "itemName - initialized to "none" in the default constructor" is technically impossible?
     
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The correct way.


    and

     
    Campbell Ritchie
    Marshal
    Posts: 80230
    424
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Welcome to the Ranch

    Please note what it says on this forum:-

    We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.

    Please avoid complete solutions until the poster has had enough time to work it out for themselves. It doesn't help learning simply to provide an answer. In this case it is obviously an old t‍hread and complete solutions cannot do any harm.zwj;
     
    Straws are for suckers. Now suck on this tiny ad!
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic