Question

In: Computer Science

Write a program that sorts prices of 10 tacos in ascending order based on the price,...

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. The indices indicate the combination. For instance, a taco price at index 5 has its name also at index 5 of the other array.
    • HINT: It is a good idea that after using keyboard.nextDouble() to write the following line: keyboard.nextLine();. The scanner will not consume everything in the buffer unless you tell it to using nextLine.
  • 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
  • Arrays must be part of the solution, and other built in Java data structures, such as ArrayLists, may not be used.
  • Sorting must be implemented (bubble sort, selection sort, etc.) in the solution, and other built in Java sorters may not be used.

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

Solutions

Expert Solution

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:


Related Solutions

Write a Java program that sorts an array of “Student” in an aescending order of their...
Write a Java program that sorts an array of “Student” in an aescending order of their “last names”. The program should be able to apply (Insertion sort): Student [] studs = new Student[8]; s[0] = new Student("Saoud", "Mohamed", 3.61); s[1] = new Student("Abdelkader", "Farouk", 2.83); s[2] = new Student("Beshr" , "Alsharqawy", 1.99); s[3] = new Student("Nader", "Salah", 3.02); s[4] = new Student("Basem", "Hawary", 2.65); s[5] = new Student("Abdullah", "Babaker", 2.88); s[6] = new Student("Abdelaal", "Khairy", 3.13); s[7] = new Student("Mohamedain",...
in JAVA, Project: Displaying Words in Ascending Alphabetical Order Write a program that reads words from...
in JAVA, Project: Displaying Words in Ascending Alphabetical Order Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. 1. You must use one of following Concrete class to store date from the input.txt file. Vector, Stack, ArrayList, LinkedList 2. To sort those words, you should use one of existing interface methods available in Collection class.
Create program which sorts letters of a string based on ASCII value. The program will then...
Create program which sorts letters of a string based on ASCII value. The program will then print the sorted string to stdout. Use C programming language. - Only use stdio.h - Input prompt should say "Enter string of your choice: " - Remove any newline \n from input string - Implement sorting operation as a function. Should use selection sort algorithm, but you may use a different algorithm - Output should print sorted string on new line Example:     Enter...
This phyton program takes in three integer parameters and displays them in ascending order. You can...
This phyton program takes in three integer parameters and displays them in ascending order. You can assume that the numbers entered are integers. You CAN'T use lists, min(), max(), or the sort utility. The point is to use if tests. sortThreeIntegers( 39, 2, 5 ) should display: The numbers in order are: 2 5 39 sortThreeIntegers( 14, -12, -1000 ) should display: The numbers in order are: -1000 -12 14 def sortThreeIntegers( x, y, z ): < your code goes...
In python write a program which prints the longest substring of numbers which occur in ascending...
In python write a program which prints the longest substring of numbers which occur in ascending order s=342153476757561235
Write a program that will ask the user for three words (strings). Re-arranges them in ascending...
Write a program that will ask the user for three words (strings). Re-arranges them in ascending order by using a function (it should return void). Global variables are forbidden. Hint: You don't need to exchange the values among variables. You can just print them in the correct order. (C++ Please) Example of Output: Word 1: apple Word 2: orange Word 3: strawberry -------- orange apple strawberry
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers...
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers by repeatedly calling a “swap” subroutine. The original unsorted list of integers should be received from the keyboard input. Your program should first prompt the user “Please input an integer for the number of elements:”. After the user enters a number and return, your program outputs message “Now input each element and then a return:”. For example, if the user enters 5 as the...
Write a function that receives a StaticArray and sorts its content in non-descending order. Sorting must...
Write a function that receives a StaticArray and sorts its content in non-descending order. Sorting must be done ‘in place’, meaning the original input array will be modified. You may assume that the input array will contain at least one element and that values stored in the array are all of the same type (either all numbers, or strings, or custom objects, but never a mix of those). You do not need to write checks for these conditions. You can...
Write a program in C that sorts 3 given numbers, defined as (a,b,c) from highest to...
Write a program in C that sorts 3 given numbers, defined as (a,b,c) from highest to lowest. I already have this code finding the maximum. Need to just add the sorting code onto it. int A, B, C; A = 4; B=3; C=7; Max = A If (B > max) { Max = B; } Else if (c > max) { Max = c; }
c++ Write a program that displays the status of an order. a) Program uses 2 functions...
c++ Write a program that displays the status of an order. a) Program uses 2 functions (in addition to main ()). b) The first function asks the user for the data below and stores the input values in reference parameters. c) Input data from user: # of spools ordered, # of spools in stock, any special shipping & handling charges over and above the $10 rate. d) The second function receives as arguments any values needed to compute and display...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT