Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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

Need help with an OOP exercise

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys

doing a beginners c++ course in my community college

only have a very little python done years ago

i have this exercise i need to do:

Write a program that takes in how many exercises you do (swim,run,pull ups and add one more) and how many
calories you burn in total at the end for each day, then work out how many you burned for the week.
ask the user what exercises were done for the day and for how many units (1 unit = 20min)
ask the user how many minutes they ran or swam for (no less than 10minutes (if they did the exercise) and no greater than 25min)
lookup what calories are burned / hour for each exercise, though this obviously doesn't have to be accurate.

i was shown how to plan such things and have done up a diagram, would like to plan more before coding so if you could check this for me i would appreciate it.
let me know if there is anything wrong

thanks again

oopcalorieexercise.jpg
[Thumbnail for oopcalorieexercise.jpg]
 
Marshal
Posts: 65806
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from the shapes of the boxes not being conventional UML, is swim a subtype of exercise or an instance of exercise? Your diagram suggests you think the latter.
If you are using units of 20 minutes, why are you asking about 10 to 25 minutes? I don't understand that part. Why are you using calories per hour rather than calories per minute?
 
Master Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example, why not make Exercise an abstract class or an interface with calculateCaloriesBurned() as an abstract method. This should then be implemented per discipline. I would assume that 1 unit of exercise (20min) of one sport (.e.g running) burns more calories than 20 min of another sport (e.g cycling).
 
Campbell Ritchie
Marshal
Posts: 65806
250
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Careful, BG, that we aren't going on to implementation details before the overall cocept and design of the project is finalised.
 
Brecht Geeraerts
Master Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, Campbell. That is true. It is so tempting to dive straight in but the best approach is to get the general concept thought through and only then start to implement it.
 
Campbell Ritchie
Marshal
Posts: 65806
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is easy for us who have more experience to see a design in 1 second and we are then in a position to start implementing code. Less experienced people need time to work out the design first.
 
Marshal
Posts: 14046
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before you even dive into a high-level design, you might want to clarify the requirements. This seems self-contradicting:

Eric Flynn wrote:ask the user what exercises were done for the day and for how many units (1 unit = 20min)
ask the user how many minutes they ran or swam for (no less than 10minutes (if they did the exercise) and no greater than 25min)


How does that even begin to make sense?
 
Eric Flynn
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"shapes of the boxes not being conventional UML" was just a quick sketch but point taken, yes would be instance of exercise

so regarding this:

"ask the user what exercises were done for the day and for how many units (1 unit = 20min)

sorry typed this wrong, 1 unit should equal 20 reps, not minutes
and is about 10 calories

all the exercise units are reps, not time, only running and swimming are timed

apologies

also, would it not be better to just get rid of all the methods and just have one method called doExcercise or something and just use various parameters in the call for various situations (ie depending on what you doing)
oh maybe calculateCalories can stay?




 
Junilu Lacar
Marshal
Posts: 14046
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:
also, would it not be better to just get rid of all the methods and just have one method called doExcercise or something and just use various parameters in the call for various situations (ie depending on what you doing)
oh maybe calculateCalories can stay?


In my experience, this approach where you try to think of all the different methods you need and all the fields that you need, etc. **up front** seldom works out well for non-trivial systems.  There's just waaaay too many things you're going to miss and spending all that time thinking of things up front tends to trap you in the pitfall of sunk cost. Even for small systems such as what you're wanting to write, evolutionary design, where you start out with a "theory" and slowly evolve the design as you incrementally add support for more and more functionality, keeps you from falling into that trap.

I say "theory" because the mindset is something like this: I start out with a small idea that I think will work. Then I write some test code to document what this idea might look like in code. Because there's no production code actually written, the "test code" isn't really test code: when I write it and the way I write it, it's really a specification of what you think is a good way to organize your objects. Once I think I have a good test, i.e. specification, I write the production code to make the test pass. Then I see if I can extend it further by adding some more functionality, again starting with a test to specify how I think the objects in the program should be organized and how they should behave around the new functionality.

Throughout this process, you're always watching out for "code smells" -- things that make working with the code difficult. This is very important because you don't want to keep moving forward with code that's difficult to work with and extend. This is where refactoring comes in. It's a way to head off problems by reorganizing your existing code so that it's easier to work with going forward.

In all my years of developing software, this is the process that has made the most sense to me. We're not perfect designers, even with many years of experience. The way you're being taught to approach design and problem solving harkens back to when I was still in school, more than three decades ago. The industry has learned much in those thirty years and but it seems academia is still stuck in the 1980s and 90s in the way they teach software development.
 
Junilu Lacar
Marshal
Posts: 14046
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of diving into details like thinking about classes, fields, hierarchies, etc., I would ask myself questions about what somebody using the program might want to achieve. Then I'd ask questions about what kind of information they'd have to provide to the program to achieve that goal. I'd draw out a dialog between the user and the system, kind of like how people producing movies create storyboards so they have a better idea of how they're going to shoot scenes, what it's going look like to the viewer, how the scenes support the plot and form the arc of the story.

When you think about all these high-level ideas first, it helps you see which low-level details are really important. Contrast that with the way you seem to be heading. If you were making a movie, it'd be like you were already diving into the kind of costumes your going to need, what props you're going to create, how many meals your going to prepare for the cast, etc. You're basically drowning yourself in complex details before you have a clear idea of the big picture.

 
Eric Flynn
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fantastic will use this approach
Once you have the base is it easy to modify?

I have a slightly larger project I would like to do. Will make my script and present it here

I would love to post stages up here and see what you think, your help is greatly appreciated!!!
 
Junilu Lacar
Marshal
Posts: 14046
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Absolutely. I'm happy to see your willingness to try this approach and will do what I can to help you work your way through it.
 
Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person?
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!