Advertisement
tauk

Items Prices and Arrays with functions using globa variables

Jun 25th, 2020
1,092
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. public class ItemNamesPrices3 {
  2.     public static String[] itemNames = {"DHT11", "DHT22", "HC711", "Arduino", "LDR"};
  3.     public static double[] itemPrices = {11,22 ,117, 50, 10};
  4.        
  5.     public static void main(String args[]) {
  6.         printItems();
  7.         printItemsAveragePrice();
  8.         printItemHighestPrice();
  9.         printItemLowestPrice();
  10.     }
  11.    
  12.     public static void printItems() {
  13.         for (int i = 0; i< itemNames.length; i++) {
  14.             System.out.println(itemNames[i] + " Price is AED: " + itemPrices[i]);
  15.         }
  16.     }
  17.    
  18.     public static void printItemsAveragePrice() {
  19.         double total = 0;
  20.         for (double price : itemPrices) {
  21.             total = total + price;
  22.         }
  23.        
  24.         double average = total / itemPrices.length;
  25.         System.out.println("Average Item Price:" + average);
  26.     }
  27.    
  28.     public static void printItemHighestPrice() {
  29.         int highestPriceIndex = -1;
  30.         double highestPrice = itemPrices[0]; //assume the first price is the highest
  31.         for (int i = 0 ; i < itemPrices.length; i++ ) {
  32.             //for every itemPrices[i]
  33.             if (itemPrices[i] > highestPrice) {
  34.                 //replace the highestPrice with the itemPrices[i]
  35.                 highestPrice = itemPrices[i];
  36.                 //update highestPriceIndex to i
  37.                 highestPriceIndex = i;
  38.             }
  39.         }
  40.        
  41.         //using the highest price index print the itemName and item price
  42.         System.out.println(itemNames[highestPriceIndex] + " AED:"
  43.                                     + itemPrices[highestPriceIndex]);
  44.     }
  45.    
  46.     public static void printItemLowestPrice() {
  47.         int lowestPriceIndex = 0;
  48.         double lowestPrice = itemPrices[0]; //assume the first price is the lowest
  49.         for (int i = 0 ; i < itemPrices.length; i++ ) {
  50.             //for every itemPrices[i]
  51.             if (itemPrices[i] < lowestPrice) {
  52.                 //replace the lowestPrice with the itemPrices[i]
  53.                 lowestPrice = itemPrices[i];
  54.                 //update lowestPriceIndex to i
  55.                 lowestPriceIndex = i;
  56.             }
  57.         }
  58.        
  59.         //using the lowest price index print the itemName and item price
  60.         System.out.println(itemNames[lowestPriceIndex] + " AED:"
  61.                                     + itemPrices[lowestPriceIndex]);
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement