User Tools

Site Tools


dma:java13:assignment_13

Sort these numbers


Start a new project called “Sort.”

  • Put these numbers in an array UNSORTED
  • Print out the unsorted array of numbers
  • Using either Bubble, Insertion, Selection Sort to sort the numbers
    • Pick the one that makes the most sense to you and use it
  • Print out the numbers sorted

This is what your console should look like:

Initial Array: 
30 
2 
46 
8 
64 
88 
59 
86 
7 
29

New Array: 
2 
7 
8 
29 
30 
46 
59 
64 
86 
88

Start here, if you feel really lost: http://mathbits.com/MathBits/Java/arrays/Sorting.htm


public class BubbleSort {
	public static void main (String[] args)
	{
		int[] myArray = {23,43,46,22,66,35,0,6,99999,-33};
		
		for (int i = 0; i<10; i++)
			for (int j=0; j<9; j++)
			{
				if (myArray[j]<myArray[j+1])
				{
					int temp;
					temp = myArray[j];
					myArray[j] = myArray[j+1];
					myArray[j+1] = 
				}
			}
	}

}

When you are finished upload all your project folders into your GOOGLE DRIVE or PASTEBIN.

Since you love this game so much <3


Finish up:

  • Make it so there are 3 lives that count down
  • Make it keep track of your score
		for (int i = 0; i < NTURNS; i++) {
			setUpTurn(i);
			playTurn(); 
			if(numBricksLeft==0) break;
		}
		

Bonus:

  • FileIO and keeping track of scoreboard: Scoreboard

Finally, build the Program Manager! : Instructions

Starter : Project

Solution : Project

public class Scoreboard {
 
	//for a scoreboard...we want a high score to be read in
	//we also want a score to be display
	public GLabel highScoreLabel;
	public GLabel scoreLabel;
	public int highScoreX;
	public int highScoreY;
	public int scoreX;
	public int scoreY;
	public int gameScore;
	public int highScore;
 
	/**
	 * This is the constructor...
	 * When you create your scoreboard object,
	 * this method is called first to initialize
	 * the highscore and the current game score
	 * @param highScoreX is the x location
	 * @param highScoreY is the y location
	 * @param scoreX is the x location
	 * @param scoreY is the y location
	 * This method throws an IO Exception when the File IO
	 * fails.
	 */
	public Scoreboard(int highScoreX,int highScoreY,int scoreX,int scoreY) throws IOException
	{
		//Add the high score to the scoreboard
		this.highScoreX = highScoreX;
		this.highScoreY = highScoreY;
		this.highScore = Integer.parseInt(setHighScore());
		highScoreLabel = new GLabel("High Score: " + this.highScore);
		highScoreLabel.setFont("Monospace-16"); 
		highScoreLabel.setLocation (this.highScoreX,this.highScoreY);
 
 
		//add the normal score to the scoreboard.
		this.gameScore = 0;
		this.scoreX = scoreX;
		this.scoreY = scoreY;
		scoreLabel = new GLabel("Score: " + Integer.toString(gameScore));
		scoreLabel.setFont("Monospace-16"); 
		scoreLabel.setLocation (this.scoreX,this.scoreY);
 
	}
 
	/*
	 * This method updates the gameScore
	 * @param points is the amount of points 
	 * to be added to gameScore
	 * 
	 */
	public void updateScoreBoard(int points)
	{
		this.gameScore += points;
		this.scoreLabel.setLabel("Score: " + Integer.toString(gameScore));
	}
 
	/*
	 * This method reads in the scoreboard.txt file
	 * and throws a FNFE exception when the file isn't 
	 * available.
	 * @return the high score from the file
	 */
	private String setHighScore() throws FileNotFoundException
	{
		//created a new file which refers to scoreboard.txt
		File inFile  = new File("scoreboard.txt");
		//we utilize this reader class to take in input, line by line
		//from scoreboard.txt
		BufferedReader reader = new BufferedReader(new FileReader(inFile));
		//use a temporary variable to store the file input
		String score = "";
		//try to get the input from the file.
		try {
			//set score to the first line of the file.
			score = reader.readLine();
			//close the file reader and the file.
			reader.close();
		} 
		//if we can't find the file, set score to 0
		//and print to the console that we have an error!
		catch (IOException e) {
			//print out an error message of some kind
		}
		//finally, return the score.
		return score;
 
	}
 
	/*
	 * Write the ending game score
	 * to the txt file.
	 */
	public void saveScore() throws IOException
	{
		//if the final game score is greater than our current highscore
		//we want to save it to the text file.
		if( gameScore > highScore )
		{
			//create the file we are writing to.
			File outFile = new File("scoreboard.txt");
			//create a writer, similar to our previous reader.
			BufferedWriter writer = new BufferedWriter(new FileWriter(outFile));
			//use the writer to write to the text file.
			writer.write(Integer.toString(this.gameScore));
			//close the text file.
			writer.close();
		}
	}
 
}
                // run this when the game ends
                try {
			scoreBoard.saveScore();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("I couldn't save the score");
		}
                // run this in the setup
                try {
			scoreBoard = new Scoreboard(10,20,10,50);
			add(scoreBoard.highScoreLabel);
			add(scoreBoard.scoreLabel);
		} 
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
/soe/sherol/.html/teaching/data/pages/dma/java13/assignment_13.txt · Last modified: 2013/08/08 15:33 by ffpaladin