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: 3805281 • 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

1 -10

2 -12

3 -14

4 -16

5 -18

6 -17

7 -16

8 -15

9 -14

10 -13

11 -12

2. Write a program that asks the user to enter the name of a file, and then asks the user to enter an integer number. The program should display the total count of numbers that are lesser than the input number. User Notepad or another text editor to create a simple file that can be used to test the program.

First, the user enters the file name, as:

Enter the file name: myText.txt

For example, if the file ‘mytext.txt’ contains the following numbers:

30

44

23

56

43

2

32

35

90

12

and then if the user enters an integer number, as:

The number to check: 40

the program should display the output as:

There are 6 numbers below 40

Explanation / Answer


// FluctuatingTemperature.java
import java.util.Scanner;

public class FluctuatingTemperature
{

   public static void main(String[] args)
   {
      
       Scanner scnr = new Scanner(System.in);
       int days;
       double temperature;

       System.out.println("Number of the days in the period: ");
       days = scnr.nextInt();
       System.out.println("Temperature on the first day: ");
       temperature = scnr.nextDouble();

       System.out.println(" Day Temperature");
       System.out.println("1 "+ temperature );

       for (int i = 2; i <= days/2; i++ )
       {
           temperature = temperature -2;
           System.out.println(i + " "+ temperature );
       }

       for (int i = (days/2)+1; i <= days; i++ )
       {
           temperature = temperature +1 ;
           System.out.println(i + " "+ temperature );
       }

   }

}

/*
output:

Number of the days in the period:
11
Temperature on the 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

*/




// ReadIntegerFile.java
import java.util.Scanner;
import java.io.File;
import java.io.*;

public class ReadIntegerFile
{

   public static void main(String[] args) throws IOException
   {
       Scanner scnr = new Scanner(System.in);
       Scanner scanner = new Scanner(new File("input.txt"));
       System.out.println("Enter an integer: ");
       int number = scnr.nextInt();

       int count = 0;
       while(scanner.hasNextInt())
       {
       if(scanner.nextInt() < number)
           count++;
       }

       System.out.println("There are " + count + " numbers below " + number);
   }

}

/*
input.txt
30
44
23
56
43
2
32
35
90
12

output:
Enter an integer:
40
There are 6 numbers below 40


*/