|
|
Note: if you are having
password problems with the submission system, please do a
password
reset. Your new password will be e-mailed to your
UVa account.
If you cannot find your Olsson 001 login and
password, you can have the TA reset your password for you. This password
will also be e-mailed to your UVa account.
If you cannot access
your home directory, please reset your password here.
Make sure your username for home directory is of the form ESERVICES\mst3k.
Purpose
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. You
will need to right-click on them, select "Save As...", and save them to
your home directory.
Key Concepts
- Expression evaluation
- Hand-checking code
- Simple input and output
- Expressing mathematical equations in Java
- Methods
Solving Your A, B, C's
- Examine the listing for SolvingABC.java.
Note that some parts of this program we are ignoring for now.
In particular, just examine the code within the main()
method.
- Record on paper what you expect the output to be.
- Note that the
percent sign (%) is a modulus operator -- it returns the reminder
of a division. So 7%2 equals 1, because when you divide 7 by 2, you
end up with a remainder of 1.
- Also note that when you are dividing two integer values, such as 7/2,
the result is always an integer -- thus, Java will return the result as 3.
If you divide two floating point numbers, such as 7.0/2.0, then Java will
return a floating point number (3.5).
- Download a copy of SolvingABC.java
and store it in your home directory folder. You need to
right-click on the file, and select "Save As...", and then save it to
your home directory.
- Start up JCreator and load SolvingABC.java.
- Change the headers to your name and e-mail ID (i.e. abc1d,
not your ID number). All program files that you submit for
this course need to have your name and e-mail ID at the top.
- 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 general.
- Modify SolvingABC.java to prompt and extract from the user
four integer values. To do so you will need a Scanner object.
The Scanner class is used to allow input from the keyboard,
as
discussed briefly in lecture. The Scanner object must be
defined before the definitions of a, b, c, and d.
Insert the following line into the program (right after the comment
'Scanner definition will go here').
Scanner stdin = new Scanner(System.in);
How exactly this works will we will be going over in more detail during
lecture. But the quick explanation is that this line creates
a Scanner object, which will allow us to get user input from the
keyboard. How to use the Scanner object is explained next.
Note that if you enter a non-integer (such as "foo") via the keyboard, the
program will bomb. There are ways to gracefully handle this, but we
haven't seen them yet.
- 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 variable a.
System.out.println ("Enter integer value for a: ");
This line goes right after the 'prompts and user input goes here'
comment.
- Next modify the code so that the value read in is stored in
the variable a.
Thus, you need to insert the line
a = stdin.nextInt();
This line goes right after the System.out.println()
statement from the last step. Because we are calling nextInt(),
this line will read in an integer from the keyboard, and stores that
value in the int variable a.
Note that you are declaring and intializing the int variable a under the
'variable definitions and initializations' comment, and then are giving
it a new value when you enter the 'a = stdin.nextInt();'
line.
- 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 d by putting in
both the System.out.println() statements and the
stdin.nextInt() statements for each of the remaining three
variables.
- 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 inputs.
- 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, submit
it. Note that you submit the SolvingABC.java file, not the
SolvingABC.class file.
Using Methods
- A function in Java, such as the sin()
function, or the System.out.println() function,
is called a method.
- Save MathFun.java
to your home directory -- you need to right-click on the file, and
select "Save As...".
- Change the headers to your name and e-mail ID (i.e. abc1d,
not your ID number). All program files that you submit for
this course need to have your name and e-mail ID at the top.
- Program MathFun.java first extracts two decimal values (via
the nextDouble() method in the Scanner class) and
stores them in double variables x and y.
Note that we are not expecting you to be experts with the
Scanner
class at this point. You should have a general understanding
of what
it does - it will be covered more in lecture and future labs.
- Sun provides extensive documentation of Java and its standard
libraries. Examine its description of the Math
library to complete program MathFun.java. Note that there a
lot of arithmetic functions available to use -- including sin(),
cos(), etc.
- The program is then to define and initialize several double
variables.
- squareRootX –
initialized to represent the square root of x. This one is
already done for you. Note that when you call a method in the
Math library, it must be called Math.sqrt(x) (just
putting sqrt(x) will not work).
- logY – initialized to
represent the log base 10 of y. Note that you want to return the base-10
log of y, not the base-e (or natural) log 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 Math
library documentation for help, complete MathFun.java so that it invokes the
appropriate methods to correctly initialize the six preceding
variables. For example, squareRootX
is 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 y, respectively.
- Once the program is working, submit
it. Note that you submit the MathFun.java file, not the MathFun.class
file.
Finishing up
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.
|
|