Create a class named weather report that holds a daily weather report with data member’s day_of_month, hightemp, lowtemp, amount_rain and amount_snow. Use different types of constructors to initialize the objects. Also include a function that prompts the user and sets values for each field so that you can override the default values. Generate monthly report that displays average of each attribute.
package javaapplication23;
import java.util.Scanner;
public class weather_report {
int day_of_month;
float hightemp, lowtemp, amount_rain, amount_snow;
public weather_report()
{
day_of_month=0;
hightemp=0;
lowtemp=0;
amount_rain=0;
amount_snow=0;
}
public weather_report(int dom,float ht,float lt,float ar,float as)
{
day_of_month=dom;
hightemp=ht;
lowtemp=lt;
amount_rain=ar;
amount_snow=as;
}
public void get()
{
System.out.println("\nEnter the values for the particular day:");
Scanner s=new Scanner(System.in);
System.out.println("\nEnter the day:");
day_of_month=s.nextInt();
System.out.println("\nEnter the high temperature:");
hightemp=s.nextFloat();
System.out.println("\nEnter the low temperature:");
lowtemp=s.nextFloat();
System.out.println("\nEnter the amount_rain:");
amount_rain=s.nextFloat();
System.out.println("\nEnter the amount_snow:");
amount_snow=s.nextFloat();
}
public static void main(String[] args)
{
int n;
int count;
float average;
float l=0,h=0,a=0,arain=0;
Scanner s=new Scanner(System.in);
n=s.nextInt();
weather_report []w;
w=new weather_report[5];
//w[0]= new weather_report();
for( count=0;count<n;count++)
{
w[count]=new weather_report();
w[count].get();
}
for( count=0;count<n;count++)
{
l = l+w[count].lowtemp;
h=(h+w[count].hightemp);
a=(a+w[count].amount_snow);
arain=(arain+w[count].amount_rain);
}
System.out.println("\n avg of lowtemp:"+l/n);
System.out.println("\n avg of hightemp:"+h/n);
System.out.println("\n avg of amount_snow:"+a/n);
System.out.println("\n avg of amount_rain:"+arain/n);
}
}
/*Output:
run:
2
Enter the values for the particular day:
Enter the day:
1
Enter the high temperature:
2
Enter the low temperature:
3
Enter the amount_rain:
4
Enter the amount_snow:
5
Enter the values for the particular day:
Enter the day:
2
Enter the high temperature:
1
Enter the low temperature:
2
Enter the amount_rain:
3
Enter the amount_snow:
4
avg of lowtemp:2.5
avg of hightemp:1.5
avg of amount_snow:4.5
avg of amount_rain:3.5
BUILD SUCCESSFUL (total time: 16 seconds)
*/
Comments
Post a Comment