Question #59395

Class Month has the followin instance variables:

a variable ‘name’ to store name of the month
an int array days can hold 28 30 or 31 values. Days used to store max daily temps
Constructor to specify size array & month name
setDays to specify size of array
setMonth stores name of month in name variable
getMonth to return name of the month
setTemps to allow all temps for that month to be entered into the days array
getTemp returns temp on a specified day

App program:

Creates an array of Month objects called Year & assigns appropriate names & num of days to each month
Create loop in the program to allow the temps for Jan & Feb to be entered
Display temp on 3rd Jan & 15th Feb
Write a method to display the highest temp in the 2 months & date on which it occurred
Find avg temp in a specified month
Find range of temps in a specified month (highest – lowest temp)
Find number of days in a specified month that the temp was higher than a specified value

Expert's answer

public class Month {
private String name;
private int[] days;
public Month(String name, int size) {
this.name = name;
days = new int[size];
}
public String getMonth() {
return name;
}
public void setMonth(String name) {
this.name = name;
}
public void setDays(int[] days) {
this.days = days;
}
public int getDays() {
return days.length;
}
public void setTemps(int day, int temp) {
days[day] = temp;
}
public int getTemp(int day) {
return days[day];
}
}


import java.util.Scanner;


public class App {
public static void main(String[] args) {
Month Year[] = new Month[12]; //create array
Year[0] = new Month("January", 31); //assigns appropriate names &
num of days to each month
Year[1] = new Month("February", 28);
Year[2] = new Month("March", 31);
Year[3] = new Month("April", 30);
Year[4] = new Month("May", 31);
Year[5] = new Month("June", 30);
Year[6] = new Month("July", 31);
Year[7] = new Month("August", 31);
Year[8] = new Month("September", 30);
}
Year[9] = new Month("October", 31);
Year[10] = new Month("November", 30);
Year[11] = new Month("December", 31);
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 2; i++) { //Create loop in the program to allow the temps for Jan & Feb to be entered
for (int j = 0; j < Year[i].getDays(); j++) {
int temp = sc.nextInt();
Year[i].setTemps(j, temp);
}
}
//Display temp on 3rd Jan & 15th Feb
System.out.println(Year[0].getTemp(2));
System.out.println(Year[0].getTemp(14));
highest(Year[0], Year[1]);
System.out.println(avg(Year[0])); //Find avg temp in a specified month
System.out.println(minTemp(Year[0])); //Find range of temps in a specified month (highest - lowest temp)
System.out.println(maxTemp(Year[0]));
System.out.println(number(Year[0], 10)); //Find number of days in a specified month that the temp was higher than a specified value
}
//Write a method to display the highest temp in the 2 months & date on which it occurred
public static void highest(Month m1, Month m2){
int maxTemp = m1.getTemp(0);
String month = m1.getMonth();
int index = 0;
for (int i = 1; i < m1.getDays(); i++) {
if(m1.getTemp(i) > maxTemp){
maxTemp = m1.getTemp(i);
index = i;
}
}
for (int i = 0; i < m2.getDays(); i++) {
if(m2.getTemp(i) > maxTemp){
maxTemp = m2.getTemp(i);
month = m2.getMonth();
index = i;
}
}
System.out.println(month + " " + (index + 1) + " " + maxTemp);
}
public static int avg(Month m){
int average = 0;
for (int i = 0; i < m.getDays(); i++) {
average += m.getTemp(i);
}
return average / m.getDays();
}
public static int minTemp(Month m){
int min = m.getTemp(0);
for (int i = 1; i < m.getDays(); i++) {
if (min > m.getTemp(i)) {
min = m.getTemp(i);
}
}
return min;
}
public static int maxTemp(Month m) {
int max = m.getTemp(0);
for (int i = 1; i < m.getDays(); i++) {
if (max < m.getTemp(i)) {
max = m.getTemp(i);
}
}
return max;
}
public static int number(Month m, int temp) {
int count = 0;
for (int i = 0; i < m.getDays(); i++) {
if (m.getTemp(i) > temp) {
count++;
}
}
return count;
}


http://www.AssignmentExpert.com/

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS