Results 1 to 5 of 5
  1. #1
    ejswifty is offline Member
    Join Date
    Jul 2014
    Posts
    1
    Rep Power
    0

    Default Rainfall Program

    Greetings,

    I'm new to Java, and am attempting to do an assignment...but am feeling lost right about now. We haven't been introduced to arrays yet, so our vocabulary is a bit limited.
    The assignment is in two parts...

    1.) Write a program that collects and processes data about quarterly rainfall for one year. Your program should ask the user to enter rain fall amounts for each of the four quarters in the year. You must use a looping construct to collect the input for one quarter at a time until all four quarters’ data is collected. For your program’s output, you should name which quarter (i.e. Quarter 1, Quarter 2, Quarter 3, or Quarter 4) had the most rainfall, which quarter had the least rainfall, the average rainfall for the entire year, and the total rainfall for the entire year. All output should be displayed with meaningful descriptions; and

    2.) Modify program 1 so that it will handle multiple years of rainfall data. The user should be asked at runtime to supply the actual number of years they want to enter in the program for evaluation. In addition to the output specified in program 1 for each individual year, program 2 should output the year (i.e. Year 1, Year 2, etc.) with the most total rainfall and the year with the least total rainfall. All output should be displayed with meaningful descriptions.

    We are supposed to use loops in these two programs...namely "while", "do while" or "for".


    I'm feeling clueless at this point, as we've learned small blocks of code, but not how to put them together in solid fashion.
    Here's what I've got so far, but I believe it'll take more of a "for" loop, as the second program takes it to a whole new level..



    // This program processes quarterly rainfall data for a single year.

    import java.util.Scanner;

    public class EricSwiftProgram1
    {
    public static void main (String[] args)
    {
    double quarter1, quarter2, quarter3, quarter4; // Quarterly rainfall
    double averageRainFall; // Average annual rainfall
    double totalRainFall; // Total annual rainfall
    int repeat = 0;

    Scanner inp = new Scanner(System.in);

    System.out.println ("This program will calculate:");
    System.out.println ("- which quarter had the most rainfall");
    System.out.println ("- which quarter had the least rainfall");
    System.out.println ("- the average rainfall per annum");
    System.out.println ("- the total rainfall per annum");

    do
    {
    System.out.print ("Please enter, in inches, the total");
    System.out.println (" rainfall for the first quarter of the year");
    quarter1 = inp.nextDouble ();
    System.out.print("Please enter, in inches, the total");
    System.out.println (" rainfall for the second quarter of the year");
    quarter2 = inp.nextDouble ();
    System.out.print("Please enter, in inches, the total");
    System.out.println (" rainfall for the third quarter of the year.");
    quarter3 = inp.nextDouble ();
    System.out.print("Please enter, in inches, the total");
    System.out.println (" rainfall for the fourth quarter of the year.");
    quarter4 = inp.nextDouble ();

    if (quarter1>quarter2 && quarter1>quarter3 && quarter1>quarter4);
    {
    System.out.println ("The first quarter received the greatest amount of annual rainfall.");
    }

    if (quarter2>quarter1 && quarter2>quarter3 && quarter2>quarter4);
    {
    System.out.println ("The second quarter received the greatest amount of annual rainfall.");
    }

    if (quarter3>quarter1 && quarter3>quarter2 && quarter3>quarter4);
    {
    System.out.println ("The third quarter received the greatest amount of annual rainfall.");
    }

    if (quarter4>quarter1 && quarter4>quarter2 && quarter4>quarter3);
    {
    System.out.println ("The fourth quarter received the greatest amount of annual rainfall.");
    }

    if (quarter1<quarter2 && quarter1<quarter3 && quarter1<quarter4);
    {
    System.out.println ("The first quarter received the least amount of annual rainfall.");
    }

    if (quarter2<quarter1 && quarter2<quarter3 && quarter2<quarter4);
    {
    System.out.println ("The second quarter received the least amount of annual rainfall.");
    }

    if (quarter3<quarter1 && quarter3<quarter2 && quarter3<quarter4);
    {
    System.out.println ("The third quarter received the least amount of annual rainfall.");
    }

    if (quarter4<quarter1 && quarter4<quarter2 && quarter4<quarter3);
    {
    System.out.println ("The fourth quarter received the least amount of annual rainfall.");
    }

    averageRainFall = (quarter1+quarter2+quarter3+quarter4)/4;
    totalRainFall = quarter1+quarter2+quarter3+quarter4;

    System.out.println ("The average annual rainfall was " + averageRainFall + " inches.");
    System.out.println ("The total annual rainfall was " + totalRainFall + " inches.");


    System.out.println ("Would you like to run the program again?");
    System.out.println ("Enter 1 to continue");

    repeat = inp.nextInt();

    }while (repeat == 1);

    }

    }


    Any help would be much appreciated, and thanks in advance for your time!
    Eric
    Last edited by ejswifty; 07-19-2014 at 05:56 AM. Reason: wrong screenshot

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: Rainfall Program

    I've moved this thread to a more appropriate forum.

    When posting code, please use the code tags to preserve formatting.

    It's hard to answer general "how do I do this" type questions other than to point you to google and the basic tutorials. It's much easier to answer questions that go something like "I tried X, expect Y, but got Z instead, what's going on?"
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    DayKema is offline Member
    Join Date
    Jul 2014
    Posts
    23
    Rep Power
    0

    Default Re: Rainfall Program

    There was no real question to this thread.

    Add a question and ill recheck this later.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Rainfall Program

    There is no question, just "I'm clueless". I find it strange that the OP then not tries to become "less clueless" by studying the material. That's what I did when I was stuck while I was still in school. I read and re-read the chapters until something finally clicked and I managed to get a little further. And then a little further. Until I had something working.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    maca is offline Member
    Join Date
    Jul 2014
    Location
    UK
    Posts
    16
    Rep Power
    0

    Default Re: Rainfall Program

    ejswifty, although you haven't really asked any questions, I will make some comments.

    Your program is very procedural and not very object orientated - objects are your friends. Procedural programming is generally not scalable and modifications down the road can require significant rework to the code. I would suggest that spending the time early on trying to think in an object orientated way will be of massive help to you. For example, you have been asked to create a program that collects and processes quarterly rainfall for one year. Therefore why not create a simple class called AnnualRainfall that takes four parameters of type double (double firstQuarter, double secondQuarter, double thirdQuarter, double fourthQuarter). On construction you could then store these values to private class member variables and then also calculate and store (as private class member variables) attributes such as, a (numerical) value for the quarter that has the lowest rainfall, a (numerical) value for the quarter that has the highest rainfall, a value for the average annual rainfall and a value for the total annual rainfall. You could then provide accessors for getting all of these values. This will mean that all you need to do to record multiple years of rainfall data is to create multiple AnnualRainfal objects (one for each year).

    As others have said, try asking specific questions, and you will then get specific answers.

Similar Threads

  1. Replies: 6
    Last Post: 12-15-2013, 07:40 AM
  2. Replies: 4
    Last Post: 11-21-2012, 02:19 PM
  3. Program to print average rainfall
    By kalimm in forum New To Java
    Replies: 1
    Last Post: 07-07-2012, 07:21 AM
  4. Replies: 2
    Last Post: 04-02-2011, 01:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •