Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You will fill in the code for method insertInOrder and the upSize() method at th

ID: 3881023 • Letter: Y

Question

You will fill in the code for method insertInOrder and the upSize() method at the bottom of your starter file. Main is written for you. Every time you read another number from the input file, you will pass that new value into insertInOrder. It will place the new number into the array in it's sorted place. To figure out where the new number belongs you must compare it to the last number in the array. If the new value is smaller than that element, the end element must move up one to the right. You then keep moving toward the front of the array one step at a time comparing the new value to the next element. If the new value is smaller than it, you must push that element up one place. You keep doing this until the new value is not smaller than the one you are comparing it to. At this point you can now copy the new value into the hole you just opened up in the array.

At no time is the array allowed to be in un-sorted order. You are not allowed to copy the new value into the array except the one time when you copy it into its proper sorted place. You are not allowed to create another array. You are not allowed to just fill up the array and then call Arrays.sort() or write some sorting loop on it after it's full. The array must be in sorted order at all times.

import java.util.*;

import java.io.*;

public class Lab3

{

static final int INITIAL_CAPACITY = 5;

public static void main( String args[] ) throws Exception

{

if (args.length < 1 )

{

System.out.println(" usage: C:\> java Lab3 L3input.txt ");

System.exit(0);

}

Scanner infile = new Scanner( new File( args[0] ) );

int[] arr = new int[INITIAL_CAPACITY];

int count= 0;

while (infile.hasNextInt())

{

if ( count == arr.length ) arr = upSize( arr );

insertInOrder( arr, count, infile.nextInt() );

++count;

}

infile.close();

printArray( "SORTED ARRAY: ", arr, count );

}

static void printArray( String caption, int[] arr, int count )

{

System.out.print( caption );

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

System.out.print(arr[i] + " " );

System.out.println();

}

static void insertInOrder( int[] arr, int count, int key )

{

//YOUR CODE HERE

}

static int[] upSize( int[] fullArr )

{

// YOUR CODE HERE

return null; // CHANGE TO YOUR RETURN STATEMENT

}

}

Explanation / Answer

The complete code is given here

import java.io.File;

import java.util.Scanner;

public class Main

{

static final int INITIAL_CAPACITY = 5;

public static void main( String args[] ) throws Exception

{

if (args.length < 1 )

{

System.out.println(" usage: C:\> java Lab3 L3input.txt ");

System.exit(0);

}

Scanner infile = new Scanner( new File( args[0] ) );

int[] arr = new int[INITIAL_CAPACITY];

int count= 0;

while (infile.hasNextInt())

{

if ( count == arr.length ) arr = upSize( arr );

insertInOrder( arr, count, infile.nextInt() );

++count;

}

infile.close();

printArray( "SORTED ARRAY: ", arr, count );

}

static void printArray( String caption, int[] arr, int count )

{

System.out.print( caption );

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

System.out.print(arr[i] + " " );

System.out.println();

}

static void insertInOrder( int[] arr, int count, int key)

{

if(count==0)

arr[count]=key;

else

{

int i;

for (i=0;i<count;i++)

{

if(arr[i]>key)

{

for(int j=count-1;j>=i;j--)

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

arr[i]=key;

break;

}

}

arr[i]=key;

}

}

static int[] upSize( int[] fullArr )

{

int l=fullArr.length;

int[] arr=new int[l+1];

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

arr[i]=fullArr[i];

return arr;

// CHANGE TO YOUR RETURN STATEMENT

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote