• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Help with Java donut exercise

 
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't even start thinking about a Donut class at this point. My sense is that it's too early to do that. What it does is it locks you into a design decision. I can think of a number of ways to implement the functionality without a Donut class. Maybe eventually we'll say, "You know what, I need a Donut class for this" but not when all you have translated from the script is how to display a greeting at the start of the program.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im busy with

The user is then presented with the main menu

a. Plain donut  £1.99
b. Sugar donut €2.50
c. Chocolate donut €2.99
d. Sprinkled donut €2.99
e. Exit program

and carey has a good point, how can i use name and price from object in a method to loop the menu without physically typing out the menu?

 
Junilu Lacar
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simplifying the problem will help you see how. If you had five numbers and you wanted to use a loop to display them, how would you do it? If you had five strings?

The logic to do that is fundamentally the same, so once you figure out how to display N numbers, you basically have the code to display N anything.
 
Eric Flynn
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
will write some tests
 
Eric Flynn
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok so i went to the drawing board and started again, same idea, ditched the donuts for a shop with items but its the same idea.

here is my code, im sure you will tell me what is wrong and what must change, i will let you comment first and then I have a few questions, so i havent gone further with it.

Shop Class:



ShopItem Class:



 
Saloon Keeper
Posts: 6243
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are repeating some of the same mistakes pointed out in your Donut example.

In order to reduce the redundancy of your code your items need to be stored in an array.


Do not use 'float', leave as a double.
 
Junilu Lacar
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Eric, I think you have shown enough effort at this point to warrant giving you a concrete example of what we mean. That might help clear up some of the mental hurdles I sense you're struggling with. Stay tuned...

Carey and others, feel free to give Eric some snippets of real code that you might write in this situation.
 
Carey Brown
Saloon Keeper
Posts: 6243
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An example of how DONUT_CHOICES array is used in several places, e.g. menu, to reduce code redundancy. See also Donut.toString().
 
Junilu Lacar
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, for starters, I think you've already been told about how MainIsAPain (←that's a link). Instead of having all that code in your main() method, it should only consist of a few lines of code. In fact, it would look much like this:

That's it. You see how I'm breaking things down into smaller, more focused steps but still don't have a lot of details in the code? From here, I'll go into the start() method and probably break it down into something like this:

Then I just keep drilling down into each method until it makes sense to actually write detailed statements rather than just have a bunch of high-level method calls. (Methods that have only calls to other methods are called Composed Methods, by the way)
 
Eric Flynn
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much,

i will study this and write another program from scratch
 
Junilu Lacar
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just some thoughts on methods like this:

Names are important. Make sure method names aptly describe everything that they do. If you find it difficult to come up with a succinct name that completely represents what the method does, the method is probably doing too much and needs to be decomposed further. I think that's the case in this makePurchases() method.

The main smell I'm getting from this code is a violation of the Single Level of Abstraction Principle. I'd also question the scope: why public? I think the scope can be reduced to private.

Here's another way to code that:

or something like that. (That's all just off the top of my head)

I don't concern myself with the number of lines of code or number of methods and method calls happening. The main focus is clarity of purpose and having small, readable chunks of functionality. I find this a much better way to save time because developer time measured in seconds/minutes versus tens of minutes or hours of debugging is much more significant than a few piddly microseconds you save by avoiding another method call at runtime. Besides private methods are often optimized by inlining them where they are called anyway.
 
Eric Flynn
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why can you use an array and not plain variables for looping?
 
Marshal
Posts: 65814
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Eric Flynn wrote:Why can you use an array and not plain variables for looping?

Don't understand that question, I am afraid.
 
Marshal
Posts: 7181
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Eric Flynn wrote:Why can you use an array and not plain variables for looping?


Streams to iterate over an array elements?
 
Carey Brown
Saloon Keeper
Posts: 6243
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Eric Flynn wrote:Why can you use an array and not plain variables for looping?

"Plain" variables have no implied organization to them. There is no concept that one precedes or follows another. Arrays, on the other hand, guarantee a strict linear organization where elements can be accessed randomly or sequentially by use of an index.
 
Junilu Lacar
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Eric Flynn wrote:Why can you use an array and not plain variables for looping?


To illustrate Carey's eloquently put point, here's some code:


versus

The variables one, two, etc. are individual values. Unlike the values stored in the numbers array in the first code example, these values are not organized in a way that they can be accessed in an abstract way. You can't use a regular for-loop either:

This will not give you the correct results because the semantics are wrong.
 
Evil is afoot. But this tiny ad is just an ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!