M626
Programmer
- Mar 13, 2002
- 299
I am try to compile this code in jdk1.3.1 and I keep getting a error stating that
"craps.java uses or overrides or depreciated API"
" Recompile with -depreciation"
Any help would be appricated, if you could tell me how to fix the code to work or what this error is.
thanks
HERE IS THE CODE
import java.awt.*;
import java.applet.Applet;
//changed Applet to JApplet
public class Craps extends Applet {
// constant variables for status of game
final byte WON = 0, LOST = 1, CONTINUE = 2;
// other variables used in program
long starttotal;
long betnum=0, total=0;
boolean firstRoll = true, gamestart=true; // true if first roll
int dieSum = 0; // sum of the dice
int myPoint = 0; // point if no win/loss on first roll
short wins=0, losses=0;
byte gameStatus = CONTINUE; // WON, LOST, CONTINUE
// graphical user interface components
Label die1Label, die2Label, sumLabel, pointLabel, betlabel, message, title;
TextField firstDie, secondDie, sum, point, betbox;
Button roll, yes, no, quit;
String longspace=new String(" "
String longerspace=new String(longspace+" "
// setup graphical user interface components
public void init()
{
die1Label = new Label( "Die 1" );
firstDie = new TextField( 1 );
firstDie.setEditable( false );
die2Label = new Label( "Die 2" );
secondDie = new TextField( 1 );
secondDie.setEditable( false );
sumLabel = new Label( "Sum is" );
sum = new TextField( 2 );
sum.setEditable( false );
roll = new Button( "Play Craps" );
pointLabel = new Label( "Point is" );
point = new TextField( 2 );
point.setEditable( false );
title=new Label(" Welcome to Java Craps, version 1.2 "
message=new Label(longspace+"Type in how much money you have."+longerspace);
yes=new Button("Yes"
no=new Button("No"
betbox=new TextField(25);
betlabel=new Label("Total cash: $"
quit=new Button("Quit"
add(title);
add(betlabel);
add(betbox);
add( roll );
add( die1Label );
add( firstDie );
add( die2Label );
add( secondDie );
add( sumLabel );
add( sum );
add( pointLabel );
add( point );
add(message);
add(quit);
add(yes);
add(no);
}public void start(){
quit.hide();
yes.hide();
no.hide();
}
// process one roll of the dice
public void play()
{
if ( firstRoll ) { // first roll of the dice
dieSum = rollDice();
switch( dieSum ) {
case 7: case 11: // win on first roll
gameStatus = WON;
point.setText( "" ); // clear point text field
firstRoll = true; // allow new game to start
break;
case 2: case 3: case 12: // lose on first roll
gameStatus = LOST;
point.setText( "" ); // clear point text field
firstRoll = true; // allow new game to start
break;
default: // remember point
gameStatus = CONTINUE;
myPoint = dieSum;
point.setText( Integer.toString( myPoint ) );
firstRoll = false;
break;
}
}
else {
dieSum = rollDice();
if ( dieSum == myPoint ) // win by making point
gameStatus = WON;
else
if ( dieSum == 7 ) // lose by rolling 7
gameStatus = LOST;
}
total=refresh(gameStatus, betnum, total, betbox);
}
public long refresh(byte gsts, long somecash, long allcash, TextField clear){
long newcash=allcash;
if (gsts==WON){
message.setText(longspace+"Congratulations, you win! Want to play again?"+longerspace);
++wins;
firstRoll=true;
newcash=allcash+somecash;
clear.setText(""
if (newcash>=0) quit.show();
}else if (gsts==LOST){
message.setText(longspace+"Sorry, you lost this one. Better luck next time."+longerspace);
++losses;
firstRoll=true;
clear.setText(""
newcash=allcash-somecash;
if (newcash>=0) quit.show();
}else message.setText(longspace+"Roll again -- you'll win if you get 'Point'."+longerspace);
showStatus(wins+" Win"+plural(wins)+"; "+losses+" Loss"+eplural(losses)+"; "+" Balance: "+balance(newcash));
clear.setEditable(firstRoll);
repaint();
if (firstRoll) betlabel.setText("Place bet: $"
else betlabel.setText("Your bet: $"
return newcash;
}
// call method play when button is clicked
public boolean action( Event e, Object o )
{if (gamestart){
if(!isfloat(betbox.getText())) {
message.setText(longspace+"Really. Enter your starting money."+longerspace);
repaint();
}else game();
}else{
if (e.target==roll || e.target==betbox){
if(!isfloat(betbox.getText())) {
message.setText(longspace+"Oops. You just placed an invalid bet."+longerspace);
repaint();
}
betnum=(long)(100*Float.valueOf(betbox.getText()).floatValue());
if (betnum>0)
play();
else message.setText(longspace+"Oops. You just placed an invalid bet."+longerspace);
repaint();
}if (e.target==quit){
if (total<0) {
message.setText(longspace+"You can't quit -- you still owe us money!"+longerspace);
quit.hide();
repaint();
}else if (!firstRoll){
message.setText(longspace+"But you're in the middle of a game!"+longerspace);
repaint();
}else{
quit.hide();
roll.hide();
betbox.hide();
message.setText(longspace+"Do you really want to quit Java Craps?"+longerspace);
yes.show();
no.show();
}
}if (e.target==no){
betbox.show();
roll.show();
yes.hide();
no.hide();
message.setText(longspace+"We thought you might change your mind."+longerspace);
repaint();
}
if (e.target==yes) exit();
}return true;
}
// roll the dice
int rollDice()
{
int die1, die2, workSum;
die1 = 1 + (int) ( Math.random() * 6 );
die2 = 1 + (int) ( Math.random() * 6 );
workSum = die1 + die2;
firstDie.setText( Integer.toString( die1 ) );
secondDie.setText( Integer.toString( die2 ) );
sum.setText( Integer.toString( workSum ) );
return workSum;
}
public static boolean isfloat(String s){
char c[]=s.toCharArray();
boolean deci=false;
for (byte aritm=0; aritm<c.length; aritm++){
if ((c[aritm]!='.' && !Character.isDigit(c[aritm]) &&!(aritm==0 && c[aritm]=='-')) || s=="" || (c[aritm]=='.' && deci==true))
return false;
if (c[aritm]=='.') deci=true;
}
return true;
}
public void exit(){
no.hide();
yes.hide();
die1Label.hide();
firstDie.hide();
die2Label.hide();
secondDie.hide();
sumLabel.hide();
sum.hide();
pointLabel.hide();
point.hide();
title.setText(" Thank You for playing Java Craps 1.2 "
betlabel.setText(longspace+"Please play again sometime!"+longspace);
if (total<=starttotal) message.setText(longspace+"Next time, try harder and win some money!"+longerspace);
else message.setText(longspace+"Don't forget your money! You won "+balance(total-starttotal)+"."
repaint();
stop();
destroy();
}
public String balance(long money){
String zero;
if (money%100!=0 && money%10==0) zero=new String("0"
else zero=new String (""
String returnme=new String("$"+money/100.0+zero);
return returnme;
}
public String plural(int n){
if (n!=1) return "s";
else return "";
}
public String eplural(int n){
if (n!=1) return "es";
else return "";
}
public void game(){
starttotal=(long)(100*Float.valueOf(betbox.getText()).floatValue());
total=refresh(gameStatus, betnum, starttotal, betbox);
gamestart=false;
roll.setLabel("Roll Dice"
message.setText(longspace+"Bet some money and roll the dice."+longerspace);
betlabel.setText("Place bet: $"
betbox.setText(""
}
}
"craps.java uses or overrides or depreciated API"
" Recompile with -depreciation"
Any help would be appricated, if you could tell me how to fix the code to work or what this error is.
thanks
HERE IS THE CODE
import java.awt.*;
import java.applet.Applet;
//changed Applet to JApplet
public class Craps extends Applet {
// constant variables for status of game
final byte WON = 0, LOST = 1, CONTINUE = 2;
// other variables used in program
long starttotal;
long betnum=0, total=0;
boolean firstRoll = true, gamestart=true; // true if first roll
int dieSum = 0; // sum of the dice
int myPoint = 0; // point if no win/loss on first roll
short wins=0, losses=0;
byte gameStatus = CONTINUE; // WON, LOST, CONTINUE
// graphical user interface components
Label die1Label, die2Label, sumLabel, pointLabel, betlabel, message, title;
TextField firstDie, secondDie, sum, point, betbox;
Button roll, yes, no, quit;
String longspace=new String(" "
String longerspace=new String(longspace+" "
// setup graphical user interface components
public void init()
{
die1Label = new Label( "Die 1" );
firstDie = new TextField( 1 );
firstDie.setEditable( false );
die2Label = new Label( "Die 2" );
secondDie = new TextField( 1 );
secondDie.setEditable( false );
sumLabel = new Label( "Sum is" );
sum = new TextField( 2 );
sum.setEditable( false );
roll = new Button( "Play Craps" );
pointLabel = new Label( "Point is" );
point = new TextField( 2 );
point.setEditable( false );
title=new Label(" Welcome to Java Craps, version 1.2 "
message=new Label(longspace+"Type in how much money you have."+longerspace);
yes=new Button("Yes"
no=new Button("No"
betbox=new TextField(25);
betlabel=new Label("Total cash: $"
quit=new Button("Quit"
add(title);
add(betlabel);
add(betbox);
add( roll );
add( die1Label );
add( firstDie );
add( die2Label );
add( secondDie );
add( sumLabel );
add( sum );
add( pointLabel );
add( point );
add(message);
add(quit);
add(yes);
add(no);
}public void start(){
quit.hide();
yes.hide();
no.hide();
}
// process one roll of the dice
public void play()
{
if ( firstRoll ) { // first roll of the dice
dieSum = rollDice();
switch( dieSum ) {
case 7: case 11: // win on first roll
gameStatus = WON;
point.setText( "" ); // clear point text field
firstRoll = true; // allow new game to start
break;
case 2: case 3: case 12: // lose on first roll
gameStatus = LOST;
point.setText( "" ); // clear point text field
firstRoll = true; // allow new game to start
break;
default: // remember point
gameStatus = CONTINUE;
myPoint = dieSum;
point.setText( Integer.toString( myPoint ) );
firstRoll = false;
break;
}
}
else {
dieSum = rollDice();
if ( dieSum == myPoint ) // win by making point
gameStatus = WON;
else
if ( dieSum == 7 ) // lose by rolling 7
gameStatus = LOST;
}
total=refresh(gameStatus, betnum, total, betbox);
}
public long refresh(byte gsts, long somecash, long allcash, TextField clear){
long newcash=allcash;
if (gsts==WON){
message.setText(longspace+"Congratulations, you win! Want to play again?"+longerspace);
++wins;
firstRoll=true;
newcash=allcash+somecash;
clear.setText(""
if (newcash>=0) quit.show();
}else if (gsts==LOST){
message.setText(longspace+"Sorry, you lost this one. Better luck next time."+longerspace);
++losses;
firstRoll=true;
clear.setText(""
newcash=allcash-somecash;
if (newcash>=0) quit.show();
}else message.setText(longspace+"Roll again -- you'll win if you get 'Point'."+longerspace);
showStatus(wins+" Win"+plural(wins)+"; "+losses+" Loss"+eplural(losses)+"; "+" Balance: "+balance(newcash));
clear.setEditable(firstRoll);
repaint();
if (firstRoll) betlabel.setText("Place bet: $"
else betlabel.setText("Your bet: $"
return newcash;
}
// call method play when button is clicked
public boolean action( Event e, Object o )
{if (gamestart){
if(!isfloat(betbox.getText())) {
message.setText(longspace+"Really. Enter your starting money."+longerspace);
repaint();
}else game();
}else{
if (e.target==roll || e.target==betbox){
if(!isfloat(betbox.getText())) {
message.setText(longspace+"Oops. You just placed an invalid bet."+longerspace);
repaint();
}
betnum=(long)(100*Float.valueOf(betbox.getText()).floatValue());
if (betnum>0)
play();
else message.setText(longspace+"Oops. You just placed an invalid bet."+longerspace);
repaint();
}if (e.target==quit){
if (total<0) {
message.setText(longspace+"You can't quit -- you still owe us money!"+longerspace);
quit.hide();
repaint();
}else if (!firstRoll){
message.setText(longspace+"But you're in the middle of a game!"+longerspace);
repaint();
}else{
quit.hide();
roll.hide();
betbox.hide();
message.setText(longspace+"Do you really want to quit Java Craps?"+longerspace);
yes.show();
no.show();
}
}if (e.target==no){
betbox.show();
roll.show();
yes.hide();
no.hide();
message.setText(longspace+"We thought you might change your mind."+longerspace);
repaint();
}
if (e.target==yes) exit();
}return true;
}
// roll the dice
int rollDice()
{
int die1, die2, workSum;
die1 = 1 + (int) ( Math.random() * 6 );
die2 = 1 + (int) ( Math.random() * 6 );
workSum = die1 + die2;
firstDie.setText( Integer.toString( die1 ) );
secondDie.setText( Integer.toString( die2 ) );
sum.setText( Integer.toString( workSum ) );
return workSum;
}
public static boolean isfloat(String s){
char c[]=s.toCharArray();
boolean deci=false;
for (byte aritm=0; aritm<c.length; aritm++){
if ((c[aritm]!='.' && !Character.isDigit(c[aritm]) &&!(aritm==0 && c[aritm]=='-')) || s=="" || (c[aritm]=='.' && deci==true))
return false;
if (c[aritm]=='.') deci=true;
}
return true;
}
public void exit(){
no.hide();
yes.hide();
die1Label.hide();
firstDie.hide();
die2Label.hide();
secondDie.hide();
sumLabel.hide();
sum.hide();
pointLabel.hide();
point.hide();
title.setText(" Thank You for playing Java Craps 1.2 "
betlabel.setText(longspace+"Please play again sometime!"+longspace);
if (total<=starttotal) message.setText(longspace+"Next time, try harder and win some money!"+longerspace);
else message.setText(longspace+"Don't forget your money! You won "+balance(total-starttotal)+"."
repaint();
stop();
destroy();
}
public String balance(long money){
String zero;
if (money%100!=0 && money%10==0) zero=new String("0"
else zero=new String (""
String returnme=new String("$"+money/100.0+zero);
return returnme;
}
public String plural(int n){
if (n!=1) return "s";
else return "";
}
public String eplural(int n){
if (n!=1) return "es";
else return "";
}
public void game(){
starttotal=(long)(100*Float.valueOf(betbox.getText()).floatValue());
total=refresh(gameStatus, betnum, starttotal, betbox);
gamestart=false;
roll.setLabel("Roll Dice"
message.setText(longspace+"Bet some money and roll the dice."+longerspace);
betlabel.setText("Place bet: $"
betbox.setText(""
}
}