1. Member
Join Date
Dec 2009
Posts
22
Rep Power
0

## A few questions about arrays and loops

I have a few questions about what to do with the following code. The assignment is as follows.

First Part
4. The user should be prompted to choose whether to display the data in Fahrenheit and inches or in Celsius and centimeters.
5. Calculate the average temperature and the total precipitation in Fahrenheit and inches, respectively. (In the next lesson you will add the code to convert temperature to Celsius and precipitation to centimeters.)
6. Display the data as neatly as possible in two columns (see the expected output below).Do not spend too much time fussing with column alignment, because you will learn how to precisely format Strings and numbers in lesson 06.02. (If misaligned values are starting to annoy you, feel free to use the printf() method in this
assignment.)
7. When you complete this assignment and turn it in for a grade in the next lesson, your instructor will paste in data from a different location to test your program. You should also test the program with an alternate set of data, as a precaution. Leave both sets in the source code, but comment out one location.

Second Part
4. Convert the temperature to Celsius and the precipitation to centimeters. Remember, the menu gives you the option to display the results in either set of units.
5. Display the data in both columns to one decimal place.
6. Calculate the average of each column.
7. When you complete this assignment and turn it in for a grade, your instructor will paste in data from a different location to test your program. So, you should also test the program with an alternate set of data as a precaution. Leave both sets in the source code, but comment out one location.

So basically, the few questions I have are:
1) How do I take the user's input after the questions "Temperature?" and "Rain?" and compare the first letter of them and do a loop based on the first letter.
2) Also, how do I ignore the case of that letter that they input.
3) How would you guys suggest doing the loops (4 loops? something simpler)?
4) How do you change each value in the arrays? Like how would I take each value in the Fahrenheit array and multiply it by 5/9 to get celcius without doing * 5/9 on every single value?

Thank you all very much!

You can just answer 1 question if answering everything at once is too time-consuming. Any help is appreciated :)

/* Jamison Hyman
* 12/21/09
* This program displays the average temperature and the annual
precipitation for a selected city.
*/

import java.util.Scanner;
import java.io.PrintStream;
import java.lang.String;

public class AnnualClimate1 {

public static void main(String[] args)

{
Scanner in = new Scanner(System.in);

String userDecideTemp;
String userDecidePrecip;

System.out.print("Temperature (F or C?): ");
userDecideTemp = in.next();
System.out.print("Rainfall (I or C?): ");
userDecidePrecip = in.next();

String faren = ("f");
String celc = ("c");
String cm = ("c");

String climate = ("Climate Data");
String location = ("Location: Tallahasee, Florida");
String month = ("Month");
String temperature = ("Temperature");
String precipitation = ("Precipitation");
String f = ("(F)");
String c = ("(C)");
String i = ("(In)");
String cmc = ("(Cm)");
String userF = ("f");

System.out.printf("%25s\n",climate);
System.out.printf("%34s\n\n",location);
System.out.printf("%s%19s%s%16s%s\n",month,tempera ture,f,precipitation,i);
System.out.println("****************************** *****************");

String [] months = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug"," Sep",
"Oct","Nov","Dec"};

double [] temp = {51.8,54.8,61.1,66.4,74.4,80.4,82.4,82.1,78.9,69.1 ,60.4,53.7};

double sumTemp = (temp[0] + temp[1] + temp[2] + temp[3] + temp[4] + temp[5] + temp[6]
+ temp[7] + temp[8] + temp[9] + temp[10] + temp[11]);

double [] precip = {5.4, 4.6, 6.5, 3.6, 5.0, 6.9, 8.0, 7.0, 5.0, 3.3, 3.9, 4.1};

double sumPrecip = (precip[0] + precip[1] + precip[2] + precip[3] + precip[4] +
precip[5] + precip[6] + precip[7] + precip[8] + precip[9] + precip[10] + precip[11]);

String annual = ("Annual:");

System.out.printf("%s%14s%16s\n", months[0],temp[0],precip[0]);
System.out.printf("%s%14s%16s\n", months[1],temp[1],precip[1]);
System.out.printf("%s%14s%16s\n", months[2],temp[2],precip[2]);
System.out.printf("%s%14s%16s\n", months[3],temp[3],precip[3]);
System.out.printf("%s%14s%16s\n", months[4],temp[4],precip[4]);
System.out.printf("%s%14s%16s\n", months[5],temp[5],precip[5]);
System.out.printf("%s%14s%16s\n", months[6],temp[6],precip[6]);
System.out.printf("%s%14s%16s\n", months[7],temp[7],precip[7]);
System.out.printf("%s%14s%16s\n", months[8],temp[8],precip[8]);
System.out.printf("%s%14s%16s\n", months[9],temp[9],precip[9]);
System.out.printf("%s%14s%16s\n", months[10],temp[10],precip[10]);
System.out.printf("%s%14s%16s\n", months[11],temp[11],precip[11]);
System.out.println("****************************** *****************");
System.out.print("Average: ");
System.out.printf("%8.1f",sumTemp/12);
System.out.printf("%12s",annual);
System.out.printf("%5.1f\n",sumPrecip);

}
}

2. So basically, the few questions I have are:
for 1), try Scanner to read a String, then use substring(0,1) ?

for 2), see equalsIgnoreCase() in String class

for 3), maybe set a boolean variable 'done' and while not done,
Java Code:
```boolean done = false;
while (! done) {
// determine what was input
// if (thing input == 'Q' ) {
//   done = false;
//   continue;  // or break, i guess
//
// process something
}```
in that sense, i guess you could even loop while(true)

for 4), if have an array of values and want to perform the same multiplication or division operation on each element, you have to loop over the array
Java Code:
```  double[] inputs = new double[] { 1.0, 2.0, 2.5, 2.3, 1.6 };

double[] outputs = new double[inputs.length];

double multiplier = (double)5/9;

for (int i = 0; i < inputs.length; i++) {
outputs[i] = inputs[i] * multiplier;
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•