| CPSC/DTSC 115L: Introduction to Computer Science | Fall 2026 |
Input: a temperature in CelsiusHow should Katherine do this conversion? She knows that the freezing temperature 0 °C is equal to 32 °F, and the boiling temperature 100 °C is equal to 212 °F. From this, she computes the ratio of Fahrenheit to Celsius as follows: (212-32)/100 = 180/100 = 9/5. Then, using f for Fahrenheit and c for Celsius, the conversion formula must have the form f = 9/5 × c + k for some constant k. Since she knows that f = 32 when c = 0, this means that k must be equal to 32. In summary, she comes up with the following algorithm in pseudocode:
Output: the equivalent temperature in Fahrenheit
/**
* Class name: CtoF
* A Java application to convert a temperature in Celsius to the equivalent
* temperature in Fahrenheit.
*
* @author Katherine Johnson
* @version 1.0, 09/16/2026
*/
import java.io.*; // I/O classes
import java.util.Scanner; // Scanner class
public class CtoF {
public static void main(String args[]) throws IOException {
Scanner s = new Scanner(System.in);
System.out.print("Enter a temperature in Celsius: ");
int c = s.nextInt();
double f = (9.0/5.0)*c + 32;
System.out.println("The equivalent temperature in Fahrenheit is " +
f + " degrees.");
} // main()
} // CtoF
Now, modify Katherine's solution to design and implement a Java program to do
the exact opposite: convert a temperature given in Fahrenheit to the
equivalent temperature in Celsius. For this, first write down on a piece of
paper your I/O specification and algorithm in pseudocode. Then
implement your algorithm in a new Java class named FtoC in a file
named FtoC.java. After compiling it successfully, run your program
with five test cases, including 32 °F and 212 °F. Does your program
give correct output? When completed, show your work to the instructor or a TA.
As before, first write down on a piece of paper your I/O specification and algorithm in pseudocode. Then implement your algorithm in a Java class named computeGrade in a file named computeGrade.java. After compiling it successfully, run your program with five test cases. When completed, show your work to the instructor or a TA.Enter the score of Exam 1: 87 Enter the score of Exam 2: 78 Enter the score of Final Exam: 77 Your course grade is: 80.3%
https://moodle.trincoll.edu/mod/assign/view.php?id=658046If you have completed this laboratory early, stay and work on Homework 2 for the rest of the time.
|
|
CPSC/DTSC 115L home page |
|
|