`import java.util.Random;
public class Main {
public static void main(String[] args) {
int[][] board = new int[5][5];
int sNA = 5;
// new GUI();
sequenceMaker(board);
drawBoard(board);
}
//
public static void drawBoard(int[][] board2D) {
int m=1;
for(int i = 0; i < board2D.length; i++){
for(int j=0; j < board2D.length; j++){
System.out.print(board2D[i][j]+" ");
}
System.out.println();
}
}
public static void sequenceMaker(int[][] board2D) {
Random rand = new Random();
int m = 1;
int x = 0;
while(x < 24){
int columnRandom = rand.nextInt(5);
int rowsRandom = rand.nextInt(5);
x += 1;
if(board2D[columnRandom][rowsRandom] == 0) {
board2D[columnRandom][rowsRandom] = m;
m += 1;
}
else if(board2D[columnRandom][rowsRandom] == m) {
while(board2D[columnRandom][rowsRandom] == m) {
columnRandom = rand.nextInt(5);
rowsRandom = rand.nextInt(5);
board2D[columnRandom][rowsRandom] = m;
m+=1;
}
}
}
}
}`
This is what I wrote to this point, but the output doesn't include every index, maximally going to 15-16ish.
I tried a while loop, so that if another integer is in the place of randomally generated number, it generates those number once again. I don't know why my output is incomplete though.
I have solved my own question (I think).
What I have changed is that in the sequenceMaker() function, I have moved the x+=1 into the if statements. The code looks like this :
public static void sequenceMaker(int[][] board2D) {
Random rand = new Random();
int m = 1;
int x = 0;
while(x < 25){
int columnRandom = rand.nextInt(5); // random column
int rowsRandom = rand.nextInt(5); // random row
if(board2D[columnRandom][rowsRandom] == 0) {
board2D[columnRandom][rowsRandom] = m;
m += 1;
x += 1;
}
else if(board2D[columnRandom][rowsRandom] != 0) {
while(board2D[columnRandom][rowsRandom] == 0) {
columnRandom = rand.nextInt(5);
rowsRandom = rand.nextInt(5);
board2D[columnRandom][rowsRandom] = m;
m+=1;
x += 1;
}
}
}
}
I hope my mediocrity helped someone!
public static void sequenceMaker(int[][] board2D) {
//it will initialize your array with random integers
Random rand = new Random();
for(int i=0; i<board2D.length; i++){
for(int j=0; j < board2D[i].length; j++ ){
int randomNo = rand.nextInt(20); //you can change the bound as required
board2D[i][j] = randomNo;
}
}
}
Related
How could I optimise this code to take the String[] games values from the main method and have a separate method: public static int points(String[] games). I am super new to Java and don't really understand how to invoke methods.
public class TotalPoints {
public static void main(String[] args) {
String[] games = {"1:0","2:0","3:0","4:0","2:1","3:1","4:1","3:2","4:2","4:3"};
int sum = 0;
int matches = 10;
int x = 0;
int y = 0;
for (int i = 0; i < games.length; i++) {
String[] pieces = games[i].split(":");
x = Integer.parseInt(pieces[0]);
y = Integer.parseInt(pieces[1]);
}
for (int j = 0; j < matches; j++) {
if (x > y) {
sum = sum + 3;
} else if (x == y) {
sum = sum + 1;
}
}
System.out.println(sum);
}
}
You can write something like
public class TotalPoints {
public static void main(String[] args) {
int sum = points(args);
System.out.println(sum);
}
public static int points(String[] games) {
int sum = 0;
int matches = 10;
int x = 0;
int y = 0;
for (int i = 0; i < games.length; i++) {
String[] pieces = games[i].split(":");
x = Integer.parseInt(pieces[0]);
y = Integer.parseInt(pieces[1]);
}
for (int j = 0; j < matches; j++) {
if (x > y) {
sum = sum + 3;
} else if (x == y) {
sum = sum + 1;
}
}
return sum;
}
}
And when you run this class, pass the arguments from command line like
java TotalPoints "1:0" "2:0" "3:0" "4:0" "2:1" "3:1" "4:1" "3:2" "4:2" "4:3"
very simple:
public class TotalPoints {
public static void main(String[] args) {
String[] games = {"1:0","2:0","3:0","4:0","2:1","3:1","4:1","3:2","4:2","4:3"};
int result = points(games);
}
public static int points(String[] games) {
//dowhat ever you want and return an int value
}
}
I suggest this to you
public class TotalPoints {
public static void main(String[] args) {
String[] games = {"1:0","2:0","3:0","4:0","2:1","3:1","4:1","3:2","4:2","4:3"};
int sum = points(games);
System.out.println(sum);
}
private static int points(String[] games) {
int sum = 0;
int matches = 10;
int x = 0;
int y = 0;
for (String game : games) {
String[] pieces = game.split(":");
x = Integer.parseInt(pieces[0]);
y = Integer.parseInt(pieces[1]);
}
for (int j = 0; j < matches; j++) {
if (x > y) {
sum = sum + 3;
}
else if (x == y) {
sum = sum + 1;
}
}
return sum;
}
}
I replace for (int i = 0; i < games.length; i++)
by
for (String game : games)
it's a simpler way to browse a list
we're supposed to multiply every element in the matrix by a number, in this case "k". not sure what i'm doing wrong but it wont work. HELP! i have edited and added the whole project so far.
public class Matrix {
private int r;//rows
private int c;//columns
private int[][] neo; //2D array
public static void main(String[] args) {
Matrix m1 = new Matrix(3,4);
Matrix m2 = new Matrix(3,4);
System.out.println(m2);
try {
Matrix m3 = m1.multiply(m2);
} catch (Exception e) {
e.printStackTrace();
}
m2.scaleMult(k);
}//main
public Matrix(int row, int column) {
r = row;
c = column;
neo = new int[r][c];
for(int i = 0; i < neo.length; i++) {
for (int j = 0; j < neo[i].length; j++) {
neo[i][j] = (int) (1 + Math.random() * 100);
}//forLoop
}//forLoop
System.out.println(Arrays.toString(neo));
}//Matrix
public Matrix copyMatrix(Matrix m) {
Matrix copy = new Matrix(m.r, m.c);
for (int i = 0; i < r; i++) {
System.arraycopy(this.neo[i], 0, copy.neo[i], 0, this.neo[i].length);
}//forLoop
return copy;
}//copyMatrix
public void scaleMult(int k){
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
this.neo[i][j] * k;
}//scaleMult
public boolean equals(Matrix m2) {
if (this.r != m2.r || this.c != m2.c) {
return false;
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (this.neo[i][j] != m2.neo[i][j]) {
return false;
}
}
}
return true;
}//equalsMethod
public Matrix multiply(Matrix m2) throws Exception {
if (this.c != m2.r) {
throw new RuntimeException();
}//if
Matrix m3 = new Matrix(this.r, m2.c);
for (int i = 0; i < this.r; i++) {
for (int j = 0; j < m2.c; j++) {
m3.neo[i][j] = 0;
for (int k = 0; k < this.c; k++) {
m3.neo[i][j] += this.neo[i][k] * m2.neo[k][j];
}//forK
}//forJ
}//forI
return m3;
}//multiplyMethod
}//class
Change your scaleMult method to this and it should work:
public void scaleMult(int k) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
this.neo[i][j] *= k;
// you forgot to assign the value, basically we multiply neo[i][j] by k and make neo[i][j] receive that value
// making this.neo[i][j] = this.neo[i][j] *k; would work as well
}
}
}//scaleMult
Also, based on the example you provided on your main method, your code will launch an exception on the multiply method, because as you specified, you can only multiply two matrixes if one's columns amount is equal to the other's row amount. Other than that, I would advise on creating a new class just for your main method.
If your question is multiply a matrix by a number, then here is a very simple example.
public static void main(String []args){
int[][] a = {{1,2,3},{4,5,6}};
int[][] b=new int[2][3];
int i,j,k=2;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
b[i][j]=a[i][j]*k;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
System.out.println(b[i][j]);
}
There must be some array variable on left which should be assigned the multiplied value.
Do this helped? If not, please post your complete code.
I am trying to solve question at Reverse Game
When I submit my code, in some of the testcases it is getting timeout.
I think problem may be in reverseSubArray() method but I am not sure how to improve performance here.
Following is my code:
public class ReverseGame
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int testCases = Integer.parseInt(scanner.nextLine());
int[] numberOFBalls = new int[testCases];
int[] ballNumberArray = new int[testCases];
for (int i = 0; i < testCases; i++)
{
numberOFBalls[i] = scanner.nextInt();
ballNumberArray[i] = scanner.nextInt();
}
for (int i = 0; i < testCases; i++)
{
process(numberOFBalls[i], ballNumberArray[i]);
}
scanner.close();
}
private static void process(int totalNumberOFBalls, int ballNumber)
{
int[] ballsArray = new int[totalNumberOFBalls];
int maximumNumberOnBall = totalNumberOFBalls - 1; // This is because
// balls are numbered
// from 0.
// As the first step is to reverse the Balls arrangement, So insert into
// ballsArray in descending order of index.
for (int i = 0; i < totalNumberOFBalls; i++)
ballsArray[i] = maximumNumberOnBall--;
for (int i = 1; i < totalNumberOFBalls; i++)
{
ballsArray = reverseSubArray(ballsArray, i);
}
int position = findPosition(ballsArray, ballNumber);
System.out.println(position);
}
private static int[] reverseSubArray(int[] a, int fromIndex)
{
int temp = 0, counter = 1;
int midIndex = (a.length - fromIndex) / 2;
for (int i = fromIndex; i < fromIndex + midIndex; i++)
{
temp = a[a.length - (counter)];
a[a.length - (counter)] = a[i];
a[i] = temp;
counter++;
}
/*
* System.out.println(); for (int i = 0; i < a.length; i++)
* System.out.print(a[i] + " ");
*/
return a;
}
private static int findPosition(int[] ballsArray, int ballNumber)
{
for (int i = 0; i < ballsArray.length; i++)
{
if (ballsArray[i] == ballNumber)
return i;
}
return 0;
}
}
The time complexity of your solution is O(n ^ 2). It is too slow for n = 10 ^ 5. So you need to use a better algorithm. Here is simple linear solution which uses the fact that we do not need to know the positions of all balls(we need only the k-th):
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int testsCount = in.nextInt();
for (int t = 0; t < testsCount; t++) {
int n = in.nextInt();
int k = in.nextInt();
// Simulates all rotations,
// but keeps track only of the k-th ball.
// It does not matter what happens to the others.
for (int i = 0; i < n; i++)
if (k >= i)
k = i + n - 1 - k;
out.println(k);
}
out.flush();
}
}
This solution has an O(n) time complexity and easily passes all test cases.
It is actually possible to find the positions of all balls in linear time, but it is not required here.
import java.util.*;
public class Zhangbubble
{
public static void main (String[] args)
{
int Bub[] = new int[6];
Random randy = new Random();
boolean Done = false;
for (int x=0; x<6; x++)
{
Bub[x] = randy.nextInt(100);
System.out.println (Bub[x]);
}
System.out.println ("This is the original array");
while (! Done)
{
Done = true;
for (int x = 0; x<Bub.length-1; x++)
{
if(Bub[x+1] > Bub[x])
{
int temp = Bub[x];
Bub[x] = Bub[x+1];
temp = Bub[x+1];
Done = false;
}
else
{
Done = false;
}
}
for(int x = 0; x<6; x++)
{
System.out.print(Bub[x]+" ");
}
}
}
}
So my programming teacher asked us to make a bubble sort in java using a boolean. His example shows the code in a while loop with for loops. This code is suppose to continuously sort until it has the numbers in the array organized from least to greatest. However, I'm really lost and I can't seem to figure out where I'm going wrong. Any help would be greatly appreciated!
The problem is in your switching algorithm. You are assigning temp twice.
int temp = Bub[x];
Bub[x] = Bub[x+1];
temp = Bub[x+1]; //Here should assign Bub[x+1] to temp
//Example: Bub[x+1] = temp
edit-Actually, there could be some improvement in the sorting algorithm itself, too. Personally, I like to do it this way:
public class Sort {
private static int[] array = { 3, 8, -1, 7, 0, 3 };
public static void main(String[] args) {
for(int i = 0; i < array.length - 1; i++) {
for(int j = i + 1; j < array.length; j++) {
if(array[i] > array[j]) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
for(int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}
this working
public static void main(String[] args) {
int Bub[] = new int[6];
Random randy = new Random();
boolean Done = false;
for (int x=0; x<6; x++)
{
Bub[x] = randy.nextInt(100);
System.out.println (Bub[x]);
}
System.out.println ("This is the original array");
while ( ! Done)
{
for (int x = 0; x<Bub.length-1; x++)
{
if(Bub[x+1] > Bub[x])
{
int temp = Bub[x];
Bub[x] = Bub[x+1];
Bub[x+1]=temp ;
Done = false;
}
else
{
Done = false;
}
}
for(int x = 0; x<6; x++)
{
System.out.print(Bub[x]+" ");
}
Done = true;
}
}
Sorry for my bad English.
I write a game for Android. I simply need to generate the 4 numbers that are not repeated (all different).
This is my code, but it doesn't work :/
k = 3 in start
Random rand = new Random();
a = rand.nextInt(10);
b = rand.nextInt(10);
c = rand.nextInt(10);
d = rand.nextInt(10);
for (int i = 1; i < k; i++) {
if (a == b) {
b = rand.nextInt(10);
k++;
}
else {
k = 0;
}
}
for (int i = 1; i < k; i++) {
if (a == c || b == c ) {
c = rand.nextInt(10);
k++;
}
else {
k = 0;
}
}
for (int i = 1; i < k; i++) {
if (a == d || b == d || c == d) {
d = rand.nextInt(10);
k++;
}
else {
k = 0;
}
}
When I try to bring the number, they can be repeated.
make a List of 10 Integers [0, 1, 2, 3, ... 9] e.g. ArrayList, call Collections.shuffle() and take first four elements
java.util.Random random = new java.util.Random();
java.util.HashSet<Integer> ints = new java.util.HashSet<Integer>();
do {
ints.add(random.nextInt(10));
} while (ints.size()<4);
If you need non-trivial random number generation you need to review this for Android. http://android-developers.blogspot.com/2013/08/some-securerandom-thoughts.html
try this
import java.util.Random;
public class Test {
public static void main(String args[]){
Random rand = new Random();
int[] array = new int[4];
int count=0;
while(count < 4){
int randomNumber = rand.nextInt(10);
if(!contains(array,randomNumber)){
array[count] = randomNumber;
count++;
}
}
printArray(array);
}
public static boolean contains( int[] array, int key) {
boolean result = false;
for (int i = 0; i < array.length; i++){
if(array[i]== key)
result = true;
}
return result;
}
public static void printArray(int[] array){
for(int i=0;i<array.length;i++){
System.out.println(array[i]);
}
}
}