• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with creating methods  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For this problem I'm supposed to help a client at a local fitness company. The client provides data in the form of a text file. Each row is the number of calories consumed for breakfast, lunch, and dinner and the data included is for one week. (7 lines of text)
I'm supposed to print out: 1. list of the total number of calories consumed each day, 2. average number of calories consumed each day, 3. average number of calories consumed in each of the three meals, 4. the maximum number of calories consumed in any specific day, 5. and the maximum number of calories consumed in any one meal. All of these must have their own seperate methods and not be all put into the main method.
This is what I have so far but am having trouble actually starting each method.
Any help of any kind would be greatly appreciated!

The text file includes:
200 1000 800
450 845 1200
800 250 400
0 1500 1800
600 500 1000
700 1400 1700
675 400 900

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Store the data you read in some form of data structure.As you have limited data,
you can use arrays and for your case 2 dimensional array(3x7) would suit the needs.
Now once you have the data in your hand, use it in specific methods.
For example to list the total number of calories consumed each day I would write
method like 'printTotalCaloriesConsumedEachDay' wherein I would just iterate
through array and sum up values in all three cells for each row and print it.


Manish
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

There ain't no such thing as a 2D array in Java™.. What you have been told about is an array of arrays.
The code you have posted is difficult to read because you haven't availed yourself of the space available (which is unlimited), and squashed the { against the preceding line where they are difficult to find. If you use the indenting convention where you have { at the end of a line, separate the { from the line by several spaces.

Why have you marked everything static? That suggests to me that you are not creating objects. Why have you not got a diet class which you create objects from? I would suggest you need to go back to the drawing board and create a diet class, with methods like recordNextMeal, getCaloriesForWeek and overridden toString.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The code you have posted is difficult to read because you haven't availed yourself of the space available (which is unlimited)...

Actually, not quite. Especially here.

@Scott: Please read the DontWriteLongLines page for clarification, because you could probably do with breaking yours up a bit.

Winston
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All right … I suppose “unlimited” was a bit of an exaggeration.
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

There ain't no such thing as a 2D array in Java™


My bad. I stand corrected.

Manish
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem. Lots of people think there are 2D arrays.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!