Question
This exercise requires that you write a Java program that uses classes in the Java Class Library to create binary trees and then use those
This exercise requires that you write a Java program that uses classes in the Java Class Library to create binary trees and then use those trees to sort a collection of Integers read from a file.
You are to write a Java program that has at least two Java classes. The main class should contain the main function and be the driver of your program. The second need only implement the Comparator interface. This will require a single method called compare. It may be an internal class within your main class or it may be separate.
Your program should declare two TreeSet objects from the Java Class Library TreeSet class. You may think of your TreeSet objects as being binary trees, which as you know can be used to sort the elements of the tree. The TreeSet constructor is overloaded; there are four versions. You are interested in two of those four. The first takes a single argument, a collection of elements to be sorted in their natural order. (An ArrayList
The second TreeSet object should create an empty object that will sort any collection loaded into it in reverse sequence. The objects constructor should have a single argument, a new object of your class that implements the Comparator interface. This Comparator class should contain a method named compare and should sort the loaded collection in reverse sequence.
Your program should input a data file which contains integers. You should not modify the file itself. You will be able to view and become familiar with the file, but you should still write a general program. You should not write your program to work with this file, specifically. Your program should work properly for files containing any number of integers. Perhaps you should consider loading the integers you read into an Arraylist as you read them. Remember that a Java Set, a TreeSet is an example, will not accept duplicates. If the input file contains multiple instances of the same Integer, only one instance will be loaded into the TreeSet.
You should input the Integers from the file and insert them appropriately into your two TreeSets.
System.out.println will accept a collection as an argument and output a comma-separated list enclosed in square brackets. The elements of a TreeSet are a collection. You should use this feature to print a clearly labeled, formatted report of your results. Your report should contain the collection of Integers read from the file, number of Integers read from the file, the collection in natural order of the Integers loaded into the first TreeSet, and the collection on Integers in reverse order from the second TreeSet.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started