Ask Question
1 September, 04:32

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).

+2
Answers (1)
  1. 1 September, 05:39
    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?
Get 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 & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers