Dana Vrajitoru
C101/I210 Computer Programming

C101/I210 Lab 3

Date: Wednesday, January 29, 2020.
Due date: Wednesday, February 5, 2020.

Objective: To use variables in Java, arithmetic expressions, and input / output statements.

Ex. 1. Temperature Conversion.

a. Project.

Create an Eclipse project called lab3, and inside, create a class called Convert in a package called lab3 and containing the main method. Write your name, course, semester, and lab number at the top as a comment.

b. Declaring variables.

Declare two float variables called tempF and tempC, representing a temperature value in degrees Fahrenheit and Celsius respectively. Assign a value that you want to tempF, such as 80.

c. Computation.

Assign a value to the variable tempC that is computed as the value of tempF with the following formula:

(tempF - 32) / 9 * 5

d. Output.

Output the result with the following line that appends the text "The temperature in Celsius is" with the value of tempC converted to a string:

System.out.println("The temperature in Celsius is " + tempC);

Test the program with various values for the temperature to be converted. For example, 80F converts to 26.67C, 32F to 0C, -40F to -40C, 20F to -6.67C, 50F to 10C.

Ex. 2. Inputting the temperature.

At the top of the program, before the class declaration, but inside the package declaration, add the following line:

import java.util.Scanner;

In the main, before or after you declare the variables, add a declaration of a Scanner:

Scanner scan = new Scanner(System.in);

Then before the assignment converting temperature to Celsius, output a message asking for the value of the temperature in Fahrenheit, then input a float value using the scanner into tempF:

tempF = scan.nextFloat();

Now you rerun the program to covert a different value to Celsius without rewriting the code. Test the program to make sure it work.

Turn in: the file Convert.java.

Upload it to Canvas in Assignments - Homework 3, or wait until you have completed the homework to upload both files at the same time.