Random method for a quiz - java

Each method contain a question with multiple choice. When i call the method in the main, i need to shuffle it and make sure there are no repetition.
public static void main(String[] args) {
question_1();
question_2();
question_3();
question_4();
//continue to question 15
question_15();
}
thing that i tried.
int question_A = question_1();
int question_B = question_2();
int question_C = question_3();
int question_D = question_4();
//to question 15
int question_O = question_15();
//then i created an array
int [] question = new int[14];
question[0] = question_A;
question[1] = question_B;
question[2] = question_C;
question[3] = question_D;
//to last array
question[14] = question_O;
//to random it here is the code
Random r = new Random();
for (int counter = 0; counter <10; ++counter){
int swap_Index = r.next Int(15-counter)+counter; //there is an space between next Int, that because i was getting not properly formatted in the edit box
int temp = question[counter];
question[counter] = question[swap_Index];
question[swap__Index] = temp;
int[] question_To_Ask = new int[10];
for (int count = 0; count<10; ++count){
question_To_Ask[count] = question[count];
}
The reason the random does not work is because it starts executing the program at
int question_A = question_1();
for the random, i also tried any way such as Math.random. None of these worked and yeah, please do not use advance technique to solve this problem as i am a beginner.

The easy way to do this is using a list:
List<Question> questions = new ArrayList<Question>();
questions.add(question_1);
questions.add(question_2);
questions.add(question_3);
.....
Collections.shuffle(questions);

See, I dunno what you are doing with all these methods, but let us consider another approach put every question in a database, use a Collection like say hashmap in ur java code access ur database and based on the id of the question u can call whichever question u want to call and for the shuffling part there is a predefined function called shuffle in java, u can use it to shuffle ur question collection. Just a suggestion, try it, i think it is a better approach.

You could do something like this
public static void main(String[] args) {
// declare the variables first
int q1 = 1;
int q2 = 2;
...
int q15 = 15;
int[] questions = new int[] { q1, q2, q3, q4, ... q15 };
System.out.println("Before Shuffle");
for (int i : questions) {
System.out.println(i);
}
shuffle(questions); // here we do the shuffle
System.out.println("After Shuffle");
for (int i : questions) {
System.out.println(i);
}
}
public static void shuffle(int[] questions) {
Random random = new Random();
for (int i = 0; i < questions.length; i++) {
int newIndex = random.nextInt(questions.length - 1);
swap(questions, i, newIndex);
}
}
private static void swap(int[] questions, int oldIndex, int newIndex) {
int temp = questions[oldIndex];
questions[oldIndex] = questions[newIndex];
questions[newIndex] = temp;
}

Related

random elements from a list DURING the addition

There are 20 names in my code.
my function has 2 options to add elements to a list I've:
1.
Inserting all the 20 names to the list:
public void addNames() {
list.add("name1");
list.add("name2");
...
list.add("name20");
}
2.
Adding only 5 random names(from the 20 names) to the list. For doing it, I thought about 2 ways. What's the best way to random 5 names from the 20? maybe you have a better way.
A.
Using a random set of indices (each value will be between 0 to 19 because there are 20 names) and before the 'add' I'll check if adding them or not by some counter:
public void addNames() {
// adding 5 random indices between 0 to 19 to the set
Set<Integer> set = new HashSet<Integer>();
Random r = new Random();
Set<Integer> indices = new HashSet<>(numRandomNames); //==5
for (int i = 0; i < numRandomNames; ++i) {
int index = r.nextInt(numNames - 0); //==19
indices.add(index);
}
int counter = 0;
if (indices.contains(counter)) {
list.add("name1");
}
counter++;
if (indices.contains(counter)) {
list.add("name2");
}
counter++;
if (indices.contains(counter)) {
list.add("name3");
}
...
}
B.
RandomList that extends List and overrides the 'add' function to do the same as 'A.' does BUT the override 'add' will decide whether adding the value inside the function so my function will look the same as 1. with the override 'add' function
Do you think about a better solution? if not, then which one is better? (A or B?). I just saw that people recommends not to extend the java collection but I think it's the best solution from these 2 solutions.
NOTE
====
my code can have 10000 names or more even so I don't want to add all the 10,000 names to this\other list and then random 5 of them to other list. I prefer to do it DURING the addition in order to avoid many places of the list while I don't really need them.
EDIT
an answer to ProgrammerTrond:
I'm not sure I'll do it but what I asked me to show is my suggestion of 2.B:
public class RandomList<Integer> implements List<Integer> {
private int addCallsCounter;
private Set<Integer> setIndices = null;
public RandomList(final int numElements, final int maxVal, final int minVal) {
addCallsCounter = 0;
setIndices = new HashSet<Integer>(numElements);
Random r = new Random();
while (setIndices.size() < numElements) {
int index = r.nextInt(maxVal - minVal + 1) + minVal;
if (setIndices.contains(index) == false) {
setIndices.add(index);
}
}
}
#Override
public boolean add(Integer object) {
if (setIndices.contains(addCallsCounter++)) {
this.add(object);
return true;
}
return false;
}
}
and from my code I'll do so:
RandomList randList = new RandomList(5);
randList.add("name1");
randList.add("name2");
randList.add("name3");
...
randList.add("name19");
randList.add("name20");
but my problem is that I need to implement MANY abstract methods of List pfff. RandomList cann't be abstract too because then it won't be able to be instantiated.
try this:
List<Integer> index = new ArrayList<>();
List<String> five_names = new ArrsyList<>();
List<String> allnames = new ArrayList<>();
store five random values
for(int i = 0;i < 5;i++){
int index_no = getrandomNumber();
index.add(index_no);
five_names.add(allnames.get(index_no));
}
getRandomNumber method:
public int getRandomNumber(){
Random rnd = new Random();
int x = rnd.nextInt(20);
if(index.contains(x)){
return getRandomNumber();
}else{
return x
}
}
Why not like this? You don't need the random index list in your list implementation. Didn't you just want a method that would add to a list 5 random names drawn from a set of available names?
import java.util.*;
public class ListAdding {
private static List<String> allNames = Arrays.asList("name1", "name2", "name3", "name4", "name5", "name6", "name7");
public static void main(String[] args) {
new Temp().test();
}
void test() {
List<String> list = new ArrayList<>();
list.add("Bernie");
addFiveRandom(list);
for (int i = 0; i < list.size(); i++) {
System.out.println(i + ": " + list.get(i));
}
// Example: 0: Bernie
// 1: name2
// 2: name3
// 3: name6
// and so on
}
void addFiveRandom(List<String> toBeAddedTo) {
List<Integer> indices = new ArrayList<>();
while (indices.size() < 5) {
int newIndex = new Random().nextInt(5);
if (!indices.contains(newIndex))
indices.add(newIndex);
}
for (Integer index : indices) {
toBeAddedTo.add(allNames.get(index));
}
}
}

Need help understanding how to sort info from text file using java

I am trying to build a program that reads in info from a .txt file, which contains 20 individuals. Each person has four fields, the team they belong to, their batting average, and home run totals. I need to add each individual to their team(4 players to each team), total up the team home run totals, and rank the 5 teams in order.
I am able to read the text in properly to a single array, consisting of each individual, but I cannot figure out how to also use this data to create a 2D Array. Using the 2D array I would, put the players on the correct teams, and add their home run totals. I want to sort the home run totals from greatest to smallest for each team, and each individual. I am have done my best to find answers, and to learn on other posts and sites, but I am just stumped with the concept of creating 2D arrays and how to sort them.
Updated explanation:
This is what the info should look like for the single array:
[Team][Name][avg][Home Runs]
Then I JUST want to sort the [Home Runs] column, from greatest to smallest, but don't know how to just access that portion of the array.
The 2D array should look like this:
[Team] [Total Team Home Runs]
Once again, sorting from greatest to smallest.
Example of the .txt file looks like this:
Team: Name: Avg:HR:
MILRyan Braun .31015
STLMatt Adams .28718
PITSterling Marte .26420
CINJoey Votto .30224
CUBAnthony Rizzo .27422
PITAndrew McCutchen .29522
MILAdam Lind .28013
The following class reads in the .txt file and puts it in array.
public class ReadTxt {
static String[] teamm = new String[20];
static String[] name = new String[20];
static int[] avg = new int[20];
static double[] homeRuns = new double[20];
static String teams;
static int i;
public void Players(String[] teamm, String[] name, int[] avg, double[] homeRuns){
String[] team = new String[20];
File txtFile = new File("C:\\Users\\Users Name\\Desktop\\homerun.txt");
try{
Scanner txtScan = new Scanner(txtFile);
while(txtScan.hasNext()){
for(i = 0; i < 20; i++){
teams = txtScan.nextLine();
team[i] = teams;
}
}
}
catch(FileNotFoundException e){
System.out.println("File not found" + txtFile);
}
for (i = 0; i < team.length; i++){
System.out.println(team[i]);
}
}
}
The next class is my attempt at sorting:
public class Sort {
static String[] teamm = new String[20];
static String[] name = new String[20];
static int[] avg = new int[20];
static double[] homeRuns = new double[20];
private int index = 0;
private int US = 0;
static double[] homeRunArray;
public void Players(String[] teamm, String[] name, int[] avg, double[] homeRuns){
homeRunArray[index] = ReadTxt.homeRuns[index];
index++;;
US++;
}
public void selectionSort(){
double temp;
int min;
for(int i = 0; i < US-2; i++){
min = i;
for(int j=i+1; j<= US-1; j++){
if(min !=i){
temp = homeRunArray[i];
homeRunArray[i] = homeRunArray[min];
homeRunArray[min] = temp;
}
}
}
}
public void printArray(double[] homeRuns){
for(int i = 0; i < 20; i++){
System.out.print(homeRunArray[i]);
}
System.out.print("\n");
}
}
I don't get your question, but I think you are kind of stuck in your 2D-Array problem...
I would recommend you to create a class and implement Comparable (or use a Comparator). Something like the code below, or even better, make a real Player class. This is much easier to understand.
public class Sorter {
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(new File("team"));
List<SortableLine> lines = new ArrayList<SortableLine>();
while(scanner.hasNext()) {
lines.add(new SortableLine(scanner.nextLine()));
}
Collections.sort(lines);
for(SortableLine line : lines) {
System.out.println(line.line);
}
} catch(FileNotFoundException e) {
System.err.println("File not found");
}
}
private static class SortableLine implements Comparable<SortableLine> {
private String sortCol;
private String line;
private SortableLine(String line) {
this.line = line;
this.sortCol = line.substring(24, 26);
}
public int compareTo(SortableLine other) {
return sortCol.compareTo(other.sortCol);
}
}
}

How to read copy the array with the array from another class? [In loading file]

I want to create a game and I need to read file from the notepad
when I use my loadfile.java alone, it work very well. Then, I would like to copy my data into datafile.java as it will be easier for me to do the fighting scene. However, I can't copy the array in my loadfile.java to the datafile.java and I don't understand why.
import javax.swing.*;
import java.io.*;
import java.util.Scanner;
public class loadfile
{
static String filename = "Save.txt";
static int size = 4;
static int s;
static int[] number;
static String[] line;
private static void load() throws IOException
{
BufferedReader reader = new BufferedReader(new FileReader(filename));
while (reader.readLine()!= null)
{
size++;
}
size -= 4;
reader.close();
line = new String[size];
number = new int[size];
BufferedReader reader2 = new BufferedReader(new FileReader(filename));
for (int i = 0; i < size; i++)
{
line[i] = reader2.readLine();
}
reader2.close();
for (int i = 4; i < size; i++)
{
number[i] = Integer.parseInt(line[i]);
}
}
public static String[] getData()
{
return line;
}
public static int[] getNumber()
{
s = size - 4;
int[] num = new int[s];
for (int i = 0; i < s; i++)
{
num[i] = number[i+4];
}
return num;
}
public static int getDataSize()
{
return size;
}
public static int getNumberSize()
{
return size - 4;
}
This is my loadfile.java
I use the file with 4 names and 9 * n int in the notepad as I want to check whether I have the character first before I read the file. However, before I can handle this problem, I got another problem that I can't copy the array into my datafile.java
The datafile.java is separate with two constructor. One is for Starting the game and one is for loading the data. The constructor with the (int num) is the problem I have. First, I would like to show the java first:
import java.util.Arrays;
import java.io.*;
public class datafile
{
private static String[] data;
private static int[] number;
private static String[] name;
private static int[] a, d, s;
private static int[] hp, maxhp;
private static int[] mp, maxmp;
private static int[] lv, exp;
public datafile()
{
initialization();
name[0] = "Pet";
a[0] = 100;
d[0] = 100;
s[0] = 100;
hp[0] = 500;
mp[0] = 500;
maxhp[0] = 500;
maxmp[0] = 500;
exp[0] = 100;
lv[0] = 1;
}
public datafile(int num) throws IOException
{
initialization();
loadfile l = new loadfile();
for (int i = 0; i < l.getNumberSize(); i++)
{
number[i] = l.getNumber()[i];
}
for (int i = 0; i < l.getDataSize(); i++)
{
data[i] = l.getData()[i];
}
for(int i = 0; i < 4; i++)
{
name[i] = data[i];
}
for(int i = 0; i < 4; i++)
{
a[i] = number[1+(i*9)];
d[i] = number[2+(i*9)];
s[i] = number[3+(i*9)];
hp[i] = number[4+(i*9)];
mp[i] = number[5+(i*9)];
maxhp[i] = number[6+(i*9)];
maxmp[i] = number[7+(i*9)];
lv[i] = number[8+(i*9)];
exp[i] = number[9+(i*9)];
}
}
public static String getName(int n)
{
return name[n];
}
public static int getAttack(int n)
{
return a[n];
}
public static int getDefense(int n)
{
return d[n];
}
public void initialization()
{
name = new String[3];
a = new int[3];
d = new int[3];
s = new int[3];
hp = new int[3];
mp = new int[3];
maxhp = new int[3];
maxmp = new int[3];
lv = new int[3];
exp = new int[3];
}
public static void main (String[] args) throws IOException
{
new datafile(1);
}
}
When I run the program, the debugging state this line
data[i] = l.getData()[i];
as an error
I don't know what wrong with this line and I tried so many different ways to change the way the copy the method. However, it didn't work
The error says this:
Exception in thread "main" java.lang.NullPointerException
at datafile.<init>(datafile.java:38)
at datafile.main(datafile.java:92)
I hope you guys can help me with this problem because I don't want to fail with my first work
in your datafile(int num)
you call
loadfile l = new loadfile();
but you never call the load() method on you loadfile
l.load();
Edit: my bad, I didn't see your initialization method, but regardless, I'm going to stick with my recommendation that you radically change your program design. Your code consists of a kludge -- you've got many strangely named static array variables as some kind of data repository, and this suggests that injecting a little object-oriented design could go a long way towards creating classes that are much easier to debug, maintain and enhance:
First I recommend that you get rid of all of the parallel arrays and instead create a class, or likely classes, to hold the fields that need to be bound together and create an ArrayList of items of this class.
For example
public class Creature {
private String name;
private int attack;
private int defense;
// constructors here
// getters and setters...
}
And elsewhere:
private List<Creature> creatureList = new ArrayList<>();
Note that the Creature class, the repository for some of your data, should not be calling or even have knowledge of the code that loads the data, but rather it should be the other way around. The class that loads data should create MyData objects that can then be placed within the myDataList ArrayList via its add(...) method.
As a side recommendation, to help us now and to help yourself in the future, please edit your code and change your variable names to conform with Java naming conventions: class names all start with an upper-case letter and method/variable names with a lower-case letter.

Having trouble working with a class with an array

When i call my TruthTable class & populate it w/inputs, I can't access individual slots in the array when I'm trying to set the inputs of the AND gates?
threeAndGates.java -the class where the error happens
import java.util.Scanner;
public class threeAndGates {
public static void main(String[] args){
LogicGate and1 = new LogicGate(LogicGate.AND);
LogicGate and2 = new LogicGate(LogicGate.AND);
LogicGate and3 = new LogicGate(LogicGate.AND);
System.out.print("What is the number of Inputs? ");
Scanner scan = new Scanner(System.in);
int numOfInputs = scan.nextInt();
System.out.print("What is the number of Outputs? ");
int numOfOutputs = scan.nextInt();
TruthTable Table1 = new TruthTable(numOfInputs,numOfOutputs);
Table1.PopulateTruthTable();
//below is where it is giving me "the type of the expression must be an array type but it resolves to TruthTable"
for(int r = 0; r<(Math.pow(2, numOfInputs)) ; r++ ){
and1.setInput1(Table1[r][0]);
and1.setInput2(Table1[r][1]);
and2.setInput1(Truth1[r][2]);
and2.setInput2(Truth1[r][3]);
and3.setInput1(and1.getOutput());
and3.setInput2(and2.getOutput());
Table1[r][numOfInputs + numOfOutputs] = and3.getOutput();
}
Table1.printTruthTable();
}
}
TruthTable.java
public class TruthTable {
private int numOfInputs;
private boolean[][] table;
public TruthTable(int inputs, int outputs){
this.numOfInputs = inputs;
int rows = (int) Math.pow(2,inputs);
int columns = inputs + outputs;
table = new boolean[rows][columns];
}
public void printTruthTable(){
for(int r = 0 ; r < table.length ; r++){
for(int c = 0; c < table[r].length; c++)
System.out.printf("%-5b ", table[r][c]);
System.out.println();
}
}
public String toString(){
String outStr = new String();
for(int r = 0; r < table.length; r++){
for(int c = 0; c < table[r].length; c++)
outStr += String.format("%-5b ", table[r][c]);
outStr += '\n';
}
return outStr;
}
public boolean[][] PopulateTruthTable(){
String s;
String r ="";
int[] Line = new int[numOfInputs];
boolean bit;
for ( int i= 0; i < Math.pow(2,numOfInputs) ; i++){
int x = numOfInputs - Integer.toBinaryString(i).length();
for(int j = 0; j<x ; j++)
r += "0";
s = r + Integer.toBinaryString(i);
for(int k=0; k<s.length() ;k++){
Line[k] = s.charAt(k)-48;
}
for(int m=0 ; m<numOfInputs ; m++){
if(Line[m]==1) bit = true;
else bit = false;
table[i][m] = bit;
}
r="";
}
return table;
}
}
Your TruthTable class is not an Array. It contains an Array. You could add a get and set method to your TruthTable class:
public boolean getValueAt(int x, int y) {
return this.table[x][y];
}
public void setValueAt(int x, int y, boolean value) {
this.table[x][y] = value;
}
and use that to work with the TruthTable values.
This is unrelated to your problem, but when naming variables in your classes, the general practice is to use lower case. For example you have:
TruthTable Table1 = new TruthTable(numOfInputs,numOfOutputs);
would be better
TruthTable table1 = new TruthTable(numOfInputs,numOfOutputs);
and probably best as
TruthTable truthTable = new TruthTable(numOfInputs,numOfOutputs);
The better and more consistent you name things the easier it will be to read down the road.
Your TruthTable class isn't a mutil-dimensional array; it has a multi-dimensional array field. Therefor, you can not use the following syntax:
tableInstance[x][y]
If you TruthTable's table field was public, or better yet, it had a getter, you could do womthing like this instead...
tableInstance.getTable()[x][y]
Some languages (like C#) also support operator overloading which would allow you define the behaviour of using the [] index operator (or others like +, /, etc.). This would allow you to make the indexing work. Unfortunately, Java doesn't have this feature.
This is more of a comment than an answer but I needed more space.
If your code causes you problems later, may I make a suggestion? Break down populateTruthTable into 2 or 3 methods, putting each loop in it's own well named method because Each method should do exactly one thing
Also you probably shouldn't be accessing the array directly from the main class, instead put all the code from your main classes "for" loop into a method in the TruthTable class and call that method from main because you should Tell an object what to do rather than asking for it's data.
I'm not trying to say you're doing it wrong or anything, you are obviously doing very well, but it's always good to pick up more coding tricks/practices as you go along and you seem like you are at the level where these would come in handy.

How to check that a string is generated only once

I have this method that generates random questions, I want to be able to generate each question once and no more than once.
How could I do that?
This is the code so far:
package boss;
import java.util.Random;
import javax.swing.JFrame;
public class Boss {
public static void main(String[] args) {
LoginWindow window = new LoginWindow();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
public String getQuestions() {
String [] question = new String[30];
question[0] = "hello";
question[1] ="yo";
question[2] ="b";
question[3] ="ha";
//Generating random questions
Random r = new Random();
int i=r.nextInt(4);
String quest=question[i];
return quest;
}
}
You're not generating questions in your example - you're picking them from a fixed set stored in an array. It sounds like you just want to shuffle the array, then iterate over part of it until you have seen the required number of questions. So - suggest you shuffle the questions, then just iterate over the shuffled array, or shuffle an array of indexes 0..n and iterate over those in the original list of questions.
There's plenty of approaches to shuffling, perhaps the simplest is a single pass over the input data, swapping each element with some other randomly chosen element.
Use an ArrayList instead of a table. Removes the displayed question from the ArrayList when it has been displayed.
you have to keep a list of the ones you've already used and check against that.
boolean used[] = new boolean[30];
int i;
do {
Random r = new Random();
i=r.nextInt(4);
} while(used[i] == true);
String quest=question[i];
used[i] = true;
A fairly simple solution would be to keep a record of all the questions you have asked and only generate ones that you have not:
private ArrayList<Integer> questionsAsked = new ArrayList<>();
public String getQuestions()
{
String [] question = new String[30];
question[0] = "hello";
question[1] ="yo";
question[2] ="b";
question[3] ="ha";
//Generating random questions
Random r = new Random();
int i = r.nextInt(question.length);
//keep looping until you find a question you have not asked
while(questionsAsked.contains(i))
{
i = r.nextInt(question.length);
}
//add that question to the list of questions already asked
questionsAsked.add(i);
//ask the question
return question[i];
}
You can use Collections.shuffle together with queue remove and lazy question generation, working example:
import java.util.*;
public class Mkt {
private Queue<String> questions = null;
public Mkt() {
for(int i = 0; i < 10; i++) {
System.out.println(getQuestion());
}
}
public String getQuestion() {
if(questions == null || questions.size() == 0) {
questions = generateQuestions();
}
return questions.remove();
}
private Queue<String> generateQuestions() {
List<String> list = Arrays.asList("hello", "yo", "b", "ha");
Collections.shuffle(list);
return new LinkedList<String>(list);
}
public static void main(String[] args) {
new Mkt();
}
}
Sample run:
$ javac Mkt.java && java Mkt
ha
yo
hello
b
b
ha
hello
yo
hello
ha
You could solve this with a "shuffle" algorithm. Basically randomize (shuffle) your array and then just pick the next item from the list.
One of the easiest shuffle algorithms is Knuth's: http://en.wikipedia.org/wiki/Knuth_shuffle
Pseudocode to shuffle your array:
Random rand = new Random();
for (int i=questions.Length-1; i>=0; --i)
{
int nextRand = rand.Next(i);
// Switch the randomly selected 'next' to the current pointer in the array
string temp = questions[nextRand];
questions[nextRand] = i;
questions[i] = temp;
}
Keep track of what you've already picked.
String [] question = new String[30];
boolean[] picked = new boolean[30];
...
if (!picked[i])
{
String quest=question[i];
picked[i] = true;
}
else
// choose another
(Obviously you'll need to restructure your code and also deal with knowing when you've exhaused your question supply and all have been picked)

Categories