I need help with the JAVA assignment. Supposedly it needs to be done with nested
ID: 3756783 • Letter: I
Question
I need help with the JAVA assignment. Supposedly it needs to be done with nested if statements not loops. I have no idea where to start.
Imagine you have pet gerbils that like to descend into holes in the ground and acquire precious metal nuggets. A gerbil can only carry 10 ounces. The precious metals are ordered in descending value (Rhodium is the highest and Iron is the lowest) here:
Rhodium
Platinum
Gold
Iron
You want to train your gerbils such that, when sent into a hole, the gerbils return with the most valuable haul. For example, if the hole contains 5 ounces of Rhodium, 6 of Platinum, 4 of Gold, 7 of Iron, your gerbil should return with 5 ounces of Rhodium and 5 of Platinum.
1. Create a project called Project2. Within this project, create a class called GreedyGerbils. All of your code should be inside your main function in this one class.
2. Your program should do the following steps:
a. Ask the user for total amounts of all 4 metals contained in the hole.
b. Decide how many ounces of each metal the gerbil needs to take to have the most valuable haul. Remember, a gerbil can only carry 10 ounces.
c. Print how many ounces of each metal the gerbil will take.
Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
Gerbils.java
-------------
import java.util.Scanner;
public class Gerbils {
public static void main(String[] args) {
int maxOunce;
int rhodium, platinum, gold, iron;
int rhoCollected = 0, platCollected = 0, goldCollected = 0, ironCollected = 0;
int toCollect = 0;
Scanner keyboard = new Scanner(System.in);
System.out.print("What is the maximum ounces gerbil can carry? ");
maxOunce = keyboard.nextInt();
System.out.println("Enter the ounces availabe for each metal");
System.out.print("Rhodium ? ");
rhodium = keyboard.nextInt();
System.out.print("Platinum ? ");
platinum = keyboard.nextInt();
System.out.print("Gold ? ");
gold = keyboard.nextInt();
System.out.print("Iron ? ");
iron = keyboard.nextInt();
toCollect = maxOunce;
if(toCollect > 0){
if(toCollect <= rhodium){
rhoCollected = toCollect;
}
else{
rhoCollected = rhodium;
}
toCollect -= rhoCollected;
}
if(toCollect > 0){
if(toCollect <= platinum){
platCollected = toCollect;
}
else{
platCollected = platinum;
}
toCollect -= platCollected;
}
if(toCollect > 0){
if(toCollect <= gold){
goldCollected = toCollect;
}
else{
goldCollected = gold;
}
toCollect -= goldCollected;
}
if(toCollect > 0){
if(toCollect <= iron){
ironCollected = toCollect;
}
else{
ironCollected = iron;
}
toCollect -= ironCollected;
}
System.out.println("The different metals collected by the gerbil are");
System.out.println("Rhodium: " + rhoCollected + " ounces");
System.out.println("Platinum: " + platCollected + " ounces");
System.out.println("Gold: " + goldCollected + " ounces");
System.out.println("Iron: " + ironCollected + " ounces");
}
}
output
------
What is the maximum ounces gerbil can carry? 10
Enter the ounces availabe for each metal
Rhodium ? 5
Platinum ? 6
Gold ? 4
Iron ? 7
The different metals collected by the gerbil are
Rhodium: 5 ounces
Platinum: 5 ounces
Gold: 0 ounces
Iron: 0 ounces