Design a class TemperaturePattern that has the following two fields: number_of_d
ID: 3816756 • Letter: D
Question
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.
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
Explanation / Answer
We have designed a class TemperaturePattern which returns the temperature on the final day within the given period of time after reading the temperature on the first day and the time period in days from the user.
//import java packages Scanner,Random and io.*
import java.util.Scanner;
import java.util.Random;
import java.io.*;
public class TemperaturePattern
{
//class starts,declare variables
int number_of_days_in_period;
int temperature_on_first_day;
//a constructor to set the values of these two fields :number_of_days_in_period, temperature_on_first_day
public void setTemp(int n,int t1)
{
number_of_days_in_period=n;
temperature_on_first_day=t1;
}
//A method to generate final Temperature within given range using max and min temp values from user
public static int genTempPattern(int min, int max)
{
Random rand=new Random();;
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
//main begins
public void main(String []args){
int days,temp,finaltemp,min,max;
//intitalise new object of class and read input using Scanner class
TemperaturePattern tp=new TemperaturePattern();
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of days in the specified period: ");
days = in.nextInt();
System.out.println("Enter the temperature on the first day: ");
temp=in.nextInt();
setTemp(days,temp);
System.out.println("Enter the minimum and maximum temperature values in your data");
//Read int values of input using nextInt()
min=in.nextInt();
max=in.nextInt();
finaltemp=genTempPattern(min,max);
//Display final day temperature
System.out.println("Temperature on the final day would be: "+finaltemp);
//main ends
}
//class ends
}
Output:
Enter the number of days in the specified period:
4
Enter the temperature on the first day:
25
Enter the minimum and maximum temperature values in your data
10
40
Temperature on the final day would be: 28
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.