Ive already wrote code that will take the temp for each month and then show and calculate the total, average, most and least rainfall for the year and output that. How do I replace the most and least rainfall with actually months names?
code so far:
import java.util.Scanner;
import java.text.DecimalFormat;
public class Rainfall
{
public static void main(String[] args)
{
String [] months={"Janurary","Febuary","March","April","May","June","July","August","September","October","November","December"};
final int MONTHS = 12;
double[] rain = new double[MONTHS];
initRain(rain);
double total = totalRain(rain);
double average = averageRain(rain, total);
int most = mostRain(rain);
int least = leastRain(rain);
// Decimal Format
DecimalFormat digit = new DecimalFormat("#.0");
// Output
System.out.println("The total rainfall of the year is " + digit.format(total));
System.out.println("The average rainfall of the year is " + digit.format(average));
System.out.println("The month with the highest amount of rain is " + (most + 1));
System.out.println("The month with the lowest amount of rain is " + (least + 1));
}
public static void initRain(double[] array)
{
Scanner keyboard = new Scanner(System.in);
for (int x = 0; x < array.length; x++)
{
System.out.print("Enter Rainfall for month " + (x + 1) + ": ");
array[x] = keyboard.nextDouble();
}
}
public static double totalRain(double[] array)
{
double total = 0;
for (int x = 0; x < 12; x++)
total += array[x];
return total;
}
public static double averageRain(double[] array, double total)
{
return total / array.length;
}
public static int mostRain(double[] array)
{
double maximum = array[1];
int value = 0;
for (int i=0; i < 12; i++) {
if (array[i] >= maximum) {
maximum = array[i];
value = i;
}
}
return months[index];
}
public static int leastRain(double[] array)
{
double minimum = array[0];
int value = 0;
for (int i=0; i < 12; i++)
{
if (array[i] <= minimum) {
minimum = array[i];
value = i;
}
}
return value;
}
}
Well, you've got an index, and you've got an array of the names of the months, so... ?
String most = months[mostRain(rain)];
I guess that this code is not even compiling
see
public static int mostRain(double[] array)
{
double maximum = array[1];
int value = 0;
for (int i=0; i < 12; i++) {
if (array[i] >= maximum) {
maximum = array[i];
value = i;
}
}
return months[index];
}
as it is expecting to be returned an int but you are returning a String (also index does not exist), so what you want is
public static int mostRain(double[] array)
{
double maximum = 0.00; // change this too
int value = 0;
for (int i=0; i < 12; i++) {
if (array[i] >= maximum) {
maximum = array[i];
value = i;
}
}
return value; // change this
}
then you can use it above like
System.out.println("The month with the highest amount of rain is " + months [mostRain (rain)]);
Of course, make the similar change for leastRain too.
Related
This question already has answers here:
reading from console with scanner in a loop for
(2 answers)
Closed 3 years ago.
This program is to take input from the user and to give output back showing how many numbers were above the average of the array and below it. I'm trying to put a condition on the loops to exit getting input.
import java.util.Scanner;
public class analyzeScores {
public static void count(int[] list) {
Scanner input = new Scanner(System.in);
for(int i = 0; i < list.length;i++) {
if(list[i] != 0)
list[i] = input.nextInt();
}
}
public static void sorts(int[] lists, int average) {
int high = 0;
int low = 0;
for(int i = 0; i < lists.length; i++) {
if(lists[i] >= average) {
high +=1;
}
else {
low += 1;
}
}
System.out.println("The number of higher then average scores is " + high);
System.out.println("The number of lower then average scores is " + low);
}
public static void main(String[] args) {
int[] list = new int[10];
System.out.println("Enter the scores: ");
count(list);
int total = 0;
for (int i = 0; i < list.length;i++) {
total += list[i];
}
total = total / list.length;
sorts(list, total);
}
}
I'm trying to figure out how to implement a way to input 0 to exit the loop in the count(int[] list) method. I tried to implement if(list[i] != 0) but messes the whole code up
you just have to add an else condition to your if statement in the loop,
This should work if the rest of your code works
import java.util.Scanner;
public class analyzeScores {
public static void count(int[] list) {
Scanner input = new Scanner(System.in);
for(int i = 0; i < list.length;i++) {
if(list[i] != 0){
list[i] = input.nextInt();
}else{
break;
}
}
}
public static void sorts(int[] lists, int average) {
int high = 0;
int low = 0;
for(int i = 0; i < lists.length; i++) {
if(lists[i] >= average) {
high +=1;
}
else {
low += 1;
}
}
System.out.println("The number of higher then average scores is " + high);
System.out.println("The number of lower then average scores is " + low);
}
public static void main(String[] args) {
int[] list = new int[10];
System.out.println("Enter the scores: ");
count(list);
int total = 0;
for (int i = 0; i < list.length;i++) {
total += list[i];
}
total = total / list.length;
sorts(list, total);
}
}
so in my program is where the user enters list of numbers one at a time, and when I would end the list numbers with the "end" statement which is set to -1, and once I do that I get my average, and maximum, and minimum, my problem is that when I do get the minimum output it would be -1 everytime, I'm having trouble to remove the -1 from the array, any ideas???
import java.util.Scanner; //import scanner to user scanner tool
public class Average { //creating public class
public static void main(String[]args) { //creating public static main
Scanner input = new Scanner(System.in); //creating scanner input to grab user input
System.out.println("Please enter a list of numbers, entering -1 to end the list: ");
double[] numbers = new double[20]; //creating 20 count array
double sum = 0;
int count = 0;
double average;
int end = -1;
for(int i = 0; i<numbers.length; i++) {
System.out.print("Enter a number: ");
numbers[i] = input.nextDouble();
if(numbers[i]== end) {
break;
}
sum+= numbers[i];
count++;
}
// gets average from user input of numbers
average = sum/count;
System.out.println("Average is: " + average);
double max = maxim(numbers);
System.out.println("Max: " + max);
double min = minim(numbers);
System.out.println("Min: " + min);
}
//method for finding out maximum number from user input
public static double maxim(double[] array) {
double maxNum = array[0];
for(int i = 1; i<array.length; i++) {
if(array[i] > maxNum) {
maxNum = array[i];
}
}
return maxNum;
}
//method for finding out minimum number from user input
public static double minim(double[] array) {
double minNum = array[0];
for(int i = 1; i<array.length; i++) {
if(array[i] < minNum && array[i]!=-1) {
minNum = array[i];
}
}
return minNum;
}
}
You need to docouple part of code that responsible for reading user input from code that compute statistics. And for remove -1 from resulting array you need simple don't put this value to result. When user input random number first check if it is not -1, and after that put it in result. Something like that:
import java.util.Scanner;
public class Average {
public static void main(String[]args) { //creating public static main
double[] numbers = readInputNumbers();
System.out.println("Average is: " + average(numbers));
System.out.println("Max: " + max(numbers));
System.out.println("Min: " + min(numbers));
}
public double[] readInputNumbers() {
Scanner input = new Scanner(System.in); //creating scanner input to grab user input
System.out.println("Please enter a list of numbers, entering -1 to end the list: ");
double[] numbers = new double[20];
final int endInput = -1;
for(int i = 0; i < numbers.length; i++) {
System.out.print("Enter a number: ");
double nextNumber = input.nextDouble();
if(nextNumber == endInput) {
break;
} else {
numbers[i] = nextNumber;
}
}
return numbers;
}
public static double max(double[] array) {
double maxNum = array[0];
for(int i = 1; i<array.length; i++) {
if(array[i] > maxNum) {
maxNum = array[i];
}
}
return maxNum;
}
public static double min(double[] array) {
double minNum = array[0];
for(int i = 1; i<array.length; i++) {
if(array[i] < minNum && array[i]!=-1) {
minNum = array[i];
}
}
return minNum;
}
public static double average(double[] numbers) {
double sum = 0;
for(int i = 0; i < numbers.length; i++) {
sum = sum + numbers[i];
}
return sum / numbers.length;
}
}
In jdk since 8 version your could be simplify this task like this:
import java.util.Scanner;
import java.util.stream.*;
public class Average {
public static void main(String[]args) { //creating public static main
double[] numbers = readInputNumbers();
DoubleSummaryStatistics statistics = DoubleStream.of(numbers).summaryStatistics();
System.out.println("Average is: " + statistics.getAverage()));
System.out.println("Max: " + statistics.getMax());
System.out.println("Min: " + statistics.getMin());
}
public double[] readInputNumbers() {
Scanner input = new Scanner(System.in); //creating scanner input to grab user input
System.out.println("Please enter a list of numbers, entering -1 to end the list: ");
double[] numbers = new double[20];
final int endInput = -1;
for(int i = 0; i < numbers.length; i++) {
System.out.print("Enter a number: ");
double nextNumber = input.nextDouble();
if(nextNumber == endInput) {
break;
} else {
numbers[i] = nextNumber;
}
}
return numbers;
}
}
You could take the input into a temp variables and store it in the array only if it isn't the end flag (-1). E.g.:
for(int i = 0; i<numbers.length; i++) {
System.out.print("Enter a number: ");
double temp = input.nextDouble();
if(temp == end) {
break;
}
numbers[i] = temp;
sum += numbers[i];
count++;
}
I have to create classes to be implemented with the main class someone else has and for some reason I am not getting the right outputs, I'm not sure where this error is coming from.
Expected Output:
Median = 44.5
Mean = 49.300
SD = 30.581
public class StatPackage {
int count;
double [] scores;
StatPackage() {
count = 0;
scores = new double[500];
}
public void insert (double value) {
if (count < 500){
scores[count] = value;
++ count;
}
}
public double Mean () {
double sum = 0;
//For loop for calculating average or mean
for(int i = 0; i < count; i++){
sum += (scores[i]);
}
double average = sum/count;
return average;
}
public double Median() {
int min;
int tmp;
int size;
for (int i = 0; i < count; i ++)
{
min = i;
for (int pos = i + 1; pos < count; pos ++)
if (scores [pos] < scores [min])
min = pos;
tmp = (int)scores [min];
scores [min] = scores [i];
scores [i] = tmp;
}
double median = 0;
if (count % 2 == 0){
median = (scores[scores.length/2-1] + scores[scores.length/2])/2;
}
else {
median = (scores[((scores.length/2))]);
}
return median;
}
public double Variance () {
double variance = 0;
double sum = 0;
//For loop for getting the variance
for(int i = 0; i < count; i++){
sum += scores[i];
variance += scores[i] * scores[i];
}
double varianceFinal = ((variance/count)-(sum*sum)/(count*count));
return (varianceFinal);
}
public double StdDev (double variance) {
double sum = 0;
for(int i = 0; i < count; i++){
sum += scores[i];
variance += scores[i] * scores[i];
}
double varianceFinal = ((variance/count)-(sum*sum)/(count*count));
return Math.sqrt(varianceFinal);
}
You have a problem in your Median() method. Try changing
if (count % 2 == 0){
median = (scores[scores.length/2-1] + scores[scores.length/2])/2;
}
else {
median = scores[scores.length/2];
}
to
if (count % 2 == 0)
median = (scores[count/2] + scores[count/2 - 1])/2;
else
median = scores[count/2];
Because you have a fixed-size array with 500 elements, the code you have will return the mean value of the items at position 249 and 250, which will be 0 if you have less than 251 values in your array.
In a fit of charity/insanity, I quickly wrote a driver program to try to test this thing.
public static void main(String[] args)
{
StatsPackage sp = new StatsPackage();
for (int i = 0; i < 101; ++i) {
sp.insert(i);
}
System.out.println("count: " + sp.count);
System.out.println("mean: " + sp.Mean());
System.out.println("median: " + sp.Median());
System.out.println("variance: " + sp.Variance());
}
Beyond the fact the median doesn't compute the right values (and #BadCash may have the solution for that problem), there is no ArrayOutOfBounds thrown.
EDIT: making the update as #BadCash suggested to the Median method seems to resolve the median not giving the correct answer.
The code is to run simulations to find out the probability of n people sharing the same birthday.
I compared randomly assigned birthdates to an array of dates. For any dates that has more than 1 equal value, I added one to the numerator.
However, the answer comes out wrong for the code. I am not sure why.
import java.util.Scanner;
public class birthday {
public static void main (String[] args) {
Scanner inp = new Scanner(System.in);
System.out.println("How many trials");
int n = inp.nextInt();
//variable declaration
double[] birthdate = new double[n];
int num = 0;
int numerator = 0;
double bday = 0;
int trials = 0;
//assign birthdays to n people
for (int i = 0; i < n; i++)
{
birthdate[i] = Math.floor(Math.random() * 365) + 1;
System.out.println(birthdate[i]);
}
for (int i = 1; i <= 365; i++)
{
for (int j = 0; j < n; j++)
{
bday = birthdate[j];
//compare birthdates to dates
if (bday == i)
{
num++;
if (num > 1)
{
numerator++;
}
}
}
num = 0;
}
double ans = (double) numerator / n;
System.out.println("The answer is " + ans);
}
}
For any dates that has more than 1 equal value, I added one to the numerator.
That's not what your code does. For any date with at least 2 persons having birthday at that date you add the number of those people minus 1 to the numerator.
If you want your code to work according to the above statement, you have to change the following code
for (int j = 0; j < n; j++)
{
bday = birthdate[j];
//compare birthdates to dates
if (bday == i)
{
num++;
if (num > 1)
{
numerator++;
}
}
}
num = 0;
to this code:
for (int j = 0; j < n; j++)
{
bday = birthdate[j];
//compare birthdates to dates
if (bday == i)
{
num++;
}
}
if (num > 1)
{
numerator++;
}
num = 0;
This way the code if (num > 1) numerator++ isn't repeated for every person (starting from the second one), but done just once per date.
Anyway, I doubt that either version of the code calculates you the "probability of n people sharing the same birthday". If that's what you want to approximate, you should repeat the whole experiment a lot of times, count, in how many of those cases n people were sharing their birthday, and divide it by the number of experiments:
import java.util.Scanner;
public class birthday {
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
System.out.println("How many trials?");
int numExperiments = inp.nextInt();
System.out.println("How many persons?");
int n = inp.nextInt();
// variable declaration
int dups = 0;
for (int k = 0; k < numExperiments; k++) {
boolean foundDup = false;
int[] birthdate = new int[n];
// assign birthdays to n people
for (int i = 0; i < n; i++) {
birthdate[i] = (int) (Math.random() * 365) + 1;
}
// check, if there is a duplicate
for (int i = 1; i <= 365; i++) {
int num = 0;
for (int j = 0; j < n; j++) {
// compare birthdates to dates
if (birthdate[j] == i) {
num++;
}
}
if (num > 1) {
foundDup = true;
}
num = 0;
}
// count cases with duplicates
if (foundDup) {
dups++;
}
}
double ans = (double) dups / numExperiments;
System.out.println("The answer is " + ans);
}
}
I'm stuck... My code was working earlier, but now it just hangs. On top of that I can't seem to get my getHighest and getSmallest methods to return the correct values. I don't know if I'm just not catching something. Any help would be great!
import java.util.Scanner;
import java.text.DecimalFormat;
public class Rainfall
{
public static void main(String[] args)
{
final int MONTHS = 12;
double[] rain = new double[MONTHS];
initRain(rain);
double total = totalRain(rain);
double average = averageRain(rain, total);
double most = mostRain(rain);
double least = leastRain(rain);
DecimalFormat digit = new DecimalFormat("#.0");
System.out.println("The total rainfall of the year is " + digit.format(total));
System.out.println("The average rainfall of the year is " + digit.format(average));
System.out.println("The month with the highest amount of rain is " + most);
System.out.println("The month with the lowest amount of rain is " + least);
}
public static void initRain(double[] array)
{
Scanner keyboard = new Scanner(System.in);
for (int x = 0; x < array.length; x++)
{
System.out.print("Enter Rainfall for month " + (x + 1) + ": ");
array[x] = keyboard.nextDouble();
}
}
public static double totalRain(double[] array)
{
double total = 0;
for (int x = 0; x < 12; x++)
total += array[x];
return total;
}
public static double averageRain(double[] array, double total)
{
return total / array.length;
}
public static double mostRain(double[] array)
{;
double maximum = array[1];
int value = 0;
for (int i=0; i < 12; i = i++)
{
if (array[i] >= maximum)
maximum = array[i];
value = i;
}
return value;
}
public static double leastRain(double[] array)
{
double minimum = array[0];
int value;
for (int i=0; i < 12; i++)
{
if (array[i] <= minimum)
minimum = array[i];
value = i;
}
return value;
}
}
Your program hangs because of this line:
for (int i=0; i < 12; i = i++)
The problem is that i++ returns the value of i before the variable is incremented, so the increment step of your loop is the same as writing i=i. Thus, the variable i never reaches the escape condition of the loop.
it should be:
for (int i=0; i < 12; i++)
Cleaned up your code a little bit, can be improved a lot. There were many simple errors.
import java.util.Scanner;
import java.text.DecimalFormat;
public class Rainfall
{
public static void main(String[] args)
{
final int MONTHS = 12;
double[] rain = new double[MONTHS];
initRain(rain);
double total = totalRain(rain);
double average = averageRain(rain, total);
int most = mostRain(rain);
int least = leastRain(rain);
DecimalFormat digit = new DecimalFormat("#.0");
System.out.println("The total rainfall of the year is " + digit.format(total));
System.out.println("The average rainfall of the year is " + digit.format(average));
System.out.println("The month with the highest amount of rain is " + (most + 1));
System.out.println("The month with the lowest amount of rain is " + (least + 1));
}
public static void initRain(double[] array)
{
Scanner keyboard = new Scanner(System.in);
for (int x = 0; x < array.length; x++)
{
System.out.print("Enter Rainfall for month " + (x + 1) + ": ");
array[x] = keyboard.nextDouble();
}
}
public static double totalRain(double[] array)
{
double total = 0;
for (int x = 0; x < 12; x++)
total += array[x];
return total;
}
public static double averageRain(double[] array, double total)
{
return total / array.length;
}
public static int mostRain(double[] array)
{
double maximum = array[1];
int value = 0;
for (int i=0; i < 12; i++) {
if (array[i] >= maximum) {
maximum = array[i];
value = i;
}
}
return value;
}
public static int leastRain(double[] array)
{
double minimum = array[0];
int value = 0;
for (int i=0; i < 12; i++)
{
if (array[i] <= minimum) {
minimum = array[i];
value = i;
}
}
return value;
}
}