Create a folder named section1
for your work on these
exercises. You can find instructions for doing so
here.
In VS Code, select the File->Open Folder or File->Open menu
option, and use the resulting dialog box to find and open your
section1
folder. The name of the folder should appear in the
Explorer pane on the left-hand side of the VS Code window.
Important: In general, you should not use File->Open File to open a specific file in VS Code. Rather, you should create a folder for the files that belong to a given assignment or section, and you should use File->Open Folder or File->Open to open the folder.
In Java, what is an identifier? 1
Indicate whether the following names are valid Java identifiers. 2
SECRET_NUMBER
average?num
2two
two2
secret word
my#variable
Are the identifiers secretNum
and secretnum
the same? 3
In Java, what is the difference between the statement
System.out.print(...)
and System.out.println(...)
?
4
In VS Code, select File->New Text File, which will open up an empty editor window. It will initially have a name that is something like Untitled-1.
Select File->Save, and give the file the following name:
Section1.java
Important: When naming a Java file, the case of the letters
matters. Make sure to use the exact combination of upper-case and
lower-case letters that we specify. For this file, make sure to
begin with an upper-case S
.
Copy and paste the following buggy code into the editor window
for Section1.java
:
Public Class section1 { public static main(String args) { System.out.println(Go Crimson); System.out.pritnln("S-111 is the best"!); System.out.println("///Are you having fun?\\\"); System.out.println() System.println("Bye!"; { }
Identify all of the bugs in the program and fix them. 5
Note: VS Code can help you to identify the bugs. Use the Terminal->New Terminal menu option, which should open a window known as a Terminal below the editor window. Click on the word PROBLEMS at the top of the Terminal window, which should reveal a list of syntax errors in the code.
Keep making the necessary changes until no more problems remain and you are able to run the program.
Write a System.out.println(...)
statement that would produce
the following output: 6
Joe said, "We're my teaching assistant's favorite class!"
Write a Java class called HarvardSpirit
that produces
the following output. Make sure your HarvardSpirit.java
file has a block comment at the top listing the file name,
the author, and a short description of the file. 7
///////////// HARVARD S-111 \\\\\\\\\\\\\
The following StickFigure
class draws a stick figure graphic
using only text characters. Copy this code into a new file
in VS Code, then write a method drawFigure()
that draws an
entire stick figure. Then change the main()
method so that
it calls drawFigure()
twice. 8
public class StickFigure { public static void main(String[] args) { System.out.println("\\ O /"); System.out.println(" \\|/"); System.out.println(" |"); System.out.println(" / \\"); System.out.println("/ \\"); System.out.println(); System.out.println(); System.out.println("\\ O /"); System.out.println(" \\|/"); System.out.println(" |"); System.out.println(" / \\"); System.out.println("/ \\"); } }
What output is produced by the following program? Note: Don’t just copy the code into VS Code! First, trace the program by hand, and then you can verify your answer by creating a new file for the program in VS Code and running it. 9
public class Blastoff { public static void main(String[] args) { three(); two(); one(); System.out.println("blastoff"); } public static void one() { three(); System.out.println("one"); } public static void two() { one(); System.out.println("two"); three(); } public static void three() { System.out.println("three"); } }
An identifier is a name used in code to refer to a method, class, or other construct. Programmers have control over what identifiers to use when writing a program. ↩
SECRET_NUMBER
and two2
are valid identifiers. average?num
is invalid because it contains a ?
character. 2two
is invalid,
since it starts with a number. secret word
is invalid because
it contains a space. my#variable
is invalid because it contains
a #
character. ↩
secretNum
and secretnum
are not the same, since identifiers
are case-sensitive. Here, secretNum
and secretnum
would
refer to different things. ↩
System.out.println(...)
automatically includes a newline
character (\n
) after the string, whereas System.out.print(...)
does not include a newline. ↩
The following is a fixed version of the class:
public class Section1 { public static void main(String[] args) { System.out.println("Go Crimson"); System.out.println("S-111 is the best!"); System.out.println("///Are you having fun?\\\\\\"); System.out.println(); System.out.println("Bye!"); } }
System.out.println("Joe said, \"We're my teaching assistant's favorite class!\"");
↩
The output is as follows:
three three one two three three one blastoff
Last updated on June 25, 2025.