This needs to be coded in Java. Using nothing more complex than methods/loops/ar
ID: 3590172 • Letter: T
Question
This needs to be coded in Java. Using nothing more complex than methods/loops/arrays.
Instructions You work for a paint shop and have been tasked with writing a program to calculate the cost of paint purchased. Prompt the user for the number of paint orders. Do not accept 0 or less for the number of paint orders. Prompt the user for the number of gallons of paint purchased for each order. Each gallon of paint costs $19.99. Do not accept 0 or less for the number of gallons .Calculate the cost of each order. Store the cost of each order in an array. Display the price of each order . Use a Selection sort algorithm to sort the order prices in the array from smallest to largest Display the sorted orders. Display the total price of all orders. Note: You will not read a file in this project. All code should be coded in the Main method Sample Input Enter the number of paint orders to process: 5 Enter the number of gallons for order 1: 3 Enter the number of gallons for order 2:7 Enter the number of gallons for order 3: 4 Enter the number of gallons for order 4: 2 Enter the number of gallons for order 51 Sample Output Smallest to largest orders: $19.99 $39.98 $59.97 $79.96 $139.93 The total cost of all paint orders is: $339.83Explanation / Answer
PaintOrders.java
import java.util.Scanner;
public class PaintOrders {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number of paint orders to process: ");
int n = scan.nextInt();
int orders[] = new int[n];
double prices[] = new double[n];
for(int i=0;i<n;i++) {
System.out.println("Enter the number of gallons for order "+(i+1)+": ");
orders[i] = scan.nextInt();
if(orders[i] <= 0) {
System.out.print("Invalid Input.");
i--;
}
}
for(int i=0;i<n;i++) {
prices[i] = orders[i] * 19.99;
}
selectionSort(prices);
double total = 0;
System.out.println("Small to Large Orders: ");
for(int i=0;i<n;i++) {
total += prices[i];
System.out.printf("$%.2f ",prices[i]);
}
System.out.printf("The total cost of all paint orders is: $%.2f ",total);
}
public static void selectionSort(double prices[]){
for (int i = 0; i < prices.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < prices.length; j++)
if (prices[j] < prices[index])
index = j;
double smallerNumber = prices[index];
prices[index] = prices[i];
prices[i] = smallerNumber;
}
}
}
Output:
Enter the number of paint orders to process:
5
Enter the number of gallons for order 1:
3
Enter the number of gallons for order 2:
7
Enter the number of gallons for order 3:
4
Enter the number of gallons for order 4:
2
Enter the number of gallons for order 5:
1
Small to Large Orders:
$19.99
$39.98
$59.97
$79.96
$139.93
The total cost of all paint orders is: $339.83
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.