To be written using Java; Design a class TemperaturePattern that has the followi
ID: 3817630 • Letter: T
Question
To be written using Java;
Design a class TemperaturePattern that has the following two fields: number_of_days_in_period and temperature_on_first_day. Class should have
a constructor to set the values of these two fields.
a method findFinalDayTemperature that would return the temperature on the final day of the period specified by the number_of_days_in_period field. The temperature fluctuates in a similar way as described in Homework 4, Question 1.
Test this class, the program will first ask the users to enter the number of days in the specified period and the temperature on the first day.
For example, if a user enters as follows:
Number of the days in the period: 11
Temperature on the first day: -10
Then the program should display the following:
Temperature on the final day would be: -12
way temp fluctuates in question 4. #1
The method should display the following: Day Temperature
1 -10
2 -12
3 -14
4 -16
5 -18
6 -17
7 -16
8 -15
9 -14
10 -13
11 -12
Explanation / Answer
//TemperaturePattern.java
import java.util.Scanner;
public class TemperaturePattern {
//Attributes
int number_of_days_in_period,temperature_on_first_day;
//Constructor
public TemperaturePattern(int number_of_days_in_period,int temperature_on_first_day){
this.number_of_days_in_period = number_of_days_in_period;
this.temperature_on_first_day = temperature_on_first_day;
}
//Find Final Day Temperature
public int findFinalDayTemperature(){
int i=0;
int d = number_of_days_in_period/2;
int temp = this.temperature_on_first_day;
//Iterating till number of days
while(number_of_days_in_period>i){
//Checking for temperature
if(i>=d){
if(i==d){
temp = temp+2;
}
++temp;
System.out.println((i+1)+" "+temp);
}else{
//Variation in the temperature
System.out.println((i+1)+" "+temp);
temp = temp-2;
}
i++;
}
return temp; //Returns temperature
}
public static void main(String[] args) {
//REading temperature and number of days
Scanner s = new Scanner(System.in);
System.out.println("Enter the number of Days in the specified Period:");
int days = s.nextInt();
//REading temperature
System.out.println("Enter the temperature in the first Day:");
int temp = s.nextInt();
//Passing temp and days into Constructor
TemperaturePattern tp = new TemperaturePattern(days,temp);
//Calculating finalDayTemperature
int fp = tp.findFinalDayTemperature();
System.out.println("The final Day temperature is:"+fp);
}
}
Output:
Enter the number of Days in the specified Period:
11
Enter the temperature in the first Day:
-10
1 -10
2 -12
3 -14
4 -16
5 -18
6 -17
7 -16
8 -15
9 -14
10 -13
11 -12
The final Day temperature is:-12
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.