221k views
5 votes
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).

User Sparkofska
by
4.9k points

1 Answer

4 votes

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

User Ken You
by
5.7k points