Ask Question
11 December, 19:58

Write a method that finds the cheapest name of apples, and returns back a String stating what the apple name is and how much it costs. The method should take two arrays as two parameters (you do not need to make a two dimensional array out of these arrays).

+1
Answers (1)
  1. 11 December, 23:47
    0
    The question is incomplete! Complete question along with answer and step by step explanation is provided below.

    Question:

    Two arrays are given:

    String apples[] = {"HoneyCrisp", "DeliciousRed", "Gala", "McIntosh", "GrannySmith"};

    double prices[] = {4.50, 3.10, 2.45, 1.50, 1.20};

    Write a method that finds the cheapest name of apples, and returns back a String stating what the apple name is and how much it costs. The method should take two arrays as two parameters (you do not need to make a two dimensional array out of these arrays).

    Answer:

    The complete java code along with the output is prrovided below.

    Java Code:

    public class ApplesCheap

    {

    public static String Cheapest (String apples[], double prices[])

    {

    int temp = 0;

    for (int i = 0; i < apples. length; i++)

    {

    if (prices[i] < prices[temp])

    {

    temp = i;

    }

    }

    return apples[temp];

    }

    public static void main (String[] args)

    {

    / / define the array of apple names and their prices

    String apples[] = {"HoneyCrisp", "DeliciousRed", "Gala", "McIntosh", "GrannySmith"};

    double prices[] = {4.50, 3.10, 2.45, 1.50, 1.20};

    / / print the cheapest apple by calling the function Cheapest

    System. out. println ("The cheapest apple is: " + Cheapest (apples, prices));

    }

    }

    Output:

    The cheapest apple is: GrannySmith
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a method that finds the cheapest name of apples, and returns back a String stating what the apple name is and how much it costs. The ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers