Lab10p

advertisement
CMPT 183 Lab 10
Pre Lab Activity
Objective: At the end of this lab you will be able to:

Write a complete Java class that simulates an object, a single Die.
Suppose that you wish to model something that you see in everyday life, namely something that you wish
to simulate with a computer. The first step is to examine this object, and determine what characteristics
it has, along with actions that it can perform, or have performed to it. Let us try to model a single die as
an object in a computer program.
1. Write your answers on this paper or a separate one, this will be
turned in!
2. What characteristics does this single die have, write down as many as you can
think of right now.
3. What does this die do? What can it do (if there is anything) or what can someone
do to it.
4. Now that we have established these two things, let’s combine them. What do the
characteristics have to do with the actions and vice versa? Just group the things
together.
5. In item one, we established things that can be considered variables and constants,
so in this step declare them as such. Eliminate things that may not be necessary.
6. In item two, we established methods (functions). Try to write some functions to
simulate the actions. Eliminate things that may not be necessary.
7. Create a class using the variables, constants, and methods you have just created.
For now everything can be public (just so that you know it will work), but try to
identify what items COULD be private. Variables usually can be private, with a
function to return the value of the variable being public. This prevents access to
the variable [referred to as “Information Hiding”].
8. Check everything, and go to the computer to start.
CMPT 183
Lab # 10
Objective: At the end of this lab you will be able to,
 Create a JAVA class file containing a single self created object.
 Utilize this new class file with the help of a driver program.
 Modify an existing program to utilize objects.
1. Open an editor such as “pico” with the file name “die.java”.
2. Type the following class template.
/*************************
*Author: !YOUR NAME!
*
*File:
Die.java
*
*Date:
10-30-02
*
*************************/
import java.util.Random;
public class Die
{
}
3. Using what we did in the pre-lab activity, add the necessary functions, variables,
and constants to this class definition.
4. Save the program with ctrl - X, make sure it has the name “Die.java”!
5. Compile Die.java using javac.
6. Correct errors if necessary.
7. Open “NewDice2.java” with “pico”. Make the necessary changes to the file to
utilize the new “Die” class. This would mean things like creating die objects,
using a function from the die class to simulate a roll, being able to select the size
of the die. Etc.
8. Save the program with ctrl - X, make sure it has the name “NewDice2.java”!
9. Compile NewDice2.java using javac.
10. Correct errors if necessary.
11. Run NewDice2 with java, do not run Die
12. Make a script file, Lab8.txt, show the code of Newdic2.java and Die.java
(use cat) and also the output of Newdice2.java several times. Also hand in the
sheet of paper with your hand written work from the pre-lab.
13. Print Lab8.txt lpr –PRI108H Lab8.txt
Download