Java Programming Basics
Week of 31 January, 2005
Note: if you are having password problems, please do a
reset. Your new password will be e-mailed to your UVa account.
This should correct the problem.
This laboratory provides us with our first opportunity to decompose a problem
into manageable pieces to solve.
There are two files you will have to submit for this lab:
SolvingABC.java and MathFun.java. Note that most of the "good
programming practices" listed on the
HW J1 page
are not required for this lab, due to the time constraints (it would be great if
you put them in, but you don't have to - there is no grade penalty for leaving
them out). The only one that is required is the header, which must include
your name and e-mail ID.
- Expression evaluation
- Hand-checking code
- Simple input and output
- Expressing mathematical equations in Java
- Class methods
Solving Your A, B, C's
- Examine the listing for SolvingABC.java.
- Record on paper what you expect the output to be.
- Download a copy of SolvingABC.java and store it in your home directory folder.
- Start up JCreator and compile and run SolvingABC.java.
- Did you get the same answers for your manual calculations as you did from the
computer program? If there are differences, try to determine why. If you cannot
determine the reason for the differences, ask a laboratory instructor for help.
- Allowing a user to supply input values is a better practice than hard-coding
the values in program instructions. Accepting input makes the program more
- Modify SolvingABC.java to prompt and extract from the user four integer
values. To do so you will need a Scanner object. The Scanner object must be
defined before the definitions of a, b, c, and d.
Scanner stdin = new Scanner(System.in);
- Add a prompt that tells the users of the program what you want them to do. In
this case, we first want to prompt the user to supply an integer for the
System.out.print("Enter integer value for a: ");
- Next modify the definition of a so that it is initialized with the next input
int a = stdin.nextInt();
- It is important to prompt the user for each value separately. So modify the
program also to individually prompt and extract values for variables b, c, and
- Save the program and compile it.
- After developing a program or modifying an existing one, a key question is,
“Does the program run correctly?” One way is to hand-check the program.
Hand-checking a program involves computing the results by hand for some inputs
and making sure the results agree with what the computer outputs for the same
- You can hand-check your modified program by using as inputs the values that
were used to initialize the integer variables a, b, c, and d in the original
program. Run your program and enter the values that were used to initialize the
variables a, b, c, and d in the original program. Did you get the same results?
- Once the program is working,
Using Class Methods
- Sun provides extensive documentation of Java and its
Examine its description of the
Math library to complete program MathFun.java.
- Program MathFun.java is to first extract two decimal values
(via the nextDouble() method in the Scanner class) and store them in
double variables x and y.
- The program is then to define and initialize several double variables.
- squareRootX – initialized to represent the square root of
- logY – initialized to represent the log base 10 of y
- expX – initialized to represent ex , where
e is Euler’s number
- minXY – initialized to represent the smaller of x and y
- maxXY – initialized to represent the larger of x and y
- xPowerY – initalized to represent xy
- The program then displays these variables. Using the
documentation for help, complete have MathFun.java invoke the appropriate
methods to correctly initialize the six preceding variables. For example,
squareRootX can be defined in the following manner.
- double squareRootX = Math.sqrt(x);
- Compile and run your completed program using 3 and 4 as input values for x and
- Once the program is working,
Be sure to log out of home directory when you are done. Also, please
delete any files you have saved on the local computer (but obviously don't
delete them from your home directory). Ask your TA if you have any
questions about this.
Work on the homework
Use any remaining time to work on