Question

In: Computer Science

Introduction In this lab, we will look at various aspects of methods in Java programming such...

Introduction

In this lab, we will look at various aspects of methods in Java programming such as passing arguments to a method, use of local variables, and returning a value from a method.

Exercise-1: RainFall Statistisc(50 pts)

Write a program that asks user the total rainfall for the last six months of two consecutive years and do the following:

  1. the total rainfall per year
  2. the average monthly rainfall (for two years)
  3. the month with the most rain (per year)

You need to break your code into the following methods:

  • run: It asks the user to enter the year and rainfall values for each month (last six months) and reads the input as a double from the keyboard. It calls isValid method for validity check of the year. If isValid method returns true, then program will ask for the rainfall values from July to December for that year. Each time user will enter the rainfall values; isValid method will be called to check for the validity of the input. Once, the user enters all the required input, and then the method will show the output in the format as shown above.

  • isValid: It returns true in the following scenario:
  1. year should greater than 1990.
  2. The monthly rainfall cannot be negative

Otherwise, it returns false.

Exercise 2: (30 pts)

Add a code segment to the code that you complète in Exercise1 to do the following :

  1. Display the year that received the most rain.

Solutions

Expert Solution

Here is the answer for your question in Java Programming Language.

Kindly upvote if you find the answer helpful.

NOTE : I have given code for both Excercise 1 and Excercise 2. Please comment if you have any doubts.

###############################################################

CODE :


import java.text.DecimalFormat;
import java.util.Scanner;

public class RainFallStatistics {
static DecimalFormat f = new DecimalFormat("#.##");
public static void main(String[] args){
System.out.println("===========================================");
System.out.println("RAINFALL STATISTICS");
System.out.println("===========================================");
run();
}
public static void run(){
//Required variables
String[] months = { "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" };
double[][] yearRainFalls = new double[2][7];   
double[] totalRainFalls = {0,0};
double[] highestRainFall = {0,0};
double[] averageMonthlyRainFalls = new double[6];
  
Scanner s = new Scanner(System.in);
//Read information from user
for(int i = 0;i<2;i++){
//Read year as input from user
System.out.print("Enter the #" + (i+1) + " year: ");
yearRainFalls[i][0] = s.nextInt();
//validate it and ask repeatedly if invalid data enetered
while(!isValid(yearRainFalls[i][0],"year")){
System.out.println("Year should be greater than 1990....Try again");
System.out.print("Enter the first year: ");
yearRainFalls[i][0] = s.nextInt();
}
for(int j=1;j<7;j++){
//Read rainfall of each month of the year
System.out.print("Enter rainfall of " + months[j-1] + " : ");
yearRainFalls[i][j] = s.nextDouble();   
//validate it and ask repeatedly if invalid data enetered
while(!isValid(yearRainFalls[i][j],"rainfall")){
System.out.println("Rainfall cannot be negative....Try again");
System.out.print("Enter rainfall of " + months[j-1] + " : ");
yearRainFalls[i][j] = s.nextDouble();
}
//Sum up rainfalls of current year
totalRainFalls[i] += yearRainFalls[i][j];
//Find highest rainfall
if(yearRainFalls[i][j] > highestRainFall[i]){
highestRainFall[i] = yearRainFalls[i][j];
}
}
System.out.println("=============================================");
}
//Calculate averge rainfall
for(int i=1;i<7;i++){
averageMonthlyRainFalls[i-1] = (yearRainFalls[0][i] + yearRainFalls[1][i]) / 2;
}
//Display output
System.out.println("==============================");
System.out.println("Rain Fall Statistics");
System.out.println("==============================");
System.out.println("Total rainfall of year " + (int)(yearRainFalls[0][0]) + " is " + totalRainFalls[0]);
System.out.println("Total rainfall of year " + (int)yearRainFalls[1][0] + " is " + totalRainFalls[1]);
  
System.out.println("Average Monthly Rainfall: ");
for(int i = 0;i<6;i++){
System.out.println("For the month of " + months[i] + " : " + f.format(averageMonthlyRainFalls[i]));
}
System.out.println("==============");
System.out.println("Highest rainfall of the year " + (int)yearRainFalls[0][0] + " is " + highestRainFall[0]);
System.out.println("Highest rainfall of the year " + (int)yearRainFalls[1][0] + " is " + highestRainFall[1]);
  
  //EXERCISE - 2 CODE SEGMENT
System.out.println("Year that received most rain " + (int)((totalRainFalls[0] > totalRainFalls[1])?yearRainFalls[0][0]:yearRainFalls[1][0]));
}
//isValid method that takes value and type of input
public static boolean isValid(double value,String type){
//if it is year checks whether year is > 1990
if(type.equalsIgnoreCase("year")){
if(value > 1990)
return true;   
}else if(type.equalsIgnoreCase("rainfall")){
//If it is rainfall checks if it is non-negative
if(value > 0)
return true;
}
return false;
}
}

######################################################################

SCREENSHOTS :

Please see the screensots of the code below for the indentations of the code.

################################################################

OUTPUT :

Input

Output

Any doubts regarding this can be explained with pleasure :)


Related Solutions

Java Programming Assignment 6-1 We have covered the “Methods” this week. Apply the concepts that we...
Java Programming Assignment 6-1 We have covered the “Methods” this week. Apply the concepts that we have learnt in class and come up with a “problem scenario” for which you can create a “solution”. Utilize any/all of the examples from the book and class that we discussed. You program should be interactive and you should give a detailed statement about what the “description of the program – purpose and how to use it”. You should also use a “good bye”...
COP 2800, Java Programming Assignment 6-1 Using Java create a program using the “Methods” we covered...
COP 2800, Java Programming Assignment 6-1 Using Java create a program using the “Methods” we covered this week. come up with a “problem scenario” for which you can create a “solution”. Utilize any/all of the examples from the book and class that we discussed. Your program should be interactive and you should give a detailed statement about what the “description of the program – purpose and how to use it”. You should also use a “good bye” message. Remember to...
For My Programming Lab - Java: Write a Temperature class that will hold a temperature in...
For My Programming Lab - Java: Write a Temperature class that will hold a temperature in Fahrenheit and provide methods to get the temperature in Fahrenheit, Celsius, and Kelvin. The class should have the following field: • ftemp: a double that holds a Fahrenheit temperature. The class should have the following methods : • Constructor : The constructor accepts a Fahrenheit temperature (as a double ) and stores it in the ftemp field. • setFahrenheit: The set Fahrenheit method accepts...
This week we are going to look at aspects of mental illness, stigma and the manner...
This week we are going to look at aspects of mental illness, stigma and the manner in which society treats those who suffer from mental illness.Look at one or two of the resources posted in the assignment folder. Discuss your opinion as to how your chosen case was handled either by the court system, by mental health professionasl, or by the mental health system or society or schools.
Java Programming In this assignment we are going to create our own programming language, and process...
Java Programming In this assignment we are going to create our own programming language, and process it Java. programming language has 6 commands enter add subtract multiply divide return enter, add, subtract, multiply, divide all take 1 parameter (a double value). return takes no parameters.
This is for my Advanced Java Programming class. The book we use is Murach's Java Servlet's...
This is for my Advanced Java Programming class. The book we use is Murach's Java Servlet's and JSP 3rd Edition. I need help modifying some code. I will post the code I was told to open that needs to be modified below. In this exercise, you'll enhance the Future Value application to store the amount and interest rate in the user's session. That way, the user can experiment with different numbers of years to see the value of his or...
We started creating a Java class for Car. In this lab we are going to complete...
We started creating a Java class for Car. In this lab we are going to complete it in the following steps: 1- First create the Car class with some required instance variables, such make, model, year, price, speed, maxSpeed, isOn, isMoving, and any other properties you like to add. 2- Provide couple of constructors for initializing different set of important properties, such as make, model, year, and price. Make sure that you do not repeat initialization of instance variables in...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
Java Programming language. Proof of concept class design based on the following ideas Look at your...
Java Programming language. Proof of concept class design based on the following ideas Look at your refrigerator and think about how you would model it as a class. Considerations include: A refrigerator is made by a company on a manufacturing date and has an overall size based on length, width, and height A refrigerator contains a number of shelves and drawers for storing dairy, meats, and vegetables A refrigerator also has storage areas on the door for things like bottled...
JAVA programming- answer prompts as apart of one java assignment Static static variables static methods constants...
JAVA programming- answer prompts as apart of one java assignment Static static variables static methods constants Create a class Die representing a die to roll randomly. ☑ Give Die a public final static int FACES, representing how many faces all dice will have for this run of the program. ☑ In a static block, choose a value for FACES that is a randomly chosen from the options 4, 6, 8, 10, 12, and 20. ☑ Give Die an instance variable...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT