In: Computer Science
Write a program that sorts prices of 10 tacos in ascending order based on the price, using arrays.
Requirements:
Example Output
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
I WROTE THE CODE ALONG WITH THE COMMENTS
CODE:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner keyboard=new Scanner(System.in); //Scanner
class used to scan input.
//variables delcarion.
int i=0,j;
double temp1;
String temp2;
String names[]=new String[10];
double prices[]=new double[10];
//scanning input.
System.out.println("Welcome to the taco price sorter!
Enter 10 taco names and prices i will sort it!");
for(i=0;i<10;i++)
{
System.out.println("Enter the name of
taco "+(i+1));
names[i]=keyboard.nextLine();
System.out.println("Enter taco's
price");
prices[i]=keyboard.nextDouble();
keyboard.nextLine();
}
//bubble sort used to sort names and prices based on
prices of ascending order
for(i=0;i<10;i++)
{
for(j=0;j<10-i-1;j++)
{
if(prices[j]>prices[j+1]) //conditon for
swapping.
{
temp1=prices[j];
prices[j]=prices[j+1];
prices[j+1]=temp1;
temp2=names[j];
names[j]=names[j+1];
names[j+1]=temp2;
}
}
}
//print results.
System.out.println("Sorted Tacos are");
for(i=0;i<10;i++)
{
System.out.println("Taco Prices "+names[i]+"
"+prices[i]);
}
}
}
OUTPUT:
SCREENSHOT OF THE CODE: