Intro to Computer Science CSCI-UA.0101 New York University Courant Institute of Mathematical Sciences Fall 2017 1
1. Please obtain a copy of Introduction to Java Programming, 11th (or 10th) Edition, Brief Version, by Y. Daniel Liang 2. Please read in Introduction to Java Programming (IJP) - Chapter 1 - all except the NetBeans and Eclipse sections - you will read about Eclipse in a future assignment. Getting your programming environment ready 3. You will require access to a computer that has Java 1.8 installed. To see if your computer has Java installed, open a terminal window and type: java -version The terminal window on a Mac can be opened from Applications -> Utilities -> Terminal.app On Windows, it s called a Command Prompt window; there are many ways to open it (see here). The simplest is to click open the Start menu, then click All apps -> Command Prompt. Let me know if you have trouble. If Java Is installed, you should see: $ java -version java version "1.8.0_121" Java(TM) SE Runtime Environment (build 1.8.0_121-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode) If Java (the JDK) is not installed, or if you would like to install a newer version of it (we need 1.8), perform the following steps: - Install the 64-bit version (if your system is a 32-bit operating system, send me a note) of Java JDK 1.8.0. Download it from here and follow installation instructions: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html - After installation is complete, note the directory you installed it in and type the following to ensure Java 1.8 was installed successfully: java -version 2
4. Writing your first Java program Write your first Java program and compile it from the command line (please post any issues on the Forum; we will help you get this working). First, you ll want to do a make directory to create a directory that will hold your Java source code files. I d recommend creating a cs101 directory, and a cs101_hw_1 subdirectory within the cs101 directory. a. Use a text editor to create a file named HW1.java and type (or cut and paste) the Java program shown below. Save the file as: HW1.java to the subdirectory named: cs101_hw_1 / HelloWorld is the name of the Java class. public clss HelloWorld // Display the string print(hello world!") } } b. Compile the program from the command line (your terminal window) using the following command: javac HW1.java 3
5. Debugging your first Java program a. Don t be alarmed, you should expect to see lots of syntax errors reported - I see 15: $ javac HW1.java HW1.java:1: error: class, interface, or enum expected / HelloWorld is the name of the Java class. HW1.java:1: error: class, interface, or enum expected / HelloWorld is the name of the Java class. HW1.java:1: error: <identifier> expected / HelloWorld is the name of the Java class. HW1.java:2: error: ';' expected HW1.java:2: error: ';' expected HW1.java:2: error: ';' expected HW1.java:2: error: <identifier> expected HW1.java:2: error: illegal start of type HW1.java:2: error: <identifier> expected HW1.java:3: error: ';' expected public clss HelloWorld HW1.java:3: error: <identifier> expected public clss HelloWorld HW1.java:4: error: <identifier> expected HW1.java:6: error: ')' expected HW1.java:6: error: unclosed string literal HW1.java:6: error: not a statement 15 errors 4
b. The point of this program is to print out Hello World! - let s start at the top and fix the errors one by one. It s often beneficial to start at the top because a syntax error early in the program can have bad side effects and cause other errors. c. What is causing so many errors? Sometimes there s just one mistake in the code but many errors reported. Of course! The class name and the filename must match, but they don t in our case. Try renaming the file from HW1.java to the correct name, HelloWorld.java, and then recompile (the filename must match the class name): javac HelloWorld.java d. Well that s not fair - we fixed an error but still there are 15 errors. Let s look at the first error message: $ javac HW1.java HW1.java:1: error: class, interface, or enum expected / HelloWorld is the name of the Java class. Note the caret symbol () - that s an arrow that the Java compiler is using to give you a hint as to where the problem is. In our case, the caret points to the /. Why would that be? Ah - it should be a comment - we can change it to // for a single line comment. Recompile: $ javac HW1.java 5
e. Great! We are down to only 7 errors now: HelloWorld.java:2: error: class, interface, or enum expected HelloWorld.java:2: error: <identifier> expected HelloWorld.java:3: error: ';' expected public clss HelloWorld HelloWorld.java:4: error: <identifier> expected HelloWorld.java:6: error: ')' expected HelloWorld.java:6: error: unclosed string literal HelloWorld.java:6: error: not a statement 7 errors f. Let s look at the very first error we have: That s odd, we have a class declared. This means that for some reason, Java doesn t think a class was defined - something is blocking the Java compiler from seeing the line: public clss HelloWorld Wait! Look at clss - there s a typo in our spelling of class. Fix it and recompile. 6
g. Still not good - we have eight errors. HelloWorld.java:2: error: class, interface, or enum expected HelloWorld.java:2: error: <identifier> expected HelloWorld.java:3: error: '{' expected public class HelloWorld HelloWorld.java:4: error: <identifier> expected HelloWorld.java:6: error: ')' expected HelloWorld.java:6: error: unclosed string literal HelloWorld.java:6: error: not a statement HelloWorld.java:8: error: reached end of file while parsing } 8 errors h. You know what to do - the first error at the top of the list is: But that means the Java compiler still can t see our class. What is preventing it from seeing the class now? - we already fixed the typo. Here s what precedes our class declaration line in the file: // HelloWorld is the name of the Java class. public class HelloWorld First line is a comment - looks fine. But what is the second line? Is it a programming statement? It reads like a sentence - it s a comment, but we haven t told Java that it s a comment. To do that, we need to place // at the front of the line. Change the line to this and recompile: // (Alternatively, you could use /* */ for multi-line commenting, that would work too.) 7
i. How did we do? We are down to five errors! HelloWorld.java:3: error: '{' expected public class HelloWorld HelloWorld.java:4: error: <identifier> expected HelloWorld.java:6: error: ')' expected HelloWorld.java:6: error: unclosed string literal HelloWorld.java:6: error: not a statement 5 errors j Great progress! You know what to do - look at the first error - hmm, { expected - it thinks we don t have enough { for the number of }. Count them up - are we missing one? k Yes, the opening curly bracket for the class block is missing - change the line to: public class HelloWorld { l. After recompiling, we are down to four errors (the { error is gone): HelloWorld.java:4: error: <identifier> expected HelloWorld.java:6: error: ')' expected HelloWorld.java:6: error: unclosed string literal HelloWorld.java:6: error: not a statement 4 errors 8
m. What s the first error? - HelloWorld.java:4: error: <identifier> expected Hmm, identifier expected, and the caret is pointing right after the parameter type declaration - of course! The name of the parameter is missing. Change that line to the following and recompile: public static void main(string[] args ) { n. Great! Three errors left. The top one is: HelloWorld.java:6: error: ')' expected The caret indicates something is wrong with the string we are trying to print out. The error message tells us that Java was expecting to see a ) at the location pointed to by the caret. We could put a ) there, but we know that wouldn t be correct because Hello, in this case, is not the name of a variable, for instance, it s part of a string to be output. The problem is that strings need to be enclosed in quotes - this string is missing the leading quote. What you can note from this error is that Java does its best to provide a hint as to what is wrong, but as you can see, the hint may be wrong. Change the line to the following and recompile your program: print("hello world!") o. One error left: HelloWorld.java:6: error: ';' expected p r i n t ( " H e l l o w o r l d! " ) 1 error This time Java gives us a very clear indication of what is wrong - a ; is missing - every line in Java needs to end with a semi-colon. Fix the code and recompile it. 9
p. Surprise! Still not done. We fixed the earlier error, but that made it possible for Java to process more of the program - and it found a new error: HelloWorld.java:6: error: cannot find symbol p r i n t ( " H e l l o w o r l d! " ) ; symbol: method print(string) location: class HelloWorld 1 error q. What to do? It says that it cannot find the symbol pointed to by the caret - it can t find print. That s because print can t be referenced directly - it needs to be referenced as System.out.println. Change print to System.out.println and recompile. r. Hooray! There are no more errors. Now to run our program. Type the following to run your program: java HelloWorld If all went well, you will see this printed out: Hello World! s. Before uploading your solution to NYU Classes, add a header line at the top of the HelloWorld.java file that includes your name, the class name and section, and the date. Remember, these are comments. After you add the header info, recompile your program and re-run it - make sure it still works - no errors. Upload your fixed HelloWorld.java program to NYU Classes. To receive full credit, it must have the header, compile, run, and produce the correct result. 10