Question

In: Computer Science

Create an ID table class that maps identifiers to memory addresses for a Parser java program....

Create an ID table class that maps identifiers to memory addresses for a Parser java program. The first identifier will be at address 0, the second at address 1, and so on.

I am working on a Lexer/Parser project for java. I've completed the lexer and parser portion of the program but I'm looking for some assistance in creating a ID table class.

That has specific methods that add token identifiers into a hashmap.

Here are the specifications that I'm looking for assistance with.

Define the class IdTable with the following data members and methods:

a) a hashmap data member with String key and integer value. The keys are the identifiers and the values represent the address in memory in which the identifier will be stored. Assign the index for each entry such that the first id will have address 0, second id will have address 1 and so on

b) add - this method adds an entry to the map, you need only send the id.

c) getAddress - this method returns the address associated with an id, or -1 if not found.

d) toString method that prints the string values of the hashmap.

Here is my code so far. Mainly need help with b and c. Thank you for your assistance. I Will like and comment.

import java.util.HashMap;

public class IdTable {
    public String key;
    public int value;

    // create hashmap type data member
    HashMap<String, Integer> table = new HashMap<String,Integer>();


    // todo create add method
    public void add() {

    }
    
    //todo create get address method


    // write toString method for ID table
    @Override
    public String toString() {
        return "IdTable{" +
                "table=" + table +
                '}';
    }



}

Solutions

Expert Solution

Program Code Screenshot :

Sample Output :

Program Code to Copy

import java.util.HashMap;

class IdTable {
    //Variable to keep track of addresses
    private int address = 0;

    // create hashmap type data member
    HashMap<String, Integer> table = new HashMap<String, Integer>();


    // todo create add method
    public void add(String id) {
        //Add id to the map with the current address. Increment the address
        table.put(id, address++);
    }

    public int getAddress(String id) {
        //Return the address for the given
        return table.get(id);
    }

    //todo create get address method


    // write toString method for ID table
    @Override
    public String toString() {
        return "IdTable{" +
                "table=" + table +
                '}';
    }
}

class Main{
    public static void main(String[] args) {
        IdTable table = new IdTable();
        table.add("name");
        table.add("section");
        table.add("grade");
        System.out.println("Address of 'section' is "+table.getAddress("section"));
        System.out.println(table);
    }
}

Related Solutions

Java program In this assignment you are required to create a text parser in Java/C++. Given...
Java program In this assignment you are required to create a text parser in Java/C++. Given a input text file you need to parse it and answer a set of frequency related questions. Technical Requirement of Solution: You are required to do this ab initio (bare-bones from scratch). This means, your solution cannot use any library methods in Java except the ones listed below (or equivalent library functions in C++). String.split() and other String operations can be used wherever required....
Describe the rules governing identifiers. Can identifiers be re-used in the same Java class file? Provide...
Describe the rules governing identifiers. Can identifiers be re-used in the same Java class file? Provide three examples of different kinds of invalid identifiers. Java
For Java Let's get some practice with maps! Create a public class CountLetters providing a single...
For Java Let's get some practice with maps! Create a public class CountLetters providing a single static method countLetters. countLetters accepts an array of Strings and returns a Map from Strings to Integers. (You can reject null arguments using assert.) The map should contain counts of the passed Strings based on their first letter. For example, provided the array {"test", "me", "testing"} your Map should be {"t": 2, "m": 1}. You should ignore empty Strings and not include any zero...
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
Create a java program that: - Has a class that defines an exception -Have that exception...
Create a java program that: - Has a class that defines an exception -Have that exception throw(n) in one method, and be caught and handled in another one. -Has a program that always continues even if incorrect data is entered by the user -has a minimum of 2 classes in it
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...
object oriented programming java Create a Student class that have two data members id (assign to...
object oriented programming java Create a Student class that have two data members id (assign to your ID) and name (assign to your name). Create the object of the Student class by new keyword and printing the objects value. You may name your object as Student1. The output should be like this: 20170500 Asma Zubaida
JAVA program Create a class called Array Outside of the class, import the Scanner library Outside...
JAVA program Create a class called Array Outside of the class, import the Scanner library Outside of main declare two static final variables and integer for number of days in the week and a double for the revenue per pizza (which is $8.50). Create a method called main Inside main: Declare an integer array that can hold the number of pizzas purchased each day for one week. Declare two additional variables one to hold the total sum of pizzas sold...
Java program. Need to create a class names gradesgraph which will represent the GradesGraphtest program. Assignment...
Java program. Need to create a class names gradesgraph which will represent the GradesGraphtest program. Assignment Create a class GradesGraph that represents a grade distribution for a given course. Write methods to perform the following tasks: • Set the number of each of the letter grades A, B, C, D, and F. • Read the number of each of the letter grades A, B, C, D, and F. • Return the total number of grades. • Return the percentage of...
Using maps in Java. Make a public class called ExampleOne that provides a single class (static)...
Using maps in Java. Make a public class called ExampleOne that provides a single class (static) method named firstOne. firstOne accepts a String array and returns a map from Strings to Integer. The map must count the number of passed Strings based on the FIRST letter. For example, with the String array {“banana”, “apples”, “blueberry”, “orange”}, the map should return {“b”:2, “a”:1, “o”:1}. Disregard empty Strings and zero counts. Retrieve first character of String as a char using charAt, or,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT