Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a complete Java program in a file called FilterSort.java that reads all the tokens from a file named data.txt that is to be found

Write a complete Java program in a file called FilterSort.java that reads all the tokens from a file named data.txt that is to be found in the same directory as the running program. The program should ignore all tokens that cannot be read as an integer and read only the ones that can. After reading all of the integers, the program should print all the integers back to the screen, one per line, from the smallest to the largest. For example if data.txt contains

10 5five 10 1.5 2 2.0 20

Then your program should print to the screen

2

10

10

20

Your program should not prompt the user for anything and should print nothing except the sorted list of integers (if there are no integers your program should print nothing). If the file does not exist, then your program should instead print exactly "File not found" and exit.

Your program should start by allocating an array of size 8 and fill it with the first 8 tokens. On the ninth token, it should replace the array with one of size 16 and copy all the integers already read. On the 17th token, it should replace the array with one of size 32 and copy all the integers already read. Etc. Since this doubling the size of an array is a repeated operation that is easily factored into a coherent chunk, you should write a method that performs this task. Also, be sure you read the textbook about the Arrays class and the java documentation about the static method Arrays.sort(a, fromIndex, toIndex). You should not write your own sorting algorithm.

A little starter code:

/**

* Program that reads tokens from file data.txt and prints the integer

* tokens found in it to the screen in increasing order.

*

* @author Your Name (your chosen 4-digit ID)

* @version date of submission

*/

public class FilterSort {

/**

* Allocates and returns an integer array twice the size of the one

* supplied as a parameter. The first half of the new array will

* be a copy of the supplied array and the second half of the new

* array will be zeros.

*

* @param arr the array to be copied

* @return array twice the size of arr with its initial

* elements copied from arr

* @throws NullPointerException if arr is null.

*/

public static int[] doubleArrayAndCopy(int[] arr) {

}

public static void main(String[] args) {

int[] data = new int[8];

...

data = doubleArrayAndCopy(data);

...

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Moving Objects Databases

Authors: Ralf Hartmut Güting, Markus Schneider

1st Edition

0120887991, 978-0120887996

More Books

Students also viewed these Databases questions

Question

Witten implementation plan for software application Summarized plan

Answered: 1 week ago