Class 1: Homework. Intro to Computer Science CSCI-UA.0101 New York University Courant Institute of Mathematical Sciences Fall 2017

Similar documents
Chapter Two Bonus Lesson: JavaDoc

CS 177 Recitation. Week 1 Intro to Java

Introduction to Java. Java Programs Classes, Methods, and Statements Comments Strings Escape Sequences Identifiers Keywords

Getting Started with Java. Atul Prakash

List of Slides 1 Title 2 Chapter 2: Sequential execution and program errors 3 Chapter aims 4 Section 2: Example:Hello world 5 Aim 6 Class: programs ar

17 Hello world 18 Type: String: literal 19 Standard API: System: out.println() 20 Hello world 21 Statement 22 Statement: simple statements are ended w

MEAP Edition Manning Early Access Program Get Programming with Java Version 1

Java Bytecode (binary file)

Lecture (01) Getting started. Dr. Ahmed ElShafee

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

C++ Support Classes (Data and Variables)

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS)

Code Ninjas: Introduction to Computer Science. Macomb Science Olympiad Presented by Swati Dharia

This is a combination of a programming assignment and ungraded exercises

Get JAVA. I will just tell you what I did (on January 10, 2017). I went to:

CHAPTER 1. Introduction to JAVA Programming

Lesson 1A - First Java Program HELLO WORLD With DEBUGGING examples. By John B. Owen All rights reserved 2011, revised 2015

Lesson 04: Our First Java Program (W01D4

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Getting Started with Eclipse/Java

Lab # 2. For today s lab:

For live Java EE training, please see training courses at

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

Java Programming. What is a program? Programs Recipes. Programs as Recipes 8/16/12. Pancakes In one bowl. mix: 1½ cup flour

C02: Overview of Software Development and Java

Lecture 4: Introduction to Java and your Development Environment

Midterms Save the Dates!

Summer Assignment for AP Computer Science. Room 302

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

2 Getting Started. Getting Started (v1.8.6) 3/5/2007

Getting Started (1.8.7) 9/2/2009

AP Computer Science Summer Assignment (updated 5/29/2018) DUE : Sept. 4, 2018

Lecture 1 - Introduction (Class Notes)

CHAPTER 1 Introduction to Computers and Java

CMSC 150 LECTURE 1 INTRODUCTION TO COURSE COMPUTER SCIENCE HELLO WORLD

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

Setup and Getting Startedt Customized Java EE Training:

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Programming Principles 1 (CSC131) & 2 (CSC132) Software usage guide

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

Eclipse Environment Setup

CS 231 Data Structures and Algorithms, Fall 2016

Java/RealJ Troubleshooting Guide

Introduction to Java

Chapter 1: Introduction to Computers and Java

8/23/2014. Chapter Topics. Introduction. Java History. Why Program? Java Applications and Applets. Chapter 1: Introduction to Computers and Java

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 1

Summer Assignment for the School Year

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

Computer Hardware. Java Software Solutions Lewis & Loftus. Key Hardware Components 12/17/2013

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Mr.Kailley s AP Chem programming Unit Set up instruction sheet for PC s (Mac will be kind of similar: good luck) 1) Go to the following website:

Instructions. First, download the file

CS110/CS119 Introduction to Computing (Java) Bob Wilson S-3-176

Running Java Programs

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

CompSci 125 Lecture 02

Basic Keywords Practice Session

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Java with Eclipse: Setup & Getting Started

Chapter 1: A First Program Using C#

Slide 1 Java Programming 1 Lecture 2D Java Mechanics Duration: 00:01:06 Advance mode: Auto

Exercise 1: Intro to Java & Eclipse

CS11 Java. Fall Lecture 1

CS 11 java track: lecture 1

COMP 110/401 APPENDIX: INSTALLING AND USING ECLIPSE. Instructor: Prasun Dewan (FB 150,

CSCI 1103: Introduction

13 th Windsor Regional Secondary School Computer Programming Competition

Lecture 02, Fall 2018 Friday September 7

Inf1-OOP. Textbooks. Who and What. Organisational issues. Why Java? Course Overview. Hello, World! in Java

Chapter 1 Lab Algorithms, Errors, and Testing

Certified Core Java Developer VS-1036

Introduction to Java

Variables are used to store data (numbers, letters, etc) in MATLAB. There are a few rules that must be followed when creating variables in MATLAB:

You should see something like this, called the prompt :

Developing Android applications in Windows

CS 201 Software Development Methods Spring Tutorial #1. Eclipse

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Creating a Program in JCreator. JCreator is then used to create our program. But the first step is to create a new file.

Algorithms and Programming I. Lecture#12 Spring 2015

Midterms Save the Dates!

Program Development. Program Development. A Foundation for Programming. Program Development

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

COMP 110 Project 1 Programming Project Warm-Up Exercise

AP Computer Science Unit 1. Programs

The Command Shell. Fundamentals of Computer Science

Department of Computer Science University of Pretoria. Introduction to Computer Science COS 151

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Introduction. Introduction to OOP with Java. Lecture 01: Introduction to OOP with Java - AKF Sep AbuKhleiF -

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

Java Programming. What is a program? Programs as Recipes. Programs Recipes 8/20/15. Pancakes In one bowl

You should now start on Chapter 4. Chapter 4 introduces the following concepts

Who and what can help? Inf1-OP. Lecturer: Timothy Hospedales TA: Natalia Zon

Transcription:

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