Write a program that sorts prices of 10 tacos in ascending order based on the pr
ID: 669443 • Letter: W
Question
Write a program that sorts prices of 10 tacos in ascending order based on the price, using arrays.
Requirements:
The user enters the name of the taco and then the price of the taco
HINT: Two arrays make this problem simpler. One with the names and the other with the prices.
HINT: It is a good idea that after using keyboard.nextDouble() to write the following line: keyboard.nextLine();. There is a bug in the scanner where it will not consume everything the buffer unless you tell it to using nextLine. If you’re not sure what that means then don’t worry it will be covered eventually, but just no for now use that.
After 10 tacos are entered they are sorted based on the price
You can use any sorting method such as bubble sort or selection sort
Display the results at the end
Yes. You have to use arrays.
Example Dialog:
Welcome to the taco price sorter! Enter 10 taco names and prices and I'll sort it!
Enter the name of taco 1
Crunchy Taco
Enter taco's price
1.19
Enter the name of taco 2
Crunchy Taco Supreme
Enter taco's price
1.59
Enter the name of taco 3
Soft Taco
Enter taco's price
1.19
Enter the name of taco 4
Soft Taco Supreme
Enter taco's price
1.59
Enter the name of taco 5
Chicken Soft Taco
Enter taco's price
1.79
Enter the name of taco 6
Crispy Potato Soft Taco
Enter taco's price
0.99
Enter the name of taco 7
Double Decker Taco
Enter taco's price
1.89
Enter the name of taco 8
Double Decker Taco Supreme
Enter taco's price
2.29
Enter the name of taco 9
Doritos Locos Taco (Nacho Cheese)
Enter taco's price
1.49
Enter the name of taco 10
Doritos Locs Tacos(Fiery) Supreme
Enter taco's price
1.89
Sorted Tacos are
Taco Prices Crispy Potato Soft Taco 0.99
Taco Prices Crunchy Taco 1.19
Taco Prices Soft Taco 1.19
Taco Prices Doritos Locos Taco (Nacho Cheese) 1.49
Taco Prices Crunchy Taco Supreme 1.59
Taco Prices Soft Taco Supreme 1.59
Taco Prices Chicken Soft Taco 1.79
Taco Prices Double Decker Taco 1.89
Taco Prices Doritos Locs Tacos(Fiery) Supreme 1.89
Taco Prices Double Decker Taco Supreme 2.29
Explanation / Answer
package mani;
import java.util.Scanner;
public class tacos
{
public static void main(String[] args)
{
String[] name=new String[10];
double[] price=new double[10];
Scanner keyboard=new Scanner(System.in);
System.out.println("Welcome to the taco price sorter! Enter 10 taco names and prices and I'll sort it!");
for(int i=1;i<=10;i++){
System.out.println("Enter the name of taco "+i+": ");
name[i-1]=keyboard.nextLine();
System.out.println("Enter taco's price: ");
price[i-1]=keyboard.nextDouble();
keyboard.nextLine();
}
int n = 10;
int k;
double temp;
String tempN;
for (int m = n; m >= 0; m--) {
for (int i = 0; i < n - 1; i++) {
k = i + 1;
if (price[i] > price[k]) {
temp=price[i];
price[i]=price[k];
price[k]=temp;
tempN=name[i];
name[i]=name[k];
name[k]=tempN;
}
}
}
System.out.println("Sorted Tacos are: ");
for(int i=0;i<10;i++){
System.out.println("Taco Prices "+name[i]+" "+price[i]);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.