import java.util.ArrayList; import java.util.Random; public class NumSorting { public static int numOfComps = 0, numOfSwaps = 0; public static void main(String[] args)
[Code] ....
What is wrong with my code in this sorting program? It won't copy the random generated numbers to the sort method. How to get the random generated numbers to copy to the sort method. Below is what the program is displaying.
I have been assigned to write a program that has a user input random numbers then the program is to sort them in different ways. I have the coding for the different sorts but, I have an error saying that I am missing a return statement in the "Bubble" method. I am trying to return "arr[i]" in the "for loop" which gives me this error, and when I try to take the "return arr[i]" outside of the "for()" loop the error reads the it cannot locate variable "i".
import java.awt.* ; import java.awt.event.*; import javax.swing.*; public class SwingSorts extends JFrame implements ActionListener { JRadioButton bubble; JRadioButton selection;
I have been working on getting the merge sort working, I have a complteted code but am not getting the right results... Here is my code and the results are "876323149" ...
public class MergeSort { private static int[] local; // for use in copying private static int[] a; public static void main(String[] args) { int[] test = {2,3,1,4,7,8,6,3,9};
Merge sort implementation on the given array i listed in the code below. I know Arrays.sort() or Collections.sort() could do the trick but i want to achieve it through merge sort.
import java.util.*; public class Merge{ public static void main(String[] args){ String[] myStringArray=new String[]{"z","a","z","b","c","e","z","f" ,"g","a","w","d","m" ,"x","a","R" ,"q","r","y","w","j","a","v","z","b"}; } }
I've playing around with linked lists and methods for sorting. So far I've tested the iterative sort, insertion sort, quick sort and they all worked perfectly. Now, I am trying to implement merge sort that would take a linked list of jobs and sort them according to their priority. I found a few solutions on the web, of which I am trying to implemented this one: LeetCode.
My system is a simple one, I do have a linked list of print jobs, each of which has the priority. The following code should sort my print queue and return the link node of the first sorted element. Here's the code.
//defining a job that has priority public class Job { private int priority;
[Code]....
The problem I've been trying to solve is that I am getting the stack overflow at line
ListNode<T> h1 = mergeSort(left);
meaning that I am getting into a loop somewhere down through the process of breaking the linked list into half, half or halfs and so on.
I'm having a problem printing out the descending order of my array. The array order goes like (Title,Studio,Year). I try to create to ints with the compareTo method but when the program is run the I get array out of bounds. Could the answer possibly be that in order not not have the out of bounds error, to create a for loop inside of the while?
public class Movie2 { // instance variables private int year; private String Title; private String Studio;
I have this assignment to write a Merge Sort algorithm using recursion. To start I have a very tough time picturing what is happening when it comes to recursion, but I do understand how merge sorting works. At the moment I feel as though a very good portion of my code is correct, but I am having trouble with the recursion in the main method [ mergeSort(Queue<T> queue) ].
I have another 4 or so hours to pass in my assignment finished or not, and at this point I can honestly say I have no clue how to make my code work. I tried working through the problem on paper with a simple queue of size 3, but even that is a struggle. On paper my code works perfectly fine, so there is definitely something I am missing.
Below is what I have along with my JUnit test.
Java Code:
private Queue<T> output = new Queue<T>(); private Queue<T> output1 = new Queue<T>(); private Queue<T> output2 = new Queue<T>(); public Queue<T> mergeSort(Queue<T> queue) { // TODO 1 if(queue.size() <= 1) { return queue;
I've just written a program that generates 100 numbers that range from 0 ~ 25 using arrays, the program calls another method that sorts the even numbers into a separate array and returns the array. I need it to display both arrays, however, when I run my program, the numbers of both arrays are mixed together, and I'm not sure how to separate them.
[ public class Array1 { public static void main(String[] args) { int array [ ] = new int[100]; for (int i = 0; i < array.length; i++) { array[i] = (int) (Math.random() * 26);
I'm trying to make a program that generates 20 random integers between 1 and 20 and then prints the list of random numbers to the screen. After that, I want to print a different list to screen with the same numbers from the first list only skipping any number that has been already printed to the screen. So two lists are printed to the screen. The first one has 20 random numbers. The second one has those same 20 numbers but only prints the numbers in the first list that aren't duplicated. So if m
y list of 20 random integers contains three 2s and two 14s, only one 14 and one 2 is printed to the second list. Currently, my code generates 20 numbers from 1 to 20 and stores those numbers in an array but I don't know how to print solve the second part of my problem. I don't know how to print the s different list only without duplicate numbers. As a result, my output is nothing because it doesn't print any number from the first list as oppose to skipping only duplicate one.
public void randomNum(){ System.out.println("Twenty random integers: "); int max = 20; // max value for range int min = 1; // min value for range Random rand = new Random(); int[] all = new int[20];
I tried out doing number (generated randomly) != (another number) but that does not work. If I for example want a number between 1 and 10, but I do not want the number 5, what can I do in order to make this happen?
I have an array that I filled with 30 random characters, but now I am trying to sort them in ascending order and the descending order using lambda expressions.
public class RandomCharacters { public static void main(String args[]){ Random r =new Random(); char myarray[] = new char [30]; for (int i = 0 ; i < 30; i++)
I am working on my homework, and everything is fine. But I was wondering if I can fix one thing. basically it prompt from a user for a text file name and save the content as an array. some part of my code is here:
public class arraysort{ static BufferedReader kb = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws Exception { System.out.print("Enter the first name of the file: "); String input = kb.readLine(); int[] firstArray = readFile(input); firstArray = bubbleSort(firstArray);
Code below. I am not sure if my logic is correct. I want to prompt a user to enter registration numbers from 100 to 1000, store the numbers in an array then sort them.
import java.util.Scanner; class regnumber { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("registration number ", 100,1000);
Basically it prompt from a user for a text file name and save the content as an array. some part of my code is here:
public class arraysort{ static BufferedReader kb = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws Exception { System.out.print("Enter the first name of the file: "); String input = kb.readLine();
[Code] ....
As you you may figure, it works if I put numbers separated by each lines. I was wondering if I can change it so that it works when I put numbers separated by space rather then each lines.
I am trying to code a program that orders 5 random numbers from high to low with the basic coding i know (im starting to learn theory of methods... so you can imagine) When i run the code i cant get all 5 numbers ordered but my logic says the code is right although it's pretty confusing.
I know you could code in a simpler way but first i wanna get it as it is right now. When i debug (on my own way cause i dont know how to actually use it) shows line 72 with yellow color.
public class NuevaCalculadora { import java.util.Scanner; public static void main(String[] args) {
I'm supposed to write a program, which reads float numbers from a file, puts them in an array and sorts the array. After that I'm suppose to add the numbers so that when I add the 1 and 2 number of the array, I'm suppose to save the sum on the position of number 1, then I add number 3 and 4 and save the sum on position 2 etc. Also if my array has an uneven number of floats it's suppose to add the last 3 and not 2 numbers in the last iteration. The problem is the method throws an ArrayOutOfBounds Exception but I can't seem to find my mistake.
That's the second method. The first one just stores the sum in another variable and then returns it. Also is there a way in that I can Scanner/File/array etc. and initialize the array only once so I don't write the same code two times like it is now.
package sumOfFloats;
import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Scanner; public class SumOfFloats { public static float sumFloats() throws FileNotFoundException{
Assume that vehicles are going through a two-way traffic intersection. There are three types of vehicles: car, motor bikes and trucks. Generate a series of 10 random integers, between 1 and 3,inclusive.The numbers represent the type of vehicle as stated below:
NumberVehicle Category 1Car 2Motor bikes 3Trucks
Write a program, using a for loop, to count how many vehicles going through the traffic intersection are cars, motor bikes and trucks. Then, the program should print out the numbers for each vehicle category. There is no user input for this program. How do i do it so they will add up the sum of each vehicle?
The answer should be something like
Number of cars = X Number of motor bikes = Y Number of Trucks = Z
I have a class project working on that is supposed to ask the user for input and create a random set of #'s in 4 rows. However it compiles but doenst run At one point Eclipse was telling me Scanner not closed?
import java.util.Scanner; public class Matrixbuilder { // input a number that is used to build a matrix. ie = 4 rows and 4 colums //get the number //build the matrix of random integers //print the matrix
I am currently taking a class in the field. My assignment is to generate 6 unique random numbers using a "Do While" expression. I might be mistaken but doesnt the inner loop execute first and then it works its way out? With this logic I believe my code should work but then again its not.
public class DoLottery { public static void main (String args[]) { int max= 10; int random; int random2; int random3;
[code]...
I originally had output at the end but decided to comment it out to see if code would execute if I placed it every time the test was true.
I've been trying to find the easiest way to write to generate a number which is between intervals of a arbitrary min and a max value.I've been searching for this but I don't find this particular thing.I've found that this combination works:
For a project we have to "shuffle" items in an array using random numbers. We are supposed to generate random numbers and use those numbers to exchange array elements. But I am not sure what that means, "exchange array elements". Does that mean you generate 2 random numbers within the length of the array, and then switch the items at those locations in the array?
I need to fix a program in which the user must get a three random numbers in a row. They have five chances. This is what i have so far:
public static void main(String[] args)throws IOException { // TODO code application logic here BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int num1=(int)(Math.random()*(9-0)); int num2=(int)(Math.random()*(9-0)); int num3=(int)(Math.random()*(9-0));