| | |
java rainfall program
![]() |
•
•
Join Date: Apr 2009
Posts: 13
Reputation:
Solved Threads: 0
Java Syntax (Toggle Plain Text)
import java.io.*; import java.util.*; public class lab11_2{ Scanner in=new Scanner(System.in); private int month=12; private int sum; private int value; private int rainfall; private double total=0; private double average; private double standard_deviation; private double largest; private double smallest; private double months[]; public lab11_2(){ months=new double[12]; } public void setMonths(){ for(int n=1; n<=month; n++ ){ n=in.nextInt(); } } public double getTotal(){ double total=0; for(int i=0; i<12;i++){ total=total+months[i]; System.out.println("The total rainfall for the year is"+ total); } return total; } public double getAverage(){ average=total/value; System.out.println("The average rainfall of the year is"+ average); total=in.nextDouble(); return getTotal()/12; } public double getLargest() { double largest=0; for ( int i = 0; i < 12; i++) { if ( months[i] > largest) { largest = months[i]; System.out.println("The largest amount of rainfall was" +largest+"inches in month 8"); } } return largest; } public double getSmallest() { double smallest = value; for ( sum = 1; sum < month; sum++) { if (value < smallest ) { smallest = value; System.out.println("The smallest amount of rainfall was" +smallest+"inches in month 11"); } } return smallest; } public double getStandard_deviation(){ double n = 0; standard_deviation=Math.sqrt(n); n=Math.pow(((rainfall-average, 2.0)/months-1))); } public static void main(String[]args){ lab11_2 r =new lab11_2(); System.out.println("Enter the rainfall (in inches) for month #1:"); System.out.println("Enter the rainfall (in inches) for month #2:"); System.out.println("Enter the rainfall (in inches) for month #3:"); System.out.println("Enter the rainfall (in inches) for month #4:"); System.out.println("Enter the rainfall (in inches) for month #5:"); System.out.println("Enter the rainfall (in inches) for month #6:"); System.out.println("Enter the rainfall (in inches) for month #7:"); System.out.println("Enter the rainfall (in inches) for month #8:"); System.out.println("Enter the rainfall (in inches) for month #9:"); System.out.println("Enter the rainfall (in inches) for month #10:"); System.out.println("Enter the rainfall (in inches) for month #11:"); System.out.println("Enter the rainfall (in inches) for month #12:"); r.setMonths(); System.out.println(" Total" + r.getTotal()); System.out.println("Smallest" + r.getSmallest()); System.out.println("Largest" + r.getLargest()); System.out.println("Average" + r.getAverage()); } }
Im getting errors finding the standard deviation, also the program wont let me input the rainfall for each of the months...
can you guys help me?
•
•
Join Date: Apr 2008
Posts: 1,186
Reputation:
Solved Threads: 172
You don't seem to call setMonths() anywhere, so that's why it's not doing anything. Perhaps you should call it at the end of your constructor?
I don't know the math for std dev, but your method:
a) sets it to sqrt(n), when n=0. Seems pointless
b) then sets n to some formula, but does nothing with it
c) doesn't
I don't know the math for std dev, but your method:
a) sets it to sqrt(n), when n=0. Seems pointless
b) then sets n to some formula, but does nothing with it
c) doesn't
return anything Last edited by JamesCherrill; May 21st, 2009 at 11:13 am.
•
•
Join Date: Apr 2008
Posts: 1,186
Reputation:
Solved Threads: 172
Ooops Sorry, missed it lurking there. Mea culpa. So your prog will prompt 12 times for the 12 months, then read the input for all 12. Is that what you intend? Maybe change the prompt to "enter rainfall for months 1-12", or change setMonths to take a month number as parameter and just set that one month so you can call it after each month prompt?
•
•
Join Date: Apr 2009
Posts: 13
Reputation:
Solved Threads: 0
okay, i did this....
now getting no output
import java.io.*;
import java.util.*;
public class lab11_2{
Scanner in=new Scanner(System.in);
private int month=12;
private int sum;
private int value;
private int rainfall;
private double total=0;
private double average;
private double standard_deviation;
private double largest;
private double smallest;
private double months[];
public lab11_2(){
months=new double[12];
}
public void setMonths(int m){
for(int n=1; n<=month; n++ ){
months[m]=in.nextInt();
n=in.nextInt();
}
}
public double getTotal(){
double total=0;
for(int i=0; i<12;i++){
total=total+months[i];
System.out.println("The total rainfall for the year is"+ total);
}
return total;
}
public double getAverage(){
average=total/value;
System.out.println("The average rainfall of the year is"+ average);
total=in.nextDouble();
return getTotal()/12;
}
public double getLargest()
{
double largest=0;
for ( int i = 0; i < 12; i++)
{
if ( months[i] > largest)
{
largest = months[i];
System.out.println("The largest amount of rainfall was" +largest+"inches in month 8");
}
}
return largest;
}
public double getSmallest()
{
double smallest = value;
for ( sum = 1; sum < month; sum++)
{
if (value < smallest )
{
smallest = value;
System.out.println("The smallest amount of rainfall was" +smallest+"inches in month 11");
}
}
return smallest;
}
public static void main(String[]args){
lab11_2 r =new lab11_2();
Scanner in= new Scanner(System.in);
r.setMonths(0);
r.setMonths(1);
r.setMonths(2);
r.setMonths(3);
r.setMonths(4);
r.setMonths(5);
r.setMonths(6);
r.setMonths(7);
r.setMonths(8);
r.setMonths(9);
r.setMonths(10);
r.setMonths(11);
r.setMonths(12);
System.out.println(" Total" + r.getTotal());
System.out.println("Smallest" + r.getSmallest());
System.out.println("Largest" + r.getLargest());
System.out.println("Average" + r.getAverage());
}
}
now getting no output
import java.io.*;
import java.util.*;
public class lab11_2{
Scanner in=new Scanner(System.in);
private int month=12;
private int sum;
private int value;
private int rainfall;
private double total=0;
private double average;
private double standard_deviation;
private double largest;
private double smallest;
private double months[];
public lab11_2(){
months=new double[12];
}
public void setMonths(int m){
for(int n=1; n<=month; n++ ){
months[m]=in.nextInt();
n=in.nextInt();
}
}
public double getTotal(){
double total=0;
for(int i=0; i<12;i++){
total=total+months[i];
System.out.println("The total rainfall for the year is"+ total);
}
return total;
}
public double getAverage(){
average=total/value;
System.out.println("The average rainfall of the year is"+ average);
total=in.nextDouble();
return getTotal()/12;
}
public double getLargest()
{
double largest=0;
for ( int i = 0; i < 12; i++)
{
if ( months[i] > largest)
{
largest = months[i];
System.out.println("The largest amount of rainfall was" +largest+"inches in month 8");
}
}
return largest;
}
public double getSmallest()
{
double smallest = value;
for ( sum = 1; sum < month; sum++)
{
if (value < smallest )
{
smallest = value;
System.out.println("The smallest amount of rainfall was" +smallest+"inches in month 11");
}
}
return smallest;
}
public static void main(String[]args){
lab11_2 r =new lab11_2();
Scanner in= new Scanner(System.in);
r.setMonths(0);
r.setMonths(1);
r.setMonths(2);
r.setMonths(3);
r.setMonths(4);
r.setMonths(5);
r.setMonths(6);
r.setMonths(7);
r.setMonths(8);
r.setMonths(9);
r.setMonths(10);
r.setMonths(11);
r.setMonths(12);
System.out.println(" Total" + r.getTotal());
System.out.println("Smallest" + r.getSmallest());
System.out.println("Largest" + r.getLargest());
System.out.println("Average" + r.getAverage());
}
}
•
•
Join Date: Apr 2008
Posts: 1,186
Reputation:
Solved Threads: 172
setMonths now contains a mixture of the old code and the new, and it's really not going to work! All you need is the code I posted before. The user interface would be clearer if you left in the original prompts before each setMonths(...) call. ie
System.out.println("Enter the rainfall (in inches) for month #1:");
setMonths(0);
System.out.println("Enter the rainfall (in inches) for month #2:");
setMonths(1);
etc
System.out.println("Enter the rainfall (in inches) for month #1:");
setMonths(0);
System.out.println("Enter the rainfall (in inches) for month #2:");
setMonths(1);
etc
![]() |
Similar Threads
- very basic java GUI Program... (Java)
- Need Help with java Inventory program Part3 (Java)
- Java date program (Java)
- What is wrong with my java program (Java)
- Java math program (Java)
- Java BankAccount program (Java)
- New to Java, can't get program to compile (Java)
- Very New to java, skeleton of program given (Java)
- cheeky java program (Java)
Other Threads in the Java Forum
- Previous Thread: how to draw rectangle on applet based on mousedragevent
- Next Thread: How to Add Configuration in Java
Views: 618 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for Java
access actionlistener android applet arguments array arrays binary bluetooth build c# chat class classes client code combobox component convert coordinates data database desktop draw eclipse error event exception fast file filei/o fractal givemetehcodez graphics gui helpwithhomework html ide image inheritance input integer interface j2me java javafx jframe jmf jni jpanel jtextfield lazy linked linked-list list loop main method methods mobile netbeans newbie number object oracle output page parameter pattern phone pixel print problem program programming read regex remove robot scanner search server service set size sms socket software sort sql string swing system text thread timer translate tree url web






