Welcome to Dream.In.Code
Become a Java Expert!

Join 150,416 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,037 people online right now. Registration is fast and FREE... Join Now!




Java array problem

 
Reply to this topicStart new topic

Java array problem, Find hottest and coldest days of the year

Brake2583
19 Mar, 2008 - 05:42 AM
Post #1

New D.I.C Head
*

Joined: 20 Feb, 2008
Posts: 6

Hello, I am trying to write a program to find the hottest and coldest days of the year, but the format for the output must be something like The hottest day of the year was Monday July 14 and The coldest day of the year was Saturday December 29. I have been working on this for several days and still haven't been able to crack it. Any help would be greatly appreciated.
Thank you.

CODE

import java.util.*;  
class Exercise2
{
public static void main(String args[])
{
  int year[]=new int [365];
  int a=0;
  int above75 = 0;
  int below33 = 0;
  int monthSum1=0;
  int jan;
  while (a<=30)     
  {
   jan=(int)Math.floor(25+Math.random()*(38-25+1));
   year[a]=jan;
   monthSum1=monthSum1+jan;
   if (jan>75)
   {
    above75++;
   }
   else if (jan<33)
   {
    below33++;
   }
   a++;
  }
  int feb;  
  int monthSum2=0;  
  while (a>30&&a<=58)  
  {  
   feb=(int)Math.floor(26+Math.random()*(40-26+1));  
   year[a]=feb;  
   monthSum2=monthSum2+feb;
   if (feb>75)
   {
    above75++;
   }
   else if (feb<33)
   {
    below33++;
   }
   a++;     
  }  
  int mar;
  int monthSum3=0;    
  while (a>58&&a<=89)    
  {
   mar=(int)Math.floor(34+Math.random()*(48-34+1));
   year[a]=mar;
   monthSum3=monthSum3+mar;
   if (mar>75)
   {
    above75++;
   }
   else if (mar<33)
   {
    below33++;
   }
   a++;
  }    
  int apr;
  int monthSum4=0;
  while (a>89&&a<=119)
  {
   apr=(int)Math.floor(43+Math.random()*(58-43+1));
   year[a]=apr;
   monthSum4=monthSum4+apr;
   if (apr>75)
   {
    above75++;
   }
   else if (apr<33)
   {
    below33++;
   }
   a++;    
  }            
  int may;
  int monthSum5=0;
  while (a>119&&a<=150)
  {
   may=(int)Math.floor(52+Math.random()*(68-52+1));
   year[a]=may;
   monthSum5=monthSum5+may;
   if (may>75)
   {
    above75++;
   }
   else if (may<33)
   {
    below33++;
   }
   a++;    
  }            
  int jun;
  int monthSum6=0;
  while (a>150&&a<=180)
  {
   jun=(int)Math.floor(62+Math.random()*(77-62+1));
   year[a]=jun;
   monthSum6=monthSum6+jun;
   if (jun>75)
   {
    above75++;
   }
   else if (jun<33)
   {
    below33++;
   }
   a++;    
  }            
  int jul;
  int monthSum7=0;
  while (a>180&&a<=211)
  {
   jul=(int)Math.floor(68+Math.random()*(83-68+1));
   year[a]=jul;
   monthSum7=monthSum7+jul;
   if (jul>75)
   {
    above75++;
   }
   else if (jul<33)
   {
    below33++;
   }
   a++;    
  }            
  int aug;
  int monthSum8=0;
  while (a>211&&a<=242)
  {
   aug=(int)Math.floor(60+Math.random()*(75-60+1));
   year[a]=aug;
   monthSum8=monthSum8+aug;
   if (aug>75)
   {
    above75++;
   }
   else if (aug<33)
   {
    below33++;
   }
   a++;    
  }            
  int sep;
  int monthSum9=0;
  while (a>242&&a<=272)
  {
   sep=(int)Math.floor(49+Math.random()*(65-49+1));
   year[a]=sep;
   monthSum9=monthSum9+sep;
   if (sep>75)
   {
    above75++;
   }
   else if (sep<33)
   {
    below33++;
   }
   a++;    
  }            
  int oct;
  int monthSum10=0;
  while (a>272&&a<=303)
  {
   oct=(int)Math.floor(41+Math.random()*(54-41+1));
   year[a]=oct;
   monthSum10=monthSum10+oct;
   if (oct>75)
   {
    above75++;
   }
   else if (oct<33)
   {
    below33++;
   }
   a++;    
  }            
  int nov;
  int monthSum11=0;
  while (a>303&&a<=333)
  {
   nov=(int)Math.floor(31+Math.random()*(43-31+1));
   year[a]=nov;
   monthSum11=monthSum11+nov;
   if (nov>75)
   {
    above75++;
   }
   else if (nov<33)
   {
    below33++;
   }
   a++;    
  }            
  int dec;
  int monthSum12=0;
  while (a>333&&a<=364)
  {
   dec=(int)Math.floor(47+Math.random()*(61-47+1));
   year[a]=dec;
   monthSum12=monthSum12+dec;
   if (dec>75)
   {
    above75++;
   }
   else if (dec<33)
   {
    below33++;
   }
   a++;    
  }        
  int monthAverage1 = monthSum1 / 31;
  int monthAverage2 = monthSum2 / 28;
  int monthAverage3 = monthSum3 / 31;
  int monthAverage4 = monthSum4 / 30;
  int monthAverage5 = monthSum5 / 31;
  int monthAverage6 = monthSum6 / 30;
  int monthAverage7 = monthSum7 / 31;
  int monthAverage8 = monthSum8 / 31;
  int monthAverage9 = monthSum9 / 30;
  int monthAverage10 = monthSum10 / 31;
  int monthAverage11 = monthSum11 / 30;
  int monthAverage12 = monthSum12 / 31;
  System.out.println("Avg for Jan is "+monthAverage1);
  System.out.println("Avg for Feb is "+monthAverage2);
  System.out.println("Avg for Mar is "+monthAverage3);
  System.out.println("Avg for Apr is "+monthAverage4);
  System.out.println("Avg for May is "+monthAverage5);
  System.out.println("Avg for Jun is "+monthAverage6);
  System.out.println("Avg for Jul is "+monthAverage7);
  System.out.println("Avg for Aug is "+monthAverage8);
  System.out.println("Avg for Sep is "+monthAverage9);
  System.out.println("Avg for Oct is "+monthAverage10);
  System.out.println("Avg for Nov is "+monthAverage11);
  System.out.println("Avg for Dec is "+monthAverage12);
  System.out.println("The hottest day of the year was "+highesttemp);
  System.out.println("The coldest day of the year was "+lowesttemp);
  System.out.println("Total number of days below 33 degrees Fahrenheit: "+below33);
  System.out.println("Total number of days above 75 degrees Fahrenheit: "+above75);
}  
}


User is offlineProfile CardPM
+Quote Post

letthecolorsrumble
RE: Java Array Problem
19 Mar, 2008 - 06:02 AM
Post #2

Student of The Sun
Group Icon

Joined: 7 Nov, 2007
Posts: 550



Thanked: 1 times
My Contributions
This is a part of code, you will need to apply for each month calculation.

CODE

//intializing highest and lowest temp only once
  int highesttemp = 0;  
  int lowesttemp  = 999;
  
  int jan;
  
  while (a <= 30) {
       jan=(int)Math.floor(25+Math.random()*(38-25+1));
       year[a]=jan;
       monthSum1=monthSum1+jan;
       if (jan>75)
       {
        above75++;
        if(jan > highesttemp)
            highesttemp = jan;
       }
       else if (jan<33)
       {
        below33++;
        if(jan < lowesttemp)
            lowesttemp = jan;
       }
       a++;
  }


I'd suggest you use methods to do your calculations as you can see the code for all the months is repeating.

This is what I received at the output:
CODE

Avg for Jan is 31
Avg for Feb is 33
Avg for Mar is 41
Avg for Apr is 49
Avg for May is 60
Avg for Jun is 67
Avg for Jul is 75
Avg for Aug is 67
Avg for Sep is 58
Avg for Oct is 47
Avg for Nov is 36
Avg for Dec is 54
The hottest day of the year was 83
The coldest day of the year was 25
Total number of days below 33 degrees Fahrenheit: 28
Total number of days above 75 degrees Fahrenheit: 18


Hope that helps :)
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 08:10PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month