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",...
***C++ Coding*** Write a program for sorting a list of integers in ascending order using the...
***C++ Coding*** Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Please include comments to understand code. Requirements Implement the following functions: int readData( int **arr) arr is a pointer to pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array arr. The first integer number in the file is the number of intergers....
C++ Write a program for sorting a list of integers in ascending order using the bubble...
C++ Write a program for sorting a list of integers in ascending order using the bubble sort algorithm Requirements Implement the following functions: Implement a function called readData int readData( int **arr) arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array arr. The first integer number in the file is the number of intergers. After the first number,...
1a .Write a program that perform insertion sort (ascending order) on an input array and print...
1a .Write a program that perform insertion sort (ascending order) on an input array and print the total number of comparisons that have been made. You can implement by using any programming languages such as C/C++. For example, in the case of array B = [ 30 , 10 , 20 , 40 ], there are 4 needed comparisons to perform insertion sort (30 vs 10, 20 vs 30, 20 vs 10, and 40 vs 30). 1b. Write a program...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT