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

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

Life Without NetBeans

CSCI 161: Introduction to Programming I Lab 1b: Hello, World (Eclipse, Java)

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

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

Lab # 2. For today s lab:

CSC116: Introduction to Computing - Java

CPS109 Lab 1. i. To become familiar with the Ryerson Computer Science laboratory environment.

CompSci 125 Lecture 02

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

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS)

ENCM 339 Fall 2017: Editing and Running Programs in the Lab

Tutorial 1 CSC 201. Java Programming Concepts عؾادئماظربجمةمبادؿكدامماجلاصا

CS 177 Recitation. Week 1 Intro to Java

CS 231 Data Structures and Algorithms, Fall 2016

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

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

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

Getting Started (1.8.7) 9/2/2009

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

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

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

Getting Started with Java. Atul Prakash

Unit 13. Linux Operating System Debugging Programs

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

Certified Core Java Developer VS-1036

last time in cs recitations. computer commands. today s topics.

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

Java Bytecode (binary file)

The Command Shell. Fundamentals of Computer Science

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

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

Initial Coding Guidelines

IT151: Introduction to Programming (java)

CSC116: Introduction to Computing - Java

St. Edmund Preparatory High School Brooklyn, NY

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

You should see something like this, called the prompt :

Intro to Linux. this will open up a new terminal window for you is super convenient on the computers in the lab

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

Lec 3. Compilers, Debugging, Hello World, and Variables

COMP 110 Project 1 Programming Project Warm-Up Exercise

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

Computers and Computation. The Modern Computer. The Operating System. The Operating System

CSC116: Introduction to Computing - Java

Session 1: Accessing MUGrid and Command Line Basics

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Lecture (01) Getting started. Dr. Ahmed ElShafee

Linux File System and Basic Commands

Object-Oriented Programming in Java

Chapter 2. Editing And Compiling

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

Lesson 04: Our First Java Program (W01D4

Computer Programming-1 CSC 111. Chapter 1 : Introduction

CMSC 201 Spring 2017 Lab 01 Hello World

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

sftp - secure file transfer program - how to transfer files to and from nrs-labs

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

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

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

Week 2: Data and Output

CMSC 201 Spring 2018 Lab 01 Hello World

assembler Machine Code Object Files linker Executable File

Unit 10. Linux Operating System

Summer Assignment for AP Computer Science. Room 302

Introduction: What is Unix?

Computer Science 62 Lab 8

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

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 9: OCT. 4TH INSTRUCTOR: JIAYIN WANG

Assignment Submission HOWTO

Welcome to CSSE 220. We are excited that you are here:

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Lesson 3 Transcript: Part 1 of 2 - Tools & Scripting

CSE 101 Introduction to Computers Development / Tutorial / Lab Environment Setup

CMSC 150 LECTURE 1 INTRODUCTION TO COURSE COMPUTER SCIENCE HELLO WORLD

Department of Computer Science. Software Usage Guide. CSC132 Programming Principles 2. By Andreas Grondoudis

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

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

Introduction to Java. Lecture 1 COP 3252 Summer May 16, 2017

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 1

These two items are all you'll need to write your first application reading instructions in English.

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

Building Java Programs. Introduction to Programming and Simple Java Programs

Week - 01 Lecture - 04 Downloading and installing Python

Lab 1 Introduction to UNIX and C

CS 11 java track: lecture 1

Lecture 02, Fall 2018 Friday September 7

Principles of Bioinformatics. BIO540/STA569/CSI660 Fall 2010

How to make a "hello world" program in Java with Eclipse *

Software and Programming 1

Working with the Command Line

Computer Principles and Components 1

CSCI 1103: Introduction

EECS 211 Lab 2. Getting Started. Getting the code. Windows. Mac/Linux

CHAPTER 1 Introduction to Computers and Java

Setting up your Computer

Project 1. Java Control Structures 1/17/2014. Project 1 and Java Intro. Project 1 (2) To familiarize with

Chapter 1 Introduction to Computers, Programs, and Java

Javac and Eclipse tutorial

Transcription:

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

Notice Class Website http://www.cs.umb.edu/~jane/cs114/ Reading Assignment Chapter 1: Introduction to Java Programming Homework 1 It is due this coming Sunday at 11:59 PM. I want you to write me an email answering a number of questions. You must send me this email from an account you use frequently. I will use this email address to notify you new developments. Title of the email: CS/IT 114 HW 1 2

Review Tools for This Course You must use DrJava for all your course work You can get DrJava from http://drjava.org You must transfer all your Java files to users3.cs.umb.edu An easy way to do this is to use FileZilla which can be download https://filezilla- project.org/ If use a PC, can download Java from https://www.java.com/en/download/help/windows_manual_download.xml If use a PC, can download PuTTY from http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html You must get these tool in order to do the work required for this course. If you get behind in this course, you will have a hard time catching up. 3

Compiled versus Interpreted Languages As I mentioned in the last class there are two types of computer languages Compiled languages Interpreted languages All computer programs start out as text files that contain instructions in a computer language. These text files are called the source code. But computers cannot understand the source code. They can only run binary files containing instructions in machine language Compiled languages use a compiler to create an executable file. Interpreted or scripting languages take a different approach. They read each line of code and execute it directly. 4

Compiled Languages Language like C and C++ are compiled languages A program called a compiler turns the source code file into an executable file The executable file contains machine language instructions that the computer understands But the executable file will only run on one platform If I compiled a C program on my Mac, that file would not run under Windows Compiled programs are fast and efficient 5

Scripting Languages Languages like Perl, Python and PHP are interpreted languages In a scripting language the source code is not compiled Instead, it is run inside a program called an interpreter. Once an interpreter has been written for a given platform... the interpreter can run any program written in that scripting language So all you have to distribute are the source code files But programs written in a scripting language run more slowly... than similar programs that are compiled 6

How Java Works Java takes a middle course between compiled and interpreted languages You take a Java source file and run it through the Java compiler, javac The source file must have the.java extension The compiler creates an object file with extension.class This file does not contain machine language instructions Instead it contains what are called Java bytecodes So you cannot run this.class file directly on the machine Instead you must run it inside the Java interpreter, java The Java interpreter is also called the Java Runtime Environment (JRE)... or the Java Virtual Machine (JVM) 7

How Java Works Java interpreters have been written for all major platforms That means you can distribute a single set of.class files... that will run on just about all platforms Java gives developers most of the efficiency of a compiled language... but the objects files can be run almost anywhere 8

A Simple Java Program Let's see what a real java program looks like Here is the Hello program from the textbook public class Hello { } public static void main(string[ ] args) { } System.out.println("Hello, world!") ; Notice that the name of the source file, Hello.java is the same as the class name, Hello, with a.java extension 9

A Simple Java Program To compile this program, I run $ javac Hello.java There are now two files whose name, before the extension, is Hello $ ls Hello* Hello.class Hello.java To run the file I use $ java Hello Hello World! Notice that the argument I gave to java was not the name of a file. It was the name of the class 10

New Material Things That Make Java Hard Java is an object oriented language This means that every Java program must do certain things... that don't seem to be directly related to solving a problem In a language like Python I could write the following program... to add the numbers from 1 to 1000 sum = 0 for i in range(1, 1000): sum += i print sum 11

Things That Make Java Hard In Java, a program to the same thing would look like this public class Sum_1000 { public static void main(string[ ] args) { int sum = 0; for (int i = 0; i < 1000; i++){ sum += i; } System.out.println(sum); } } I ve highlighted in red all the extra things that Java requires 12

Things That Make Java Hard Why does Java do this? Different computer languages are designed for different purposes A scripting language like Python was designed to get something done fast Scripting languages aren't very fussy, which is great for doing something quickly Languages like Java were designed for writing big programs. If you were writing software for a bank, you wouldn't user Python You would use Java. Because all the fussiness built into Java makes it harder to make certain kinds of errors Java is a language for big projects where many different programmers work together and they have to follow certain rules to avoid chaos 13

Objects & Classes in Java Java is an object oriented language An object is an entity that has state and behavior is known as an object e.g. chair, bike, car etc. It can be physical or logical such as banking system. Objects are created inside the computer's RAM A class is a group of objects that has common properties. Classes are the templates used to create objects Classes are software written in Java Objects are the contents of RAM created from a class There is only one class for each object... but a single class can create many objects 14

Java Classes Think of the molds that children use with clay Let's say they are molds of dinosaurs and each mold is of a different dinosaur A child can use one mold to stamp out many copies of each dinosaur A class is like the mold and the clay dinosaurs are like the objects So one mold can make many dinosaurs... and one class can make many objects The classes are contained in Java source code files and the objects are created in the computers memory using the classes 15

Java Classes Every Java source file contains at least one class... and the name of the source file is the class name with the.java extension A Java program is made up of one or more source files... each containing at least one Java class Most often, Java source files have a single class inside them The source file for the class must have the same name as the class... along with the extension.java You compile a Java source file using the Java compiler, javac $ javachello.java 16

Java Classes This will create a.class file whose filename will be the name of the class followed by the.class extension $ ls Hello.class Hello.java You distribute a Java program by providing the.class files The Java interpreter, java, can run these.class files $ java Hello Hello World! Notice that I did not use the.class extension when running the program. I just used the name of the class Each Java class usually contains one or more methods A method is a series of instructions in Java that performs a specific task 17

Writing a Java Class A program written in Java consists of one or more source code files Each of these files consists of one or more Java classes You cannot write a Java program that is not contained in a class This is one of those things that Java is fussy about A Java class has a particular format If you do not use this format, the code will not compile... even if you make a mistake of a single letter 18

Writing a Java Class Here is the format for writing a simple Java class public class CLASS_NAME { } METHOD... Everything in CAPITAL_LETTERS is a placeholder. You replace the words in capitals with something else. For example, in this simple Java program public class Hello { } public static void main(string[ ] args) { } System.out.println("Hello, world!") ; replaced replaced 19

Writing a Java Class When I show you an example format... anything NOT in CAPITAL_LETTERS must be typed exactly as show The... means you can have as many methods as you like They are placeholders for text you supply The first line of a class is the class header The class header contains important information about the class The name of the class It's permissions The permissions determine who can run the class All the classes for this course will be public That means anyone can use them 20

Java Methods The actual work in a Java program is done by methods Methods perform a specific task In other programming languages, what Java calls methods are called functions or procedures Methods are the basic unit of work in a Java program Methods are composed of individual statements A statement is a line of code that contains a complete Java command Every Java statement must have a semicolon, ;, at the end If you leave it off, you will get a compile error Every method starts with a method header The method header contains important information about the method... including the name of the method 21

The main Method In order to run a Java program, there must be one source code file with a method called main The main method starts the action when you run a Java program A class without a main method cannot be run by itself Sometimes people refer to the main method as the driver The main method is in overall control of what happens when you run a Java program It usually does this by calling other methods 22

The main Method The main method has the following format public static void main(string[] args) { } STATEMENT;... In other words, the main method can hold many statements between the curly braces, { } The method header for a main method is always public static void main(string[] args) You will learn what each of these words mean in CS/IT 115 For now, don't worry about their meaning. Just type them exactly as you see here 23

Using DrJava DrJava is a program which helps you write Java programs It is simple version of a type of program called an IDE IDE stands for Integrated Development Environment Other popular IDEs are Eclipse Microsoft Visual Studio NetBeans You do not need an IDE to write Java programs Any text editor will do 24

Using DrJava Programming editors have special features that make programming easier One such editor is sublime Text Here is what Hello.java looks like in sublime Text running on my Mac Notice that different words have different colors The colors indicate that different words are of different types This feature is called colorization 25

Using DrJava Here is what the same code looks like in DrJava 26

Using DrJava Once you have typed in the code into DrJava, you can compile the code inside DrJava If your code has no syntax errors, you will see a message telling you the compilation worked 27

Using DrJava If you made a mistake, DrJava will display an error message The line where the error occurs is highlighted in yellow This line leaves out the open curly brace needed to start a class definition Syntax errors like this are the leading cause of frustration for students DrJava makes it easier to spot syntax error is the failure to close curly braces 28

Using DrJava If you click to the right of an opening brace... DrJava will show you the code it encloses 29

Using DrJava Once you have corrected the errors, you can run the program inside DrJava 30

Creating Your First DrJava File When you open DrJava for the first time it looks like this To display line numbers, launch DrJava and choose Edit, Preferences, and Display Options. Click on Show All Line Numbers. 31

Creating Your First DrJava File The Java statements are typed in the large window on the right The first thing you have to type is the class header Words in blue are called keywords 32

Creating Your First DrJava File What follows the class header is the text of the class itself This text must be enclosed within curly braces, { } I usually put the first curly brace on the same line as the method header, then immediately add a blank line and close the curly brace DrJava highlight the area between the braces in light blue This is how DrJava shows that the curly braces are matched Adding the closing curly brace now means I won t forget to add it later which would give me a compile error 33

Creating Your First DrJava File What follows the class header is the text of the class itself This text must be enclosed within curly braces, { } I usually put the first curly brace on the same line as the method header, then immediately add a blank line and close the curly brace DrJava highlight the area between the braces in light blue This is how DrJava shows that the curly braces are matched Adding the closing curly brace now means I won t forget to add it later which would give me a compile error 34

Creating Your First DrJava File The next thing I have to do is add a main method... and the curly braces that enclose the method statements 35

Creating Your First DrJava File Now I write the statements for the method... each on their own line followed by a semicolon If you leave out the semicolon you will get a compile error 36

Creating Your First DrJava File We have to save the file before we can compile it If you click on the Save button, it will bring up a dialog box The file name in the dialog box is the same as the class name. Otherwise you will get a compiler error DrJava doesn t show you the.java extension, but it adds it anyway. Also notice the directory in which the file will be stored You have to go to that directory when you copy files to users3.cs.umb.edu You can create this directory by clicking the New Folder button 37

Creating Your First DrJava File The Java compiler, javac, is built into DrJava To compile the program, click on the Compile button If there are no mistakes, you will see "Compilation completed" in the box on the lower left. 38

Creating Your First DrJava File DrJava is written in Java. So it runs inside the Java interpreter, java This means DrJava can run your compiled Java programs for you To run the program, click on the Run button If there are no errors you should see the output of the program in the box on the lower left 39

Practice, Practice, Practice Programming does not comes easily to most people Programming languages are formal languages They have a rigid structure That's because your programs have to be understood by another computer program the compiler If there is any ambiguity in a line of code, the compiler cannot do its job You don't have to be a genius to program Java But you do have to have patience And most of all, you must practice The more you practice the better you will be That is why I assign the code entry exercises 40

Why Do I Make You Use Unix? Unix is an operating system developed in the 70's by Bell Labs In the early days it was used mostly by universities and researchers Linux is a free and open source version of Unix Unix and Linux are widely used In science and engineering In servers In these environments, linux/unix is usually run without a GUI (Graphical User Interface) Instead people use the command line To get a sense of how Java is used in the real world... you should have some exposure to the Unix command line I won't ask you any questions about Unix on an assignment, quiz or test 41

Using Unix In order to compile and run your Java programs on Unix... you must learn a few basic Unix commands You must learn how to Move from one directory to another List the contents of a directory Move a file from one place to another Create a new directory Compile a Java program Run a Java program That's only six commands I have created a web page with some basic Unix commands and features in http://www.cs.umb.edu/~jane/cs114/unix_essentials.html 42

Working on the Assignments There are two types of assignments for this class Class Exercises Homework For each of these assignments you must Write a program using DrJava Compile the program in DrJava Get the program to run properly in DrJava Log into users3.cs.umb.edu Create a directory to hold the file Copy the.java file to the Unix machine Use the Java compiler to create a.class file Run the program using the Java interpreter, java It takes some getting used to but you can do it. 43

Copying Files to a Unix Machine After you have your Java program running in DrJava... you need to move it to users3.cs.umb.edu users3 is the hostname A hostname is the name of a computer on the network cs.umb.edu is the name of the networkusers3 is connected to Together they make something called a domain name You need to use a domain name when connecting to a computer on the Internet To copy files to a Linux/Unix machine you need a secure copying program There are many such programs for Windows... but I have a Mac so I am going to show you how to do it on a Mac 44

Copying Files to a Unix Machine Mac OS is really a variety of Unix... but it's hard to see that because it is buried under an nice Graphical User Interface The Mac OS Terminal application gives me a Unix command line on my Mac The first thing I have to do is go to the directory that holds the file I want to copy On my machine, my DrJava directory is located in the Documents directory... so first I have to go there $ cd Documents 45

Copying Files to a Unix Machine Everything before $ is not something you type Its the Unix prompt... telling you Unix is ready to listen to your next command Your prompt will look different from mine Then I need to list the contents to be sure I'm in the right place $ ls Now I need to go to my Drjava directory $ cd drjava And list contents again $ ls 46

Copying Files to a Unix Machine I want to copy the.java file not the.class file On my Mac I would use the scp (secure copy) command like this $ scp Hello.java jane@users3.cs.umb.edu: jane is my Unix username and users3.cs.umb.edu is the domain name You must have the : after the domain name This tells scp to copy Hello.java to your home directory You will find the instructions for transferring files from a Windows machine: http://www.cs.umb.edu/~jane/cs114/remote_access_windows.html And the instructions for a Mac:http://www.cs.umb.edu/~jane/cs114/remote_access_mac.html 47

The Hierarchical Filesystem Before we go any further, we needed to talk a little about the way Unix arranges files Unix uses a hierarchical filesystem Which means that all files are kept in directories (folders)... and directories live inside other directories... with one special directory at the top called root... which is written as / The hierarchical filesytem can be a little confusing If you are every confused by where you are when connected to Unix... use the cd command to go back to your home directory 48

How to Log On to a Unix Machine After you have moved your file to your home directory, you need to connect to users3 On Windows you will use PuTTY to log on remotely to a Unix machine: http://www.cs.umb.edu/~jane/cs114/remote_access_windows.html This link shows how it is done on a Mac: http://www.cs.umb.edu/~jane/cs114/remote_access_mac.html On my Mac, I use the ssh command ssh jane@users3.cs.umb.edu I am now in my home directory which I can see by using the pwd (print workingdirectory) command $ pwd /home/jane 49

Creating a Unix Directory The second homework assignment, which is due next Sunday... is be to type the Hello.java program This will have to be copied to a hw2 directory... inside a hw directory... inside your it114 directory Here's how you do it After you log in, you should be in your home directory To make sure, you can use the pwd command $ pwd home/jane 50

Creating a Unix Directory There should be an it114 directory in your home directory You can use ls to see if this is true $ ls it114 You need to go to this directory using cd $ cd it114 This directory should be empty $ ls $ 51

Creating a Unix Directory You need to create the hw directory... usingmkdir (make directory) $ mkdirhw To be sure this works, use ls $ ls hw Now you need to go to this directory $ cd hw And create a hw2 directory $ mkdirhw2 52

Moving a File in Unix You move files using the mv (move) command. Here is the format for the command mv FILE DIRECTORY The simplest way to move a file from one directory to another is to go to the directory that contains the file $ cd Remember, when you use cd without an argument, it takes you to your home directory Once in the home directory we can use the following command $ mv Hello.java it114/hw/hw2 it114/hw/hw2 is a path... a list of directories that goes from one directory to another 53

Compiling and Running a Java Program on Unix To compile and run the program first go to the directory that holds the source code $ cd it114/hw/hw2 Let's be sure the source code is really in this directory $ ls Hello.java To compile the source code we use the Java compiler javac $ javachello.java 54

Compiling and Running a Java Program on Unix Runningls we see the compiler has created a new file $ ls Hello.class Hello.java Hello.class is the object file that javac created from Hello.java We need the object file to run the program with the Java interpreter java $ java Hello Hello, world! Notice that the argument to java was Hello... not the name of the object file, Hello.class java takes as its argument the name of the class 55