2 Replies - 201 Views - Last Post: 05 December 2017 - 08:38 AM Rate Topic: -----

#1 banbaluta24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 23-September 13

Data Structure using OOP

Posted 04 December 2017 - 06:56 PM

I have this data structure project and here is the classes I implemented and the .csv file
My problem is when I run the program the Hash Table content supposed to display from zero (0) to 100 but it Starts from 1 - 101
I"m trying to fix it my keep displaying the same content from 1 - 101
/**
 * 
 * @author Samici
 *
 */
import java.text.*;
public class HashTable {
	private int freeSpaces;

    private final LinkedList[] LINKED_LIST;

    private static final NumberFormat FORMATTER = NumberFormat.getInstance();

    public HashTable() {
        LINKED_LIST = new LinkedList[freeSpaces = 101];
    }

    public void insert(String state, int population) {
        int hash = toUnicodeModulus(state);

        if (LINKED_LIST[hash] == null) {
            freeSpaces--;

            LINKED_LIST[hash] = new LinkedList(new Node(state, population));
        } else {
            LINKED_LIST[hash].insert(new Node(state, population));
        }
    }

    public double find(String state) {
        LinkedList list = LINKED_LIST[toUnicodeModulus(state)];

        if (list == null) {
            System.out.println(state + " is not found");

            return -1;
        }

        for (Node node = list.head; node != null; node = node.nextNode) {
            if (node.stateName.equals(state)) {
                System.out.println(state + " is found with a population of " + FORMATTER.format(node.statePopulation));

                return node.statePopulation;
            }
        }

        System.out.println(state + " is not found");

        return -1;
    }

    public void delete(String state) {
        int hash = toUnicodeModulus(state);

        if (LINKED_LIST[hash] == null) {
            return;
        }

        if (LINKED_LIST[hash].remove(state)) {
            freeSpaces++;

            LINKED_LIST[hash] = null;
        }
    }

    /**
     * Prints a formatted representation of
     * the hash table to the console.
     */
    public void display() {
        for (int i = 0; i < LINKED_LIST.length; i++) {
            if (LINKED_LIST[i] == null) {
                System.out.println((i + 1) + ". Empty");
                continue;
            }

            for (Node node = LINKED_LIST[i].head; node != null; node = node.nextNode) {
                System.out.print((i + 1) + ". ");

                node.printNode();
            }
        }
    }

    /**
     * Prints the amount of free spaces and collisions
     * inside the hash table.
     */
    public void printFreeAndCollisions() {
        int collisions = 0;

        for (LinkedList list : LINKED_LIST) {
            if (list == null) {
                continue;
            }


            if (list.head != list.tail) {
                collisions++;
            }
        }

        System.out.println("Hash table has " + freeSpaces + " empty spaces and " + collisions + " collisions.");
    }

    /**
     * A helper method (hash function) to convert a String
     * to its respective hash.
     */
    private int toUnicodeModulus(String state) {
        int hash = 0;

        for (char c : state.toCharArray()) {
            hash += c;
        }

        return hash % LINKED_LIST.length;
    }

    private class Node {

        String stateName;

        int statePopulation;

        Node nextNode;

        public Node(String state, int population) {
            stateName = state;
            statePopulation = population;
        }

        public void printNode() {
            System.out.printf("%-25s%,10d\n", stateName, statePopulation);
        }
    }

    /**
     * A LinkedList class that will hold
     * multiple Node objects with the same
     * hash.
     */
    private class LinkedList {


        Node head;

        Node tail;

        public LinkedList(Node node) {
            this.head = node;
            this.tail = node;
        }

        public void insert(Node node) {
            tail.nextNode = node;
            tail = node;
        }


        public boolean remove(String state) {

            Node previousNode = head;
          

            if (head.stateName.equals(state) && tail.stateName.equals(state)) {
                System.out.println(state + " has been deleted from hash table");
                return true;
            }


            if (head.stateName.equals(state)) {
                head = head.nextNode;
                System.out.println(state + " has been deleted from hash table");
                return false;
            }

            /**
             * Iterate through the list and remove
             * the correct Node according to its
             * state name.
             */
            for (Node node = head; node != null; node = node.nextNode) {
                if (node.stateName.equals(state)) {
                    previousNode.nextNode = node.nextNode;
                    System.out.println(state + " has been deleted from hash table");
                    return false;
                }

                previousNode = node;
            }

            System.out.println(state + " cannot be found within the hash table and thus was NOT deleted");
            return false;
        }
    }
}


/**
 * 
 * @author Samici
 *
 */
import java.io.*;
import java.nio.file.*;
import java.util.*;
public class Project5 {
	private static final HashTable HASH_TABLE = new HashTable();
	  
    private static final Scanner SCANNER = new Scanner(System.in);

    public static void main(String[] args) throws Exception {
        System.out.println("Project 5");
        System.out.println("Hash Tables");
        System.out.println();

        File file = null;
      
       /*
       * Prompt the user for an existing file and
       * store it in a variable.
       */
        do {
            System.out.print("Enter the file name: ");

            file = new File(SCANNER.nextLine());
        } while (!file.exists());
      
       /*
       * Read the file into a list of Strings.
       */
        List<String> lines = Files.readAllLines(file.toPath());
      
       /*
       * Add the entries to the hash table.
       */
        for (int i = 1; i < lines.size(); i++) {
            String[] information = lines.get(i).split(",");

            HASH_TABLE.insert(information[0], Integer.parseInt(information[3]));
        }

        System.out.println("\nThere were " + (lines.size() - 1) + " state records read into the hash table.");

        System.out.println("\nHash table content:\n");

        HASH_TABLE.display();

        System.out.println();

        HASH_TABLE.delete("Vermont");
        HASH_TABLE.delete("California");
        HASH_TABLE.delete("South Carolina");

        System.out.println();

        HASH_TABLE.find("Florida");
        HASH_TABLE.find("Rhode Island");
        HASH_TABLE.find("California");

        System.out.println();

        HASH_TABLE.delete("Kentucky");
        HASH_TABLE.delete("Minnesota");
        HASH_TABLE.delete("West Virginia");

        System.out.println();

        HASH_TABLE.display();

        System.out.println();

        HASH_TABLE.printFreeAndCollisions();
    }

}
:code:
and here is the .csv file:
State,Capital,Abbreviation,Population,Region,US House SeatsAlabama,Montgomery,AL,4833722,South,7Alaska,Juno,AK,735132,West,1Arizona,Phoenix,AZ,6626624,Southwest,9Arkansas,Little Rock,AR,2959373,South,4California,Sacramento,CA,38332521,West,53Colorado,Denver,CO,5268367,West,7Connecticut,Hartford,CT,3596080,New England,5Delaware,Dover,DE,925749,Middle Atlantic,1Florida,Tallahassee,FL,19552860,South,27Georgia,Atlanta,GA,9992167,South,14Hawaii,Honolulu,HI,1404054,West,2Idaho,Boise,ID,1612136,West,2Illinois,Springfield,IL,12882135,Midwest,18Indiana,Indianapolis,IN,6570902,Midwest,9Iowa,Des Moines,IA,3090416,Midwest,4Kansas,Topeka,KS,2893957,Midwest,4Kentucky,Frankfort,KY,4395295,South,6Louisiana,Baton Rouge,LA,4625470,South,6Maine,Augusta,ME,1328302,New England,2Maryland,Annapolis,MD,5928814,Middle Atlantic,8Massachusetts,Boston,MA,6692824,New England,9Michigan,Lansing,MI,9895622,Midwest,14Minnesota,St Paul,MN,5420380,Midwest,8Mississippi,Jackson,MS,2991207,South,4Missouri,Jefferson City,MO,6044171,Midwest,8Montana,Helena,MT,1015165,West,1Nebraska,Lincoln,NE,1868516,Midwest,3Nevada,Carson City,NV,2790136,West,4New Hampshire,Concord,NH,1323459,New England,2New Jersey,Trenton,NJ,8899339,Middle Atlantic,12New Mexico,Santa Fe,NM,2085287,Southwest,3New York,Albany,NY,19651127,Middle Atlantic,27North Carolina,Raleigh,NC,9848060,South,13North Dakota,Bismarck,ND,723393,Midwest,1Ohio,Columbus,OH,11570808,Midwest,16Oklahoma,Oklahoma City,OK,3850568,Southwest,5Oregon,Salem,OR,3930065,West,5Pennsylvania,Harrisburg,PA,12773801,Middle Atlantic,18Rhode Island,Providence,RI,1051511,New England,2South Carolina,Columbia,SC,4774839,South,7South Dakota,Pierre,SD,844877,Midwest,1Tennessee,Nashville,TN,6495978,South,9Texas,Austin,TX,26448193,Southwest,36Utah,Salt Lake City,UT,2900872,West,4Vermont,Montpelier,VT,626630,New England,1Virginia,Richmond,VA,8260405,Middle Atlantic,11Washington,Olympia,WA,6971406,West,10West Virginia,Charleston,WV,1854304,Middle Atlantic,3Wisconsin,Madison,WI,5742713,Midwest,8Wyoming,Cheyenne,WY,582658,West,1

This post has been edited by modi123_1: 05 December 2017 - 09:54 AM
Reason for edit:: In the future, please use the [code] tag button in the editor.


Is This A Good Question/Topic? 0
  • +

Replies To: Data Structure using OOP

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3534
  • View blog
  • Posts: 16,017
  • Joined: 20-September 08

Re: Data Structure using OOP

Posted 05 December 2017 - 03:34 AM

Please use code tags as you were asked. andrewsw's Posted Image instructions will show you how to use code-tags. It isn't difficult.
EACH JAVA FILE should have its own code tags

This post has been edited by g00se: 05 December 2017 - 04:06 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3534
  • View blog
  • Posts: 16,017
  • Joined: 20-September 08

Re: Data Structure using OOP

Posted 05 December 2017 - 08:38 AM

Quote

My problem is when I run the program the Hash Table content supposed to display from zero (0) to 100

Then don't use i + 1 in the display() method
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1