Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,633 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,054 people online right now. Registration is fast and FREE... Join Now!




class Globetrotter; separate class player is included

 
Reply to this topicStart new topic

class Globetrotter; separate class player is included, this will not compile

learningjava
post 11 Oct, 2008 - 11:30 AM
Post #1


New D.I.C Head

*
Joined: 6 Sep, 2008
Posts: 28

CODE
//players roll dice then when they land on location [0] (North America)
the player wins.
import javax.swing.JOptionPane;
import java.util.*;
import java.io.*;
public class GlobeTrotter
{
    private Player[] player;
    public static void main(String[] args) throws IOException, ClassNotFoundException{
    // get the destinations
    String[] destinations = new String[12];
    Scanner in = new Scanner(new File("Destinations.txt"));
    int i = 0;
    try{
        while(in.hasNext()){
            destinations[i++] = in.nextLine();
          }//end while in.hasNext
        }
        finally{
        }
        // create the players
        Player player[] = new Player[4];
        player[0]= new Player("Jerry");
        player[1]= new Player("Gary");
        player[2]= new Player("Mary");
        player[3]= new Player("Larry");
        int id = 0;
        int winner = -1;
        // Loop to play the game
        while(winner<0){
           winner = move(player[id], id, destinations);
        //Find out if there was a winner
           if(winner<0){
               id = (++id) % 4;
            }else{
                JOptionPane.showMessageDialog(null, "Congratulations, " + player[id].getName()
                + "you have landed on North America and have won the game!");
            }//end if winner<0
          }//end while
        }
        //----
        private static int move(Player[] player){
            //spin the spinner
          int rollDice = ((int)(Math.random() * 100) %3 +1);
          //move the player
          int newLoc=player.move(rollDice);
          //tell the player where they are
          JOptionPane.showMessageDialog(null, player.getName() + "has rolled a " + steps
            + "and has landed on " + destinations[player.getLocations()]);
            if(newLoc == 0){
                return i;
            }else {
                return -1;
            }//end if newLoc == 0
        }//end Method move
    }//end class
// ------------------------------------------------------------------------------------------------------------------
public class Player
{
    private String name;
    private String location;
    //sets and gets
    public void setName(String newName){name = newName;}
    public void setLocation(String newLoc){location = newLoc;}
    public String getName(){return name;}
    public String getLocation(){return location;}
    
}


This post has been edited by learningjava: 11 Oct, 2008 - 04:47 PM
User is offlineProfile CardPM

Go to the top of the page

William_Wilson
post 11 Oct, 2008 - 03:49 PM
Post #2


lost in compilation

Group Icon
Joined: 23 Dec, 2005
Posts: 3,970



Thanked 15 times

Dream Kudos: 3275

Expert In: Java, C, Javascript

My Contributions


asking someone to debug is excessive as there are many errors, but I will help with the basics:

Your player class is missing a constructor accepting a String:
CODE

public Player(String aName)
{
     name = aName;
}


in your GlobeTrotter class, you have defined i inside your main method, it is not a global variable, so it cannot be used in your move method. You will either need to make this global (define it between the class definition and the start of your main), return a constant or return a variable from within the move method.

the Player class does not have a method named getLocation, you will need to create one, that has a prototype of: public int getLocation() since you return it as an array index, it MUST return an int.

you need a method in your Player class named move that accepts an int: public int move(int die) because of this line: int newLoc=player.move(rollDice);

you call: move(player[id], id, destinations); but your move method only takes a player array, so you will need to add this method or change the line I quoted to only pass a player array (player[]) and work that way.

Fixing all of these may result in new errors, but feel free to post back with any issues or problems. Make sure to include the errors you are receiving it makes the processes of helping a lot faster smile.gif
User is offlineProfile CardPM

Go to the top of the page

learningjava
post 11 Oct, 2008 - 04:21 PM
Post #3


New D.I.C Head

*
Joined: 6 Sep, 2008
Posts: 28

This is actually the answer to an assignment that the teacher gave me.
It still will not compile I think she has a few minor things wrong, I still
haven't got it worked out yet.
QUOTE(William_Wilson @ 11 Oct, 2008 - 04:49 PM) *

asking someone to debug is excessive as there are many errors, but I will help with the basics:

Your player class is missing a constructor accepting a String:
CODE

public Player(String aName)
{
     name = aName;
}


in your GlobeTrotter class, you have defined i inside your main method, it is not a global variable, so it cannot be used in your move method. You will either need to make this global (define it between the class definition and the start of your main), return a constant or return a variable from within the move method.

the Player class does not have a method named getLocation, you will need to create one, that has a prototype of: public int getLocation() since you return it as an array index, it MUST return an int.

you need a method in your Player class named move that accepts an int: public int move(int die) because of this line: int newLoc=player.move(rollDice);

you call: move(player[id], id, destinations); but your move method only takes a player array, so you will need to add this method or change the line I quoted to only pass a player array (player[]) and work that way.

Fixing all of these may result in new errors, but feel free to post back with any issues or problems. Make sure to include the errors you are receiving it makes the processes of helping a lot faster smile.gif

User is offlineProfile CardPM

Go to the top of the page

learningjava
post 11 Oct, 2008 - 04:43 PM
Post #4


New D.I.C Head

*
Joined: 6 Sep, 2008
Posts: 28

CODE
i don't think I have the player class right it still doesn't work
public class Player
{
    private String name;
    private int location;
    //sets and gets
    public void setName(String newName){name = newName;}
    public void setLocation(int newLoc){location = newLoc;}
    public String getName(){return name;}
    public int getLocation(){return location;}
}
     public Player(String aName)
     {
         name = aName
      }
//-------------------------------------------------------------------
import javax.swing.JOptionPane;
import java.util.*;
import java.io.*;
public class GlobeTrotter
{
    private Player[] player;
    public static void main(String[] args) throws IOException, ClassNotFoundException{
    // get the destinations
    String[] destinations = new String[12];
    Scanner in = new Scanner(new File("Destinations.txt"));
    int i = 0;
    try{
        while(in.hasNext()){
            destinations[i++] = in.nextLine();
          }//end while in.hasNext
        }
        finally{
        }
        // create the players
        Player player[] = new Player[4];
        player[0].setName("Jerry");
        player[1].setName("Gary");
        player[2].setName("Mary");
        player[3].setName("Larry");
        int id = 0;
        int winner = -1;
        // Loop to play the game
        while(winner<0){
           move();
        //Find out if there was a winner
           if(winner<0){
               id = (++id) % 4;
            }else{
                JOptionPane.showMessageDialog(null, "Congratulations, " + player[id].getName()
                + "you have landed on North America and have won the game!");
            }//end if winner<0
          }//end while
        }
        //----
        private static int move(player[]){
            //spin the spinner
          int steps = ((int)(Math.random() * 100) %3 +1);
          //move the player
          int newLoc=player.move(steps);
          //tell the player where they are
          JOptionPane.showMessageDialog(null, player.getName() + "has rolled a " + steps
            + "and has landed on " + destinations[player.getLocations()]);
            if(newLoc == 0){
                return i;
            }else {
                return -1;
            }//end if newLoc == 0
        }//end Method move
    }//end class
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 04:03AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month