It's UWAweek 16 (1st semester, week 7)

help2005

This forum is provided to promote discussion amongst students enrolled in CITS2005 Object Oriented Programming.

Please consider offering answers and suggestions to help other students! And if you fix a problem by following a suggestion here, it would be great if other interested students could see a short "Great, fixed it!"  followup message.

How do I ask a good question?
Displaying the 2 articles in this topic
Showing 2 of 50 articles.
Currently no other people reading this forum.


 UWA week 10 (1st semester, week 2) ↓
SVG not supported

Login to reply

👍?
helpful
4:06pm Fri 8th Mar, ANONYMOUS

Hello, I have a question regarding how/if I would use Scanner to input different options each time I call the class? Or if there is another way? This is what I wrote for one of the coding bat questions: public class CaughtSpeeding { public static int caugSpee(int speed, boolean isBirthday) { if (isBirthday) speed = speed - 5; if (speed <= 60) return 0; if (speed >= 81) return (2); return (1); } public static void main(String[] args) { int bday = caugSpee(95, true); System.out.println(bday); } } Would I change main and add scanner (or whatever other option) or even just make the whole thing into main (incl 1st method) as a simpler option? Hope this makes sense lol TIA!


 UWA week 11 (1st semester, week 3) ↓
SVG not supported

Login to reply

👍?
helpful
11:07am Mon 11th Mar, Andrew G.

There are a couple of options for how to get input in a Java program that we have covered so far...

You can indeed use Scanner. You can construct a Scanner object wherever it makes sense (probably in main, for a little example like this) and use it to read input from stdin. Our slides and the documentation for Scanner have various examples on how to use Scanner and the methods it provides.

Alternatively, you could parse your input from command line arguments. Recall that the String[] args parameter of the main method is a list of the arguments passed when your program was run from the command line. You can use something like Integer.parseInt() to parse one of the arguments as an input that you can then pass to your method.

Of course there are several other ways of getting input into your programs at runtime, but either of these should be suitable for your purposes.

Cheers,
Gozz

The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Written by [email protected]
Powered by history
Feedback always welcome - it makes our software better!
Last modified  5:07AM Sep 06 2023
Privacy policy