Tomorrow is the Earth Day, the day when people all over the Earth join in activities that help us preserve the environment. The students of an unnamed department of an unnamed university decided to join by taking care of the cactuses and other plants in front of the departmental building and on the nearby hill.
1-2. Volunteer students are asked what activity they want to join on the Earth Day. Depending on their answers, they are given labels: F corresponds to plants in Front of the building, H for plants on the Hill, and B stands for Both.
1. Use switch to write down a code that, based on the letter, prints out the appropriate task:
switch(letter){
case `F': System.out.println("Taking care of the plants " +
"in front of the building");
break;
case `H': System.out.println("Taking care of the plants " +
"on the hill");
break;
case `B': System.out.println("Taking care of the plants " +
"in both locations");
break;
default: Systems.out.println("Invalid entry");
}
2. Write the code that does the same but uses
if instead of switch.
Explain the comparative advantages and disadvantages of using
if and switch. For extra credit: explain
comparative advantages and disadvantages of using ?:
if (letter == `F')
System.out.println("Taking care of the plants " +
"in front of the building");
else if (letter == `H')
System.out.println("Taking care of the plants " +
"on the hill");
else if (letter == `H')
System.out.println("Taking care of the plants " +
"in both locations");
else
Systems.out.println("Invalid entry");
Switch:
* advantage: it can handle cases when there are more than two options
* disadvantage: it is difficult to use for complex nested conditions
If:
* advantage: can handle complex nested conditions easily
* disadvantage: if we have more than two options, using if is
cumbersome
?:
* advantage: faster to write, takes less space
* disadvantage: not as readable as if
3-5. As students register for the Earth Day activities,
each student types in his or
her name and the corresponding letter (F, H, or B). In the resulting
file:
3. Write a code for filling in the file; use "that's all folks" as the sentinel.
PrintWriter outFile = new PrintWriter(new FileWriter("turkey.dat"));
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String firstName="";
String lastName;
String name;
String letter;
while (!firstName.equals("that's all folks")){
System.out.println("Please enter your first name");
firstName = reader.readLine();
System.out.println("Please enter your last name");
lastName = reader.readLine();
System.out.println("Please enter a letter describing "+
"your participation in the Earth Day");
letter = reader.readLine();
if (!firstName.equals("that's all folks")){
outFile.println(firstName);
outFile.println(lastName);
outFile.println(letter);
}
}
4. Wtite a code that transfers this information from the file into
three arrays: array of first names, array of last names (String
arrays), and array of letters (character array).
BufferedReader inFile = new BufferedReader(new FileReader("turkey.dat"));
String[] firstName = new String[100];
String[] lastName = new String[100];
char[] letter = new char[100];
String line;
int i = 0;
line = inFile.readLine();
while (line != null){
firstName[i] = line;
lastName[i] = inFile.readLine();
letter[i] = inFile.readline().charAt(0);
line = inFile.readline();
i++;
}
inFile(close);
5. After the
arrays are filled, use the for loop to compute the following
three numbers:
int front = 0;
int hill = 0;
int both = 0;
for (i = 0; i < letter.length; i++){
if (letter[i] == `F')
front++;
else if (letter[i] == `H')
hill++;
else if (letter[i] == `B')
{front++; hill++; both++;}
}
6-7. In addition to taking care of the plants, the local chapter for
the Sierra Club also decided to collect donations to take care of the
plants in the Amazon River Rainforest. The
information about the donations is stored in the array
amazon[], so that amazon[0] is the amount donated by
the first person, amazon[1] is the amount donated by the
second person, etc. Let members be the total number of
persons who donates money for this cause.
Write down a method that computes the average
donation; use += and for loop. Your code should catch the
exception corresponding to division by 0.
Trace your method on the example when only two folks donate: a
professor donates 100 dollars and a student donates 1 dollar.
public static double average(int[] amazon, int members){
try{
double sum = 0.0;
for(int i = 0; i < members; i++)
sum += amazon[i];
if(members == 0)
throw new ArithmeticException();
else
return sum/members;
}
catch(ArithmeticException ex){
System.out.println("You entered 0 contributors, " +
"so we cannot compute the average");
return 0;
}
}
Trace: e.g., call it in the main program with
int[] donations = {100,1};
double averageDonation = average(donations,2);