Question

In: Computer Science

20.1: Checking on the Drought [(JAVA, ECLIPSE), Please keep the program as short as possible] The...

20.1: Checking on the Drought

[(JAVA, ECLIPSE), Please keep the program as short as possible]

  • The California drought is a topic that is weighing heavily on the minds of many people in this state.
  • With all the rain we have gotten this year, we are all hoping the drought is over
  • Let's examine some rainfall data for Silicon Valley to get a sense of how this year's rainfall compares to the average rainfall.
  • For this assignment, you will be practicing arrays and File I/O.
  • First, create a new file called average_rain.txt.
  • Then, copy and paste the following data into your file. Make sure that each value is on its own line and that there are no blank lines between values.

2.99
3.31
2.05
1.06
0.39
0.08
0.08
0.08
0.24
0.79
1.89
2.13

  • This data represents the average monthly rainfall for San Jose as measured between 1961-1990.
  • Note that each data point is the total rainfall for one of the 12 months of the year. The first data point represents the average rainfall in January and the last data point represents the average rainfall in December.
  • Save your file and then create a new text file called rain2018_19.txt.
  • Copy and paste the following data into this file. Again make sure that each data point is on its own line and that there are no blank lines between data points.

2.89
5.61
3.20
0.00
0.00
0.00
0.00
0.00
0.00
0.00
1.92
1.53

  • This data represents the rainfall in San Jose from March 2018-February 2019, with the first number representing the rainfall in January 2018 and the last number representing the rainfall in December 2018
  • Note that each data point is the average rainfall for one of the 12 months of the year.
  • The first data point represents the average rainfall in January, and the last data point represents the average rainfall in December.
  • Now, open Eclipse and create a new Java source file named Rainfall.java.
  • The first goal of our program is to read the input data from our two files and store the data in two separate arrays - one array for the 2018-19 rainfall data and one array for the average rainfall data.
  • What size should you make these arrays? Hint: How many months are there in the year?
  • After you have declared your arrays, you need to open each file and, using a loop, write the data into the slots of your array.
  • What kind of loop do you need here? Hint: Do you know in advance how many data points are in each file? Try a for loop.
  • You will need to open two different files and write the contents into two different arrays.
  • Therefore, you will need to close the stream for one file before you open the other file, like so:

input.close();

input = new Scanner(averageFile);

  • Your program should write the results to a file in the form of a table that looks like this:

Rainfall in San Jose: A Comparison

Average        2018-9        Deficit
2.99        2.89        0.10
3.31        5.61        -2.30
2.05        3.2        -1.15
1.06        0.0        1.06
0.39        0.0        0.39
0.08        0.0        0.08
0.08        0.0        0.08
0.08        0.0        0.08
0.24        0.0        0.24
0.79        0.0        0.79
1.89        1.92        -0.03
2.13        1.53        0.60

Total deficit: -0.06

  • To do so, you will need another loop to write the contents of your arrays to the file. Create another textfile called rain_comparison.txt and write the above data to the file.
  • Note that you will need to adjust the number of tabs until your data is lined up properly as in the image above.
  • Note that you should also print the total deficit below the table, as shown above. Hint: keep a running sum of the deficit inside your final for loop

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// average_rain.txt

2.99
3.31
2.05
1.06
0.39
0.08
0.08
0.08
0.24
0.79
1.89
2.13

_________________

// rain2018_19.txt

2.89
5.61
3.20
0.00
0.00
0.00
0.00
0.00
0.00
0.00
1.92
1.53

__________________

// Rainfall.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Rainfall {

   public static void main(String[] args) {
       //Declaring one Dimensional Array
       double averageRain[]=new double[12];
       double rain2018_19[]=new double[12];
       //Declaring variables
       double totalDeficit=0,def=0;
       Scanner sc=null;
try {
   //Opening the file and read the data and populate the values into an array
           sc=new Scanner(new File("average_rain.txt"));
           for(int i=0;i<averageRain.length;i++)
           {
               averageRain[i]=sc.nextDouble();
           }
           sc.close();
           sc=new Scanner(new File("rain2018_19.txt"));
           for(int i=0;i<rain2018_19.length;i++)
           {
               rain2018_19[i]=sc.nextDouble();
           }
           sc.close();
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       }
//Displaying the Results
System.out.println("Average\t2018-9\t\tDeficit");
for(int i=0;i<averageRain.length;i++)
{
   def=averageRain[i]-rain2018_19[i];
   System.out.printf("%.2f\t\t%.2f\t\t%.2f\n",averageRain[i],rain2018_19[i],def);
   totalDeficit+=def;
}
//Displaying the output
System.out.printf("Total deficit:%.2f",totalDeficit);

   }

}
______________________

Output:

Average   2018-9       Deficit
2.99       2.89       0.10
3.31       5.61       -2.30
2.05       3.20       -1.15
1.06       0.00       1.06
0.39       0.00       0.39
0.08       0.00       0.08
0.08       0.00       0.08
0.08       0.00       0.08
0.24       0.00       0.24
0.79       0.00       0.79
1.89       1.92       -0.03
2.13       1.53       0.60
Total deficit:-0.06

_______________Could you plz rate me well.Thank You


Related Solutions

(Keep the response as short as possible please) What is a liquidated damages clause in a...
(Keep the response as short as possible please) What is a liquidated damages clause in a contract?     a) Is it a good thing?     b) Why might the clause not be enforced by the courts? What is specific performance? In which situation would specific performance more likely be granted...sale of land contract or personal services contract? Why?
Please use Java Eclipse and show code/output Please create a program that determines when a good...
Please use Java Eclipse and show code/output Please create a program that determines when a good day to go to the beach is. Please use the users input and its returning output. If the weather is 70 degree or greater, the program should say yes it is a good day to go If the weather is less than 70 degrees to say no the weather is not a good day to go
USE C++ and please keep program as simple as possible and also comment so it is...
USE C++ and please keep program as simple as possible and also comment so it is easy to understad Create a structure called time. Its three members, all type int, should be called hours, minutes, and seconds. Write a program that prompts the user to enter a time value in hours, minutes, and seconds. This should be in 12:59:59 format. This entire input should be assigned first to a string variable. Then the string should be tokenized thereby assigning the...
Hello, I need this done with Java and ran in Eclipse if possible. A university wants...
Hello, I need this done with Java and ran in Eclipse if possible. A university wants to demonstrate its political correctness by applying the Supreme Court’s “Separate but equal is inherently unequal” doctrine to gender as well as race. As such, the university has decided that both genders will use the same bathroom facilities. However, in order to preserve some tradition, it decrees that when a woman is in the bathroom, only other women may enter, and when a man...
write on eclipse java Write a program named lab5 that will play a game of Blackjack...
write on eclipse java Write a program named lab5 that will play a game of Blackjack between the user and the computer. Create a second class named Card with the following: Define the following private instance variables: cardValue (int) & cardSuite (String) Write a constructor with no parameters that will Set cardValue to a random number between 1 and 13 Generate a second random number between 0 and 3 and assign cardSuite a string based on its value (0 –...
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a tester to make sure the Apple class is crisp and delicious. Instructions: First create a class called Apple The class Apple DOES NOT HAVE a main method Some of the attributes of Apple are Type: A string that describes the apple.  It may only be of the following types:  Red Delicious  Golden Delicious  Gala  Granny Smith Weight: A decimal value representing...
Write a Java Program to determine if a candidate qualifies for a FHA Mortgage. (using eclipse...
Write a Java Program to determine if a candidate qualifies for a FHA Mortgage. (using eclipse IDE) The qualifications given are: Residence: The home must be your primary residence. Down payment: They must have a credit score of at least 563. The Java Program needs to do the following. This can all be done in the main() method. Create a boolean variable to store the Residence requirement. Prompt the user to "Enter 1 if the Home will be the primary...
IN JAVA ECLIPSE PLEASE The xxx_Student class A Student has a: – Name - the name...
IN JAVA ECLIPSE PLEASE The xxx_Student class A Student has a: – Name - the name consists of the First and Last name separated by a space. – Student Id – a whole number automatically assigned in the student class – Student id numbers start at 100. The numbers are assigned using a static variable in the Student class • Include all instance variables • Getters and setters for instance variables • A static variable used to assign the student...
Create a Java Program to calculate luggage costs. USING ECLIPSE IDE The Business Rules are: A....
Create a Java Program to calculate luggage costs. USING ECLIPSE IDE The Business Rules are: A. Two bags per person are free. B. The Excess Bag Charge is $75 per bag. The program needs to do the following: 1. In your main method(),    Create integers to store the number of Passengers and also the total number of bags    Prompt for the number of passengers on a ticket.    Prompt for the total number of bags for all passengers...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT