1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
ruslelena [56]
2 years ago
10

Write a RainFall class that stores the total rainfall for each of 12 months into an array of doubles. The program should have me

thods that return the following:
the total rainfall for the year
the average monthly rainfall
the month with the most rain
the month with the least rain
Demonstrate the class in a complete program.
Input Validation: Do not accept negative numbers for monthly rainfall figures.
import java.io.*;
import java.util.*;
public class Rainfall
{
Scanner in = new Scanner(System.in);
private int month = 12;
private double total = 0;
private double average;
private double standard_deviation;
private double largest;
private double smallest;
private double months[];
public Rainfall()
{
months = new double[12];
}
public void setMonths()
{
for(int n=1; n <= month; n++)
{
System.out.println("Enter the rainfall (in inches) for month #" + n + ":" );
months[n-1] = in.nextInt();
}
}
public double getTotal()
{
total = 0;
for(int i = 0; i < 12; i++)
{
total = total + months[i];
}
System.out.println("The total rainfall for the year is" + total);
return total;
}
public double getAverage()
{
average = total/12;
System.out.println("The average monthly rainfall is" + average);
return average;
}
public double getLargest()
{
double largest = 0;
int largeind = 0;
for(int i = 0; i < 12; i++)
{
if (months[i] > largest)
{
largest = months[i];
largeind = i;
}
}
System.out.println("The largest amout of rainfall was" + largest +
"inches in month" + (largeind + 1));
return largest;
}
public double getSmallest()
{
double smallest = Double.MAX_VALUE;
int smallind = 0;
for(int n = 0; n < month; n++)
{
if (months[n] < smallest)
{
smallest = months[n];
smallind = n;
}
}
System.out.println("The smallest amout of rainfall was" + smallest +
"inches in month " + (smallind + 1));
return smallest;
}
public static void main(String[] args)
{
Rainfall r = new Rainfall();
r.setMonths();
System.out.println("Total" + r.getTotal());
System.out.println("Smallest" + r.getSmallest());
System.out.println("Largest" + r.getLargest());
System.out.println("Average" + r.getAverage());
}
}
Computers and Technology
1 answer:
RSB [31]2 years ago
8 0

Answer:

Explanation:

The code provided worked for the most part, it had some gramatical errors in when printing out the information as well as repeated values. I fixed and reorganized the printed information so that it is well formatted. The only thing that was missing from the code was the input validation to make sure that no negative values were passed. I added this within the getMonths() method so that it repeats the question until the user inputs a valid value. The program was tested and the output can be seen in the attached image below.

import java.io.*;

import java.util.*;

class Rainfall

{

   Scanner in = new Scanner(System.in);

   private int month = 12;

   private double total = 0;

   private double average;

   private double standard_deviation;

   private double largest;

   private double smallest;

   private double months[];

   public Rainfall()

   {

       months = new double[12];

   }

   public void setMonths()

   {

       for(int n=1; n <= month; n++)

       {

           int answer = 0;

           while (true) {

               System.out.println("Enter the rainfall (in inches) for month #" + n + ":" );

               answer = in.nextInt();

               if (answer > 0) {

                   months[n-1] = answer;

                   break;

               }

           }

       }

   }

   public void getTotal()

   {

       total = 0;

       for(int i = 0; i < 12; i++)

       {

           total = total + months[i];

       }

       System.out.println("The total rainfall for the year is " + total);

   }

   public void getAverage()

   {

       average = total/12;

       System.out.println("The average monthly rainfall is " + average);

   }

   public void getLargest()

   {

       double largest = 0;

       int largeind = 0;

       for(int i = 0; i < 12; i++)

       {

           if (months[i] > largest)

           {

               largest = months[i];

               largeind = i;

           }

       }

       System.out.println("The largest amount of rainfall was " + largest +

               " inches in month " + (largeind + 1));

   }

   public void getSmallest()

   {

       double smallest = Double.MAX_VALUE;

       int smallind = 0;

       for(int n = 0; n < month; n++)

       {

           if (months[n] < smallest)

           {

               smallest = months[n];

               smallind = n;

           }

       }

       System.out.println("The smallest amount of rainfall was " + smallest +

               " inches in month " + (smallind + 1));

   }

   public static void main(String[] args)

   {

       Rainfall r = new Rainfall();

       r.setMonths();

       r.getTotal();

       r.getSmallest();

       r.getLargest();

       r.getAverage();

   }

}

You might be interested in
Which of the following is not a key component of a structure?
Nezavi [6.7K]
Name
I am not sure about it
8 0
3 years ago
Write a function called calculate() that accepts three integer Numbers as arguments, compute these values : Sum and Product and
Dmitrij [34]

Answer:

int* calculate(int a,int b,int c){

   int result[] = {0,0};

   int sum = a+b+c;

   int product = a*b*c;

   result[0] = sum;

   result[1] = product;

   return result;

}

Explanation:

The function is a block of the statement which performs the special task.

The function can return one integer, not more than one integer.

If we want to return multiple values then, we can use array.

we store the result in the array and return that array to the main function.

This is the only possible way to return multiple values.

So, define the function with return type array and declare the array with zero value.

Then, calculate the values and store in the variable after that, assign to the array.

Finally, return that array.  

5 0
2 years ago
A software license gives the owner the to use software.
Sveta_85 [38]
Yes, but under the given terms of service
3 0
2 years ago
Read 2 more answers
What kind of printer is used with multipart forms such as those with point of sale machines?A) dot-matrixB) daisy-wheelC) inkjet
ivanzaharov [21]

Answer:

The answer is "Option A".

Explanation:

The Dot matrix printer is also known as an impact matrix printer. It is a multi-part form printer because it prints data on paper. This printer uses a hammer and a ribbon printer, that shapes pictures from dots. and other options are not correct, that can be described as follows:

  • In option B, daisy-wheel printer prints data on printer only one side that's it is not correct.
  • In option C, the Inkjet printer is capable to print data in multi-part forms, but this printer does not data in continuous forms.
  • In option D, the color printer allows you to print data in color format like images, graphs, and banner. It prints data only on one side, that's why it is not correct.  
6 0
2 years ago
What does FTTB mean?
EleoNora [17]

  • <em><u>Fiber to the building</u></em>

Explanation:

  • <em><u>Fiber to the building</u></em><em><u> </u></em><em>is a type of fiber-optic cable installation where the fiber cable goes to a point on a shared property and the other cabling provides the connection to single homes, offices or other spaces.</em>

<h2><em>hope </em><em>it</em><em> helps</em><em>!</em></h2>
3 0
2 years ago
Other questions:
  • The largest group of Linux users is likely to be
    7·1 answer
  • Which of the following definitions describes a chemical hazard?
    8·2 answers
  • You are a network technician for a small corporate network. It's been decided that the office needs a wireless network for emplo
    6·1 answer
  • Harry Potter and his friends are at Ollivander's with Ron, finally replacing Charlie's old broken wand. Hermione decides the bes
    11·1 answer
  • Are to print or find<br> the sum<br> 5 numbers<br> of<br> using flow chart
    6·1 answer
  • Match each term to the appropriate definition. DBMS data mining hash file index key field locking protocol relation roll back sc
    10·1 answer
  • An IT professional with a customer-service
    13·1 answer
  • Instructions
    8·1 answer
  • The command to get out of the loop.<br>a.<br>i exit<br>ii. break<br>iii. comment<br>iv. for​
    12·1 answer
  • Write a function called changeCharacter that takes three parameters – a character array, its size, and the replacement character
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!