Question

In: Computer Science

Can anyone merge these two java programs I keep getting an error can't find main classes....

Can anyone merge these two java programs I keep getting an error can't find main classes.

MySorts.java

public class MySorts {

public static void insertSort(int[] arr) {

int i, temp, j;

for (i = 1; i < arr.length; i++) {

temp = arr[i];

j = i - 1;

while (j >= 0 && arr[j] > temp) {

arr[j + 1] = arr[j];

j = j - 1;

}

arr[j + 1] = temp;

}

}

public static void selectSort(int[] arr) {

int temp;

for(int i=0; i<arr.length; i++){

for(int j=i+1; j<arr.length; j++){

if(arr[i]>arr[j]){

temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

}

}

}

private static int pivot(int[] arr, int begin, int end) {

int p = arr[end];

int i = (begin-1);

for (int j=begin; j<end; j++)

{

if (arr[j] < p)

{

i++;

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

}

int temp = arr[i+1];

arr[i+1] = arr[end];

arr[end] = temp;

return i+1;

}

private static void quickSortRecursive(int[] arr, int begin, int end) {

if (begin < end)

{

int pi = pivot(arr, begin, end);

quickSortRecursive(arr, begin, pi-1);

quickSortRecursive(arr, pi+1, end);

}

}

public static void quickSort(int[] arr) {

quickSortRecursive(arr, 0, arr.length-1);

}

public static void mergeSort(int[] arr) {

mergeSortRecursive(arr, 0, arr.length-1);

}

private static void mergeSortRecursive(int[] arr, int begin, int end) {

if (begin < end)

{

int m = (begin+end)/2;

mergeSortRecursive(arr, begin, m);

mergeSortRecursive(arr , m+1, end);

merge(arr, begin, m, end);

}

}

private static void merge(int[] arr, int start, int middle, int end) {

int n1 = middle - start + 1;

int n2 = end - middle;

int L[] = new int [n1];

int R[] = new int [n2];

for (int i=0; i<n1; ++i)

L[i] = arr[start + i];

for (int j=0; j<n2; ++j)

R[j] = arr[middle + 1+ j];

int i = 0, j = 0;

int k = start;

while (i < n1 && j < n2)

{

if (L[i] <= R[j])

{

arr[k] = L[i];

i++;

}

else

{

arr[k] = R[j];

j++;

}

k++;

}

while (i < n1)

{

arr[k] = L[i];

i++;

k++;

}

while (j < n2)

{

arr[k] = R[j];

j++;

k++;

}

}

}

Proj1.java

import java.util.*;

public class Proj1 {

public static final int RANGE = 100000;

/**

* Randomly fill up the given array with integers from 0 to 100000

*/

public static void fillArray(int[] arr) {

Random randGen = new Random();

for (int i = 0; i < arr.length; i++)

arr[i] = randGen.nextInt(RANGE);

}

/**

* copy the content in second array to first array. We assume two array has same

* size

*

* @param firstArray the destination of copy action

* @param secondArray the source of copy action

*/

public static void copy(int[] firstArray, int[] secondArray) {

for (int i = 0; i < secondArray.length; i++)

firstArray[i] = secondArray[i];

}

/**

* isSorted

*

* @return true if the array is sorted from least to largest or is sorted from

* largest to least

*/

public static boolean isSorted(int[] arr) {

if (arr.length <= 1)

return true;

int index = 0;

while (index < arr.length - 1 && arr[index] == arr[index + 1]) {

index++;

}

// bypass first equal elements

if (index == arr.length - 1)

return true; // all elements are the same

else if (index < arr.length - 1 && arr[index] > arr[index + 1]) // possible descend

{

for (int i = index + 2; i < arr.length; i++)

if (arr[i] > arr[i - 1])

return false;

} else // sort for ascend

{

for (int i = index + 2; i < arr.length; i++)

if (arr[i] < arr[i - 1])

return false;

}

return true;

}

/**

* test the sort with given array. The test array will not be modified

*

* @param arr The array that is used to test the sort. The array will not be

* modified

* @name The name of sort method that is to be tested

*/

public static void testSort(int[] arr, String name) {

long totalTime = 0;

int[] a = new int[arr.length];

copy(a, arr); // copy arr to a

// get start time

long start = Calendar.getInstance().getTimeInMillis();

if (name.equals("Insert Sort"))

MySorts.insertSort(a);

else if (name.equals("Select Sort"))

MySorts.selectSort(a);

else if (name.equals("Quick Sort"))

MySorts.quickSort(a);

else if (name.equals("Merge Sort"))

MySorts.mergeSort(a);

long end = Calendar.getInstance().getTimeInMillis();

if (isSorted(a)) {

totalTime = end - start;

System.out.println("Execution time for " + name + " is: " + totalTime + " milliseconds");

} else

System.out.println("The " + name + " did not work properly");

}

/**

* display the test menu

*/

public static void displayMenu() {

System.out.println("***************************");

System.out.println("* MENU *");

System.out.println("* 1. Fill Array *");

System.out.println("* 2. Select Sort *");

System.out.println("* 3. Insert Sort *");

System.out.println("* 4. Quick Sort *");

System.out.println("* 5. Merge Sort *");

System.out.println("* 6. Quit *");

System.out.println("***************************");

}

public static void main(String[] args) {

int choice;

int[] arr = new int[RANGE];

Scanner input = new Scanner(System.in);

do {

displayMenu();

System.out.println("Enter you choice: ");

choice = input.nextInt();

switch (choice) {

case 1: // generate a new random filled array

fillArray(arr);

break;

case 2: // select sort

testSort(arr, "Select Sort");

break;

case 3: // insert sort

testSort(arr, "Insert Sort");

break;

case 4: // quick sort

testSort(arr, "Quick Sort");

break;

case 5: // merge sort

testSort(arr, "Merge Sort");

break;

case 6: // quit

System.out.println("Make sure that you have good documentation!");

break;

default: // wrong choice

}

} while (choice != 6);

}

}

Solutions

Expert Solution

import java.util.*;

class MySorts {

public static void insertSort(int[] arr) {

int i, temp, j;

for (i = 1; i < arr.length; i++) {

temp = arr[i];

j = i - 1;

while (j >= 0 && arr[j] > temp) {

arr[j + 1] = arr[j];

j = j - 1;

}

arr[j + 1] = temp;

}

}

public static void selectSort(int[] arr) {

int temp;

for(int i=0; i<arr.length; i++){

for(int j=i+1; j<arr.length; j++){

if(arr[i]>arr[j]){

temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

}

}

}

private static int pivot(int[] arr, int begin, int end) {

int p = arr[end];

int i = (begin-1);

for (int j=begin; j<end; j++)

{

if (arr[j] < p)

{

i++;

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

}

int temp = arr[i+1];

arr[i+1] = arr[end];

arr[end] = temp;

return i+1;

}

private static void quickSortRecursive(int[] arr, int begin, int end) {

if (begin < end)

{

int pi = pivot(arr, begin, end);

quickSortRecursive(arr, begin, pi-1);

quickSortRecursive(arr, pi+1, end);

}

}

public static void quickSort(int[] arr) {

quickSortRecursive(arr, 0, arr.length-1);

}

public static void mergeSort(int[] arr) {

mergeSortRecursive(arr, 0, arr.length-1);

}

private static void mergeSortRecursive(int[] arr, int begin, int end) {

if (begin < end)

{

int m = (begin+end)/2;

mergeSortRecursive(arr, begin, m);

mergeSortRecursive(arr , m+1, end);

merge(arr, begin, m, end);

}

}

private static void merge(int[] arr, int start, int middle, int end) {

int n1 = middle - start + 1;

int n2 = end - middle;

int L[] = new int [n1];

int R[] = new int [n2];

for (int i=0; i<n1; ++i)

L[i] = arr[start + i];

for (int j=0; j<n2; ++j)

R[j] = arr[middle + 1+ j];

int i = 0, j = 0;

int k = start;

while (i < n1 && j < n2)

{

if (L[i] <= R[j])

{

arr[k] = L[i];

i++;

}

else

{

arr[k] = R[j];

j++;

}

k++;

}

while (i < n1)

{

arr[k] = L[i];

i++;

k++;

}

while (j < n2)

{

arr[k] = R[j];

j++;

k++;

}

}

}

public class Proj1 {

public static final int RANGE = 100000;

/**

* Randomly fill up the given array with integers from 0 to 100000

*/

public static void fillArray(int[] arr) {

Random randGen = new Random();

for (int i = 0; i < arr.length; i++)

arr[i] = randGen.nextInt(RANGE);

}

/**

* copy the content in second array to first array. We assume two array has same

* size

*

* @param firstArray the destination of copy action

* @param secondArray the source of copy action

*/

public static void copy(int[] firstArray, int[] secondArray) {

for (int i = 0; i < secondArray.length; i++)

firstArray[i] = secondArray[i];

}

/**

* isSorted

*

* @return true if the array is sorted from least to largest or is sorted from

* largest to least

*/

public static boolean isSorted(int[] arr) {

if (arr.length <= 1)

return true;

int index = 0;

while (index < arr.length - 1 && arr[index] == arr[index + 1]) {

index++;

}

// bypass first equal elements

if (index == arr.length - 1)

return true; // all elements are the same

else if (index < arr.length - 1 && arr[index] > arr[index + 1]) // possible descend

{

for (int i = index + 2; i < arr.length; i++)

if (arr[i] > arr[i - 1])

return false;

} else // sort for ascend

{

for (int i = index + 2; i < arr.length; i++)

if (arr[i] < arr[i - 1])

return false;

}

return true;

}

/**

* test the sort with given array. The test array will not be modified

*

* @param arr The array that is used to test the sort. The array will not be

* modified

* @name The name of sort method that is to be tested

*/

public static void testSort(int[] arr, String name) {

long totalTime = 0;

int[] a = new int[arr.length];

copy(a, arr); // copy arr to a

// get start time

long start = Calendar.getInstance().getTimeInMillis();

if (name.equals("Insert Sort"))

MySorts.insertSort(a);

else if (name.equals("Select Sort"))

MySorts.selectSort(a);

else if (name.equals("Quick Sort"))

MySorts.quickSort(a);

else if (name.equals("Merge Sort"))

MySorts.mergeSort(a);

long end = Calendar.getInstance().getTimeInMillis();

if (isSorted(a)) {

totalTime = end - start;

System.out.println("Execution time for " + name + " is: " + totalTime + " milliseconds");

} else

System.out.println("The " + name + " did not work properly");

}

/**

* display the test menu

*/

public static void displayMenu() {

System.out.println("***************************");

System.out.println("* MENU *");

System.out.println("* 1. Fill Array *");

System.out.println("* 2. Select Sort *");

System.out.println("* 3. Insert Sort *");

System.out.println("* 4. Quick Sort *");

System.out.println("* 5. Merge Sort *");

System.out.println("* 6. Quit *");

System.out.println("***************************");

}

public static void main(String[] args) {

int choice;

int[] arr = new int[RANGE];

Scanner input = new Scanner(System.in);

do {

displayMenu();

System.out.println("Enter you choice: ");

choice = input.nextInt();

switch (choice) {

case 1: // generate a new random filled array

fillArray(arr);

break;

case 2: // select sort

testSort(arr, "Select Sort");

break;

case 3: // insert sort

testSort(arr, "Insert Sort");

break;

case 4: // quick sort

testSort(arr, "Quick Sort");

break;

case 5: // merge sort

testSort(arr, "Merge Sort");

break;

case 6: // quit

System.out.println("Make sure that you have good documentation!");

break;

default: // wrong choice

}

} while (choice != 6);

}

}


Related Solutions

In java, I keep getting the error below and I can't figure out what i'm doing...
In java, I keep getting the error below and I can't figure out what i'm doing wrong. Any help would be appreciated. 207: error: not a statement allocationMatrix[i][j];
I keep getting this error, Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for...
I keep getting this error, Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0    at simpleInheritance/simpInher.Dwelling$DriverTest.main(Dwelling.java:146) Can someone help me fix it? import java.io.BufferedReader;    import java.io.FileNotFoundException;    import java.io.FileReader;    import java.util.*;    import java.io.*;    import java.io.FileWriter;    import java.io.IOException;    class Dwelling {    /*    Declaring Variables    */    String streetAddress;    String city;    String state;    String zipCode;    int bedrooms;    double bathrooms;       /*   ...
Syntax error in C. I am not familiar with C at all and I keep getting...
Syntax error in C. I am not familiar with C at all and I keep getting this one error "c error expected identifier or '(' before } token" Please show me where I made the error. The error is said to be on the very last line, so the very last bracket #include #include #include #include   int main(int argc, char*_argv[]) {     int input;     if (argc < 2)     {         input = promptUserInput();     }     else     {         input = (int)strtol(_argv[1],NULL, 10);     }     printResult(input);...
I keep getting minor errors I can't figure out and I don't know how to convert...
I keep getting minor errors I can't figure out and I don't know how to convert decimal .10 to percentage 10% either.   With these functions defined now expand the program for a company who gives discounts on items bought in bulk. Create a main function and inside of it ask the user how many different items they are buying. For each item have the user input a price and quantity, validating them with the functions that you wrote. Use your...
I am getting an error at linen 57 and can't figure out how to fix it....
I am getting an error at linen 57 and can't figure out how to fix it. // Java program to read a CSV file and display the min, max, and average of numbers in it. import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Scanner; public class Main {     // method to determine and return the minimum number from the array     public static int minimum(int numbers[])     {         int minIdx = 0;         for(int i=1;i<numbers.length;i++)         {             if((minIdx...
My code works in eclipse, but not in Zybooks. I keep getting this error. Exception in...
My code works in eclipse, but not in Zybooks. I keep getting this error. Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at java.base/java.util.Scanner.next(Scanner.java:1478) at Main.main(Main.java:34) Your output Welcome to the food festival! Would you like to place an order? Expected output This test case should produce no output in java import java.util.Scanner; public class Main {    public static void display(String menu[])    {        for(int i=0; i<menu.length; i++)        {            System.out.println (i + " - " + menu[i]);...
I keep getting the same error Error Code: 1822. Failed to add the foreign key constraint....
I keep getting the same error Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'test_ibfk_5' in the referenced table 'appointment', can you please tell me what is wrong with my code: -- Table III: Appointment = (site_name [fk7], date, time) -- fk7: site_name -> Site.site_name DROP TABLE IF EXISTS appointment; CREATE TABLE appointment (    appt_site VARCHAR(100) NOT NULL, appt_date DATE NOT NULL, appt_time TIME NOT NULL, PRIMARY KEY (appt_date, appt_time), FOREIGN KEY (appt_site)...
I keep getting an error that I cannot figure out with the below VS2019 windows forms...
I keep getting an error that I cannot figure out with the below VS2019 windows forms .net framework windows forms error CS0029 C# Cannot implicitly convert type 'bool' to 'string' It appears to be this that is causing the issue string id; while (id = sr.ReadLine() != null) using System; using System.Collections.Generic; using System.IO; using System.Windows.Forms; namespace Dropbox13 { public partial class SearchForm : Form { private List allStudent = new List(); public SearchForm() { InitializeComponent(); } private void SearchForm_Load(object...
I am making a html game with phaser 3 I keep getting an error at line...
I am making a html game with phaser 3 I keep getting an error at line 53 of this code (expected ;) I have marked the line that needs to be fixed. whenever I add ; my whole code crashes const { Phaser } = require("./phaser.min"); var game; var gameOptions = {     tileSize: 200,     tileSpacing: 20,     boardSize: {     rows: 4,     cols: 4     }    }    window.onload = function() {     var gameConfig = {         width: gameOptions.boardSize.cols * (gameOptions.tileSize +             gameOptions.tileSpacing) + gameOptions.tileSpacing,...
I keep getting this error "LetterDemo.cs(21,14): error CS1519: Unexpected symbol `string' in class, struct, or interface...
I keep getting this error "LetterDemo.cs(21,14): error CS1519: Unexpected symbol `string' in class, struct, or interface member declaration" Can someone please help me. Here is my code: using static System.Console; class LetterDemo {    static void Main()    {      Letter letter1 = new Letter();      CertifiedLetter letter2 = new CertifiedLetter();      letter1.Name = "Electric Company";      letter1.Date = "02/14/18";      letter2.Name = "Howe and Morris, LLC";      letter2.Date = "04/01/2019";      letter2.TrackingNumber = "i2YD45";      WriteLine(letter1.ToString());      WriteLine(letter2.ToString() +       " Tracking number: " + letter2.TrackingNumber);    } } class Letter {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT