Skip to main content

Generating Random Numbers to Fill array. Java Beginner needing help!

1 reply [Last post]
Aceistheguy
Offline
Joined: 2012-04-24
Points: 0

Hello all!
I am new to this site, and Java programming.
My problem is: Write a program that produces random permutations of the numbers 1 to 10. eg. (1,4,7,10,2,9,8,3,6,5) or (10,7,9,2,1,3,6,5,4,8). My class needs to have the following methods: // Displays the array permutatedNumbers to the console public void displayPermutedArray(){} // Repeat the selection of a number 10 times to populate the permutated array public void generatePermutation(){} Make an array to hold the permuted numbers. Make a second array and fill it with the numbers 1 to 10 in order. Pick one of the numbers from the second array randomly, remove it, and write it to the permutation array. Repeat this 10 times. Test the functionality in your main method by calls to the generatePermutation() and displayPermutedArray() methods.

I am new to Java programming and this seems mind blowing to me :( I know how to create arrays and fill them with numbers entered. I will include a sample of how far my knowledge goes in this area to give you an idea of my skill level. The code I am including is a "sort" of tic-tac-toe I guess. I am writing on netbeans. On a side note I have never posted a code block before so If this does not display correctly I am extending an apology in advance! Thanks to all for the help!

import java.util.Scanner;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Ace
*/
public class TwoDArrayExamples {
    private char[][] board;
    public static void main(String [] args){
        TwoDArrayExamples twoD = new TwoDArrayExamples();
        twoD.initializeArray();
        Scanner scan = new Scanner(System.in);
        char play = 0;
        while (play != 'q') {
        twoD.printArray();
        System.out.println("Enter row and column to change and char to add");
        twoD.makePlay(scan.nextInt(),scan.nextInt(),scan.next());
        }
    }
    private void initializeArray(){
        board = new char [3][3];
        for(int row = 0; row < board.length; row++){
            for (int column = 0; column < board[row].length; column++){
                board[row][column] = '-';
            }
        }
    }
   
    private void printArray(){
        for(int row = 0; row < board.length; row++){
            for(int column = 0; column < board[row].length; column++){
                System.out.print(board[row][column]+"  ");
            }
            System.out.println();
        }
    }
    public void makePlay(int row, int col, String play ){
        board[row][col] = play.charAt(0);
    }
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
2kjava
Offline
Joined: 2012-04-18
Points: 0

This sounds like a programming excerise set by a tutor so I'm only going to provide a hint!

Look at the java.util.Random package. It provides constructors and methods to initialise and generate random numbers.

John.