RU beehive logo ITEC dept promo banner
ITEC 120
2012fall
dbraffitt
ibarland

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs

lab11a
Command-line Java
optional: encoding messages

Compiling and Running Java from the command line

We can still write, compile, and run Java programs without using an IDE like BlueJ or Eclipse.

Your task

  1. cd to a directory on your H: drive where you keep your ITEC120 files.
  2. Write a program with just a main that prints out main to world, main to world, come in world?. Compile and run it.
  3. Modify your main so that it prompts the user to enter their favorite number and their favorite beverage, and (for now) just prints out that information.
    Enter your favorite number: -3
    Enter your favorite beverage: ginger-ale
    I read the int -3 and the string ginger-ale.
    
  4. Instead of printout that information, print the user's favorite verse of the beverage song.
    Enter your favorite number: -3
    Enter your favorite beverage: ginger-ale
    In that case, you'll love this verse:
    -3 bottles of ginger-ale on the wall,
    -3 bottles of ginger-ale;
      take one down,
      pass it around,
    -4 bottles of ginger-ale on the wall.
    

    Of course you'll modify copy in your lab10 solution to add main appropriately. (You can also use posted solution [link opens better in Firefox than IE].)
  5. Finally: Modify main so that it does the above repeatedly, until they enter control-C (sometimes written “^C”) to signify the end-of-input, instead of a number+name.
    Windows ^C vs Unix ^D:

    On unix (and mac), use control-D (“^D”) to signify end-of-input rather than control-C (for Windows).

    unverified: On Windows-8, you also need to use control-D?

    Use java.util.Scanner#hasNext.
    Scanner timing: Note that like all Scanner methods, hasNext can feel slightly counterintuitive since it answers about the next input past or future; if there is no past input queued up, then the function-call will “block” until some input finally does arrive. This can be mis-interpreted as an infinite loop, if the user doesn't realize the computer is waiting for them to type something.
    Enter your favorite number (or ^C to quit): -3
    Enter your favorite beverage: ginger-ale
    In that case, you'll love this verse:
    -3 bottles of ginger-ale on the wall,
    
    -4 bottles of ginger-ale on the wall.
    
    Enter your favorite number: 12345
    Enter your favorite beverage: cheerwine
    In that case, your favorite song must be:
    12345 bottles of cheerwine on the wall,
    
    12344 bottles of cheerwine on the wall.
    
    Enter your favorite number (or ^C to quit): ^C
    Bye!
    

Practice: Encryption (optional)

Write a program which reads in a message, and prints out an encrypted version of the message (for any of the three low-brow encryption schemes mentioned below, or one of your own).
In all cases though, use helper functions as appropriate. (Can you think of useful helper functions you'll write, in advance?)

Thoughts:

1 You could even use Microsoft Word, if you save-as plain text. Word isn't tailored for Java though, so it won't give you any help with indentation or editing. You'd want to switch to a monospaced font like Courier or Consolas.      

2Actually, byte code is interpreted by the Java Virtual Machine (“JVM”); but the JVM is a program that your computer does run directly.      

3Technically, the issue is that the terminal automatically echoes all typed input.      

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs


©2012, Ian Barland, Radford University
Last modified 2012.Nov.08 (Thu)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme