Numerology

Assignment

Goal:

Numerology involves assigning a numerical value to a string based on values assigned to the individual letters according to a variety of rules. Whether you consider numerology a science, a religion, a game, total nonsense or anything else is completely up to you. In this assignment, we will just use numerology to practice the topics listed. 

Definition:

The command line parameter should consist of one or more strings.

If no command line parameter is provided, print an error message and exit.

Otherwise, do the following for each string.

For each string, do the following computation. Give a value to each letter in the string as follows.

a b c d e f g h i j k l m
1 2 3 4 5 6 7 8 9 0 10 20 30

 

n o p q r s t u v w x y z
40 50 60 70 80 90 100 110 120 130 140 150 160

Treat upper-case letters as the equivalent lower-case letters. Give the value 0 to any input characters that are not letters in Ascii, i.e., a‑z or A‑Z.

Sum the values for each character in the string. Print each string with its length and value. Line the numbers up neatly.

At the end of the program, print a table the number of times each letter was used as input, displaying the characters columnwise. Line the numbers up neatly. Also print a line showing the total number of letters in the input.

Method:

In addition to the main program, create two classes, DataTable.javaand TableEntry.java.

The DataTable class should contain an array of TableEntry items. You will also need a constructor and several functions to access the table, such as the following:

public int getTableValue(char x)            – return the numerological value for character x

public void bumpCount(char x)       – add 1 to the count for character x

public int getTableValue(char x)            – return the count for character x (for printing)

Remember that when you declare an array as a class member, you are only declaring the reference variable. However, since Java has strong typing, the declaration must indicate that the variable is an array of TableEntry. Therefore the declaration should look like this:

private TableEntry[] innerTable;

The actual memory will be defined in the constructor. Therefore the constructor should contain the following code:

innerTable = new TableEntry[26];

The constructor should call the constructor for TableEntry for each array element.

The TableEntry class should have variables for the letter of the alphabet, its numerological value and its frequency count. It should also have the usual getters and setters.

Use the appropriate method of the String class to convert to lower case. You can use the charAt method to retrieve a specific character from a String.

Here is sample output from the bash script above:

Parm  Length  Total  String

—-  ——  —–  ——

1      20    666  Le Empereur Napoleon

2      13    666  quarante-deux

3      15    666  L’russe Besuhof

a  3   g  0   l  3   q  1   v  0

b  1   h  1   m  1   r  4   w  0

c  0   i  0   n  3   s  3   x  1

d  1   j  0   o  3   t  1   y  0

e  9   k  0   p  2   u  5   z  0

f  1

Total letters = 43

Here is the output from the sample command given above:

Parm  Length  Total  String

—-  ——  —–  ——

1      10     50  eeeeeeeeee

2       9     54  fffffffff

a  0   g  0   l  0   q  0   v  0

b  0   h  0   m  0   r  0   w  0

c  0   i  0   n  0   s  0   x  0

d  0   j  0   o  0   t  0   y  0

e 10   k  0   p  0   u  0   z  0

f  9

Total letters = 19 

Solution 

DataTable.java

public class DataTable {

privateTableEntry[] innerTable;

publicDataTable() {

// initialize the table and its entries

innerTable = new TableEntry[26];

for (char c = ‘a’; c <= ‘i’; c++) { // a -> i

innerTable

[c – ‘a’]

= new TableEntry(c, c – ‘a’ + 1);

}

innerTable[‘j’ – ‘a’] = new TableEntry(‘j’, ‘j’ – ‘a’ + 1);

for (char c = ‘k’; c <= ‘z’; c++) { // k -> z

innerTable

[c – ‘a’]

= new TableEntry(c, (c – ‘k’ + 1) * 10);

}

}

// get the numerological for the character

publicintgetTableValue(char x) {

x = (“” + x).toLowerCase().charAt(0); // convert to lowercase

if (x >= ‘a’ && x <= ‘z’) {

returninnerTable[x – ‘a’].getValue();

}

return 0;

}

// increase the count for the character

public void bumpCount(char x) {

x = (“” + x).toLowerCase().charAt(0); // convert to lowercase

if (x >= ‘a’ && x <= ‘z’) {

TableEntry entry = innerTable[x – ‘a’]; // increase count

entry.setCount(entry.getCount() + 1);

}

}

// return the count for the character

publicintgetTableCount(char x) {

x = (“” + x).toLowerCase().charAt(0); // convert to lowercase

if (x >= ‘a’ && x <= ‘z’) {

returninnerTable[x – ‘a’].getCount();

}

return 0;

}

// accumulate the total value of all the characters

publicintgetTotalValue() {

int total = 0;

for (TableEntry entry : innerTable) {

total += entry.getValue() * entry.getCount();

}

return total;

}

// accumulate the total count of all the characters

publicintgetTotalCount() {

int count = 0;

for (TableEntry entry : innerTable) {

count += entry.getCount();

}

return count;

}

}

Hw3.java

public class Hw3 {

public static void main(String[] args) {

if (args.length == 0) {

System.out.println(“No string is provided.”);

return;

}

// to accumulate the count of all characters

DataTable total = new DataTable();

// print the table header

System.out.println(“Parm  Length  Total  String”);

System.out.println(“—-  ——  —–  ——\n”);

// calculate for each string in the parameters

for (int i = 0; i <args.length; i++) {

String value = args[i];

DataTable table = new DataTable();

// add character into the table

for (int j = 0; j <value.length(); j++) {

char x = value.charAt(j);

table.bumpCount(x);

total.bumpCount(x);

}

System.out.printf(“%4d  %6d  %5d  %s\n”,

i + 1, value.length(), table.getTotalValue(), value);

}

System.out.println();

printCountTable(total);

}

public static void printCountTable(DataTable total) {

// display the overall count

for (char c = ‘a’; c <= ‘e’; c++) {

System.out.printf(“%c%3d   “, c, total.getTableCount(c));

for (int i = 1; i < 5; i++) {

char x = (char)(c + 6 + (i – 1) * 5);

System.out.printf(“%c%3d   “, x, total.getTableCount(x));

}

System.out.println();

}

System.out.printf(“%c%3d   \n”, ‘f’, total.getTableCount(‘f’));

System.out.printf(“\nTotal letters = %d\n\n”, total.getTotalCount());

}

} 

hw3bat.bash

#! /bin/bash

java Hw3 “Le Empereur Napoleon” “quarante-deux” “L’russeBesuhof” 

TableEntry.java

public class TableEntry {

private char c;

privateint value; // numerological value

privateint count;

publicTableEntry(char c, int value) {

this.c = c;

this.value = value;

this.count = 0;

}

// getters

public char getCharacter() {

return c;

}

publicintgetValue() {

return value;

}

publicintgetCount() {

return count;

}

// setters

public void setCharacter(char c) {

this.c = c;

}

public void setValue(int value) {

this.value = value;

}

public void setCount(int count) {

this.count = count;

}

}