Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,154,158 members, 7,821,937 topics. Date: Wednesday, 08 May 2024 at 10:12 PM

Shuffling Multidimensional Array Of Values - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Shuffling Multidimensional Array Of Values (1104 Views)

Java Array Problem / Can An Array In Java Hold Multiple Types? / What The Different Between ENUM And ARRAY In Java (2) (3) (4)

(1) (Reply) (Go Down)

Shuffling Multidimensional Array Of Values by oladapo32(m): 9:42pm On Jul 07, 2016
I am very new to android development. But, there is this project I am working where I need to generate algebra equations( as in ax + b = c)and ask for the user to give the answer, x. I need to generate numbers that would be set in a Text View randomly so users wont have to solve the same equations every time. So, I created a repository of a,b, c, and the operators values like so:

String[][] equationA = {
{"1","2", "+", "5" },
{"6", "17", "-", "4"},
{"5", "2", "-", "3"},
{"3", "5", "-", "7"},
{"5", "2", "-", "3"},
{"2", "5", "-", "7"},
{"5", "10", "-", "6"},
{"5", "10", "-", "6"},
{"5", "10", "-", "6"},
{"5", "10", "-", "6"}
};
String[][] equationB = {
{"1","2", "+", "5" },
{"4", "5", "-", "10"},
{"9", "4", "-", "3"},
{"5", "10", "-", "6"},
{"5", "10", "-", "6"},
{"5", "10", "-", "6"},
{"9", "10", "-", "6"},
{"5", "10", "-", "6"},
{"5", "10", "-", "6"},
{"5", "10", "-", "6"}
};
String[][] equationC = {
{"1","2", "+", "5" },
{"5", "10", "-", "6"},
{"8", "18", "-", "0"},
{"5", "10", "-", "6"},
{"5", "10", "-", "6"},
{"56", "10", "-", "6"},
{"5", "10", "-", "6"},
{"5", "10", "-", "6"},
{"5", "10", "-", "6"},
{"5", "10", "-", "6"}
};
String[][] equationD = {
{"1","2", "+", "5" },
{"2", "8", "-", "4"},
{"6", "2", "-", "8"},
{"6", "9", "-", "1"},
{"8", "8", "-", "3"},
{"5", "10", "-", "6"},
{"5", "10", "-", "6"},
{"5", "10", "-", "6"},
{"5", "10", "-", "6"},
{"5", "10", "-", "6"}
};
String[][] equationE = {
{"0","2", "+", "5" },
{"9", "8", "-", "6"},
{"3", "6", "-", "7"},
{"2", "6", "+", "4"},
{"4", "8", "-", "9"},
{"8", "10", "-", "6"},
{"5", "10", "=", "6"},
{"5", "10", "-", "6"},
{"9", "1", "=", "6"},
{"5", "10", "-", "6"}
};
I also created an Arraylist where I hope to reshuffle the equations A, B,C, D and E. like:

ArrayList <String[][]> puzzle = new ArrayList<String[][]>();
puzzle.add(equationA);
puzzle.add(equationB);
puzzle.add(equationC);
puzzle.add(equationD);
puzzle.add(equationE);
Collections.shuffle(Arrays.asList(puzzle));
A challenge I am now faced with is how I can reshuffle the equations and place the variable a, b, c, and the operator randomly in a text view I have created with xml. This is the how I referenced the textview

final int[] answers = new int[17];
final int[] variablesa = new int[17];
final int[] variablesb = new int[17];
final int[] variablesc = new int[17];
final char[] operators = new char[17];

final TextView variable_a_1 = (TextView)

findViewById(R.id.variable_a_1);
final TextView variable_a_2 = (TextView) findViewById(R.id.variable_a_2);
final TextView variable_a_3 = (TextView) findViewById(R.id.variable_a_3);
final TextView variable_a_4 = (TextView) findViewById(R.id.variable_a_4);
final TextView variable_a_5 = (TextView) findViewById(R.id.variable_a_5);
final TextView variable_a_6 = (TextView) findViewById(R.id.variable_a_6);
final TextView variable_a_7 = (TextView) findViewById(R.id.variable_a_7);
final TextView variable_a_8 = (TextView) findViewById(R.id.variable_a_8);
final TextView variable_a_9 = (TextView) findViewById(R.id.variable_a_9);
final TextView variable_a_10 = (TextView) findViewById(R.id.variable_a_10);
final TextView variable_a_11 = (TextView) findViewById(R.id.variable_a_11);
final TextView variable_a_12 = (TextView) findViewById(R.id.variable_a_12);
final TextView variable_a_13 = (TextView) findViewById(R.id.variable_a_13);
final TextView variable_a_14 = (TextView) findViewById(R.id.variable_a_14);
final TextView variable_a_15 = (TextView) findViewById(R.id.variable_a_15);
final TextView variable_a_16 = (TextView) findViewById(R.id.variable_a_16);
final TextView variable_a_17 = (TextView) findViewById(R.id.variable_a_17);
final TextView variable_b_1 = (TextView) findViewById(R.id.variable_b_1);
final TextView variable_b_2 = (TextView) findViewById(R.id.variable_b_2);
final TextView variable_b_3 = (TextView) findViewById(R.id.variable_b_3);
final TextView variable_b_4 = (TextView) findViewById(R.id.variable_b_4);
final TextView variable_b_5 = (TextView) findViewById(R.id.variable_b_5);
final TextView variable_b_6 = (TextView) findViewById(R.id.variable_b_6);
final TextView variable_b_7 = (TextView) findViewById(R.id.variable_b_7);
final TextView variable_b_8 = (TextView) findViewById(R.id.variable_b_8);
final TextView variable_b_9 = (TextView) findViewById(R.id.variable_b_9);
final TextView variable_b_10 = (TextView) findViewById(R.id.variable_b_10);
final TextView variable_b_11 = (TextView) findViewById(R.id.variable_b_11);
final TextView variable_b_12 = (TextView) findViewById(R.id.variable_b_12);
final TextView variable_b_13 = (TextView) findViewById(R.id.variable_b_13);
final TextView variable_b_14 = (TextView) findViewById(R.id.variable_b_14);
final TextView variable_b_15 = (TextView) findViewById(R.id.variable_b_15);
final TextView variable_b_16 = (TextView) findViewById(R.id.variable_b_16);
final TextView variable_b_17 = (TextView) findViewById(R.id.variable_b_17);
final TextView operator_1 = (TextView) findViewById(R.id.operator_1);
final TextView operator_2 = (TextView) findViewById(R.id.operator_2);
final TextView operator_3 = (TextView) findViewById(R.id.operator_3);
final TextView operator_4 = (TextView) findViewById(R.id.operator_4);
final TextView operator_5 = (TextView) findViewById(R.id.operator_5);
final TextView operator_6 = (TextView) findViewById(R.id.operator_6);
final TextView operator_7 = (TextView) findViewById(R.id.operator_7);
final TextView operator_8 = (TextView) findViewById(R.id.operator_8);
final TextView operator_9 = (TextView) findViewById(R.id.operator_9);
final TextView operator_10 = (TextView) findViewById(R.id.operator_10);
final TextView operator_11 = (TextView) findViewById(R.id.operator_11);
final TextView operator_12 = (TextView) findViewById(R.id.operator_12);
final TextView operator_13 = (TextView) findViewById(R.id.operator_13);
final TextView operator_14 = (TextView) findViewById(R.id.operator_14);
final TextView operator_15 = (TextView) findViewById(R.id.operator_15);
final TextView operator_16 = (TextView) findViewById(R.id.operator_16);
final TextView operator_17 = (TextView) findViewById(R.id.operator_17);


final TextView variable_c_1 = (TextView) findViewById(R.id.variable_c_1);
final TextView variable_c_2 = (TextView) findViewById(R.id.variable_c_2);
final TextView variable_c_3 = (TextView) findViewById(R.id.variable_c_3);
final TextView variable_c_4 = (TextView) findViewById(R.id.variable_c_4);
final TextView variable_c_5 = (TextView) findViewById(R.id.variable_c_5);
final TextView variable_c_6 = (TextView) findViewById(R.id.variable_c_6);
final TextView variable_c_7 = (TextView) findViewById(R.id.variable_c_7);
final TextView variable_c_8 = (TextView) findViewById(R.id.variable_c_8);
final TextView variable_c_9 = (TextView) findViewById(R.id.variable_c_9);
final TextView variable_c_10 = (TextView) findViewById(R.id.variable_c_10);
final TextView variable_c_11 = (TextView) findViewById(R.id.variable_c_11);
final TextView variable_c_12 = (TextView) findViewById(R.id.variable_c_12);
final TextView variable_c_13 = (TextView) findViewById(R.id.variable_c_13);
final TextView variable_c_14 = (TextView) findViewById(R.id.variable_c_14);
final TextView variable_c_15 = (TextView) findViewById(R.id.variable_c_15);
final TextView variable_c_16 = (TextView) findViewById(R.id.variable_c_16);
final TextView variable_c_17 = (TextView) findViewById(R.id.variable_c_17);

In case it is needed, here is a snippet from my xml:

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:textAppearance="?android:attr/textAppearanceSmall"

android:text=""
android:textSize="30sp"
android:id="@+id/variable_a_3"
android:padding="5dip"
android:gravity="center"

android:textColor="@color/colorTitle"
android:background="@color/colorAccent"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="x"
android:textSize="30sp"
android:id="@+id/constant3"
android:padding="5dip"
android:gravity="center"
android:background="@color/colorPrimary"
android:textColor="@color/colorTitle"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=""
android:textSize="30sp"
android:id="@+id/operator_3"
android:padding="5dip"
android:gravity="center"
android:textColor="@color/colorTitle"
android:background="@android:color/holo_red_light"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=""
android:textSize="30sp"
android:id="@+id/variable_b_3"
android:padding="5dip"
android:gravity="center"
android:textColor="@color/colorTitle"
android:background="@android:color/holo_orange_dark" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="="
android:textSize="30sp"
android:id="@+id/equals_3"
android:padding="5dip"
android:gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"

android:text=""
android:textSize="30sp"
android:id="@+id/variable_c_3"
android:padding="5dip"
android:gravity="center"
android:textColor="@color/colorTitle"
android:background="@color/puzzle_selected"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:inputType="number"
android:text=""
android:textSize="30sp"
android:id="@+id/answer_3"
android:padding="5dip"
android:layout_marginStart="5dip"
android:gravity="center"
android:textColor="@color/colorTitle"
android:background="@android:color/holo_purple"/>

Thanks in advance for the help.
Re: Shuffling Multidimensional Array Of Values by danidee10(m): 5:05pm On Jul 09, 2016
Go to stackoverflow bro....besides the code you posted is too much....narrow it down to something small
Re: Shuffling Multidimensional Array Of Values by LordRahl001: 11:51pm On Jul 09, 2016
Well, I don't know what u wanna do clearly. But what I can advice is that u shud put all ur operators in an array, then generate a random. Number within th length of t array such that for every operation, one of t operators would b selected at random, then generaye random numbers for the operands!! Am sure with that, u will b able to randomize the operations and the operands at the same time! Instead of hard coding values! And the only resource mongering operation would be the generation of random numbers! Hope this helps
Re: Shuffling Multidimensional Array Of Values by lekropasky(m): 6:41pm On Jul 11, 2016
This is quite Long. I dont really get what your are trying to do, but i can guess this is what you are trying to do.

Like in quiz System, you want users to see Diffrent questions everytime the press 'Next' or whatever button you link the event with...Ok.
if thats what you are trying to do, try this.

Java is an OOP, so you can create a POJO( Plain Old Java Object) class, which will represent each equation...Like this--->

public class Equation{

//Create the class property/fields
public int variableA;
public int variableB;
public int variableC;
public char operator;
public int answer;

//create a constructor, remember this will be automatically called when the object is created
//Remember the uses of 'this' keyword to make refrence to the member of current class...

public Equation(int varA, int varB, int varC, char op, int ans ) {
this.variableA = varA;
this.variableB = varB;
this.variableC = varC;
this.operator = op;
this.answer = ans;
}

//now, create the getters and setters

public void setVariableA(int v_A ){
this.variableA = v_A;
}
public void setVariableB(int v_B ){
this.variableB = v_B;
}
public void setVariableC(int v_C ){
this.variableC = v_C;
}
public void setOperator(int op ){
this.operator = op;
}
public void setAnswer(int ans ){
this.answer = ans;
}
public int getVariableA(){
return this.variableA;
}
public int getVariableB(){
return this.variableB;
}
public int getVariableC(){
return this.variableC;
}
public char getOperator(){
return this.operator;
}
public int getAnswer(){
return this.answer;
}

//voila, this will make things easy
public String getQuestion(){
//form a question String. so, that it"ll be very easy to call later...
String question = getVariableA() + "x " + getOperator() + " "+getVariableB() + " = " + getVariableC();
return question;
}
}

Then Create an activity that serves as your Question/Quiz activity...

class QuizActivity extends Activity{

//create a list that"ll hold your equations

List<Equation> equationList = new ArrayList<>();
TextView equationTextView;
//create a random object to generate random numbers
Random randomNumber = new Random();
//lets say you bind the event to a Button
Button nextEquation;

protected void onCreate(Bundle instance ) {
//get textView
equationTextView = (TextView) findViewById(R.id.textview);
nextEquation = (Button) findViewById(R.id.nextButton);
//add equations to the list...
//remember equationList takes an Equation object as the List elements
equationList.add( new Equation(1,2,3,"-",5));
equationList.add( new Equation(1,4,0,"-",4));
equationList.add( new Equation(1,5,3,"-",1));
equationList.add( new Equation(1,9,3,"-",2));
equationList.add( new Equation(1,2,1,"-",1));
nextEquation.setOnClickListener(new View.setOnClickListener(){
public void onClick(View view){
//shuffle the List, generate any number between 0 and 5
int currentIndex = randomNumber.nextInt(5);
//now, the real shuffle..
//this will take the equation number 'currentIndex' from equationList and put it on the TextView.
//Each time user click the 'next Button' a new random number will be generated which causes the equationList to return diffrent equation on each click
Equation currentEquation = equationList.get(currentIndex);
equationTextView.setText(currentEquation.getQuestion());
}
});
}

}

Hope this helps...Feel free to ask questions.
Re: Shuffling Multidimensional Array Of Values by oladapo32(m): 7:42am On Jul 15, 2016
Thanks a lot @lekropasky. I am sorry for replying you this late. I am just seeing your suggestions. I will try it and get back to you. However, what I am trying to accomplish is to display like 17 different linear equations on the same activity and so there is no next button present and yes it is like a quiz system, but a quiz system in a gaming environment. I am working on a Digital Game Based Learning project. DGBL is a way of making learning fun with games. What I am doing specifically is making users solve some set of equations and have their correct answers placed as givens on a sudoku board, so they then go on to play the sudoku game after which the game is completed. In this case of 17 equations being displayed, the correct answers to the equation will be be givens on a 9 x 9 sudoku puzzle. I have done all this part, what I just need to do is to make the equations displayed and the sudoku puzzle non-static, i.e, the users will not see the same question and puzzle whenever they want to replay the game.

P.S. It is not the individual equations in an activity that would be randomized but the whole equation that forms givens to the puzzle. for instance I have created 17 questions(linear equation) that i store in equationsA that are givens(initial number placed on a sudoku board) on a solvable sudoku puzzle , each of the 17 equations will not be randomized because the solutions must be of a particular structure. I have equationsB,C,D,and E which also have 17 equations. What will be randomized is equationsA-E. So if a user sees equationsB the first time next time he replays or restarts the game he sees equationsC and so on. Hope you now understand.
Thanks again.

(1) (Reply)

How To Build A Tech Startup As A Non-technical Founder / Having Issues / How Do I Set Up Git On My Whogohost Shared Server?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 60
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.