CPSC/DTSC 115L: Introduction to Computer Science Fall 2026

Laboratory 2: Your first own program

Wednesday, September 16
Thursday, September 17

As done last week, you are expected to work with an assigned partner as a pair. Both you and your partner will receive the same grade. Both of you should always save your laboratory work on your own accounts.

Objectives

The main objectives of this laboratory are
  1. to learn how to design and implement your first own program by following the three basic steps of program development, and
  2. to continue practicing writing, compiling, running and revising Java programs.

1. Preparation

To begin, open a Terminal window and create a subdirectory named lab2 within your cpsc115 directory (if you forgot how to create directories, review Laboratory 1). You will do all your work in the directory lab2 today.

2. Warm-up exercise

Katherine was asked to design and implement a simple Java program to convert a temperature given in Celsius to the equivalent temperature in Fahrenheit. Here is her I/O specification.
Input: a temperature in Celsius
Output: the equivalent temperature in Fahrenheit
How 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:
  1. Input a temperature in Celsius and call it c.
  2. Compute f = 9/5 × c + 32.
  3. Output the equivalent temperature f.
Katherine then translates her algorithm into Java code as follows:
/**
 * 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.

3. Course-grade calculator

Design and implement a Java program that will prompt the user for three exam scores and compute the course grade based on these scores. We assume that each of Exams 1 and 2 is worth 30% of the course grade, whereas Final Exam is worth 40% of the course grade. The exam grades are in integers between 0 and 100. The course grade is a real number in the same range. Your program should behave as follows:
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%
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.

Documentation

As done in Laboratory 1, document both source files FtoC.java and computeGrade.java in the Javadoc format using a comment block as shown above. As joint work, list both your and your partner's names.

What to hand in

For completion, having all your work checked by the instructor or a TA, each partner should now submit individually both source files FtoC.java and computeGrade.java to this Moodle site:
https://moodle.trincoll.edu/mod/assign/view.php?id=658046
If you have completed this laboratory early, stay and work on Homework 2 for the rest of the time.


* CPSC/DTSC 115L home page
Valid HTML 4.01!