Ask Question
10 July, 07:51

Write a program named ChatAWhile that stores the area codes and rates in parallel arrays and allows a user to enter an area code and the length of time for a call in minutes, and then display the total cost of the call.

+3
Answers (1)
  1. 10 July, 11:27
    0
    import java. util. Scanner; public class Main { public static void main (String[] args) { int areaCode[] = {121, 122, 123, 124, 125}; double rates [] = {0.2, 0.3, 0.35, 0.15, 0.25}; Scanner input = new Scanner (System. in); System. out. print ("Enter area code: "); int a = input. nextInt (); System. out. print ("Enter calling time: "); int time = input. nextInt (); double cost = 0; for (int i=0; i < areaCode. length; i++) { if (areaCode[i] = = a) { cost = rates[i] * time; } } System. out. println ("Total cost of the call: $" + cost); } }

    Explanation:

    The solution code is written in Java.

    Firstly create two parallel arrays for area code and calling rate (Line 7-8). Presume the rate is based on call per minute.

    Next, user Scanner object to prompt user to input area code and calling time (Line 10 - 14).

    Create a for loop to identify the index where the input area code can be found in the array and then calculate the cost by using the same index i to get the corresponding rate (Line 17 - 20).

    Print the cost to console (Line 22).
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a program named ChatAWhile that stores the area codes and rates in parallel arrays and allows a user to enter an area code and the ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers