Question

In: Computer Science

I get errors in my merge method and Im not sure how to fix it to...

I get errors in my merge method and Im not sure how to fix it to make the code work

public class OrderedApp1

{

public static void main(String[] args)

{

  

int maxSize = 100; // array size

int searchKey = 55;

OrdArray1 arr, a1, a2, a3; // reference to arrays

arr = new OrdArray1(maxSize); // create the arrays

a1 = new OrdArray1(maxSize);

a2 = new OrdArray1(maxSize);

a3 = new OrdArray1(maxSize);

  

//int a3[] =new int [ a1.length + a2.length];

  

  

a1.insert(3); a1.insert(16); a1.insert(23); a1.insert(44);a1.insert(55);

a2.insert(11); a2.insert(77);a2.insert(100); //insert 10 items

  

arr.insert(77); arr.insert(99); arr.insert(44); arr.insert(55); arr.insert(22);

arr.insert(88); arr.insert(11); arr.insert(00); arr.insert(66); arr.insert(33);

  

  

// search for item

System.out.println("Array a1 is");

a1.display();

  

System.out.println("Array a2 is");

a2.display();

  

System.out.println("Array a3 is");

  

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

a3.merge(a1, a2, a3);

a3.display();

  

if( arr.find(searchKey) != arr.size() )

   System.out.println("Found " + searchKey);

else

   System.out.println("Can't find " + searchKey);

arr.display();

arr.delete(00); arr.delete(55); arr.delete(99); //delete 3 items

System.out.print("After deletion, ");

arr.display();

}

}

public class OrdArray1

{

private long[] a; // ref to array a

   private int nElems; // number of data items

public int length;

//************************************************************************

public OrdArray1(int max) // constructor

{

a = new long[max]; // create array

nElems = 0;

}

//************************************************************************

public int size()

{

return nElems;

}

//************************************************************************

public int find(long searchKey)

{

int lowerBound = 0;

int upperBound = nElems-1;

int curIn;

while(true)

{

   curIn = (lowerBound + upperBound ) / 2;

   if(a[curIn]==searchKey)

return curIn; // found it

   else if(lowerBound > upperBound)

return nElems; // can't find it

   else // divide range

{

if(a[curIn] < searchKey)

   lowerBound = curIn + 1; // it's in upper half

else

   upperBound = curIn - 1; // it's in lower half

}

}

   }

//*************************************************************************

public void insert(long value) // put element into array

{

int j;

for(j=0; j

   if(a[j] > value) // (linear search)

break;

  

for(int k=nElems; k>j; k--) // move bigger ones up

   a[k] = a[k-1];

a[j] = value; // insert it

nElems++; // increment size

}

//*************************************************************************

public boolean delete(long value)

{

int j = find(value);

if(j==nElems) // can't find it

   return false;

else // found it

{

   for(int k=j; k

a[k] = a[k+1];

   nElems--; // decrement size

   return true;

}

}

//**************************************************************************

public void display() // displays array contents

{

for(int j=0; j

   System.out.print(a[j] + " ");

System.out.println(" ");

}

public void merge (int a1, int a2, int a3)

{

int i=0, j=0, k=0;

while ( i

{

if (a1[i] < a2[j])

a3[k+1] = a1[i++];

else

a3[k++] = a2[j++];

}

while (i < a1.length)

a3[k++] = a1[i++];

while (j< a2.length)

a3[k++] =a2[j++];

}

}

Solutions

Expert Solution

// Below code is working fine ,the main fault in your code was ,in merge method you were merging //OrdArray1 class's object,but we want to merge array of OrdArray1 class's object.

/*

i did it in the following way in merge method.

where a1 is array of first argument object passed in function.

where a2 is array of second argument object passed in function.

where a3 is array of third argument object passed in function.

  long a1[]=d.a;
long a2[]=b.a;
long a3[]=c.a;

*/

// save the following code in OrderedApp1.java.

public class OrderedApp1

{

public static void main(String[] args)

{

  

int maxSize = 100; // array size

int searchKey = 55;

OrdArray1 arr, a1, a2, a3; // reference to arrays

arr = new OrdArray1(maxSize); // create the arrays

a1 = new OrdArray1(maxSize);

a2 = new OrdArray1(maxSize);

a3 = new OrdArray1(maxSize);

  

//int a3[] =new int [ a1.length + a2.length];

  

  

a1.insert(3); a1.insert(16); a1.insert(23); a1.insert(44);a1.insert(55);

a2.insert(11); a2.insert(77);a2.insert(100); a2.insert(12); a2.insert(34); //insert 10 items

  

arr.insert(77); arr.insert(99); arr.insert(44); arr.insert(55); arr.insert(22);

arr.insert(88); arr.insert(11); arr.insert(00); arr.insert(66); arr.insert(33);

  

  

// search for item

System.out.println("Array a1 is");

a1.display();

  

System.out.println("Array a2 is");

a2.display();

  

System.out.println("Array a3 is");
a3.display();
  

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

a3.merge(a1, a2, a3);

a3.display();

  

if( arr.find(searchKey) != arr.size() )

System.out.println("Found " + searchKey);

else

System.out.println("Can't find " + searchKey);

arr.display();

arr.delete(00); arr.delete(55); arr.delete(99); //delete 3 items

System.out.print("After deletion, ");

arr.display();

}

}

class OrdArray1

{

private long[] a; // ref to array a
private int nElems; // number of data items
public int length;
//************************************************************************
public OrdArray1(int max) // constructor
{
   a = new long[max]; // create array
   nElems = 0;

}

//************************************************************************

public int size()

{

return nElems;

}

//************************************************************************

public int find(long searchKey)

{

int lowerBound = 0;

int upperBound = nElems-1;

int curIn;

while(true)

{

curIn = (lowerBound + upperBound ) / 2;

if(a[curIn]==searchKey)

return curIn; // found it

else if(lowerBound > upperBound)

return nElems; // can't find it

else // divide range

{

if(a[curIn] < searchKey)

lowerBound = curIn + 1; // it's in upper half

else

upperBound = curIn - 1; // it's in lower half

}

}

}

//*************************************************************************

public void insert(long value) // put element into array

{

int j;

for(j=0; j<nElems;j++)

if(a[j] > value) // (linear search)

break;

  

for(int k=nElems; k>j; k--) // move bigger ones up

a[k] = a[k-1];

a[j] = value; // insert it

nElems++; // increment size

}

//*************************************************************************

public boolean delete(long value)

{

int j = find(value);

if(j==nElems) // can't find it

return false;

else // found it
{
for(int k=j; k<nElems-1;k++)
   a[k]=a[k+1];
nElems--; // decrement size
return true;
}
}

//**************************************************************************

public void display() // displays array contents

{

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

System.out.print(a[j] + " ");

System.out.println(" ");

}

public void merge (OrdArray1 d, OrdArray1 b, OrdArray1 c)

{
long a1[]=d.a;
long a2[]=b.a;
long a3[]=c.a;
int i=0, j=0, k=0;

while ( i<nElems && j<nElems )
{
if (a1[i] < (a2[j]))
   a3[k++] = a1[i++];

else

a3[k++] = a2[j++];

}

while (i < a1.length)

a3[k++] = a1[i++];

while (j< a2.length)

a3[k++] =a2[j++];

}

}


Related Solutions

I have a program to code for my computer science class, and Im not sure how...
I have a program to code for my computer science class, and Im not sure how to do it. If someone can explain it step by step I would appreciate it. Problem Description: Define the GeometricObject2D class that contains the properties color and filled and their appropriate getter and setter methods. This class also contains the dateCreated property and the getDateCreated() and toString() methods. The toString() method returns a string representation of the object. Define the Rectangle2D class that extends...
( In R / R studio ) im not sure how to share my data set,...
( In R / R studio ) im not sure how to share my data set, but below is the title of my data set and the 12 columns of my data set. Please answer as best you can wheather its pseudo code, partial answers, or just a suggestion on how i can in to answer the question. thanks #---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- The dataset incovid_sd_20201001.RDatacontains several variables related to infections of covid-19 for eachzip code in San Diego County as of October...
I am getting 7 errors can someone fix and explain what I did wrong. My code...
I am getting 7 errors can someone fix and explain what I did wrong. My code is at the bottom. Welcome to the DeVry Bank Automated Teller Machine Check balance Make withdrawal Make deposit View account information View statement View bank information Exit          The result of choosing #1 will be the following:           Current balance is: $2439.45     The result of choosing #2 will be the following:           How much would you like to withdraw? $200.50      The...
Im not sure how to work part f. I do not need any of the other...
Im not sure how to work part f. I do not need any of the other answers. They're already completed. Can anyone please help with partt F (1-8)? Applying the Central Limit Theorem: The amount of contaminants that are allowed in food products is determined by the FDA (Food and Drug Administration). Common contaminants in cow milk include feces, blood, hormones, and antibiotics. Suppose you work for the FDA and are told that the current amount of somatic cells (common...
Why my net pay is always 0, and how can I fix it ? here's the...
Why my net pay is always 0, and how can I fix it ? here's the code #include <iostream> #include <fstream> #include <iomanip> #include <cmath> using namespace std; int main() { ofstream out; out.open("myData.txt"); string fname;    cout << "name?" << endl; cin >> fname; double salary; cout << "salary?" << endl; cin >> salary;    double fedTax = salary * 15 / 100; double stateTax = salary* 3.5 / 100; double SST = salary * 5.75 / 100; double...
JAVA: How do I fix the last "if" statement in my code so that outputs the...
JAVA: How do I fix the last "if" statement in my code so that outputs the SECOND HIGHEST/MAXIMUM GPA out of the given classes? public class app { private static Object minStudent; private static Object maxStudent; public static void main(String args[ ]) { student st1 = new student("Rebecca", "Collins", 22, 3.3); student st2 = new student("Alex", "White", 19, 2.8); student st3 = new student("Jordan", "Anderson", 22, 3.1); student[] studentArray; studentArray = new student[3]; studentArray[0] = st1; studentArray[1] = st2; studentArray[2]...
How do I fix the "error: bad operand types for binary operator '*' " in my...
How do I fix the "error: bad operand types for binary operator '*' " in my code? What I am trying to do: double TotalPrice = TicketPrice * NoOfTickets;       My code: import javax.swing.*; /*provides interfaces and classes for different events by AWT components*/ import java.awt.event.*; import javax.swing.JOptionPane; //TicketReservation.java class TicketReservation { public static void main(String args[]) { /*Declare JFrame for place controls.*/ JFrame f= new JFrame("Movie Ticket Reservation");                                   /*Declare JLabels*/ JLabel...
I am not quite sure how to get this program working or how to start it...
I am not quite sure how to get this program working or how to start it in the first place. Write a full Java program and do the following: 1- Create a generic class and declare a one dim array as a private member. 2- Add a constructor to initialize the array. 3- A set method to set the array. 4- Sort method to sort the array. 5- Print method to print the array. 6- Reverse method to reverse the...
How would I fix my coding to allow the user to see what they have entered...
How would I fix my coding to allow the user to see what they have entered after they submit it? Where would I plug it into my code to see it? <!Doctype html> <html> <head> <meta charset="UTF-8"> <title>Login and Registeration Form Design</title> <link rel="stylesheet" type="text/css" href="../signintrial.css"> <script> function loginHandler(){ // code for handing login event } function registerHandler() {    //checking phone number    if(phonenumber(document.getElementById('phone').value))    {    //when phone number is valid    document.getElementById('demo').innerHTML = document.getElementById('fname').value + " "...
I used my catchement area to calculate my runoff using the national method. how will i...
I used my catchement area to calculate my runoff using the national method. how will i go on calculating the volume needed for storage? which equations will i use ? i want to have a storage tank that is pumped to the wetland. how will i distribute the volume and calculate the sizing for each. Can you explain with an example if you can?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT