This site requires JavaScript, please enable it in your browser!
Greenfoot back
snobiz45
snobiz45 wrote ...

2013/11/21

Won't Compile

snobiz45 snobiz45

2013/11/21

#
Can someone help me with code? It doesn't compile. import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Color; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.ArrayList; import com.mysql.jdbc.*; import java.sql.DriverManager; import java.sql.ResultSet; /** * Space. Something for rockets to fly in. * Modified code from the Capella IT Lab (Dec. 2010) based on * @author Michael Kolling * @version 1.1 */ public class Space extends World { private Counter scoreCounter; private int startAsteroids = 3; private String player; private ArrayList<String> topTen; private Connection conn; private PreparedStatement stmt; private static String SERVER_ADDR = "vle8.capella.edu"; private static String USER_NAME = "ab1234567"; // Change to your DB user name private static String PASSWORD = "ab1234567ab"; // Change to your DB password private static String DATABASE_URL = "jdbc:mysql://" + SERVER_ADDR + "/" + USER_NAME; public Space() { super(600, 400, 1); GreenfootImage background = getBackground(); background.setColor(Color.BLACK); background.fill(); createStars(300); Rocket rocket = new Rocket(); addObject(rocket, getWidth()/2 + 100, getHeight()/2); addAsteroids(startAsteroids); scoreCounter = new Counter("Score: "); addObject(scoreCounter, 60, 380); Explosion.initializeImages(); ProtonWave.initializeImages(); topTen = new ArrayList<String>(); } /** * Add a given number of asteroids to our world. Asteroids are only added into * the left half of the world. */ private void addAsteroids(int count) { for(int i = 0; i < count; i++) { int x = Greenfoot.getRandomNumber(getWidth()/2); int y = Greenfoot.getRandomNumber(getHeight()/2); addObject(new Asteroid(), x, y); } } /** * Create a given number of stars in space. */ private void createStars(int number) { GreenfootImage background = getBackground(); for(int i=0; i < number; i++) { int x = Greenfoot.getRandomNumber( getWidth() ); int y = Greenfoot.getRandomNumber( getHeight() ); int color = 120 - Greenfoot.getRandomNumber(100); background.setColor(new Color(color,color,color)); background.fillOval(x, y, 2, 2); } } public void changeScore(int pts) { scoreCounter.add(pts); } /** * This method is called when the game is over to display the final score, save to the DB, and display top 10 scores. */ public void gameOver() { try { connectToDB(); saveScore(); getTopTen(); } catch (Exception ex) { System.out.println(ex.getMessage()); } finally { addObject(new ScoreBoard(scoreCounter.getValue(), topTen), getWidth()/2, getHeight()/2); } } private void connectToDB() throws Exception { Class.forName("com.mysql.jdbc.Driver"); conn = (Connection) DriverManager.getConnection(DATABASE_URL, USER_NAME, PASSWORD); } private void saveScore() throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); String InsertSQL = "INSERT INTO scores(player, score, gamedate) VALUES(?, ?, ?)"; stmt = (PreparedStatement) conn.prepareStatement(InsertSQL); stmt.setString(1, player); stmt.setInt(2, scoreCounter.getValue()); String dateTime = format.format(Calendar.getInstance().getTime()); stmt.setString(3, dateTime); stmt.executeUpdate(); } private void getTopTen() throws Exception { String Top10SQL = "SELECT * FROM scores ORDER BY score DESC LIMIT 10"; ResultSet rs = stmt.executeQuery(Top10SQL); topTen = new ArrayList<String>(); while(rs.next()) { topTen.add(rs.getInt("score") + " - " + rs.getString("player") + " - " + rs.getString("gamedate")); } conn.close(); } public void setPlayerName(String playerName) { player = playerName; } private void addAsteroids(int count) { for(int i = 0; i < count; i++) { int x = Greenfoot.getRandomNumber(getWidth()/2); int y = Greenfoot.getRandomNumber(getHeight()/2); addObject(new Asteroid(), x, y); } } /** * Crete a given number of stars in space. */ private void createStars(int number) { GreenfootImage background = getBackground(); for(int i=0; i < number; i++) { int x = Greenfoot.getRandomNumber( getWidth() ); int y = Greenfoot.getRandomNumber( getHeight() ); int color = 120 - Greenfoot.getRandomNumber(100); background.setColor(new Color(color,color,color)); background.fillOval(x, y, 2, 2); } } /** * Create a score board with dummy result for testing. */ public void ScoreBoard() { } /** * Create a score board for the final result. */ public int ScoreBoard(int score) { /** * Create a score board with dummy result for testing. */ { this(100); } /** * Create a score board for the final result. */ public ScoreBoard(int score) { makeImage("Game Over", "Final Score: ", score); } /** * This method is called when the game is over to display the final score. */ public void gameOver() { addObject(new ScoreBoard(999), getWidth()/2, getHeight()/2); } } Thanks
davmac davmac

2013/11/21

#
Please use code tags around your code - see here. Also, what line do you get the error on, and what is the error message?
danpost danpost

2013/11/21

#
It looks like you have two of each of the methods 'createStars(int)' and 'addAsteroids(int)'. I hope this is just a copy/paste thing, as you can only have one of each.
ksksks ksksks

2013/11/21

#
I'm doing this too.
snobiz45 snobiz45

2013/11/21

#
the error is on this line. error message is - "illegal start expression" public ScoreBoard(int score) import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Color; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.ArrayList; import com.mysql.jdbc.*; import java.sql.DriverManager; import java.sql.ResultSet; /** * Space. Something for rockets to fly in. * Modified code from the Capella IT Lab (Dec. 2010) based on * @author Michael Kolling * @version 1.1 */ public class Space extends World { private Counter scoreCounter; private int startAsteroids = 3; private String player; private ArrayList<String> topTen; private Connection conn; private PreparedStatement stmt; private static String SERVER_ADDR = "vle8.capella.edu"; private static String USER_NAME = "vle\1117781"; // Change to your DB user name private static String PASSWORD = "sr1117781up"; // Change to your DB password private static String DATABASE_URL = "jdbc:mysql://" + SERVER_ADDR + "/" + USER_NAME; public Space() { super(600, 400, 1); GreenfootImage background = getBackground(); background.setColor(Color.BLACK); background.fill(); createStars(300); Rocket rocket = new Rocket(); addObject(rocket, getWidth()/2 + 100, getHeight()/2); addAsteroids(startAsteroids); scoreCounter = new Counter("Score: "); addObject(scoreCounter, 60, 380); Explosion.initializeImages(); ProtonWave.initializeImages(); topTen = new ArrayList<String>(); } /** * Add a given number of asteroids to our world. Asteroids are only added into * the left half of the world. */ private void addAsteroids(int count) { for(int i = 0; i < count; i++) { int x = Greenfoot.getRandomNumber(getWidth()/2); int y = Greenfoot.getRandomNumber(getHeight()/2); addObject(new Asteroid(), x, y); } } /** * Create a given number of stars in space. */ private void createStars(int number) { GreenfootImage background = getBackground(); for(int i=0; i < number; i++) { int x = Greenfoot.getRandomNumber( getWidth() ); int y = Greenfoot.getRandomNumber( getHeight() ); int color = 120 - Greenfoot.getRandomNumber(100); background.setColor(new Color(color,color,color)); background.fillOval(x, y, 2, 2); } } public void changeScore(int pts) { scoreCounter.add(pts); } /** * This method is called when the game is over to display the final score, save to the DB, and display top 10 scores. */ public void gameOver() { try { connectToDB(); saveScore(); getTopTen(); } catch (Exception ex) { System.out.println(ex.getMessage()); } finally { addObject(new ScoreBoard(scoreCounter.getValue(), topTen), getWidth()/2, getHeight()/2); } } private void connectToDB() throws Exception { Class.forName("com.mysql.jdbc.Driver"); conn = (Connection) DriverManager.getConnection(DATABASE_URL, USER_NAME, PASSWORD); } private void saveScore() throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); String InsertSQL = "INSERT INTO scores(player, score, gamedate) VALUES(?, ?, ?)"; stmt = (PreparedStatement) conn.prepareStatement(InsertSQL); stmt.setString(1, player); stmt.setInt(2, scoreCounter.getValue()); String dateTime = format.format(Calendar.getInstance().getTime()); stmt.setString(3, dateTime); stmt.executeUpdate(); } private void getTopTen() throws Exception { String Top10SQL = "SELECT * FROM scores ORDER BY score DESC LIMIT 10"; ResultSet rs = stmt.executeQuery(Top10SQL); topTen = new ArrayList<String>(); while(rs.next()) { topTen.add(rs.getInt("score") + " - " + rs.getString("player") + " - " + rs.getString("gamedate")); } conn.close(); } public void setPlayerName(String playerName) { player = playerName; } private void addAsteroids(int count) { for(int i = 0; i < count; i++) { int x = Greenfoot.getRandomNumber(getWidth()/2); int y = Greenfoot.getRandomNumber(getHeight()/2); addObject(new Asteroid(), x, y); } } /** * Crete a given number of stars in space. */ private void createStars(int number) { GreenfootImage background = getBackground(); for(int i=0; i < number; i++) { int x = Greenfoot.getRandomNumber( getWidth() ); int y = Greenfoot.getRandomNumber( getHeight() ); int color = 120 - Greenfoot.getRandomNumber(100); background.setColor(new Color(color,color,color)); background.fillOval(x, y, 2, 2); } } /** * Create a score board with dummy result for testing. */ public void ScoreBoard() { } /** * Create a score board for the final result. */ public int ScoreBoard(int score) { /** * Create a score board with dummy result for testing. */ { this(100); } /** * Create a score board for the final result. */ public ScoreBoard(int score) { makeImage("Game Over", "Final Score: ", score); } /** * This method is called when the game is over to display the final score. */ public void gameOver() { addObject(new ScoreBoard(999), getWidth()/2, getHeight()/2); } }
davmac davmac

2013/11/21

#
The end tag should be - i.e. a slash at the beginning. I fixed that for you now. The problem is that you have a method definition inside another method. Notice how line 176 and line 190 are the same. It looks like you've copied methods from another class, and that these methods are actually supposed to be constructors. I suggest you delete lines 173 through 185. This will resolve the current problem, although you still have further errors which need to be resolved. Seeing as it's not clear what you intend the ScoreBoard methods to do, maybe you can just remove them?
snobiz45 snobiz45

2013/11/21

#
Thank you!
You need to login to post a reply.