Take the program from the prior lesson and add some dynamic capability by asking the user for values and using them in the program.
1. Make your program prompt the user for their name. Then make your program use the name as so:
~> Welcome to the PI exponentiator! What is your name?
~> Hello so-and-so, here are your PI values:
~> same output as in prior lesson follows
2. After asking them for their name, also ask them to what power of PI would they like to see. If the value they type is not an integer, or it's greater than 10, make the program re-prompt them, telling them invalid value. E.g.
~> Welcome to the PI exponentiator! What is your name?
~> Chuck
~> Hello Chuck, what power of PI being raised to would you like?
~> none
~> Sorry Chuck, invalid value, please enter the number again.
~> 4
~> print prior lessons output to the 4th power
Goals for this lesson: Process dynamic input values, use conditional logic, basic validation, control flow with if-then constructs, introduction to Exception - in particular NumberFormatException.
Suggested reading: Intro chapters in a java book. Read the section explaining Exception.
Friday, April 10, 2009
Monday, April 6, 2009
The basic basics, using java
Download and install the latest JDK from sun.
Create a simple class file that does the following: Print to a shell (dos or unix shell) the following line 10 times, increasing the multiplying power each time.
The value of PI raised to the n power is Pi^n
Where n is the integer range of 1..10.
Compile the .java file that you created into the corresponding .class file using javac from the shell. Run the .class file using the java command.
Goals for this lesson: Get familiar with installing java and using it in its raw form. Learn basic looping control flow and simple I/O.
Suggested reading:
http://java.sun.com/javase/6/docs/api/ Math
Read the intro chapters in a java book where control flow and basic I/O are covered.
In your shell type 'java' and 'javac' and reading through what's there, trying out various options.
Create a simple class file that does the following: Print to a shell (dos or unix shell) the following line 10 times, increasing the multiplying power each time.
The value of PI raised to the n
Where n is the integer range of 1..10.
Compile the .java file that you created into the corresponding .class file using javac from the shell. Run the .class file using the java command.
Goals for this lesson: Get familiar with installing java and using it in its raw form. Learn basic looping control flow and simple I/O.
Suggested reading:
http://java.sun.com/javase/6/docs/api/ Math
Read the intro chapters in a java book where control flow and basic I/O are covered.
In your shell type 'java' and 'javac' and reading through what's there, trying out various options.
What's this blog all about?
Real simple. This is a self-study, lean and mean course designed to get you writing software quickly.
Subscribe to:
Posts (Atom)