Lab 1: Getting started
Task 0.0: Review lab policies
-
Structure. Labs will typically begin with a brief lecture and/or example to reinforce the material covered in lecture. The class will then have chance to ask questions. After that, there will be a series of exercises for you to complete. You are welcome to work on the exercises on your own or pair up with a classmate.
-
Attendance:
-
It is important that you attend the lab for which you are registered on StudentLink. If you need to attend another lab section on any given week, please notify your instructor in advance.
-
For each lab, you must be checked off by either a teaching fellow or course assistant in order to recieve credit for the lab.
-
-
Participation:
-
To get full credit for participation in lab, you must work productively and submit the work that you complete as specified by the TA. You will not be penalized if you cannot finish all of the lab exercies, but we strongly encourage you to complete the exercises outside of lab, and check your answers with the solutions when posted.
-
Don’t hesitate to ask for help! The course staff is more than happy to help you with any questions that you may have, and we will be coming around to assist you during lab.
-
Note
You cannot get credit for completing the lab tasks at home and coming to lab only to sign the attendance sheet. Part of participation is attending the lab to interact and ask questions.
-
Collaboration.
- You are encouraged to work with your classmates on the exercises in the lab, but please follow current policy and procedures.
Task 1: The Basics
Using folders
We strongly encourage you to create a separate folder for each lab
and each problem set, so that you can more easily keep track of
your work. For example, you could create a folder called lab1
for your work on this lab, and put all of the files for this lab
in that folder.
Task 1.2: Using VSCode
In Lab 0, you should have installed your IDE on your computer. If you have not, please do so ASAP.
To see how to use it, let’s take the following steps together:
-
Let’s begin by creating an easy-to-access general folder on your computer where you can store all your code throughout the semester. Example: a
CS112
general folder along with several subfolders one for eachproblem set assignment
,lab
, and anything else to keep things organized. -
Launch
VSCode
on your laptop. If you do not see theVSCode
icon on your workspace, you can:-
on Windows, use the
Windows button
in the lower-left corner of your Desktop to search for and run Visual Studio Code. -
on macOS, use the
Spotlight tool
to search for and and run Visual Studio Code.
You should begin on the Welcome page (if not, click on the File dropdown on the top left and select
Close
Folder). -
-
If you have not already done so, in your computer’s file explorer, create a folder called
lab1
inside your CS112 folder (organization is up to you). -
Select the
File->Open Folder
orFile->Open menu
option, and use the resulting dialog box to find and open thelab1
folder that you created. The name of the folder should appear in a new Explorer pane on the left-hand side of the VS Code window. -
Let’s create our first
java
file. There are many ways to do this, but for now we’ll start with the easiest. To the right side onlab1
there should be an icon labelled “New File”. SelectFile->New File
, which will open up an empty window known as an editor window for your new program. It will initially have a name that is something likeUntitled-1
. -
Select File->Save
, and give the file the nameHelloWorld.java
. -
Now let’s write our first class. Don’t worry if you don’t understand the syntax yet, this is just an introduction. Copy the following code into the file:
public class HelloWorld { public static void main(String[] args) { /* INSERT a line to output "Hello World" */ } }
-
Replace the comment with the statement to print the literal string “Hello World!”
-
Save and run the program. There are many ways to this, but the easiest is to hit the
Run
button on the top right of the window.
A few useful tips when working with VSC
:
-
A lot of
VSCode
problems and glitches can be solved by cleaning the workspace. Click on the Java Projects tab within the Explorer, and select More Actions (the three dots). Then select Clean Workspace. -
If the
VSC
Terminal is acting up, one option is to kill the terminal and restart it. In the Terminal tab, there is a trash can icon to the top right side. Select that to Kill Terminal, then restart it by either running your program again or selecting New Terminal under the Terminal tab at the top. -
Sometimes your files can get long and confusing, and finding methods within your file may get hard. VSCode provides an Outline tab in the Explorer. If you click on it, it will show you a list of locations within your file, and clicking on them will automatically bring you to that location. This can be helpful later on in the course when files get longer.
-
You have pretty much full customization of your window in VSCode. The most useful may be that you can have multiple files open side by side, allowing for easier access to multiple files simultaneously.
-
Take some time and mess around with the personalization to make your space to your liking, you’ll be using it a lot throughout the semester. On the Welcome page (you can open it under the Help tab), you can choose a Color Theme. Under the VIew tab up top, there some options under Appearance and Editor Layout that you may like.
Task 1.2: Debugging a Simple Program
Let’s start with a simple exercise to get used to the VSC
Development environment.
Download the following file (in your lab1
directory) on your computer.
Open it up using VSC
. Now pretend you are the Java compiler and try finding all
the intentional bugs in the program. How many errors can you find?
-
Fix all the errors and try to run your program. If you fixed all the compilation errors your program should run. If you did not find all the errors, look through the compiler errors to see if you can fix the remaining. Run your program once all the errors are fixed.
-
Now look at the result. Is it correct? If not, there may be a logic error. Can you find it?
Task 2: Writing our first custom program
In case you are not able to complete this task or the challenge portion in lab today, it can be a fun weekend activity! Remember lab solutions will be posted!
-
Write a Java program that greets you. Add code to the
main
method of your program to issue a prompt asking for your name, display a polite (or not so polite) greeting message and then prompt you to enter your age.Use the methods of the Scanner class to perform the user input. You should use the method
next
to read and return a string value andnextInt
to read and return an integer value. Your program may run as follows:Please enter your name: Christine Hello Christine, Welcome to CS112!!! Christine how old are you? 61 61! WOW!
Challenge (once comfortable with conditional logic):
-
Enhance your program to output a personal insult based on the value of
age
that was entered. Use the following age cuttoffs for creating your insults:1 <= 10 everyone is sweet 11 <= 17 they are dweebs 18 <= 20 they are counting down to legal age 21 exactly they just made legal age 22 <= 29 they are counting down to 30 30 <= 40 they are suffering adults 41 < 50 they are miserable adults >= 50 you are speechless!!
A few possible sample runs (but feel free to be creative!):
Please enter your name: Aileen Hello Aileen, Welcome to CS112!!! Aileen how old are you? 21 Wow Aileen! You just made it! Please enter your name: Mike Hello Mike, Welcome to CS112!!! Mike how old are you? 19 Wow Mike! You have 2 more years to go!! Please enter your name: John Hello John, Welcome to CS112!!! John how old are you? 13 Wow John! you are such a dweeb!!