Write and submit the source code for the following program. The program will use an integer array of size 10 to store the prices of smartphones. It will then determine and print the prices of the most expensive and cheapest phones. Use the following variables:
int[] prices = new int[10]; // Array of smartphone prices
Assignment
Arrays.sort(prices);
Sample Output
Enter all smartphone prices: 1100 1080 900 1200 900 700 550 800 400 250
Three Most Expensive Phones
1200
1100
1080
Three Cheapest Phones
250
400
550
Process finished with exit code 0
import java.util.Arrays; import java.util.Scanner; public class SmartPhonePrices { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Enter all smartphone prices: "); String s = scan.nextLine(); String splits[] = s.split(" "); int arr[] = new int[splits.length]; for(int i = 0;i<splits.length;i++){ arr[i] = Integer.parseInt(splits[i]); } Arrays.sort(arr); System.out.println("Three Most Expensive Phones"); for(int i = 1;i<=3;i++){ System.out.println(arr[arr.length-i]); } System.out.println("Three Cheapest Phones"); for(int i = 0;i<3;i++){ System.out.println(arr[i]); } } }
Get Answers For Free
Most questions answered within 1 hours.