Lab 6-3: Parallel Arrays In this lab, you use what you have learned about parall
ID: 3913417 • Letter: L
Question
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, v. do sot oary 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-ins) 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 JmpinJive s 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 7. Execute the program using the following data, and record the output: 112 113 Crean Car nel thi skey chocol ate Checel ate Vanilla Remember that Java is case sensitive, which means it distinguishes between uppercase and lowercase letters. This means, tor example, that Cinnamon is not the same as cinnamon. 113Explanation / Answer
UPDATED PROGRAM
import javax.swing.*;
import java.io.*;
class JumpinJive
{
public static void main(String args[]) throws Exception
{
// Declare variables
String addIn,s="";
final int NUM_ITEMS=5; // Nmaed constant
// Initialized array of add-ins.
String addIns[]={"Cream","Cinnamon","Chocolate","Amaretto","Whiskey"};
// Initialized array of add-in prices.
double addInPrices[]={0.89,0.25,0.59,1.50,1.75};
boolean foundIt=false;
int x; // loop control variable
double orderTotal=2.00; // all orders start with a 2.00 charge
double test=0; // convert string to double
while(true) // create infinite while loop until user type XXX
{
// Get user input
addIn=(String)JOptionPane.showInputDialog("Enter coffee add-in or XXX to quit: ");
if(addIn=="XXX") System.exit(0); // check condition input=XXX then exit from this program
test=Double.parseDouble(addIn); // convert String to Double
foundIt=false; // initialize fountIt=false
x=0;// initialize x=0
orderTotal=2.00; // initialize orderTotal=2.00
while(x<NUM_ITEMS) // create while loop until number of items
{
if(test==addInPrices[x]) // compare input value and addInPrices array elements
{
foundIt=true; // then boolean value foundIt=true
orderTotal=orderTotal+addInPrices[x]; // add orderTotal value
s=addIns[x]; // assign item string into s
}
x++; // increment x value
}
if(foundIt==true) // check condition founIt=true then display items into console
System.out.println("The price of "+s+" is "+orderTotal);
else
System.out.println("Sorry, we do not carry that."); // display this message
}
}
}
OUTPUT
F:PHANIJava>javac JumpinJive.java
F:PHANIJava>java JumpinJive
The price of Cream is 2.89
The price of Chocolate is 2.59
The price of Whiskey is 3.75
Sorry, we do not carry that.
The price of Amaretto is 3.5
Sorry, we do not carry that.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.