java rainfall program

Reply

Join Date: Apr 2009
Posts: 13
Reputation: custurd12 is an unknown quantity at this point 
Solved Threads: 0
custurd12 custurd12 is offline Offline
Newbie Poster

java rainfall program

 
0
  #1
May 21st, 2009
  1.  
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5.  
  6. public class lab11_2{
  7. Scanner in=new Scanner(System.in);
  8. private int month=12;
  9. private int sum;
  10. private int value;
  11. private int rainfall;
  12. private double total=0;
  13. private double average;
  14. private double standard_deviation;
  15. private double largest;
  16. private double smallest;
  17. private double months[];
  18. public lab11_2(){
  19. months=new double[12];
  20. }
  21. public void setMonths(){
  22. for(int n=1; n<=month; n++ ){
  23. n=in.nextInt();
  24. }
  25. }
  26. public double getTotal(){
  27. double total=0;
  28. for(int i=0; i<12;i++){
  29. total=total+months[i];
  30. System.out.println("The total rainfall for the year is"+ total);
  31. }
  32. return total;
  33. }
  34. public double getAverage(){
  35. average=total/value;
  36. System.out.println("The average rainfall of the year is"+ average);
  37. total=in.nextDouble();
  38. return getTotal()/12;
  39.  
  40. }
  41.  
  42. public double getLargest()
  43. {
  44. double largest=0;
  45. for ( int i = 0; i < 12; i++)
  46. {
  47. if ( months[i] > largest)
  48. {
  49. largest = months[i];
  50. System.out.println("The largest amount of rainfall was" +largest+"inches in month 8");
  51. }
  52.  
  53. }
  54. return largest;
  55. }
  56.  
  57. public double getSmallest()
  58.  
  59. {
  60. double smallest = value;
  61. for ( sum = 1; sum < month; sum++)
  62. {
  63. if (value < smallest )
  64. {
  65. smallest = value;
  66. System.out.println("The smallest amount of rainfall was" +smallest+"inches in month 11");
  67. }
  68. }
  69. return smallest;
  70. }
  71. public double getStandard_deviation(){
  72. double n = 0;
  73. standard_deviation=Math.sqrt(n);
  74. n=Math.pow(((rainfall-average, 2.0)/months-1)));
  75. }
  76. public static void main(String[]args){
  77. lab11_2 r =new lab11_2();
  78. System.out.println("Enter the rainfall (in inches) for month #1:");
  79. System.out.println("Enter the rainfall (in inches) for month #2:");
  80. System.out.println("Enter the rainfall (in inches) for month #3:");
  81. System.out.println("Enter the rainfall (in inches) for month #4:");
  82. System.out.println("Enter the rainfall (in inches) for month #5:");
  83. System.out.println("Enter the rainfall (in inches) for month #6:");
  84. System.out.println("Enter the rainfall (in inches) for month #7:");
  85. System.out.println("Enter the rainfall (in inches) for month #8:");
  86. System.out.println("Enter the rainfall (in inches) for month #9:");
  87. System.out.println("Enter the rainfall (in inches) for month #10:");
  88. System.out.println("Enter the rainfall (in inches) for month #11:");
  89. System.out.println("Enter the rainfall (in inches) for month #12:");
  90. r.setMonths();
  91. System.out.println(" Total" + r.getTotal());
  92. System.out.println("Smallest" + r.getSmallest());
  93. System.out.println("Largest" + r.getLargest());
  94. System.out.println("Average" + r.getAverage());
  95.  
  96. }
  97.  
  98. }


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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,186
Reputation: JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light 
Solved Threads: 172
JamesCherrill JamesCherrill is offline Offline
Veteran Poster

Re: java rainfall program

 
0
  #2
May 21st, 2009
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 return anything
Last edited by JamesCherrill; May 21st, 2009 at 11:13 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 13
Reputation: custurd12 is an unknown quantity at this point 
Solved Threads: 0
custurd12 custurd12 is offline Offline
Newbie Poster

Re: java rainfall program

 
0
  #3
May 21st, 2009
When you say i didnt call set months anywhere, are you talking when i put r.setMonths() under System.out.println("Enter the rainfall (in inches) for month #12:");?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,186
Reputation: JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light 
Solved Threads: 172
JamesCherrill JamesCherrill is offline Offline
Veteran Poster

Re: java rainfall program

 
0
  #4
May 21st, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 13
Reputation: custurd12 is an unknown quantity at this point 
Solved Threads: 0
custurd12 custurd12 is offline Offline
Newbie Poster

Re: java rainfall program

 
0
  #5
May 21st, 2009
yup...thats what supposed to happen, but ummm...how would i take the number as parameter and set the one month?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,186
Reputation: JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light 
Solved Threads: 172
JamesCherrill JamesCherrill is offline Offline
Veteran Poster

Re: java rainfall program

 
0
  #6
May 21st, 2009
public void setMonth(int m){
months[m] =in.nextInt();
}
...
r.setMonth(0); // etc
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 13
Reputation: custurd12 is an unknown quantity at this point 
Solved Threads: 0
custurd12 custurd12 is offline Offline
Newbie Poster

Re: java rainfall program

 
0
  #7
May 21st, 2009
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());

}

}
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,186
Reputation: JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light 
Solved Threads: 172
JamesCherrill JamesCherrill is offline Offline
Veteran Poster

Re: java rainfall program

 
0
  #8
May 21st, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 618 | Replies: 7
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC