Question

This lab serves as an intro to Java Interfaces and an Object-Oriented design strategy towards implementing...

This lab serves as an intro to Java Interfaces and an Object-Oriented design strategy towards implementing data structures. We will be using Entry.java objects which are key,value pairs, with key being an integer and value being a generic parameter. We will be sorting these Entries according to their key value. The file Entry.java is given to you and does not require any modification.

As you should know by now, bucket sort works by placing items into buckets and each bucket is sorted on its own. Once all items are placed, the bucket contents are extracted in a sequence from smallest bucket to largest. In this lab, we will have a SortedBucket object which takes an item and stores it in sorted order. We will also be using a BucketList object which creates a list of SortedBucket objects, and decides which bucket each number (Entry) goes to.

Start with looking at the two Abstract Data Types:

SortedBucketADT.java: an interface that describes what functionality a bucket has. Aside from a constructor and a toString() method, these can .add(Entry) and will place the Entry in sorted order within the bucket, and they also have a getBucketContents() method which returns the contents of the bucket.

BucketListADT.java: an interface that describes the collection of buckets. The constructor takes 3 parameters: int min, int max, int n, which represent the expected minimum of Entry keys, expected maximum of Entry keys, and the number of buckets to create. It supports methods .add(Entry) and .addAll(Entries) to add one Entry or to add many Entries. The .add( ) method should decide which bucket the Entry gets placed in. It also has a way to extract the sorted order of Entries with a .getSortedOrder() method.

You do not need to modify these ADT files. Your task will be in implementing and testing them. Namely, SortedBucket.java will implement SortedBucketADT.java and BucketList.java will implement BucketListADT.java. These are partially implemented already, and contain //TODO items throughout the file to guide you in the implementation process.

Further, there is a MainClass.java present only to show you some sample intended usage of the data structure and sorting method. You can modify or delete this file as you please. Finally, there is a Test.java class file which illustrates how one can test the contents of each bucket after adding elements to the bucket list. Once again, there are //TODO items throughout this test file with more specific instructions.

Scoring: Up to 6 points for completing the implementation and up to 4 points for the additional testing you are to include in the test file.

SUBMIT: your edited .java files and a screenshot of your coverage report that shows that your tests provide at least 80% coverage of the project files. You may alternatively show your lab instructor (in lab time) your coverage report and your test cases

0 0
Add a comment Improve this question Transcribed image text
Answer #1


As per the problem statement I have solve the problem. Please let me know if you have any doubts or you want me to modify the answer. And if you find this answer useful then don't forget to rate my answer as thumps up. Thank you! :)

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.ArrayList;

public class MainClass {

    public static void main(String[] args) {

        BucketList<String> bucketList = new BucketList<String>(0,100,6);
        bucketList.add(new Entry<String>(-10 , "Bob"));
        System.out.println(bucketList);
        bucketList.add(new Entry<String>(90 , "John"));
        System.out.println(bucketList);
        bucketList.add(new Entry<String>(83 , "Vennisa"));
        System.out.println(bucketList);
        bucketList.add(new Entry<String>(78 , "Catherine"));
        System.out.println(bucketList);
        bucketList.add(new Entry<String>(62 , "Barley"));
        System.out.println(bucketList);
        bucketList.add(new Entry<String>(74 , "Angston"));
        System.out.println(bucketList);
        bucketList.add(new Entry<String>(88 , "Andrew"));
        System.out.println(bucketList);
        bucketList.add(new Entry<String>(104 , "Cathy"));
        System.out.println(bucketList);

        // add N more random students
        int N = 10;
        ArrayList<Entry<String>> allEmployee = new ArrayList<Entry<String>>();
        for (int i=1; i<=N; i++) {
            Entry<String> e = new Entry<String>((int)(45+Math.random()*50),"emp"+i);
            allEmployee.add(e);
        }

        // check add all methos
        bucketList.addAll(allEmployee);

        // method to display all buckets and their contents
        System.out.println(bucketList);

        // method to display the final sorted order
        System.out.println(bucketList.getSortedOrder());
    }

}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.Collection;

public class BucketList<V> implements BucketListADT<V> {

    private ArrayList<SortedBucket<V>> sortedBucketList;
    private int minimum;
    private int maximum;

    //constructor
    public BucketList(int minimum, int maximum, int number) {
        sortedBucketList = new ArrayList<>(number);
        for (int i=0; i<number; i++){
            sortedBucketList.add(new SortedBucket<>());
        }
        this.minimum = minimum;
        this.maximum = maximum;
    }

    //method to add the given Entry into the bucket

    @Override
    public void add(Entry<V> item) {
        int bucketSize = sortedBucketList.size();
        int indexToInsert = bucketSize*(item.getKey()- minimum)/(maximum - minimum +1);

        indexToInsert = Math.min(indexToInsert, this.getNumberOfBuckets() - 1);
        indexToInsert = Math.max(indexToInsert, 0);

        sortedBucketList.get(indexToInsert).add(item);

    }

    //method to add all entries in the Collection

    @Override
    public void addAll(Collection<Entry<V>> bucketEntryCollection) {
        for (Entry<V> entry : bucketEntryCollection)
            this.add(entry);
    }

    //method returns a single ArrayList of sorted order
    @Override
    public ArrayList<Entry<V>> getSortedOrder() {
        ArrayList<Entry<V>> output = new ArrayList<>();
        this.sortedBucketList.forEach(t ->
                output.addAll(t.getBucketContents()));

        return output;
    }

    //method returns the content of the bucket
    @Override
    public ArrayList<Entry<V>> getBucket(int i) {

        return this.sortedBucketList.get(i).getBucketContents();
    }

    //method return the number of buckets in this list
    @Override
    public int getNumberOfBuckets(){
        return sortedBucketList.size();
    }


    //method to show content of the bucket
    public String toString() {
        // Leave this alone
        StringBuilder output = new StringBuilder("[");
        for (SortedBucket<V> s: this.sortedBucketList) {
            output.append(s.toString());
        }
        output.append("]");
        return output.toString();

    }
}


-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.Collection;


public interface BucketListADT<V> {

    void add(Entry<V> item);

    void addAll(Collection<Entry<V>> c);

    ArrayList<Entry<V>> getBucket(int i);

    public int getNumberOfBuckets();

    ArrayList<Entry<V>> getSortedOrder();

}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Entry <V> {
    private int key;
    private V value;

    public Entry(int k, V v) {
        key = k;
        value = v;
    }

    public int getKey() {
        return this.key;
    }

    public V getValue() {
        return this.value;
    }

    public String toString() {
        return this.getValue()+"="+this.getKey();
    }
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.ArrayList;


public class SortedBucket<V> implements SortedBucketADT<V>{

    private ArrayList<Entry<V>> sortedBucket;

    //constructor
    public SortedBucket() {
        sortedBucket = new ArrayList<Entry<V>>();
    }

    //method adds entry to bucket

    @Override
    public void add(Entry<V> t) {

        boolean added = false;

        for (int i = 0; i < this.sortedBucket.size(); i++) {
            if (sortedBucket.get(i).getKey() > t.getKey()) {
                sortedBucket.add(i, t);
                added = true;
                break;
            }
        }
        if(!added) {
            sortedBucket.add(t);
        }
    }


    //method to return a sorted ArrayList of Entries in this bucket

    @Override
    public ArrayList<Entry<V>> getBucketContents() {
        return sortedBucket;
    }

    @Override
    public String toString() {
        return sortedBucket.toString();
    }
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.ArrayList;


public interface SortedBucketADT<V>{

    void add(Entry<V> t);

    ArrayList<Entry<V>> getBucketContents();


}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;

import static org.junit.Assert.assertTrue;

public class TestSortedBucket {

    //Declaration of the test objects

    BucketList<String> bucketList;
    ArrayList<Entry<String>> everyone;
    Entry<String> jessica, veronica, cathy, barley, angston, angelina;

    // Instantiation of the objects */

    @Before
    public void initialize() {
        everyone = new ArrayList<>();
        jessica = new Entry<>(94 , "Jessica");
        veronica = new Entry<>(93 , "veronica");
        cathy = new Entry<>(68 , "cathy");
        barley = new Entry<>(72 , "Barley");
        angston = new Entry<>(84 , "angston");
        angelina = new Entry<>(98 , "angelina");

        everyone.add(jessica);
        everyone.add(veronica);
        everyone.add(cathy);
        everyone.add(barley);
        everyone.add(angston);
        everyone.add(angelina);

    }


    @Test
    public void test1() {
        // BucketList constructor takes (min,max,numBuckets)
        bucketList = new BucketList<String>(0,100,6);
        bucketList.addAll(everyone);
        ArrayList<Entry<String>> list4 = new ArrayList<>();
        list4.add(cathy);
        list4.add(barley);
        list4.add(angston);
        ArrayList<Entry<String>> list5 = new ArrayList<>();
        list5.add(veronica);
        list5.add(jessica);
        list5.add(angelina);
        assertTrue(bucketList.getBucket(4).equals(list4)
                && bucketList.getBucket(5).equals(list5)
                && bucketList.getBucket(0).isEmpty()
                && bucketList.getBucket(1).isEmpty()
                && bucketList.getBucket(2).isEmpty()
                && bucketList.getBucket(3).isEmpty());
    }

    @Test
    public void test2() {
        // BucketList constructor takes (min,max,numBuckets)
        bucketList = new BucketList<>(0,100,5);
        bucketList.addAll(everyone);

        ArrayList<Entry<String>> list3 = new ArrayList<>();

        list3.add(cathy);
        list3.add(barley);

        ArrayList<Entry<String>> list4 = new ArrayList<>();
        list4.add(angston);
        list4.add(veronica);
        list4.add(jessica);
        list4.add(angelina);

        assertTrue(bucketList.getBucket(0).isEmpty()
                && bucketList.getBucket(1).isEmpty()
                && bucketList.getBucket(2).isEmpty()
                && bucketList.getBucket(3).equals(list3)
                && bucketList.getBucket(4).equals(list4)
        );
    }

    @Test
    public void test3() {
        // BucketList constructor takes (min,max,numBuckets)
        bucketList = new BucketList<>(0,100,4);
        bucketList.addAll(everyone);


        ArrayList<Entry<String>> list2 = new ArrayList<>();

        list2.add(cathy);
        list2.add(barley);

        ArrayList<Entry<String>> list3 = new ArrayList<>();
        list3.add(angston);
        list3.add(veronica);
        list3.add(jessica);
        list3.add(angelina);
        assertTrue(bucketList.getBucket(0).isEmpty()
                && bucketList.getBucket(1).isEmpty()
                && bucketList.getBucket(2).equals(list2)
                && bucketList.getBucket(3).equals(list3)
        );
    }

    @Test
    public void emptyTest() {
        bucketList = new BucketList<String>(-50,50,6);
        assertTrue(bucketList.getNumberOfBuckets()==6 && bucketList.getSortedOrder().size()==0);
    }

    @Test
    public void perfectEmployeeTest() {

        bucketList = new BucketList<>(0,100,10);
        bucketList.add(new Entry<>(100,"perfect"));
        assertTrue(bucketList.getBucket(9).size()==1);
    }

    @Test
    public void outOfBoundsTest1() {
        bucketList = new BucketList<>(0,100,10);
        bucketList.add(new Entry<>(110,"superPerfect"));
        assertTrue(bucketList.getBucket(9).size()==1);
    }

    @Test
    public void outOfBoundsTest2() {
        bucketList = new BucketList<String>(0,100,10);
        bucketList.add(new Entry<>(-110,"notSoPerfect"));
        assertTrue(bucketList.getBucket(0).size()==1);
    }

    //tests that the list is in sorted order
    @Test
    public void sortedBucketTest() {
        bucketList = new BucketList<>(0,100,10);
        bucketList.addAll(everyone);

        Entry<String> bob = new Entry<>(108, "Bob");
        Entry<String> john = new Entry<>(-18, "John");

        bucketList.add(bob);
        bucketList.add(john);

        ArrayList<Entry<String>> sorted = new ArrayList<>();

        sorted.add(john);
        sorted.add(cathy);
        sorted.add(barley);
        sorted.add(angston);
        sorted.add(veronica);
        sorted.add(jessica);
        sorted.add(angelina);
        sorted.add(bob);

        assertTrue(bucketList.getSortedOrder().equals(sorted));

    }

}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Program Output :
BucketList - MainClass.java BucketList src MainClass main Main 1 BucketListADT.java X SortedBucket.java 1 SortedBucketADT.jav

Add a comment
Know the answer?
Add Answer to:
This lab serves as an intro to Java Interfaces and an Object-Oriented design strategy towards implementing...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please answer using java. For this lab you will practice accessing variables in an array and...

    Please answer using java. For this lab you will practice accessing variables in an array and determining if the values are odd. You will finish the method makeOddArray. You will store only the odd values in another array and return that array. Hint: you will need to keep track of how many items you have currently put in the new array and increment that value as you add. This will keep track of your index for the new array. Below...

  • cis 112 (java reqire to use JGRASP) Searching and Sorting Assignment This is a non-object oriented...

    cis 112 (java reqire to use JGRASP) Searching and Sorting Assignment This is a non-object oriented assignment. This assignment is due at the start of class on July 16, Create a program that populates and array with 100 random integers between 0 and 99. Then print out the array. Then prompt the user for a number, and do a sequential search on the unsorted array and return whether or not the number was in the array. Then sort the array...

  • 7.10 LAB: Sorting TV Shows (dictionaries and lists)

    Write a program that first reads in the name of an input file and then reads the input file using the file.readlines() method. The input file contains an unsorted list of number of seasons followed by the corresponding TV show. Your program should put the contents of the input file into a dictionary where the number of seasons are the keys, and a list of TV shows are the values (since multiple shows could have the same number of seasons).Sort...

  • The purpose of this project is to give students more exposure to object oriented design and...

    The purpose of this project is to give students more exposure to object oriented design and programming using classes and polymorphism in a realistic application that involves arrays of objects and sorting arrays containing objects A large veterinarian services many pets and their owners. As new pets are added to the population of pets being serviced, their information is entered into a flat text file. Each month the vet requests and updates listing of all pets sorted by their "outstanding...

  • This needs to be done in Java. Please use basic Java coding at an intro level....

    This needs to be done in Java. Please use basic Java coding at an intro level. THANK YOU FOR YOUR HELP! I WILL LEAVE A GOOD RATING! Design and implement MoneyTest and Money. Use the test-first approach, as we discussed, and document each method before implementing. Money - dollars : int - cents : int + Money (int, int) + getDollars() + getCents() + add (Money) : Money + subtract (Money) : Money + toString() : String + equals (Money)...

  • Java Lab In this lab, you will be implementing the following class hierarchy. Your concrete classes...

    Java Lab In this lab, you will be implementing the following class hierarchy. Your concrete classes must call the superclass constructor with the proper number of sides (which is constant for each concrete shape). The perimeter method is implemented in the superclass as it does not change based on the number of sides. The area method must be overridden in each subclass as the area is dependent on the type of polygon. The areas of an equilateral triangle, square, and...

  • Please use Java only: Background: Java contains several different types of interfaces used to organize collections...

    Please use Java only: Background: Java contains several different types of interfaces used to organize collections of items. You may already be familiar with the List interface, which is implemented by classes such as the ArrayList. A List represents a collection of elements from which elements can be stored or retreived, in which elements are ordered and can be retrieved by index. A List extends from a Collection, which represents a collection of elements but which may or may not...

  • In this lab you will work with abstract classes/interfaces. (Java Program) You will be implementing a...

    In this lab you will work with abstract classes/interfaces. (Java Program) You will be implementing a basic employee schema within a company. The Employee class will be an abstract class that will contain methods applicable to all employees. You will then create 2 classes called SoftwareEngineer and ProductManager. Both are different employee types based on occupation. You will create an interface called Developer which will consist of specific methods that apply to only Developers (e.g. SoftwareEngineer class will implement this,...

  • Please answer this problem in C++, Thank you! Read and study the sample program: "hashing with chaining using singly...

    Please answer this problem in C++, Thank you! Read and study the sample program: "hashing with chaining using singly linked lists", as well as "hashing with chaining using doubly linked lists". Notice this program is complete except it doesn't include the remove function. Write the remove function and test it with the rest of the program including the given main program. Upload your C++ source file. ---Here is the referenced program: "hashing with chaining using singly linked lists". Below this...

  • This should be in Java Create a simple hash table You should use an array for...

    This should be in Java Create a simple hash table You should use an array for the hash table, start with a size of 5 and when you get to 80% capacity double the size of your array each time. You should create a class to hold the data, which will be a key, value pair You should use an integer for you key, and a String for your value. For this lab assignment, we will keep it simple Use...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT