Chapter 2 Lab Java Fundamentals Lab Objectives
Chapter 2 Lab Java Fundamentals Lab Objectives
Java Fundamentals
Lab Objectives
Write arithmetic expressions to accomplish a task
Use casting to convert between primitive types
Use a value-returning library method and a library constant
Use string methods to manipulate string data
Communicate with the user by using the Scanner class or dialog boxes
Create a program from scratch by translating a pseudocode algorithm
Be able to document a program
Introduction
This lab is designed to give you practice with some of the basics in Java. We will
continue ideas from lab 1 by correcting logic errors while looking at mathematical
formulas in Java. We will explore the difference between integer division and division
on your calculator as well as reviewing the order of operations.
We will also learn how to use mathematical formulas that are preprogrammed in Java.
On your calculator there are buttons to be able to do certain operations, such as raise a
number to a power or use the number pi. Similarly, in Java, we will have programs that
are available for our use that will also do these operations. Mathematical operations that
can be performed with the touch of a button on a calculator are also available in the Math
class. We will learn how to use a Math class method to cube the radius in the formula for
finding the volume of a sphere.
This lab also introduces communicating with the user. We have already seen how
console input and output work in lab 1. We will now need to learn how to program user
input, by investigating the lines of code that we need to add in order to use the Scanner
class. We will also learn the method call needed for output.
Alternately, you may use dialog boxes for communicating with the user. An introduction
to graphical user interface (GUI) programming is explored using the JOptionPane class.
The String class is introduced and we will use some of the available methods to prepare
you for string processing.
6
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Task #1 Correcting Logic Errors in Formulas
1. Download the file NumericTypes.java (see code listing 2.1) from the Student CD
or as directed by your instructor.
2. Compile the source file, run the program, and observe the output. Some of the
output is incorrect. You need to correct logic errors in the average formula and
the temperature conversion formula. The logic errors could be due to conversion
between data types, order of operations, or formula problems. The necessary
formulas are
score1 score2
average = C = 95 F 32
numberOfScores
3. Each time you make changes to the program code, you must compile again for the
changes to take effect before running the program again.
4. Make sure that the output makes sense before you continue. The average of 95
and 100 should be 97.5 and the temperature that water boils is 100 degrees
Celsius
1. Add an import statement above the class declaration to make the JOptionPane
class available to your program.
2. In the main method, prompt the user to enter his/her first name by displaying an
input dialog box and storing the user input in a variable called firstName (you will
need to declare any variables you use).
7
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
3. Prompt the user to enter his/her last name by displaying an input dialog box and
storing the user input in a variable called lastName.
4. Concatenate the firstName and lastName with a space between them and store the
result in a variable called fullName.
5. Display the fullName using a message dialog box.
6. Compile, debug, and run, using your name as test data.
7. Since we are adding on to the same program, each time we run the program we
will get the output from the previous tasks before the output of the current task.
1. Use the charAt method to get the first character in firstName and store it in a
variable called firstInitial (you will need to declare any variables that you use).
2. Print out the user’s first initial.
3. Use the toUpperCase method to change the fullName to all capitals and store it
back into the fullName variable
4. Add a line that prints out the value of fullName and how many characters
(including the space) are in the string stored in fullName (use the method length
to obtain that information).
5. Compile, debug, and run. The new output added on after the output from the
previous tasks should have your initials and your full name in all capital letters.
8
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Task #5 – Create a program from scratch
In this task the student will create a new program that calculates gas mileage in miles per
gallon. The student will use string expressions, assignment statements, input and output
statements to communicate with the user.
Miles driven Gallons used Miles per gallon Miles per gallon
(hand calculated) (resulting output)
2000 100
500 25.5
241.5 10
100 0
7. The last set of data caused the computer to divide 100 by 0, which resulted in
what is called a runtime error. Notice that runtime can occur on programs
which compile and run on many other sets of data. This emphasizes the need to
thoroughly test you program with all possible kinds of data.
9
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
class header, giving a brief description of the class. They are also used for
documenting methods in the same way.
2. Write a documentation comment at the top of the program which indicates the
purpose of the program, your name, and today’s date.
3. Add comment lines after each variable declaration, indicating what each variable
represents.
4. Add comment lines for each section of the program, indicating what is done in
that section.
5. Finally add a comment line indicating the purpose of the calculation.
//identifier declarations
final int NUMBER = 2 ; // number of scores
final int SCORE1 = 100; // first test score
final int SCORE2 = 95; // second test score
final int BOILING_IN_F = 212; // freezing temperature
int fToC; // temperature in Celsius
double average; // arithmetic average
String output; // line of output to print out
//TASK #2 declare variables used here
//TASK #3 declare variables used here
//TASK #4 declare variables used here
10
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
output = BOILING_IN_F + " in Fahrenheit is " + fToC
+ " in Celsius.";
System.out.println(output);
System.out.println(); // to leave a blank line
11
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.