Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
 
Object-Oriented Hangman Game 
This is a transcript of today’s lecture (March 31, 2010) in I107 
I started by saying that you are clear on the following, having developed and discussed programs with 
and/or about these in class, in lab or help sessions, not to mention your reading assignments: numbers, 
primitive types, operators, operands, value, expression, if, while, for, method (procedure, function, 
subroutine) classes and objects. static members, blueprint. Instance members, constructor. Other things 
we did as part of discussing these concepts and using/exploring them: 
 we wrote a Hangman game (used java.util.Scanner) but not in OOP style  
 we talked about and developed class Student { ... } 
 defined class extension mechanism, inheritance,polymorphism, dynamic method lookup. 
 we also wrote the following classes in the process: 
class Freshman { ... } 
class Sophomore extends Freshman { ... }  
class Horse { ... }  
class Unicorn extends Horse { ... }  
class Pegasus extends Horse { ... }  
So now we want to practice our OOP knowledge by turning the Hangman game written on 03/22/2010 
into an OOP version of it. We use the following script to randomly select contributors to the lecture: 
 
The script randomly selects a student to contribute to the ongoing discussion.  
We want to write a program in Java, with a clearly specified functionality:  
 
The code above is straight from the What’s New? page of the course website: 
 
So we start “drawing” students and for everyone who comes in front to work on the instructor’s 
workstation for 2-3 minutes the task is: take the smallest useful step in the direction of the solution. 
While in front of the room you don’t necessarily represent yourself, you mainly represent the others 
present in the room. You can ask questions, name students, and certainly have to type in. Adrian is here 
to help. When done you draw another student, write down a summary of what you contributed with 
and gi back to your seat.  
First Navin came and he decided to use DrJava. 
 
So he types drJava and the Google search system responds with: 
 
Not all links are about what we need, but the first few are.  
Take the first link and we have (at http://www.drjava.org): 
 
Click on “Download Jar File” and you get: 
 
We usually don’t have enough patience to wait and we click on the “direct link” but whichever you get 
to do first amounts to this: 
 
Now recall that you need to have Java installed already on the machine where you’re doing this (JDK). 
Click OK. You see the splash screen in the middle as DrJava gets started:  
 
When it starts it looks like this: 
 
Which is where Navin wanted us to be. Great job.  
Next Celia  came and she decided to clarify the requirements:  
 
Celia:  
 Write a program that plays Hangman with the user.  
 Do it by defining a class called Hangman. 
So she starts by defining such a class. When she presses “Compile” she is presented with:  
 
We need to save. 
 
Saving the file as Hangman.java allows compilation to 
proceed and we can test our class thus far:  
 
 
 
 
 
 
Nick  comes and announces his task: set the initial word (define instance variable word).  
Now Derek  comes and he decides to pursue Nick’s line of development.  
So he sets the following tasks for himself:  
 Define constructor that use reset method (see right side of table above) 
 Discuss what it mean for word to be static  
If the word variable were to be declared static (as Nick asked a bit earlier) what are the consequences?  
So he does:  
 
He shows us how this could be used: 
 
Derek’s work (see below for explanations): 
 
What this means in terms of interaction: 
 
What if word is declared as static? 
 
Easy: the word variable will be shared among instances as we see below:  
 
 
That’s not what we want. So we eliminate the 
static keyword.  
 
And along comes Grant  deciding to 
define a second instance variable called mask (see 
above).  
 
 
 
Grant was very graciously and competently assisted in 
his decision by  Thabang (twice). 
 
Next,  Andrew came and he decided to 
make the constructor initialize the mask properly:  
 
 
 
   
Here’s how Andrew’s code works: 
 
 
Next Patrick  came and he added a 
new instance method called play. This has part of a 
larger design discussion with Thabang earlier. So 
we had: 
 
 
Jared  came next and he wrote the entire method play: 
 
 
 
 Next Jordan  came and  defined a 
method that determines when the game is over: 
 
 
 
Finally Igor  came and he defined reset and integrated gameOver into the play method: 
 
 
  
Notice on the right the fruitful integration of gameOver into play with a call of reset. 
What was reset supposed to do? What Igor had told us all along (his first contribution was to suggest to 
Nick to add a constructor to Celia’s class so we get a test word into the game):  
    void reset () { 
      word = // [1] ... some new random word ... ; 
      mask = // [2] ... basically a series of dashes as many as letters in word ... ;  
    } 
Clearly what is needed is some actual code selecting the new random code, only the two basic steps are 
adequately identified above. The demo under Igor’s picture actually is of the completed code shown on 
the next page.  
So Adrian  came to wrap the lecture, and the code, by finishing reset. 
 
You can see the two parts of method reset identified earlier clearly marked above.  The End. Thanks for 
a great time. You were absolutely fantastic!