rainFall.java - import [email protected] Lemuel M [email protected] May 2 2013 Book Title From Control Structures through Objects by Tony Gaddis Page

rainFall.java - import [email protected] Lemuel M...

This preview shows page 1 - 2 out of 2 pages.

import java.util.Scanner; /* @author Lemuel M. Uhuru * @Date May 2, 2013 * Book Title: From Control Structures through Objects by Tony Gaddis * Page: 490 Problem 1 * * Program that creates a class and stores the total * rainfall for each 12 months into an array of doubles * and demonstrates the class in a complete program. */ public class rainFall { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); final int SIZE = 12; double[] monthlyRain = new double[SIZE]; System.out.println("Enter the monthly rain fall amount: "); int i = 0; // Added input validation with 'Do-While Loop' for (i = 0; i < SIZE; i++) { do { System.out.print("Month " + (i + 1) + ": "); monthlyRain[i] = keyboard.nextDouble(); } while (monthlyRain[i] < 0); } System.out.println("The annual sum of rain: " + sumRain(monthlyRain)); System.out.println("The average sum of rain: " + averageRain(monthlyRain)); System.out.println("The most amount of rain fall: " + mostRain(monthlyRain));
Image of page 1
Image of page 2

You've reached the end of your free preview.

Want to read both pages?

Unformatted text preview: System.out.println("The least amount of rain fall: " + leastRain(monthlyRain)); } // Create a function that returns the total rainFall for the year. static double sumRain(double array) { double sum = 0; for (int i = 0; i < array.length; i++) { sum += array[i]; } return sum; } // Create a function that returns the average monthly rainFall for the year. static double averageRain(double array) { double average = 0; average = sumRain(array) / array.length; return average; } // Create a function that returns the month with the most rain. static double mostRain(double array) { double most = 0; for (int i = 0; i < array.length; i++) { if (array[i] > most) { most = array[i]; } } return most; } // Create a function that returns the month with the least rain. static double leastRain(double array) { double least = array[0]; for (int i = 0; i < array.length; i++) { if (array[i] < least) { least = array[i]; } } return least; } }...
View Full Document

  • Spring '08
  • Garcia,D
  • Java Programming, Java Code, Control flow, 12 months, @author, @date, Lemuel M. Uhuru

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors