Hi, I need some help on an array assignment. So far I have this code for the fir
ID: 3857281 • Letter: H
Question
Hi, I need some help on an array assignment.
So far I have this code for the first requirement of the assignment. Do I just replace the string adins with "CreamCaramel Whiskey chocolate Chocolate Cinnamon Vanilla" ?
Lab 6-3: Parallel Arrays
In this lab, you use what you have learned about parallel arrays to complete a partially completed Java program. The program is described in Chapter 6, Exercise 7, in Programming Logic and Design. The program should either print the name and price for a coffee add-in from the Jumpin’ Jive coffee shop or it should print the message: "Sorry, we do not carry that.".
Read the problem description carefully before you begin. The data file provided for this lab includes the necessary variable declarations and input statements. You need to write the part of the program that searches for the name of the coffee add-in(s) and either prints the name and price of the add-in or prints the error message if the add-in is not found. You can use the expanded Mail Order2 program shown in Figure 6-6 as a guide.
1. Open the source code file named JumpinJive.java using Notepad or the text editor of your choice.
2. Study the prewritten code to make sure you understand it.
3. Write the code that searches the array for the name of the add-in ordered by the customer.
4. Write the code that prints the name and price of the add-in or the error message, and also write the code that prints the total order cost.
5. Save this source code file in a directory of your choice, and then make that directory your working directory.
6. Compile the source code file, JumpinJive.java.
7. Execute the program using the following data, and record the output: Cream Caramel Whiskey chocolate Chocolate Cinnamon Vanilla
// JumpinJive.java - This program looks up and prints the names and prices of coffee orders.
// Input: Interactive.
// Output: Name and price of coffee orders or error message if add-in is not found.
import javax.swing.*;
public class JumpinJive {
public static void main(String args[]) throws Exception {
// Declare variables.
String addIn;
// Add-in ordered by customer.
final int NUM_ITEMS = 5; // Named constant
// Initialized array of add-ins.
String addIns[] = {"Whipped cream", "Cinnamon", "Chocolate sauce", "Amaretto", "Irish whisky"
};
// Initialized array of add-in prices.
double addInPrices[] = { .89, .25, .59, 1.50, 1.75};
boolean foundIt = false;
int x;
double orderTotal = 2.00; // All orders start with a 2.00 charge
// Get user input.
addIn = JOptionPane
.showInputDialog("Enter coffee add-in or XXX to quit: ");
// Write the rest of the program here.
for (int it = 0; it < NUM_ITEMS; it++)
{
//checks the addIns Equals
if (addIns[it].equals(addIn))
{
//Prints the product name and price
System.out.println("Name of the product :" + addIns[it] + " Price of the product:"
+ addInPrices[it]);
//Prints the total price of the product
System.out.println("Total Price of the product:"
+ (orderTotal + addInPrices[it]));
//If found it returns true
foundIt = true;
break;
}
}
//Checks whether it is found
if (!foundIt)
{
//Prints the Error Message
System.out.println("Sorry, we do not carry that.");
}
} // End of main() method.
} // End of JumpinJive class.
Explanation / Answer
For the data: Cream Caramel Whiskey chocolate Chocolate Cinnamon Vanilla the output is:
For Cream:
Sorry, we do not carry that.
For Caramel:
Sorry, we do not carry that.
For Whiskey:
Sorry, we do not carry that.
For chocolate:
Sorry, we do not carry that.
For Chocolate:
Sorry, we do not carry that.
For Cinnamon:
Name of the product :Cinnamon
Price of the product:0.25
Total Price of the product:2.25
For Vanilla:
Sorry, we do not carry that.
***Please provide the CF score by liking the answer. Thanks in advance.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.