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...
Write a java program StudentDriver class to test the methods of the Student class. Use data:...
Write a java program StudentDriver class to test the methods of the Student class. Use data: Mary Brown 1234 John Jones 5678 Maty Jones 1234 Note: we are including Mary Jones twice so we can test the "equals" method. We can test "compareTo", by test student1 and student2, then student2 and student3. Just submit your driver class as text file. Make sure you also test addQuiz, getTotalScore, getAverage, and toString. Don't worry about testing all of the sets and gets......
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...
How to write a Java program that has a base class with methods and attributes, subclasses...
How to write a Java program that has a base class with methods and attributes, subclasses with methods and attributes. Please use any example you'd like.
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)...
PROGRAM SIMULATION. Understand the given JAVA program and write the output. 1.     public class Places        {...
PROGRAM SIMULATION. Understand the given JAVA program and write the output. 1.     public class Places        {            public static void main(String args[])            {                   String place[]=new String[4];           place[0]="Salmaniya"; place[1]="Salmabad"; place[2]="Isa Town"; place[3] = “Manama”         System.out.println(place[3]);         System.out.println(place[1].toLowerCase());         System.out.println(place[2].substring(4,6);         System.out.println(place[3].charAt(4));         System.out.println(place[1].equals(place[2]));            } }    b. public class ChangeIt { public void doIt( int[] z ) { z[0] = 0; } } public class TestIt { public static void main ( String[] args ) {...
write JAVA program have a public class named GeometricShapes that has the main() method. In the...
write JAVA program have a public class named GeometricShapes that has the main() method. In the main() method the user needs to select if he want 2D shapes or 3D shape -if user select 2D user needs to select the shape type (Square, Circle, or Triangle) after selected shape the user needs to specify whether to find the Area or the Perimeter or to find side-length (radius for the circles), accordingly the needed constructor is used. (using Polymorphism principle) -if...
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...
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 }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT