Question

Implement a Java class that is called RainFall that uses a double array to store the...

Implement a Java class that is called RainFall that uses a double array to store the amount of rainfall for each month of the year. The class should have only one instance variable of type double[]. Implement the following methods in the RainFall class:

1. A constructor that creates and initializes all entries in the array to be -1.

2. toString: a method that returns a one-line String representation of the object that includes 12 double numbers that represent the amount of rain fall in each month of the year.

3. setMonth: a method that takes two inputs parameters where the first parameter is an int that represents is the month number (1 to 12) and the second parameter is a double that represent the rain fall for the month. The method then fills in the array with the given rain fall amount for the given month.

4. getTotal: a method that returns the total rain fall for the year.

5. getMaxRainAmount: a method that returns the value of the maximum rain amount.

6. maxRainMonth: a method that returns the month (as a number between 1 and 12) with the maximum amount of rain fall for that year.

7. greaterThan: a method that takes one input parameter of type double that represents a given amount of rain fall. The method then returns the number of months that has rain fall greater than the input amount.

Part 2: RainFallDriver class Implement a Driver class to test your RainFall class and make sure to call and test all the methods that you implemented in the Part 1 above.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:-

public class HomeworkLib {

    public static void main(String[] args) {

        RainFall rf = new RainFall();

        rf.setMonth(1, 1.5);

        rf.setMonth(2, 2.5);

        rf.setMonth(3, 8.2);

        rf.setMonth(4, 6.5);

        rf.setMonth(5, 9.1);

        rf.setMonth(6, 4.6);

        rf.setMonth(7, 7.6);

        rf.setMonth(8, 11.3);

        rf.setMonth(9, 6.1);

        rf.setMonth(10, 4.5);

        rf.setMonth(11, 9.5);

        rf.setMonth(12, 7.5);

        System.out.println("Max Rainfall is: " + rf.getMaxRainAmount());

        System.out.println("Max Rainfall month is: " + rf.maxRainMonth());

        System.out.println(rf.toString());

        System.out.println("Number of months greater than 7.5: " + rf.greaterThan(7.5));

        System.out.println("Total rain in a year: " + rf.getTotal());

    }

}

class RainFall {

    double arr[] = new double[12]; // double array of length 12

    public RainFall() { // constructor

        for (int i = 0; i < arr.length; i++) {

            arr[i] = -1; // initialising every value to -1

        }

    }

    public String toString() { // creating a string and using for loop adding all the double values to it

        String result = "";

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

            result += arr[i] + " ";

        }

        return result;

    }

    public void setMonth(int month, double rain) {

        arr[month - 1] = rain;

    }

    public double getTotal() {

        int sum = 0;

        for (int i = 0; i < arr.length; i++) { // iterating and adding every value

            sum += arr[i];

        }

        return sum;

    }

    public double getMaxRainAmount() {

        double max = arr[0];

        for (int i = 0; i < arr.length; i++) {// getting max by iterating the array and storing the max value

            if (arr[i] > max) {

                max = arr[i];

            }

        }

        return max;

    }

    public String maxRainMonth() {

        double maxrain = getMaxRainAmount(); // first getting the max rain amount

        int month = 0;

        for (int i = 0; i < arr.length; i++) { // then comparing it with the array and taking the index

            if (arr[i] == maxrain) {

                month = i + 1;

            }

        }

        String monthNames[] = { "null", "January", "February", "March", "April", "May", "June", "July", "August",

                "September", "October", "November", "December" };

        return monthNames[month]; // and using that index returning the month name

    }

    public int greaterThan(double value) {

        int result = 0; // simply iterating and comparing to get the number of months having

        // greater rain

        for (int i = 0; i < arr.length; i++) {

            if (arr[i] > value) {

                result++;

            }

        }

        return result;

    }

}

FOR ANY OTHER QUERY PLEASE COMMENT.

Add a comment
Know the answer?
Add Answer to:
Implement a Java class that is called RainFall that uses a double array to store the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java language: Write a rainfall class that stores the total rainfall for each of 12...

    In Java language: Write a rainfall class that stores the total rainfall for each of 12 months of years 2015 -2017 into an 2D array of doubles. Use the text file at Actividades/Tareas link as input of rainfall data. The program should read the input data from a text file. The program should have methods that at least return the following: • Total rainfall for the years 2015 – 2017 - return a 1D array • The average monthly rainfall...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • Java 4. Write the code of data type class ExerciseMonitor_yourLastName including data members with an double...

    Java 4. Write the code of data type class ExerciseMonitor_yourLastName including data members with an double array size 7 to store the distance of 7 days, constructors, and all the following methods Total rainfall for the year The average monthly rainfall The month with the most rain . The month with the less rain The method toString to display the result in the following format: For example if the input from the keyboard for each month as data in the...

  • Write a program that scores the total rainfall for each of 12 months into an array...

    Write a program that scores the total rainfall for each of 12 months into an array double. A sample array definition is shown below double thisYear[] = {1.6, 2.1, 1.7, 3.5, 2.6, 3.7, 3.9, 2.6, 2.9, 4.3, 2.4, 3.7}; The program should have the four functions to calculate the following 1. The total rainfall for the year 2. The average monthly rainfall 3. The month with most rain 4. The month with least rain Output: What to deliver? Your .cpp...

  • Design and implement a Java class (name it Summer Stats. java) that tracks statistics for summer...

    Design and implement a Java class (name it Summer Stats. java) that tracks statistics for summer job salaries for a group of people over several years. The only data field you need is a 2-Dimenssional array of values representing salaries. The rows the represents the people and the columns represent the years. The constructor method takes two integers representing the number of people and the number of years, then randomly generates the annual salaries and fills the array. Other class...

  • Create a simple Java class for a Month object with the following requirements:  This program...

    Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...

  • Skip list, needs to be implemented in Java: - Implement the class NodeSkipList with two components,...

    Skip list, needs to be implemented in Java: - Implement the class NodeSkipList with two components, namely key node and array of successors. You can also add a constructor, but you do not need to add any method. - In the class SkipList implement a constructor SkipList(long maxNodes). The parameter maxNodes determines the maximal number of nodes that can be added to a skip list. Using this parameter we can determine the maximal height of a node, that is, the...

  • Design and implement a Java class (name it SummerStats.java) that tracks statistics for summer job salaries...

    Design and implement a Java class (name it SummerStats.java) that tracks statistics for summer job salaries for a group of people over several years. The only data field you need is a 2-Dimenssional array of values representing salaries. The rows the represents the people and the columns represent the years. The constructor method takes two integers representing the number of people and the number of years, then randomly generates the annual salaries and fills the array. Other class methods include...

  • Java Program Create a class to store an array of with enough space to store 10 integer values. Us...

    Java Program Create a class to store an array of with enough space to store 10 integer values. Using the principle of recursion, implement the following: *getSize : returns the size of the array. *get (i): returns the i-th element of the array. If the element does not exist, it throws a "NoSuchElementException” which is a subclass of Java class RunTimeException. *add (val): inserts value as the last element of the array. If necessary, double the size of the current...

  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT