The purpose of this assignment is to assess your understanding of initializing a
ID: 3734849 • Letter: T
Question
The purpose of this assignment is to assess your understanding of initializing arrays, declaring arrays, and searching arrays by creating an application to store entrees served at a cafeteria during the work week. Since the steps are clearly spelled out for you below, there is no need to write an algorithm for this particular assignment. The following are the steps that you should follow:
(1) Create an array of String objects and populate it with the days of the work week (i.e., Monday - Friday)
(2) Create an array parallel to the one created in
(1) to store the entrees. However, you do not have this information, so you will need to write a loop that prompts the user to enter the entree served on that day. This prompt should include the day for which you are asking for the information (e.g., "What entree is being served on Monday?")
(3) Create another parallel array that stores the prices. You will also need to collect this information from the user. This time, your prompt should include the name of the entree for which you are asking for the price
(4) Prompt the user for an entree and search the array and output the day on which the entree will be served
(5) Search through the array for the highest price entree and output the name of this entree for the user Be sure to test your application, you may use the input from the sample on the next page. Take a screenshot of your successful run.
User input in bold. -1-
Explanation / Answer
Please find my implementation:
import java.io.*;
import java.util.*;
public class DemoEntries{
public static void main(String[] args){
String[] days = new String[5];
Scanner sc = new Scanner(System.in);
String[] Entrees = new String[5];
double[] prices = new double[5];
String str;
days[0] = "Monday";
days[1] = "Tuesday";
days[2] = "Wednesday";
days[3] = "Thursday";
days[4] = "Friday";
for (int i = 0; i<5; i++){
System.out.println("Enter entrees for " + days[i] + " :");
Entrees[i] = sc.nextLine();
}
for (int i = 0; i<5; i++){
System.out.println("Enter price for " + Entrees[i] + " :");
prices[i] = sc.nextDouble();
}
str = sc.nextLine();
System.out.println("Enter the name of the :");
str = sc.nextLine();
int found = 0;
for (int i = 0; i<5; i++){
if (Entrees[i].equals(str)){
System.out.println("The entree will be served on " + days[i]);
found = 1;
break;
}
}
if (found == 0){
System.out.println("Entree not found");
}
double max = prices[0];
int index = 0;
for (int i = 0; i<5; i++){
if (prices[i] > max){
max = prices[i];
index = i;
}
}
System.out.println("The highest priced entree is " + Entrees[index]);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.