• Post Reply Bookmark Topic Watch Topic
  • New Topic

CS106A Aerobic Exercise  RSS feed

 
Bruno Ribeiro
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

Here I am again stuck on an exercise!
This one I don't even know where to start:



So I tried scanners, but it wasn't taught in class, so I suppose it's not to use them.
If you could give me some lights on where to start I would be most gratefull!

Regards,
Bruno
Aerobic_CS106A.png
[Thumbnail for Aerobic_CS106A.png]
Exercise
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you haven't learned about Scanner in class, how were you taught to get input from the keyboard?

Personally, I think if a teacher doesn't specifically say you can't use something, then use it.
 
Bruno Ribeiro
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'm learning on my own through the Stanford course. It's not that I can't use it, but since it hasn't been used in the classes, I guess it's not to use it nor I know how to. I could learn it but if there is a "beginner way" I would prefer.
I usually use readint or readstring.

Thanks
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what do you need help on?
 
Bruno Ribeiro
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How should I start?
Because I need to check if the user answered at least 5 times a number above 30, is the scanner the best approach? Because I can't see a way to analyse question by question.

Thanks
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bruno Ribeiro wrote:How should I start?
Because I need to check if the user answered at least 5 times a number above 30, is the scanner the best approach? Because I can't see a way to analyse question by question.

But you started already, and quite correct.
Probably it would help if you would write down the steps what you need to do in details, so you could better understand what you have been asked to do.
Try to write down in english simple words how you understand, what has to be done. Basically all your attempts to solve problems should start at this point, even if it looks straight forward.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you want to do probably, to put all user inputs in an array, because every time user enters minutes, you override "a". So it means you have only the most recent entry.
 
Bruno Ribeiro
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking, could I create another variable, b for example, that counts if a is greater than 30. Than if b is greater the five, the user has atleast 5 entries greater than 30? Is that a good way?

Thanks
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bruno Ribeiro wrote:I was thinking, could I create another variable, b for example, that counts if a is greater than 30. Than if b is greater the five, the user has at least 5 entries greater than 30? Is that a good way?

1. Get and store all user inputs for 7 days in 1 array.
2. Then you'll be able iterate through an array and access each element (minutes) and try to apply all your logic you stated above.

It is possible to check everything on the fly, but likely it wouldn't be the best approach.
 
Bruno Ribeiro
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking about the array, I was skeptical in how to reach the values in the array. I will try that and let you know how it went. Thank you very much for the support!
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.

Oracle has nice tutorial about arrays, so you can go through, to make sure you fully understand the concept.
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The basic form for a for loop to deal with an array is this:-
for (int i = 0; i < myArray.length; i++) ...
Always start with that form of loop, though obviously there will be circumstances where you have to alter that loop.
If you are printing the index you may find it better to print (i + 1) with () so you display 1 to 7.
a = readInt("How many minutes of aerobic exercise did you do on day " + (i + 1) + "?");
If you are using a Scanner, it tells you in the Scanner documentation how to read an int from the keyboard.

I think one reason you had problems is that you aren't displaying your input. If you had inserted the following line at line 12, you would have know what was happening:-
System.out.printf("You entered %d%n", a);
Then you would know whether the entry was working.

Have you been through the Java® Tutorials about arrays. I think that will tell you how to populate an array; you then have to work out how to combine that with a for‑loop.
 
Bruno Ribeiro
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell for the additional material.

So I only had time to test my theory with the counter. It worked. I will definnetly try with arrays since I think I will need to learn them well for future challenges!
Thank you much guys for the support, I really needed someone to brainstorm with someone.
I wont post my answer here or it will ruin the exercise for other who will try this, but your advice did help me a lot!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!