Can someone help me turn this into an array below. And redo the display for the
ID: 3904746 • Letter: C
Question
Can someone help me turn this into an array below. And redo the display for the output below as well.
I don't want to use ArrayList. AREAS IN BOLD IS WHERE I DONT WANT TO USE ARRAYLIST
Here is the 2 classes.
import java.io.*;
import java.util.*;
class RainFall{
private double[] data;
public RainFall (double[] array){
data = new double[array.length];
for (int i = 0; i data[i] = array[i];
}
public double totalRainFall(){
double sum = 0;
for (int i = 0; i sum = sum + data[i];
return sum;
}
public double average(){
double sum = 0;
for (int i = 0; i sum = sum + data[i];
return sum/data.length;
}
public int mostRain(){
double max = 0;
int index = 0;
for (int i = 0; i if (data[i] > max){
max = data[i];
index = i;
}
return index;
}
public int leastRain(){
double min = 100;
int index = 0;
for (int i = 0; i if (data[i] < min){
min = data[i];
index = i;
}
return index;
}
public ArrayList monthsHigherThanAverage(){
ArrayList list = new ArrayList();
double avg = average();
for (int i = 0; i if (data[i] > avg){
list.add(i);
}
}
return list;
}
double getValue(int a){
return data[a];
}
String getName(int a){
String res = "";
switch(a){
case 1 : res = "January";
break;
case 2 : res = "February";
break;
case 3 : res = "March";
break;
case 4 : res = "April";
break;
case 5 : res = "May";
break;
case 6 : res = "June";
break;
case 7 : res = "July";
break;
case 8 : res = "August";
break;
case 9 : res = "September";
break;
case 10 : res = "October";
break;
case 11 : res = "November";
break;
case 12 : res = "December";
break;
}
return res;
}
}
public class DemoRainFall{
public static void main(String[] arg){
double[] data = {1.6, 2.1, 1.7, 3.5, 2.6, 3.7, 3.9, 2.6, 2.9, 4.3, 2.4, 3.7};
double[] data1 = new double[12];
try {
FileInputStream fin = new FileInputStream("input.txt");
Scanner sc = new Scanner(fin);
for (int i = 0; i<12; i++){
data1[i] = sc.nextDouble();
}
}
catch(Exception e){
e.printStackTrace();
}
RainFall r1 = new RainFall(data);
RainFall r2 = new RainFall(data1);
System.out.printf("The total rainfall for this year : %.1f ",r1.totalRainFall());
System.out.printf("The average rainfall for this year : %.1f ",r1.average());
System.out.printf("The average rainfall for this year : %.1f ",r1.average());
System.out.printf("The month with the highest amount of rain is %s with %.1f inches ", r1.getName(r1.mostRain()), r1.getValue(r1.mostRain()));
ArrayList num = r1.monthsHigherThanAverage();
System.out.println("There are " + num.size() + " months whose rainfall is higher than average:");
for (int i = 0; i System.out.printf("%s %.1f ",r1.getName(num.get(i)), r1.getValue(num.get(i)));
}
System.out.printf("The total rainfall for this year : %.1f ",r2.totalRainFall());
System.out.printf("The average rainfall for this year : %.1f ",r2.average());
System.out.printf("The average rainfall for this year : %.1f ",r2.average());
System.out.printf("The month with the highest amount of rain is %s with %.1f inches ", r2.getName(r2.mostRain()), r2.getValue(r2.mostRain()));
num = r2.monthsHigherThanAverage();
System.out.println("There are " + num.size() + " months whose rainfall is higher than average:");
for (int i = 0; i System.out.printf("%s %.1f ",r2.getName(num.get(i)), r2.getValue(num.get(i)));
}
}
}
Explanation / Answer
class RainFall{ private double[] data; public RainFall (double[] array){ data = new double[array.length]; for (int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.