• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Why am i not able to call manually enter data for firstSandwich?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why am i not able to call manually enter data for firstSandwich? I am trying to call one object first sandwich form what is enter into the getData(Sandwich s( method and then have the secondSandwich call the data from the default constructor in Sandwich class.
I was unable to get the code to work in the editor so i attached me 2 class for review. And help or suggestion would be greaat as I am new to java and still trying to learn it.



 
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please edit and use code tags.
 
Siens Alot
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is there a doc on how to use the code tags? I tried to use them but couldnt get them to work?
Siens66
 
Sheriff
Posts: 27451
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
UseCodeTags
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Siens Alot wrote:I am trying to call one object first sandwich form what is enter into the getData(Sandwich s( method and then have the secondSandwich call the data from the default constructor in Sandwich class.


Why? It seems like awfully tortuous logic to me.

I was unable to get the code to work in the editor...


Why not? What happened? ItDoesntWorkIsUseless (←click).

However, your Sandwich class just looks wrong at the moment:
1. You only have one constructor, which returns you a Ham on Wheat for $4.99. ALWAYS.
2. You have setter methods which allow you to change the contents.

Is that what happens in real life?

I go into Sub Stop and ask for a BLT on rye, which costs $3.50.
The guy hands me a Ham on Wheat and asks me for $4.99.
I say "No, I asked for a BLT on rye".
He then rips the entire sandwich apart, changes the ingredients and the bread, chucks all the old ingredients in the bin, and asks me for $3.50.

I suppose it could work, but I doubt if Sub Stop would be in business for very long.

Have a think about it, and see if you can't come up with a better class.

HIH

Winston
 
Siens Alot
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Winston,

I understand that logic and i was think i need to put in another constructor but the few attempts i made failed and I have looked at a few you tube videos but got even more confused.
going to try and see if i can get that code edit to work so it is easier to see and I will also try to make a second constructor again thank you for the suggestion and any further help would be great trying to read straight from the book i bought is a bit confusing an I tried to find examples to explain plain but most seem to assume i already know java which as you can see i do not.


2nd Class




So can anyone give me an example of how i would right this 2nd constructor to collect the data from user input and not just the one i have working that gives the default constructor ham sandwich?


Thank you and Also thank you for the link on how to use the code feature much appreciated.

Siens66
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Siens Alot wrote:I have looked at a few you tube videos but got even more confused.


Hmmm. Not sure if that's the best way to learn - but I could be wrong.

So can anyone give me an example of how i would right this 2nd constructor to collect the data from user input and not just the one i have working that gives the default constructor ham sandwich?


I'll try. This (basically) is what you currently have:Now doesn't that look silly? You don't create a Person and then change their name; you give them a name WHEN you create them - and normally, they will have that name for the rest of their life.

So:
1. The setter methods are redundant.
2. The class needs a constructor that allows you to supply a name - and furthermore, it should be the ONLY way of creating a Person.

All of which gives you something like this:A couple of things to remember (at least for the moment):
1. Don't add setter methods unless you KNOW you're going to need them - and even then, think twice before you do, because there are often better alternatives.
2. Always write a constructor that takes arguments for ALL the private fields in your class. Now you might also add some others, but that one is a must.

Now see if you can translate that to your Sandwich class.

HIH

Winston
 
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can read about code tags here. I have corrected them for you and also removed some unnecessary blank lines which do nothing for legibility.

Beware of nextLine after nextAnythingElse. There is a pitfall which I have never seen in any of the books. Look here for an explanation and for a possible solution.

When Winston said you might also add some other[ constructors] he meant start off with the constructor which sets all the fields and delete all the other constructors. Then see whether you need other constructors later. You probably don't. You have doubtless been told that a constructor enables you to create objects. That is only half the story. A constructor also constrains you to create objects that way, so Winston's constructor with name means you must pass the details of the name to all Person objects. It means you cannot “forget” the name.
 
Heroic work plunger man. Please allow me to introduce you to this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic