I have a test class below that calls my shuffle class which creates an ArrayList of random numbers and orders them. When I call this ArrayList from my testing class nothing happens.
static ArrayList yup = new ArrayList();
public static void main(String[]args){
Scanner input = new Scanner(System.in);
System.out.println("Enter a num to create a random list of numbers: " );
int guess = input.nextInt();
shuffle y = new shuffle(guess);
System.out.println(y.toString());
ArrayList<Integer> jh = y.getnew_list();
for(int d = 0; d < jh.size();d++){
//System.out.println(three.get(d));
System.out.println(jh.get(d));
}
shuffle one = new shuffle(guess);
}
Shuffle class:
import java.util.*;
import java.util.Random;
public class shuffle implements Comparable <shuffle> {
static int size;
static ArrayList new_list = new ArrayList();
shuffle(int size){
this.size = size;
Random rand = new Random();
for(int i = 0; i<new_list.size();i++){
int t = rand.nextInt(50) + 1;
new_list.add(t);
}
}
public ArrayList getnew_list(){
return this.new_list;
}
public int getSize(){
return size;
}
public String toString(){
String str = new String();
for(int i = 0; i<new_list.size();i++){
str += " " + new_list.get(i);
System.out.println(str);
}
return str;
}
public int compareTo(shuffle that) {
if(this.getSize() > that.getSize()){
return 1;
}
if(this.getSize() < that.getSize()){
return -1;
}
else
return 0;
}
}
in the shuffle class you are not initializing the size variable and you are using it as uninitialized variable that may be the one of the reason.
import java.util.*;
import java.util.Random;
public class shuffle implements Comparable <shuffle> {
static int size;
static ArrayList new_list = new ArrayList();
shuffle(int size){
this.size = size;
Random rand = new Random();
for(int i = 0; i<new_list.size();i++){
int t = rand.nextInt(50) + 1;
new_list.add(t);
Related
I am writing a program that takes in the number of ingredients. The prog
This is my code:
import java.util.Scanner;
public class Restaurant {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int count = scan.nextInt();
}
Change this sortCalories function to the one below. Also you need to pass price array as second parameter like this sortCalories(calories,price, ingredientName);:
public static void sortCalories(double[] calories,double[] price, String[] ingredientName) {
double temp;
String temp1;
for (int p=0; p<calories.length; p++) {
for (int j=p+1; j<calories.length; j++) {
if(calories[p]/price[p]>calories[j]/price[j]) {
temp = calories[p];
calories[p] = calories[j];
calories[j] = temp;
temp = price[p];
price[p] = price[j];
price[j] = temp;
temp1 = ingredientName[p];
ingredientName[p] = ingredientName[j];
ingredientName[j] = temp1;
}
}
}
}
i'm trying to build a code for different actions in arrays with different methods (Constructors) but i cannot seem to find a way to link them...I've made a array with 100 elements and a random variable to fill it....i'd like to know how to get my random elements from the first methods to the second one to make the comparing.... and also i'd like to not include in the 0 element in the random generator..any help?
this is my code
import java.util.*;
public class prova1{
public int min;
public void tabele(){
Random r = new Random();
int d;
int e[]= new int[100];
for(int i=0; i<e.length;i++){
d=r.nextInt(100);
e[i]=d;
System.out.println(e[i]);
}
}
public void emin(){
Random r = new Random();
int d;
int e[]=new int[100];
for(int i=0; i<e.length;i++){
d=r.nextInt(100);
e[i]=d;
if(e[i]<min){
min=e[i];
}
}
System.out.println("Vlera me e vogel eshte: " +min);
}
public static void main(String []args){
prova1 prova = new prova1();
prova.tabele();
prova.emin();
}
}
Return the array from tabele to a local variable and send it as parameter to emin
import java.util.*;
public class prova1 {
public int min;
public int[] tabele() {
Random r = new Random();
int d;
int e[]= new int[100];
for(int i=0; i<e.length;i++){
d=r.nextInt(99) + 1;
e[i]=d;
System.out.println(e[i]);
}
return e;
}
public void emin(int[] e) {
Random r = new Random();
int d;
for(int i=0; i<e.length;i++) {
if(e[i]<min) {
min=e[i];
}
}
System.out.println("Vlera me e vogel eshte: " +min);
}
public static void main(String []args){
prova1 prova = new prova1();
int[] arr = prova.tabele();
prova.emin(arr);
}
}
sorry I'm really new to all of this. I know this is a stupid/easy question, but how would I display the shuffled array after i've set it all up. I have my code below and made the class that creates the array and has the algorithm for shuffling the integers inside the array. But I can't figure out how to display the shuffled array. Heres my code below:
My main:
package lab4b;
import java.util.Scanner;
public class Lab4B {
public static void main(String[] args) {
Shuffler test = new Shuffler(15);
test.Shuffle();
test.display();
}
}
and my Shuffle class:
package lab4b;
import java.security.SecureRandom;
public class Shuffler {
private static final SecureRandom randomNumbers = new SecureRandom();
private int [] data;
public Shuffler (int size){
data = new int [size];
for(int i = 0; i<data.length;i++){
data[i]= i+1;
}
}
public void Shuffle(){
int temp;
for(int first = 0; first<data.length; first++){
int second = randomNumbers.nextInt(data.length);
temp = data[first];
data[first] = data[second];
data[second] = temp;
}
}
public void display()
{
for(int counter =0; counter<data.length; counter++ ){
System.out.print(data[counter]+ " ");
}
System.out.println();
}
}
In this loop you are reset the value of the dataarray
for(int counter =0; counter<data.length; counter++ ){
// data[counter] = counter + 1; - do not do this
System.out.print(data[counter]+ " ");
}
I have tried to print all the paths which give the given amount. But my code does not work properly. I think I am missing some points to print all possible combinations. For example;
if amount: 7 and startCoin = 25, the program needs to give me:
{5,1,1} and {1,1,1,1,1,1,1}.
Can you help me to fix these problem?
Note: Preferably Java Solutions
class Solution {
static int[] coinSet = {1,5,10,25};
static List<List<Integer>> possibleWays = new ArrayList<>();
static List<Integer> currentWay = new ArrayList<>();
private static int makeChange(int amount, int startCoin){
boolean flag = false;
for(int i =0 ; i < coinSet.length ; i++){
if(coinSet[i] == startCoin) {
flag =true;
}
}
if(!flag){
throw new IllegalArgumentException("startCoin has to be in the specified range");
}
int nextCoin = 0;
switch(startCoin) {
case 25:
nextCoin = 10;
break;
case 10:
nextCoin = 5;
break;
case 5:
nextCoin = 1;
break;
case 1:
possibleWays.add(currentWay);
currentWay = new ArrayList<>();
return 1;
}
int ways = 0;
for(int count = 0; count * startCoin <= amount; count++){
ways += makeChange(amount - (count * startCoin),nextCoin);
}
return ways;
}
public int calculateNumberOfWays(int amount, int startCoin) throws Exception {
if (amount == 0) {
throw new Exception(); }
return makeChange(amount, startCoin);
}
public static void main(String[] args) {
System.out.println(makeChange(5,25));
System.out.println(possibleWays);
}
}
This can be solved using backtracking but that is not very efficient, below is the working java code
/**
* Created by sumit sharma on 3/1/2016.
*/
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
public class Main {
static int[] coinSet = {1,5,10,25};
static List<List<Integer>> possibleWays = new ArrayList<>();
static List<Integer> currentWay = new ArrayList<>();
public static void main(String[] args) {
List<Integer> countOfCoins = new ArrayList<>();
makeChange(7, 0, countOfCoins);
//System.out.print(possibleWays);
}
private static int makeChange(int amount, int startCoinIdx, List<Integer> coinsSoFar) {
if(startCoinIdx == coinSet.length){
if(amount == 0){
possibleWays.add(coinsSoFar);
System.out.println(coinsSoFar);
}
//System.out.println(coinsSoFar);
return 0;
}
for(int count = 0;(count*coinSet[startCoinIdx]) <= amount;count++){
List<Integer> temp = new ArrayList<>();
for(int i = 0;i < coinsSoFar.size();i++) temp.add(coinsSoFar.get(i));
for(int i = 0;i < count;i++) temp.add(coinSet[startCoinIdx]);
makeChange(amount - (count * coinSet[startCoinIdx]),startCoinIdx+1, temp);
temp.clear();
}
return 0;
}
}
Link to solution on Ideone : http://ideone.com/kIckmG
I am trying to add random numbers to an empty array 20 numbers 0-99. When I run the code below it prints out 51 numbers and they are all 0.
Can someone please help me figure out what I am doing wrong here.
import java.util.Random;
public class SortedArray
{
int randomValues;
int[] value;
public SortedArray()
{
}
public int getRandom()
{
Random random = new Random();
for(int j=0; j<20; j++)
{
randomValues = random.nextInt(100);
}
return randomValues;
}
public int getArray()
{
int result = 0;
value = new int[randomValues];
for(int item : value)
{
System.out.println("The array contains " + item);
}
return result;
}
}
Here is my main method
public class ReturnSortedArray
{
public static void main(String[] args)
{
SortedArray newArray = new SortedArray();
int random = newArray.getRandom();
int array = newArray.getArray();
System.out.println(array);
}
}
In your method getArray
the code
value = new int[randomValues];
is simply creating a new empty int array of size ramdomValues.
As the default value of an int is 0, that is what you are getting
Also in your method getRandom you are setting the same value time and time again
for (...)
randomValues = random.nextInt(100);
try
public int[] getRandomArr()
{
int randomValues [] = new int [20];
Random random = new Random();
for(int j=0; j<20; j++)
{
randomValues[j] = random.nextInt(100);
}
return randomValues;
}
I see a few issues, you should probably set the values in your constructor. You could also call it a set method (since it's not actually a get). Also, your getArray() doesn't return an array. So, I think you really wanted something like this,
public class SortedArray {
private Random random = new Random();
private int[] value = new int[20];
public SortedArray() {
super();
setRandomValues();
}
public void setRandomValues() {
for (int j = 0; j < value.length; j++) {
value[j] = random.nextInt(100);
}
}
public int[] getArray() {
return value;
}
}
And then your main method, should be updated like
public static void main(String[] args) {
SortedArray newArray = new SortedArray();
int[] array = newArray.getArray();
System.out.println(Arrays.toString(array));
}