Wednesday, March 26, 2014

RainfallApp v2

This is the RainFallApp v2. It take takes the input name of the two months, then finds the rainfall amount for that month and then outputs the average between the two months.


import java.util.Scanner;

public class RainFallApp {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Please enter the first month: ");
        String aMonth = input.nextLine();

        System.out.print("Please enter the second month: ");
        String bMonth = input.nextLine();


        rainfall aRainfall = new rainfall();
        String aName = aRainfall.rainAmount(aMonth);
        Double aAmount = Double.parseDouble(aName);

        rainfall bRainfall = new rainfall();
        String bName = bRainfall.rainAmount(bMonth);
        Double bAmount = Double.parseDouble(bName);

        double Avg = (aAmount + bAmount) / 2;

        System.out.println("\nIn the month of " + aMonth + " it had " + aAmount + " inches of rain.");
        System.out.println("In the month of " + bMonth + " it had " + bAmount + " inches of rain.");
        System.out.println("The average rainfall between the two months is: " + Avg);

    }

}

class rainfall {
    private String Rain_Amount;

    String rainAmount(String rainMonth) {
        Rain_Amount = getAmount(rainMonth);
        return Rain_Amount;
    }

    private String getAmount(String rainMonth) {

        if (rainMonth.equals("Jan")) {
            Rain_Amount = "3.3";

        }
        else if (rainMonth.equals("Feb")) {
            Rain_Amount = "2.3";
        }
        else {
            System.out.println("Not a valid month name");
        }
        return Rain_Amount;
    }
}


No comments:

Post a Comment