Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Problems: I need help with Hierarchy tasks. Tester cases and codes are provided at the bottom. Thank you. | +--Alphabet | +--AlphabetException | |

Java Problems:

I need help with Hierarchy tasks. Tester cases and codes are provided at the bottom. Thank you.

| +--Alphabet | +--AlphabetException | | | +--BadIndexAlphabetException | | | +--MissingCharAlphabetException 

------------------------Here are the tasks-----------------------------

class Alphabet

Alphabet represents the set of characters that will be allowed in some message. For different messages, we can choose to use different Alphabet objects.

The primary function of Alphabets is to provide translation of characters from symbols to integers and back via its indexOf(c) and get(i) methods. Several ciphers will require translating a character like 'C' into a number. The Alphabet provides such a functionality via its a.indexOf('C') method which will produce the integer associated with C if the letter is in the alphabet. Similarly, converting a number like 8 to an equivalent character is done viaa.get(8) which returns a character from the alphabet.

Manual Inspection Criteria (5%): Adhere to the abstraction barrier set up by Alphabet and use its methods to do character/number translations elsewhere in the project. This is a required part of the manual inspection criteria.

Start with the following in your class (copy-paste it to start):

public class Alphabet { public static final Alphabet DEFAULT = new Alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 1234567890!@#$%^&*()_+-=[]{}\\|;:'\",./?<>"); // finish this class definition. } 

Fields

private String symbols

public static final Alphabet DEFAULT = .... Due to the chance to mistype this, we have provided it above and in the project pack in Alphabet.java. Please don't try to manually type things like this, throughout your entire life as a programmer.

Methods

public Alphabet(String symbols). The constructor initializes symbols.

public int indexOf(char c). Returns the index of char parameter c in string symbols. For example, the index of 'b' in "abc" is 1. Throws MissingCharAlphabetException if char parameter c is not in symbols.

public char get(int i). Returns the char at position i of symbols. For example, the char at position 1 of "abc" is 'b'. Throws BadIndexAlphabetException if int parameter i is not a position in symbols.

public int length(). Returns the length of string symbols.

public String getSymbols(). Returns symbols.

public String toString(). String representation. Example: if symbols is "ABC", returns "Alphabet(ABC)"

public boolean equals(Object other). Return true if other is another Alphabet and has the same characters and ordering as this alphabet. The below examples below should compile and produce the indicated output.

Object a1 = new Alphabet("ABCabc"); Object a2 = new Alphabet("ABCabc"); Object a3 = new Alphabet("123"); Object s1 = new String("123"); Object o1 = new Object(); System.out.println( a1.equals(a2) ); // true System.out.println( a1.equals(a3) ); // false System.out.println( a1.equals(s1) ); // false System.out.println( a1.equals(o1) ); // false 

Demonstration of Alphabet

The following DrJava session demonstrates the basic functionality of the Alphabet class. It's primary purpose is to provide translation of characters from symbols to integers and back via its indexOf(c) and get(i) methods.

Welcome to DrJava. > Alphabet a2e = new Alphabet("abcde"); > a2e.indexOf('c') 2 > a2e.indexOf('a') 0 > a2e.indexOf('z') Not in alphabet: 'z' not found in Alphabet(abcde). at Alphabet.indexOf(Alphabet.java:???) > a2e.get(3) 'd' > a2e.get(0) 'a' > a2e.get(9) MissingCharAlphabetException: Not in alphabet: 'z' not found in Alphabet(abcde). at Alphabet.indexOf(Alphabet.java:???) > a2e.length() 5 > a2e.toString() "Alphabet(abcde)" > String syms = a2e.getSymbols(); > syms "abcde" > Alphabet def = Alphabet.DEFAULT; > def.length() 93 > def.indexOf('A') 0 > def.indexOf('a') 26 > def.indexOf('0') 62 > def.indexOf('?') 90 > def.get(92) '>' > def.get(32) 'g' > def.getSymbols() "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 1234567890!@#$%^&*()_+-=[]{}\|;:'",./?<>" > def.toString() "Alphabet(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 1234567890!@#$%^&*()_+-=[]{}\|;:'",./?<>)" 

class AlphabetException

Class AlphabetException inherits from class RuntimeException.

Fields

none.

Methods

public AlphabetException(String message). Invokes the parent class's construction, supplying the given message.

--------------------------------------

class MissingCharAlphabetException

Class MissingCharAlphabetException extends AlphabetException.

Fields

public final char offender

public final Alphabet a

Methods

MissingCharAlphabetException(char offender, Alphabet a). Must both store the offender and Alphabet a, as well as call the parent constructor with a message of this pattern:

"Not in alphabet: 'a' not found in Alphabet(ABC)." 

----------------------------

class BadIndexAlphabetException

Class BadIndexAlphabetException extends AlphabetException.

Fields

public final int index

public final Alphabet a

Methods

BadIndexAlphabetException(int index, Alphabet a). Must both store the index and Alphabet a, as well as call the parent constructor with a message of this pattern:

"Not in alphabet: no index -1 found in Alphabet(ABC)." 

--------------------------------Please solve the tasks from below-------------------

public class Alphabet { public static final Alphabet DEFAULT = new Alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 1234567890!@#$%^&*()_+-=[]{}\\|;:'\",./?<>"); // finish this class definition. }

---------------------------------Here is the link to the tester cases-------------------

https://paste.ee/p/qUCzH

Thank you.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions