Problem Build an interactive program to allow a user to interactively allow a us
ID: 3819384 • Letter: P
Question
Problem
Build an interactive program to allow a user to interactively allow a user to find out the international radio code for any number of letters.
When he inputs a letter, display the letter and the corresponding international radio code.
You will be given a file of upper case letters (A-Z) and the corresponding international readio codes in a .csv file. For instance:
A, Alpha
B, Bravo
Requirements
You will one main class firstinitiallastnameInternationalCodes
You will build at least four methods:
The user may input lowercase (a-z), uppercase (A-Z), or garbage.
Filename is from the command line, args[0].
You will need to use the following design tools:
CSCI FileIn, keyBoard, Menu
Two dimensional Arrays, parallel arrays, or class.
String split()
You will use Proper Software Engineering principles.
Proper breakdown into methods
Proper use of data passing between methods.
You will liberally comment the rest of the code explaining your logic.
You will submit a compilable text file firstinitiallastnameInternationalCodes.java so that I can compile and run it.
You will comment the code with a header.
DESIGN
You will want to segment your main program into two parts.
PART 1 will read the data into a data storage method. Choose from one of the methods: multidimensional arrays, stamp data, java classes
Use a While loop and FileIn to get the data.
Part 2. Will loop through displaying a simple menu asking for the user’s letter or an exit code (say 1).
If he inputs anything else, tell him he made a mistake and go back and try again.
Recommend you use the new CSCI classes Menu and keyBoard for this part.
Once He gives you the letter Display the answer and ask for another input.
Menu code:
package CSCI;
import CSCI.*;
import java.util.*;
// This class is a utility Menu
public class Menu{
// class variables
private int menuSize;
private String[][] menuArray;
private final static String STARS = "******************************************";
private final static String BLANK = "* *";
private final static int MAXSIZE = 10;
private final static int NUMELEMENTS = 2;
private final static int ERROR = 10;
private final static String FORMATSTRING = "* %5S %27S *";
// Constructor
public Menu(int aMenuSize, String[] Keys, String[] Definitions){
menuArray = new String[NUMELEMENTS][MAXSIZE];// instantiate menuArray
if(aMenuSize > MAXSIZE){
System.out.println("** Error ** Expected Menu size " + aMenuSize
+ " More than " + MAXSIZE + " Items is too many");
menuSize = MAXSIZE;
}
else menuSize = aMenuSize;
// Fill up menuArray
for (int i = 0; i < menuSize ; ++i){
menuArray[0][i] = Keys[i];
menuArray[1][i] = Definitions[i];
}
} //end Menu
public Menu(String Key, String Definition){
menuArray = new String[NUMELEMENTS][MAXSIZE];// instantiate menuArray
menuSize = 1;
// Fill up menuArray
menuArray[0][0] = Key;
menuArray[1][0] = Definition;
} //end Menu
public void project(){
String inLine;
print(STARS);
print (BLANK);
for(int i = 0; i < menuSize; ++i){
inLine = String.format(FORMATSTRING, menuArray[0][i],menuArray[1][i]);
print(inLine);
}
print (BLANK);
print(STARS);
} // end read
public void print(String inLine){
System.out.println(inLine);
}
public void BuildMenu(){
String inLine;
setMenuSize(7);
inLine = STARS;
print(inLine);
}
public void setMenuSize(int Size){
menuSize = Size;
}
} //end Menu
Keyboard code:
package CSCI;
import CSCI.*;
import java.util.*;
// This class is a utility Menu
public class keyBoard{
// class variables
private Scanner Input;
private final static int ANINT = 0;
private final static double ADOUBLE = 0;
private final static char CKEY = 'a';
// Constructor
public keyBoard(){
Input = new Scanner(System.in);
}
public String read(){
String inLine;
System.out.print("Type input ==> ");
inLine = Input.nextLine();
return inLine;
} // end read
public String readUpperCase(){
String inLine;
String outLine;
inLine = read();
outLine = inLine.toUpperCase();
return outLine;
} // end readUpperCase
public String readLowerCase(){
String inLine;
String outLine;
inLine = read();
outLine = inLine.toLowerCase();
return outLine;
} // end readLowerCase
public char read(char myChar){
String inLine;
inLine = read();
myChar = inLine.charAt(0);
return myChar;
} // end read character
public char readLowerCase(char myChar){
myChar = read(myChar);
myChar = Character.toLowerCase(myChar);
return myChar;
} // end readLowerCase
public char readUpperCase(char myChar){
myChar = read(myChar);
myChar = Character.toUpperCase(myChar);
return myChar;
} // end readUpperCase
public int read(int myInt){
String inLine;
inLine = read();
myInt = CSCIConvert.Parse(inLine,myInt);
return myInt;
} // end read Integer
public double read(double myDouble){
String inLine;
inLine = read();
myDouble = CSCIConvert.Parse(inLine,myDouble);
return myDouble;
} // end read Integer
public boolean read(boolean myBoolean){
char myChar;
myChar = read(CKEY);
myBoolean = Parse(myChar,true);
return myBoolean;
} // end read Integer
public boolean Parse(char value, boolean ERROR){
value = Character.toUpperCase(value);
boolean result;
switch(value){
case ('T'):
case ('Y'):
case ('1'):
result = true;
break;
case ('F'):
case ('N'):
case ('0'):
result = false;
break;
default:
result = false;
break;
}
return result;
}// end Parse;
} //end Menu
Explanation / Answer
Given below is an interactive Java program which performs following steps:
Comments are added where necessary in below program for code readability.
File: MyApp.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class MyApp {
private static final int NUM_ALPHABETS = 26;
static String[][] radioCodes = new String[NUM_ALPHABETS][2];
// Check if given string is a valid one letter alphabet
private static Boolean isValidAlphabet(String alphabet) {
if ((alphabet.length() != 1) || alphabet.compareTo("A") < 0 || alphabet.compareTo("Z") > 0)
return false;
return true;
}
// Add mapping between alphabet and radio code
private static void addRadioCode(String alphabet, String code) {
int insertIndex = 0;
for (; insertIndex < NUM_ALPHABETS; insertIndex++) {
if (radioCodes[insertIndex][0] == null) {
break; // found index where mapping needs to be updated
}
if (radioCodes[insertIndex][0].equals(alphabet)) {
return; // do not update as already there is a mapping for given alphabet added
}
}
// Add mapping between alphabet and radio code
if (insertIndex < NUM_ALPHABETS) {
radioCodes[insertIndex][0] = alphabet;
radioCodes[insertIndex][1] = code;
}
}
// Find radio code for given alphabet and print it
private static void printRadioCode(String alphabet) {
int foundIndex = -1;
for (int index = 0; index < NUM_ALPHABETS; index++) {
if (radioCodes[index][0] == null) {
break; // didn't find radio code matching given alphabet
}
if (radioCodes[index][0].equals(alphabet)) {
foundIndex = index; // found radio code matching given alphabet
break;
}
}
if (foundIndex < 0) {
// didn't find radio code matching given alphabet
System.out.println("Sorry, didn't find a radio code matching given alphabet !");
return;
}
// found radio code matching given alphabet
System.out.println(radioCodes[foundIndex][0] + " : " + radioCodes[foundIndex][1]);
}
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Usage: MyApp <file-path>");
System.exit(-1);
}
// Read file path argument
String filePath = args[0];
try {
// Read file contents line by line
File file = new File(filePath);
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] fields = line.split(","); // Split words in line on comma
if (fields.length < 2) continue; // Skip invalid lines
String alphabet = fields[0].toUpperCase().trim();
String code = fields[1].trim();
if (isValidAlphabet(alphabet) == false) continue; // Skip if given letter is not a valid alphabet
// Add mapping between alphabet and radio code
addRadioCode(alphabet, code);
}
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
// Create Menu
Menu menu = new Menu(2, new String[] { "A - Z", "1"}, new String[] { "Enter a letter", "Exit"});
// Create keyBoard
keyBoard kb = new keyBoard();
while (true) {
// Display menu
menu.project();
// Read keyboard input
String input = kb.readUpperCase();
// Exit program if user enters "1"
if (input.equals("1")) return;
if (isValidAlphabet(input)) {
// User entered valid alphabet
printRadioCode(input);
} else {
// Prompt user to enter valid alphabet
System.out.println("Please enter a valid input (A - Z) or 1");
}
}
}
}
Sample Execution Output:
******************************************
* *
* A - Z ENTER A LETTER *
* 1 EXIT *
* *
******************************************
Type input ==> A
A : Alfa
******************************************
* *
* A - Z ENTER A LETTER *
* 1 EXIT *
* *
******************************************
Type input ==> B
B : Bravo
******************************************
* *
* A - Z ENTER A LETTER *
* 1 EXIT *
* *
******************************************
Type input ==> Z
Z : Zulu
******************************************
* *
* A - Z ENTER A LETTER *
* 1 EXIT *
* *
******************************************
Type input ==> 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.