Overview

The purpose of this assignment is to ensure that all students are able to create, compile, and run Java programs in a terminal using Windows Subsystem for Linux.

Pre-Lab Work

Before coming to lab, complete the following steps to install the Windows Subsystem for Linux (WSL) onto your machine and configure it to compile and run Java programs.

Open a Command Window as an Administrator

  1. In the search bubble at the bottom of the screen, type com
  2. In the window that pops up, find the App "Command Prompt"
  3. If the "Command Prompt" App is on the right side of the window, click on the "run as administrator" line. (If the "Command Prompt" App is on the left side of the window, click on the corresponding arrow to get it to appear on the right side.)
  4. You should be asked to give permission to the App and the "Command Prompt" window should have "Administrator" as part of the title for the window. If it doesn’t, close the window and repeat from step 1. (If you just click on the App icon, the App will not open with administrator permission.)

Install correct version of Ubuntu

  1. Type wsl --install on the prompt line. wsl stands for Windows Subsystem for Linux. The computer will print many lines of text in the window . Be patient.
  2. When the text prompts you to reboot the system, reboot your laptop.
  3. When your laptop is back online, it should open the "Command Prompt" automatically. There may be an error message. Close the window.
  4. Reopen the "Command Prompt" window. (Use the instructions given above.)
  5. Type wsl -l this command will prompt the computer to tell us which version of Ubuntu is installed. Most likely it will be the basic version. We want to use Ubuntu 24.04
  6. Type wsl --install -d Ubuntu-24.04 This tells the computer to install the desired version of Ubuntu. Check the text the computer displays to verify that this is the case.
  7. When prompted, enter a username. This does not need to be your MSOE username.
  8. When prompted, enter a password. The cursor will not move as you type. Be sure to record this password as we will need it later and there is no way to recover it. You will be asked to enter your password twice.
  9. The computer will print many lines of text. At the bottom of the text there should be a green prompt that looks like username@otherstuff. Type exit to leave the Ubuntu system.
  10. Type wsl --set-default Ubuntu-24.04 The computer should print that the operation was successful.

Install Java

  1. Type wsl This is the command that will use to start the Windows Subsystem for Linux.
  2. Type sudo apt-get update this makes sure that we have the most current version software in the subsystem (including Java). There will be a lot of updates listed. sudo means that this is an administrator task, you will be asked for your password (The one you created when installing Ubuntu.)
  3. Type sudo apt install default-jdk If you did this quickly enough you will not have to re-enter your password. It will list all the things that it needs to install.
  4. Type Y when prompted.
  5. Type java -version and verify that it installed version 21.0.3
  6. Type exit to exit the Windows Subsystem for Linux.
  7. Type exit again to close the command window.

Creating a folder/directory for the course

  1. Run the "Command Prompt" App (not as an administrator). You should see a prompt with C:\Users\[username].
  2. Type wsl to start WSL. You should see a prompt with blue text of /mnt/c/Users/[username]
  3. Type mkdir csc1110 to create a folder/directory for this class. (make directory)
  4. Type cd csc1110 to change into that folder/directory. (change directory)
  5. Start the Nano text editor by typing nano FirstProgram.java
  6. Type the following text in the text editor:
public class FirstProgram {
    public static void main(String[] args) {
        System.out.println("It worked");
    }
}
  1. Type CTRL-x to exit Nano (say yes to the prompt to save and keep FirstProgram.java as the filename).
  2. Type ls to list all of the files in the directory. (list) You should see the FirstProgram.java file listed.
  3. Type javac FirstProgram.java to compile the program. (javacompile)
  4. Type ls to list all of the files in the directory. You should see FirstProgram.class added to the list of files.
  5. Type java FirstProgram to run the program. You should see It worked displayed.

If you prefer, you can watch this video that covers many of the steps to install Windows Subsystem for Linux (WSL) onto your machine and get it set up to run Java programs.

The remaining portion of this assignment will be completed during lab time.

Part I

The following program asks users for the number of credits they are taking and the number of hours they plans to study each week. The program calculates the recommended number of hours to study based on the number of credits and compares that to the student's plan.

/* 
 * Course: CSC-1110
 * Fall 2025
 * Lab 1 - First Program
 * Name: Dr. Chris Taylor
 * Created: 06/10/24
 */

import java.util.Scanner;

/**
 * Class containing the entire program for lab 1 in Fall 2025 CSC-1110 course. 
 * @author taylor
 * @version 2024-06-10
 */
public class StudyTime {

    /**
     * Simple program to determine help set study expectations for a new
     * college student.
     * @param args ignored
     */
    public static void main(String[] args) {
        // Create a "reference variable"/object to gather data
        // from the keyboard
        Scanner in = new Scanner(System.in);
        
        // Request data from the user
        System.out.print("Enter the number of credits you are taking this term: ");
        int creditsTaken = in.nextInt();
        System.out.print("Enter the number of hours you plan to spend studying each week: ");
        int plannedHours = in.nextInt();

        // Calculate recommended hours of study
        int recommendedHoursLow = creditsTaken * 2;
        int recommendedHoursHigh = creditsTaken * 3;

        System.out.println("You are taking " + creditsTaken 
                 + " credits this term and plan to study " + plannedHours 
                 + " hours per week.");

        System.out.print("You plan to study ");
        if(plannedHours < recommendedHoursLow) {
            System.out.print("less than");
        } else if(plannedHours > recommendedHoursHigh) {
            System.out.print("more than");
        } else {
            System.out.print("in the range of");
        }
        System.out.println(" the recommended " + recommendedHoursLow + " - "
                 + recommendedHoursHigh + " hours per week.");

    }

}

Follow along as your instructor demonstrates how to set up WSL and install Java. Then copy the above code into the text editor and save it as StudyTime.java. Compile and run the program using the javac and java commands.

Part II

Once you have successfully compiled and run the program, create a new class called TruthsAndLie and write a program that displays two true statements about yourself and one lie about yourself. The program you write should be much less complicated than the sample program above since you will not require user input or do any calculations. Note: your program should just print the three statements. It should not identify which statements are true and which statement is a lie.

Select a partner (or be assigned one by your instructor), introduce yourselves and demonstrate your working programs. If you need help getting your program to work, ask your partner for help. If your partner needs help getting their program to work, assist them. Once both of your programs compile and run successfully, have your partner guess which of your statements is a lie.

Record both of your names and indicate how many guesses it took to guess the lie. For example, if Jane Dough and Jon D'oh were partners and Jon guessed Jane's lie on the first guess and Jane guessed Jon's lie on the second guess, your paper should look something like this:

NameNumber of guesses
Jane Dough2 guesses
Jon D'oh1 guess

Acknowledgement

This laboratory assignment was developed by Dr. Chris Taylor.

Lab Deliverables (due at the end of week 1 lab)

Make sure you follow any additional requirements and submission instructions for your instructor.