Fruit Salad

Modules - Structured Programming 1, Structured Programming 2, Procedural Programming 1, Data Structures 1

 

Teacher’s Notes:

=======================================

This assignment can be done in a lot of ways - I would introduce this as a basic assignment and then demonstrate how using methods/functions would make doing this a lot nicer.  You could also do this using an array as you will see in my second solution.

 
You could also talk about a situation where you might have 100 fruits and needed to choose 20 of them.  This solution does not scale well.  Instead, we might make an array of the selected fruits and check if the selected fruit is already in the list before we add it.
 

Assignment:

=======================================

Fruit Salad

Jenny operates a daycare and wants to make a healthy snack for the children under her care.  She decides to make a fruit salad out of three different fruits but doesn't want to make it using the same fruits every week or the children will get bored of the meal.  She has access to 10 different fruits: Apple, Orange, Pear, Watermelon, Pineapple, Kiwi, Grapes, Strawberry, Blueberry, and Banana.  Write a program that will select three of these fruits at random to make the fruit salad with.  The program will have an equal chance to choose any fruit and must NEVER choose the same fruit twice.  There is no keyboard input from the user in this program.

 

Sample Output 1:

Fruit Salad:
============
Grapes
Banana
Apple

 

Sample Output 2:

Fruit Salad:
============
Grapes
Pear
Orange
 

Solution (JAVA using Methods):

=======================================

public class HelloWorld {
    public static String randomFruit() {
        int randomNumber = (int) (Math.random() * 10 + 1);
        if (randomNumber == 1)
            return "Apple";
        else if (randomNumber == 2)
            return "Orange";
        else if (randomNumber == 3)
            return "Pear";
        else if (randomNumber == 4)
            return "Watermelon";
        else if (randomNumber == 5)
            return "Pineapple";
        else if (randomNumber == 6)
            return "Kiwi";
        else if (randomNumber == 7)
            return "Grapes";
        else if (randomNumber == 8)
            return "Strawberry";
        else if (randomNumber == 9)
            return "Blueberry";
        else
            return "Banana";
    }
 
    public static void main(String[] args) {
        String choice1 = randomFruit();
        String choice2 = "default";
        String choice3 = "default";
        do {
            choice2 = randomFruit();
        } while (choice2.equals(choice1));
        
        do {
            choice3 = randomFruit();
        } while (choice3.equals(choice1) || choice3.equals(choice2));
        System.out.println("Fruit Salad:");
        System.out.println("============");
        System.out.println(choice1);
        System.out.println(choice2);
        System.out.println(choice3);
    }
}
 

 

 

 

Solution (JAVA Using Arrays):

=======================================

public class FruitSalad {

public static void main(String[] args) {

String[] fruit = {"Apple","Orange","Pear","Watermelon","Pineapple","Kiwi","Grapes","Strawberry","Blueberry","Banana"};

int choice1 = (int)(Math.random()*10);
int choice2 = 0;
int choice3 = 0;

do {
choice2=(int)(Math.random()*10);
}while(choice2==choice1);

do {
choice3=(int)(Math.random()*10);
}while(choice3 == choice1 || choice3 == choice2);

System.out.println("Fruit Salad:");
System.out.println("============");
System.out.println(fruit[choice1]);
System.out.println(fruit[choice2]);
System.out.println(fruit[choice3]);

}

}