Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Assume that the temperature fluctuates with a particular pattern in Albany. I

ID: 3804004 • Letter: 1

Question

1. Assume that the temperature fluctuates with a particular pattern in Albany. It decreases by two degree every day in first half of a given period and increases by one degree in the second half If the number of days in a specified period is odd, the first half will be considered a day less than the second half Write a Java program that will first ask users to enter the number of days in the specified period and the temperature on the first day. The program should output a table showing the days and the temperatures on those days. For example, if a user enters as follows: Number of the days in the period: 11 Temperature on the first day: -10 The program should display the following: Day Temperature 10 12 14 16 18 17 16 15 14 10 13 11 12

Explanation / Answer

Answer 1:
Temperature.java file

import java.util.Scanner;
public class Temperature {
public static void main(String[] args) {

   int day;
   double temp;
   Scanner sc=new Scanner(System.in);
   System.out.println("Enter The Days In Specified period:");
   day=sc.nextInt();
   System.out.println("Enter the Temperature of First day:");
   temp=sc.nextDouble();
   calculate(day,temp);
}//end of main

private static void calculate(int day, double temp) {
   int divide=0;
   divide=day/2;
   System.out.println("Day       Temperature");
   for(int iloop=0;iloop<day;iloop++)
   {
       if(iloop<divide)   //decrease in temperature
       {
           System.out.println(iloop+1+"       "+temp);
           if(iloop<divide-1)
           {
               temp=temp-2;
           }
       }
       else   //increase in temperature
       {
           temp=temp+1;
           System.out.println(iloop+1+"       "+temp);
       }
   }
  
}

}//end of class Temperature


/*******OUTPUT**********
Enter The Days In Specified period:
11
Enter the Temperature of First day:
-10
Day       Temperature
1       -10.0
2       -12.0
3       -14.0
4       -16.0
5       -18.0
6       -17.0
7       -16.0
8       -15.0
9       -14.0
10       -13.0
11       -12.0

*/

Answer 2:  

Numbers Stored in the mytext.txt file
30
44
23
56
43
2
32
35
90
12

Number.java file

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Number {

public static void main(String[] args) throws IOException {
  
   int num;
   Scanner sc=new Scanner(System.in);
   System.out.println("The Number to check:");
   num=sc.nextInt();
   check(num);
   sc.close();

}//end of main

private static void check(int num) {
   int count=0;
   int check;
   String input;
   try
   {
       BufferedReader br= new BufferedReader(new FileReader("mytext.txt"));
       while((input=br.readLine()) != null )
       {
           check=Integer.parseUnsignedInt(input);   //store number in local variable
           if(check<num)   //if number less than given number
           {
               count=count+1;
           }
       }
       br.close();
       }catch(IOException e)
       {
           System.out.println(e);
       }
      
       System.out.println("There are "+count+" numbers below "+num);
   }
}//end of number class


/*********OUTPUT*********
The Number to check:
40
There are 6 numbers below 40
*/