Create Permutation Method Where J Is Chosen Randomly From 0 To I

Apr 16, 2015

I need figuring this problem out. It appears that I am attempting to generate a permutation of the string "ABCDEF" 720,000 times using this method:

In the second method, j is chosen randomly in the range from 0 to i (inclusive).

Once the permutations are generated, the program will proceed in counting the number of times each permutation occurs, calculating the chi square statistic of the situation, and creating the chi square distribution with 719 degrees of freedom, then outputting the statistic and the chi square probability of the permutations. The generatePermutation method is where all the magic happens. Only trouble, I can't figure out what I equals to. The times where I think I have i as a correct value only give me the program outputted as 1.0 probability every time. What it needs to be doing is outputting variable probability as a number always between 0 and 1, not 1 all the time. Here is my code:

assignment7part2.java:
package math3323assignment7;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import org.apache.commons.math3.distribution.ChiSquaredDistribution;
import com.google.common.collect.Multiset;
import com.google.common.collect.TreeMultiset;

[Code] ....

i is meant to step through the index of the array, but I can't figure out what i is.

View Replies


ADVERTISEMENT

Placing Randomly Chosen Words From Text File Into 2D Array

Apr 5, 2014

I have an assignment for college that involves placing randomly chosen words from a text file into a 2-d array. I have nearly completed ithowever I am having difficulty with a list of string type.What I have done so far is below,

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.io.*;
import java.lang.Math;
 
[code]....

when I try to run an instance of the program(using BlueJ) I get an error saying there is a null pointer exception at line 35 which is wordsForGrid. add (puzzleWords.get(pos));

I am thinking that this is related to the fact that at this line in the loadWordsFromFile method List <String>words=new ArrayList<String>(); I could not create an empty list of string type, i.e List<String>words = new List<String>();

Is this correct? Or am I looking in the wrong place. The textfile I am using contains over 6000 words on a separate line for each if that makes any difference.

View Replies View Related

Create For Loop That Randomly Assign Values To Each Element Within Array?

Apr 16, 2014

Started learning about Array's I'm doing an exercise where you create a for loop that randomly assigns values to each element within the array, but where is my code going wrong?

import java.util.Scanner;
public class ArrayExamples{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double exampleArray[] = new double[5];
System.out.print("Enter a Number: ");
int num1 = input.nextInt();

[Code] .....

View Replies View Related

Java Permutation Of Int Array?

Jan 13, 2015

Starting back at my Computer Science program after a few years off. Having trouble making an algorithm to print all permutations of an Int array. For example, Int[] a = new Int[1,2,3] should print :

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

I've google'd a few different algorithms but cant seem to understand what people are doing as they arnt using many comments on their code.

View Replies View Related

Permutation Of 5 Digit Number With BigInteger

Jan 23, 2014

i have tried permutation with big Integer in Java. it works fine upto 4 integer input say 3456 P 2345 but nothing happens in console when i type 5 digit input..here is my code

public class cvic {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter n and r: ");
BigInteger n = scan.nextBigInteger();
BigInteger r = scan.nextBigInteger();
System.out.println("nPr = "+fact(n).divide(fact(n.subtract(r))));

[code]....

View Replies View Related

Swing/AWT/SWT :: Color Schemes Chosen By User?

Nov 13, 2014

I'm working on a standalone GUI under Windows using JAVA SWT and eclipse. I have a colour scheme based on six colours inplemented as:

RGB rgb01=new RGB(233, 150, 122);
final Color color01=new Color(Display.getCurrent(),rgb01);
RGB rgb02=new RGB(250, 128, 114);
final Color color02=new Color(Display.getCurrent(),rgb02);
RGB rgb03=new RGB(255, 160, 122);
final Color color03=new Color(Display.getCurrent(),rgb03);
RGB rgb04=new RGB(255, 165, 0);
final Color color04=new Color(Display.getCurrent(),rgb04);
RGB rgb05=new RGB(255, 140, 0);
final Color color05=new Color(Display.getCurrent(),rgb05);
RGB rgb06=new RGB(255, 127, 80);
final Color color06=new Color(Display.getCurrent(),rgb06);

I have 8 sets of six colours prepared. I'd like to let the user choose a colour scheme and even change it while they use the application.

As can be seen the Color s are with final as if they are not the code below gives an error.

if (script_deletion_flag[x]!=null && script_deletion_flag[x]) {
itemDBAB1.setForeground(color04);
}
else
{
itemDBAB1.setForeground(color03);
}

View Replies View Related

Determine Number Of Subsets Of K Items Chosen From A Set Of N Distinct Objects

Sep 22, 2014

Part of my program requires writing a function that determines the number of subsets of k items that can be chosen from a set of n distinct objects ("n choose k" or n!/((n-k)! n!)). The only errors I am getting are from the first line of the function:

// n choose k: distinct subsets of k items chosen from n items
public static int choose (int n, int k) {
if (k <= 0)
return 1;
else
return choose(n--, k--) * (n/k);
}

View Replies View Related

How To Create ArrayList Method

Jan 28, 2015

Okay so here are the requirements.

Java Code:
/**
*The method creates an array list of integers and then prompts the user
* for an integer. As long as the user continues to enter anything other
* than -999, add the number to the array list.
*
* @return the array list of numbers
*/ mh_sh_highlight_all('java');

I've attempted this several times but am still struggling.

View Replies View Related

Create MergeSort Method Without Using Recursion

Nov 6, 2014

We are supposed to create a MergeSort method without the using recursion. Most of the code is already completed, the only thing that I believe I need are two for loops (an inner and an outter) that will make calls to the merge method. I need implementing the sort method of the merge sort algorithm without recursion, where the length of the array is a power of 2. Keep merging adjacent regions whose size is a power of 2. For ex: lengths will be 1, 2, 4, 8, 16,.

public class MergeSorter {
public static void sort(int[] a) {
//for(int i = 1; i <= a.length; i++) the parameters for the for loop are wrong.
{
merge(0,0,1,a);
merge(1,1,2,a);
merge(2,2,3,a);

[Code]...

View Replies View Related

Create A Method That Returns A New Array

Mar 4, 2014

I need to create a method that returns a new array containing the componentwise sum of its arguments(if length is the same). For instance, if the input arrays are {0,1, 2} and {2, 2, 3} then the output is {0+2, 1+2, 2+3}, i.e. {2,3,5}.If the input arrays have different numbers of elements, the method should return null.

I came with something like this, however i dont know how to make a copy of an array from two arrays. My code obviously wont compile. package whatever;

import java.util.Arrays;
public class hhhh {
public static void main(String[] args) {
double [] a = {1,2,3};
double [] b = {2,3,4};

[code]...

View Replies View Related

Can't Create A New Method Or Use Case Statements

Oct 4, 2014

I'm trying to create a simple java math question quiz using random operators, but keep sinking myself into deeper despair. The numbers must range from 0-9 and given an operator: +,-,/,*,%. Can't create a new method or use Case statements. The code isn't finished but don't want to make it any worse.

package marco;
import java.util.Scanner;
import java.util.Random;
public class Project {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Random rand = new Random();
System.out.println("How many questions do you want?");

[code]...

View Replies View Related

Create Method In Main Class And Use It On Object

Feb 26, 2015

I've been writing classes over and over for school. So I create a class outside of my main class. I create a new constructor and then create objects from my main class. I hope that makes sense. So i use methods in that class to work with the object. So I have an object name I've created <dot> method name. So I can create objects and then use methods from the class, but I'm wondering can I create a method in my main class and use it on that object? I don't understand how to do that.

View Replies View Related

How To Create Insertion Sort Method For A Vector

Jun 9, 2014

im trying to create an insertion sort method for a vector. I know how to insertionsort for an array, but for a vector im having problems

Source code:
PHP Code: package test;
import java.util.*;
import java.io.*;
public class LinearSearch {
public static void main (String[] args) {
Vector myVector = new Vector();

[Code]...

I'm getting errors at lines 38 and 39 "Left-hand side of an assignment must be a variable". "Syntax-error(s) on token(s) misplaces contructor(s)". How can i fix them ??

View Replies View Related

Swing/AWT/SWT :: Create A New PanelLogin In Main And Use Method To Get Panel

Feb 16, 2014

I have two classes: main (with JFrame) and a panelLogin class with a method returning a panel with all login components..so I create a new panelLogin in main and use the method to get the panel, put it into my JFrame. But here the problem: on the loginPanel the ActionListener for the login-event, if the input is correct,the panel shall disappear and the main panel shall appear.Should I make some kind of top class, which handels the panels?

View Replies View Related

How To Pass Object Type To A Method / Then Create Instance

Aug 9, 2014

Essentially, the code I want is:

public void randomCreate(ParentObject obj){
int x = random(0-4); //pseudo
int y = random(0-4); //pseudo
create new ParentObj(x,y);
}

ParentObject is actually abstract, so you would only ever pass one of its children objects to it, and a child object of that type would be created. It seems like there should be a way to pass a type, rather than an object, and then create an instance later down, but I don't know if that is actually possible, or if it is poor programming style.

View Replies View Related

Create 4 Types Of Strings Under Same Method But Only Draw One At A Time

May 11, 2015

I am having trouble with methods. What I want to do is be able to create 4 types of strings under the same method, but only draw one of them at a time.

i.e

UI.initialise;
UI.addButton ("pipe", this::drain);
public void drain(){
this.pipe ("pipe1");
this.pipe ("pipe2");

[Code] ....

When I press the button "drain" it will print=

pipe1

pipe2

pipe3

pipe4

I am having a lot of difficulty just printing one out after each time I press drain.

"drain"

pipe1

"drain"

pipe2 etc..

View Replies View Related

Create A Recursion Method That Takes ONLY ARRAY As A Parameter

Mar 9, 2015

so i have this question where it wants me to create a recursion method that takes ONLY THE ARRAY as a parameter, and without using loops or static variables inside the method, and then the method returns the smallest value in that array. However, i tried making the simple if statements where i compare the first element of the array with the second element using the length of the array and decreasing it to get the next elements and compare it again by calling the recursion method, but the problem is when i call the method again, the length does not decrease, even if i store it in a variable, the variable will initialize itself again, and the length wont change.

View Replies View Related

Create A Method Header Named ConvertTOKM That Take Int Parameter

Feb 7, 2015

In my book for learning java, one of the questions asks us to create a method header named convertTOKM that takes an int parameter, which is the number of miles, and returns a double value for the converted value in kilometers. I made one, but wanted to know if I was right in any way.

Here it is:public double convertTOKM(int miles, double kilometers){

View Replies View Related

Unable To Create Method That Calculates Standard Deviation Of Array

Jun 15, 2014

I am trying to create a method that calculates the standard deviation of array. What I want to be able to do is something like this

package standardDevaitionAndMean;
public class StandardDeviationTest {
public static void main (String[] args)
{
int [] array = {12,12,12,12,12,12,12};
standardDev = array.StandardDevation();
System.out.print(standardDev);
}
}

With something like this

package standardDevaitionAndMean;
public class StandardDeviation
{double mean1;
double standDev;
public double Mean(double ... array)
{
for(int i=0; i<array.length;i++)

[Code]...

So basically I want to be able to make an array in a class and be able to calculate its standard deviation with my method in my other class. I know the code I wrote is terrible but I just wanted to show what I am trying to do. I am kind of shaky on how arrays operate

View Replies View Related

Does Either Of These Randomly Generate Both 0 And 1?

Nov 22, 2014

"which code is best for randomly generating integer 0 or 1". Haven't gotten my grade back yet but in reading these during the test I didn't think any of these would kick out both of those numbers, but there was no "none of the above" option on the test.This is exactly how it appeared on the test:

A) (int)Math.random() + 1
B) (int)(Math.random() + 0.2)
C) (int)Math.random()
D) (int)(Math.random() + 0.8)
E) (int)(Math.random() + 0.5)

I've tried all of them in the cs lab 100 times each and none of them generated both numbers. 'A' kicked out 1 every time. 'B,D and E' kicked out 0.2, 0.8 and 0.5 respectively each time, and 'C' kicked out 0. Did I just not run them enough times for the result to change or am I right in thinking there's a glitch on the test?

View Replies View Related

Randomly Generating A 0 Or 1

Nov 4, 2014

My random integer always seems to be zero.. I am at the ends of my wit.

package Exercises;

import java.util.Random;
import java.util.Scanner;
import javax.swing.JOptionPane;

/**
* Heads or tails?
* That is what this is.
*/
public class num14 {

[Code] ....

Attached File(s) : New Text Document.txt (2.59K)

View Replies View Related

Create A Method Called MirrorImage Which Takes Two Integer Arrays As Input Parameters

Jan 8, 2009

Create a method called mirrorImage, which takes two integer arrays as input parameters. You may assume that the two actual parameters have the same length. Your method should return true if the arrays are the reverse of each other. Otherwise mirrorImage should return false.

Examples:

data1:{1,2,3}
data2:{3,2,1}
==> true

[code].....

I'm pointing a place outside of the array or something

runtime error (line 8, column 0): java.lang.ArrayIndexOutOfBoundsException: 10

-> array.ArrayQuestions.mirrorImage()

View Replies View Related

Randomly Generated Labyrinths

Dec 13, 2014

So i am making this game with randomly generated labyrinths and i get error in array. Here is the code:

import java.util.Random;
public class Labyrinth {
 
//0-walls
//1-path
 
[code]....
 
I've got lost in some things so my code might contain unnecessary code.

View Replies View Related

Randomly Get Index Of Array

Jan 7, 2015

I created and an Array of integers how can I get randomly get/pick the index of each array element.

View Replies View Related

Randomly Shuffling Cards

Apr 8, 2015

i'm trying to make a random card shuffler but the output would sometimes have same value multiple times. For example it might print out A5 at the fifth index then print out A5 again as the 32 index.

public class randomGenerateCards {
public static void main(String[] args) {
String temp;
String[] cards = new String[]
{ "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10", "A11",
"A12", "A13", "S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8",
"S9", "S10", "S11", "S12", "S13", "H1", "H2", "H3", "H4", "H5",
"H6", "H7", "H8", "H9", "H10", "H11", "H12", "H13", "D1", "D2",
"D3", "D4", "D5", "D6", "D7", "D8", "D9", "D10", "D11", "D12",

[code]....

View Replies View Related

Randomly Filling Tic Tac Toe Board

Oct 19, 2014

So I an assignment in Java to write a code which will randomly populate squares in a Tic Tac Toe Board. I pretty much have it I think, but I cannot get the 'O' to appear on the board, some squares will be blank. We were told to use the Random utility to generate the squares. I am attaching the .gif's which are used. Here is my code:

import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class LandryTicTacToe extends JFrame
{
/**
*
*/
private static final long serialVersionUID = -8781512780135301721L;
private final int HEIGHT = 450;//Set value for Height
private final int WIDTH = 500;//Set value for Width
private static JButton [] button = new JButton[9];//Declare array of Buttons

[Code] .....

[attachment=37084:TicTac0.gif][attachment=37085:TicTacX.gif]

Instructions Given to me:

Display a frame that contains nine labels, arranged like a Tic Tac Toe board. A label may display an image icon for X, an image icon for O, or nothing. Display images randomly in each label. Use the Random class to generate 0, 1, or 2, which corresponds to displaying an X image, an O image, or nothing.

Attached image(s)

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved