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 Beginner a)Create a class named Student that has fields for an ID number, number of...
Java Beginner a)Create a class named Student that has fields for an ID number, number of credit hours earned, and number of points earned. (For example, many schools compute grade point averages based on a scale of 4, so a three-credit-hour class in which a student earns an A is worth 12 points.) Include methods to assign values to all fields. A Student also has a field for grade point average. Include a method to compute the grade point average...
Create a Java program. The class name for the program should be 'EncryptText'. In the main...
Create a Java program. The class name for the program should be 'EncryptText'. In the main method you should perform the following: You should read a string from the keyboard using the Scanner class object. You should then encrypt the text by reading each character from the string and adding 1 to the character resulting in a shift of the letter entered. You should output the string entered and the resulting encrypted string. Pseudo flowchart for additional code to be...
In Java create a simple class named student with the following properties: id age gpa credit...
In Java create a simple class named student with the following properties: id age gpa credit hours accomplished Also, create the following methods: Constructors Getters and setters
write Java program has two classes ,( using Inheritance ) first class set ( id ,...
write Java program has two classes ,( using Inheritance ) first class set ( id , name ) and method output second class ( id , name , Mark 1 , Mark 2 , Mark 3 ) method total , average , grade , results ( if the student pass or not ) , and output method
Write a program that reads a Java source file and produces an index of all identifiers...
Write a program that reads a Java source file and produces an index of all identifiers in the file. For each identifier, print all lines in which it occurs. For simplicity, we will consider each string consisting only of letters, numbers, and underscores an identifer. Declare a Scanner in for reading from the source file and call in.useDelimiter("[^AZa-z0-9_]+"). Then each call to next returns an identifier. Java. Explain logic used lease.
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...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT