Question

In: Computer Science

write program that develop a Java class Dictionary to support the following public methods of an...

write program that develop a Java class Dictionary to support the following public methods of an abstract data type:

public class Dictionary {

// insert a (key, value) pair into the Dictionary, key value must be unique, the value is associated with the key; only the last value inserted for a key will be kept public void insert(String key, String value);

// return the value associated with the key value public String lookup(String key);

// delete the (key, value) pair public void delete (String key);

}

You will declare a class Pair to represent the (key, value) pair, and use an array of the Pair objects to keep the values in the dictionary. Your array of Pair objects should be able to be sorted by java.util.Arrays.sort(). You can decide whether to store the Pair values in the array sorted or not according to the key values.

Write the main() method to create an instance of the Dictionary object, insert 50 random (key, value) pairs, update some values behind the same key, and delete some (key, value) pairs.

2. Rewrite Your class Dictionary so it can support values of any data type, and show that it works with different value data types.

3. Discuss advantages and disadvantages for storing the Pair objects sorted or not in the Dictionary class.

4. What is running time for each method of your Dictionary class in terms of Big-O notation?

5. Trace method heapSort(T[] w) with sequence of diagrams with both heap tree view and 1-D array view how to sort integers 8, 4, 1, 2, 5, 3, 10, 7, 5, 9, 1, 12, and 5.

Solutions

Expert Solution

import java.util.ArrayList;

public class Dictionary {
    ArrayList<Pair> dict = new ArrayList<>();

    public String key,value;

    // ArrayList<String> keysInt = new ArrayList<String>();
    // ArrayList<String> valuesInt = new ArrayList<String>();

    void insert(String key, String value){
        this.dict.add(new Pair(key, value));

        this.key = key;
        this.value = value;
    }
    void insert(int key, int value){
        this.dict.add(new Pair(key, value));
    }
  
    void insert(double key, double value){
        this.dict.add(new Pair(key, value));
    }

    public String lookup(String key){
        for( int i =0; i < this.dict.size(); i++){
            if (dict.get(i).keyString.equals(key))
                return dict.get(i).valueString;
        }
        return "Key Pair Not Found!!";
    }
    public int lookup(int key){
        for (int i =0; i < this.dict.size(); i++){
            if(dict.get(i).keyInt == key){
                return dict.get(i).valueInt;
            }
        }
        return -1;
    }

    public double lookup(double key){
        for (int i =0; i < this.dict.size(); i++){
            if(dict.get(i).keyDouble == key){
                return dict.get(i).valueDouble;
            }
        }
        return -1;
    }
    public void detele(String key){
        int counter = 0;
        for (int i = 0; i < this.dict.size(); i++){
            if (this.dict.get(i).keyString.equals(key)){
                this.dict.remove(i);
                counter++;
            }
        }

        if (counter == 0) System.out.println("Key Pair not found!!");
    }
}

import java.util.Comparator;

public class Pair {
    String keyString;
    String valueString;

    int keyInt;
    int valueInt;

    double keyDouble, valueDouble;

    public Pair(String key, String value){
        this.keyString = key;
        this.valueString = value;
    }

    public Pair(int key, int value){
        this.keyInt = key;
        this.valueInt = value;
    }

    public Pair(double key, double value){
        this.keyDouble = key;
        this.valueDouble = value;
    }


    public double getkeyDouble(){
        return this.keyDouble;
    }

    public double getValueDouble(){
        return this.valueDouble;
    }

    public void setKeyDouble(double key){
        this.keyDouble = key;
    }
    public void setValueDouble(double value){
        this.valueDouble = value;
    }
    public String getKey(){
        return this.keyString;
    }
    public String getValue(){
        return this.valueString;
    }

    public void setKey(String key) {
        this.keyString = key;
    }

    public void setValue(String value) {
        this.valueString = value;
    }

    public int getKeyInt(){
        return this.keyInt;
    }
    public int getValueInt(){
        return this.valueInt;
    }

    public void setKeyInt(int keyInt){
        this.keyInt = keyInt;
    }

    public void setValueInt(int valueInt){
        this.valueInt = valueInt;
    }

    public double compareToDouble(Pair pair){
        double key;
        key = ((Pair)pair).getkeyDouble();
        return this.keyDouble - key;
    }

    public int compareToInt(Pair pair){
        int key;
        key = ((Pair)pair).getKeyInt();
        return this.keyInt - key;
    }
    public static Comparator<Pair> keyComp = new Comparator<Pair>() {
        @Override
        public int compare(Pair o1, Pair o2) {

            String key1 = o1.keyString.toUpperCase();
            String key2 = o2.keyString.toUpperCase();

            return key1.compareTo(key2);
        }
    };


}

import java.util.Random;
public class Main {

    public static void main(){
        Dictionary dict = new Dictionary();
        for (int i = 0; i < 50; i++){
            Random rand = new Random();
            String key = " " + rand.nextInt() ;
            String value = " " + rand.nextInt();
            dict.insert(key, value);
        }

    }
}


Related Solutions

write the program in java. Develop a class RentCabin that does the following: (use JavaDoc comments)...
write the program in java. Develop a class RentCabin that does the following: (use JavaDoc comments) // declare the constructor that sets the type and rate based in the sqft parm        // set values based on sqft <1000 is small with $100 per night, // sqft between 1000 and 2000, mid-sized $200 per night, and // over 2000 as a large cabin with $300 per night        //declare getRate        //declare getType        //declare setRate with int rate parm...
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** *...
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** * Calculates and returns the reverse complement of a DNA sequence. In DNA sequences, 'A' and 'T' * are complements of each other, as are 'C' and 'G'. The reverse complement is formed by * reversing the symbols of a sequence, then taking the complement of each symbol (e.g., the * reverse complement of "GTCA" is "TGAC"). * * @param dna a char array representing...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static void main(String[] args) { String bottle1_label="Milk"; float bottle1_volume=250; float bottle1_capacity=500; bottle1_volume=addVolume(bottle1_label, bottle1_volume,bottle1_capacity,200); System.out.println("bottle label: " + bottle1_label + ", volume: " + bottle1_volume + ", capacity: " +bottle1_capacity); String bottle2_label="Water"; float bottle2_volume=100; float bottle2_capacity=250; bottle2_volume=addVolume(bottle2_label, bottle2_volume,bottle2_capacity,500); System.out.println("bottle label: " + bottle2_label + ", volume: " + bottle2_volume + ", capacity: " +bottle2_capacity); } public static float addVolume(String label, float bottleVolume, float capacity, float addVolume)...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) {...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) { //1. Create a double array that can hold 10 values    //2. Invoke the outputArray method, the double array is the actual argument. //4. Initialize all array elements using random floating point numbers between 1.0 and 5.0, inclusive    //5. Invoke the outputArray method to display the contents of the array    //6. Set last element of the array with the value 5.5, use...
Please write a java program that has the following methods in it: (preferably in order)   a...
Please write a java program that has the following methods in it: (preferably in order)   a method to read in the name of a University and pass it back a method to read in the number of students enrolled and pass it back a method to calculate the tuition as 20000 times the number of students and pass it back a method print the name of the University, the number of students enrolled, and the total tuition Design Notes: The...
This program is written in Java and should be modularized in methods in one class. This...
This program is written in Java and should be modularized in methods in one class. This program will calculate the Gross Earnings, FICA tax (Medicare and Social Security taxes), Federal Tax Withheld, and Net Amount of the payroll check for each employee of a company. The output must contain numbers with 2 decimal places. The user input must be validated – if incorrect data is entered, send an error message to the user. INPUT The application must be able to...
Write a program in java that does the following: Create a StudentRecord class that keeps the...
Write a program in java that does the following: Create a StudentRecord class that keeps the following information for a student: first name (String), last name (String), and balance (integer). Provide proper constructor, setter and getter methods. Read the student information (one student per line) from the input file “csc272input.txt”. The information in the file is listed below. You can use it to generate the input file yourself, or use the original input file that is available alone with this...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and then increments the number of seconds. The counter will start at 00:00 and each time the number of seconds reaches 60, minutes will be incremented. You will not need to implement hours or a sleep function, but if minutes or seconds is less than 10, make sure a leading zero is put to the left of the number. For example, 60 seconds:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT