What would be a very simple coding solution to question 9 on page 538 in textboo
ID: 3858165 • Letter: W
Question
What would be a very simple coding solution to question 9 on page 538 in textbook Starting Out with Visual C# 4th edition, by Tony Gaddis? I am looking for very basic/elementary level C# coding used for Windows Form App. Any guidance would be greatly appreciated.
Question states:
Alphabetic Telephone Number Translator - Many companies use telephone numbers like 555-GET-FOOD so the number is easier for the customers to remember. On a standard telephone, the alphabetic letters are mapped to numbers in the following fashion (ABC = 2, DEF = 3, GHI = 4, JKL = 5, MNO = 6, PQRS = 7, TUV = 8, WXYZ = 9).
* Create an application that lets the user enter a 10-character telephone number in the format xxx-xxx-xxxx. The application should display the telephone number with any alphabetic characters that appeared in the original translated to their numeric equivalent. For example, if the user enters 555-GET-FOOD, the application should display 555-438-3663.
Explanation / Answer
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TelephoneNumberTranslator
{
public partial class TelephoneNumberTranslator : Form
{
public TelephoneNumberTranslator()
{
InitializeComponent();
}
// The isValidNumber method accepts a string and returns true if it
// contains 10 digits, or false otherwise.
private bool IsValidNumber(string str)
{
const int VALID_LENGTH = 10; // Length of a valid string
bool valid = true; // Flag to indicate validity
// Check the string's length.
if (str.Length == VALID_LENGTH)
{
valid = true;
}
else
{
valid = false;
}
// Return the status
return valid;
}
// The telephone format method accepts a string argument by reference and
// formats it as a telephone number.
private void TelephoneFormat(ref string str)
{
// First, insert the left paren at position 0.
str = str.Insert(0, "(");
// Next, insert the right paren at position 4.
str = str.Insert(4, ")");
// Next, insert the hyphen at position 8.
str = str.Insert(8, "-");
}
// Creates Dictionary to hold values
private readonly Dictionary<int, char[]> keys = new Dictionary<int, char[]>()
{
{1, new char[] {}},
{2, new[] {'a', 'b', 'c', 'A', 'B', 'C'}},
{3, new[] {'d', 'D', 'E', 'e', 'f', 'F'}},
{4, new[] {'g', 'G', 'H', 'h', 'I', 'i'}},
{5, new[] {'j', 'J', 'K', 'k', 'L','l'}},
{6, new[] {'m', 'M', 'N', 'n', 'O','o'}},
{7, new[] {'p', 'P', 'Q', 'q', 'R', 'r', 'S','s'}},
{8, new[] {'t', 'T', 'U', 'u', 'V','v'}},
{9, new[] {'w', 'W', 'X', 'x', 'Y', 'y', 'Z', 'z'}},
{0, new[] {' '}},
};
// Translates strings into numbers
public int[] Translate(string word)
{
return word.Select(c =>
keys.First(k => k.Value.Contains(c)).Key).ToArray();
}
private void numButton_Click(object sender, EventArgs e)
{
// Get a trimmed copy of the user's input.
string input = numTextBox.Text;
input = input.Replace("-", "");
if (IsValidNumber(input))
{
//TelephoneFormat(ref input);
MessageBox.Show(" The number is " + Translate(input)[0]);
}
else
{
MessageBox.Show("Invalid input");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.