Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ItemNamesPrices3 {
- public static String[] itemNames = {"DHT11", "DHT22", "HC711", "Arduino", "LDR"};
- public static double[] itemPrices = {11,22 ,117, 50, 10};
- public static void main(String args[]) {
- printItems();
- printItemsAveragePrice();
- printItemHighestPrice();
- printItemLowestPrice();
- }
- public static void printItems() {
- for (int i = 0; i< itemNames.length; i++) {
- System.out.println(itemNames[i] + " Price is AED: " + itemPrices[i]);
- }
- }
- public static void printItemsAveragePrice() {
- double total = 0;
- for (double price : itemPrices) {
- total = total + price;
- }
- double average = total / itemPrices.length;
- System.out.println("Average Item Price:" + average);
- }
- public static void printItemHighestPrice() {
- int highestPriceIndex = -1;
- double highestPrice = itemPrices[0]; //assume the first price is the highest
- for (int i = 0 ; i < itemPrices.length; i++ ) {
- //for every itemPrices[i]
- if (itemPrices[i] > highestPrice) {
- //replace the highestPrice with the itemPrices[i]
- highestPrice = itemPrices[i];
- //update highestPriceIndex to i
- highestPriceIndex = i;
- }
- }
- //using the highest price index print the itemName and item price
- System.out.println(itemNames[highestPriceIndex] + " AED:"
- + itemPrices[highestPriceIndex]);
- }
- public static void printItemLowestPrice() {
- int lowestPriceIndex = 0;
- double lowestPrice = itemPrices[0]; //assume the first price is the lowest
- for (int i = 0 ; i < itemPrices.length; i++ ) {
- //for every itemPrices[i]
- if (itemPrices[i] < lowestPrice) {
- //replace the lowestPrice with the itemPrices[i]
- lowestPrice = itemPrices[i];
- //update lowestPriceIndex to i
- lowestPriceIndex = i;
- }
- }
- //using the lowest price index print the itemName and item price
- System.out.println(itemNames[lowestPriceIndex] + " AED:"
- + itemPrices[lowestPriceIndex]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement