CS 106 Introduction to Computer Science I



Similar documents
Variables, Constants, and Data Types

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Install Java Development Kit (JDK) 1.8

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Java Crash Course Part I

Introduction to Java

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Pemrograman Dasar. Basic Elements Of Java

Chapter 1 Java Program Design and Development

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Introduction to Object-Oriented Programming

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Crash Course in Java

Computer Programming Tutorial

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Moving from CS 61A Scheme to CS 61B Java

Programming Languages CIS 443

Java Basics: Data Types, Variables, and Loops

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Java CPD (I) Frans Coenen Department of Computer Science

6.1. Example: A Tip Calculator 6-1

Chapter 2: Elements of Java

Lecture 5: Java Fundamentals III

Introduction to Java. CS 3: Computer Programming in Java

Sample CSE8A midterm Multiple Choice (circle one)

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

Introduction to Programming

ECE 122. Engineering Problem Solving with Java

AP Computer Science Java Subset

CSCI 3136 Principles of Programming Languages

An Introduction to Computer Science

Computer Programming I

Lecture 1 Introduction to Java

Object-Oriented Programming in Java

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

CS106A, Stanford Handout #38. Strings and Chars

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

J a v a Quiz (Unit 3, Test 0 Practice)

Introduction to Java Lecture Notes. Ryan Dougherty

Programming in Java Course Technology, a part of Cengage Learning.

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

Chapter 3. Input and output. 3.1 The System class

Sources: On the Web: Slides will be available on:

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

Programmierpraktikum

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

CS 1133, LAB 2: FUNCTIONS AND TESTING

2/1/2010. Background Why Python? Getting Our Feet Wet Comparing Python to Java Resources (Including a free textbook!) Hathaway Brown School

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

LAB4 Making Classes and Objects

1001ICT Introduction To Programming Lecture Notes

Introduction to Python

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Semantic Analysis: Types and Type Checking

Object Oriented Software Design

Chapter 2 Basics of Scanning and Conventional Programming in Java

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Java programming for C/C++ developers

Object Oriented Software Design

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Chapter 2 Introduction to Java programming

The Java Virtual Machine (JVM) Pat Morin COMP 3002

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

CSC230 Getting Starting in C. Tyler Bletsch

Introduction to the course, Eclipse and Python

Java Programming Fundamentals

Vim, Emacs, and JUnit Testing. Audience: Students in CS 331 Written by: Kathleen Lockhart, CS Tutor

Semester Review. CSC 301, Fall 2015

Computer Science 217

CS 325 Computer Graphics

Programming Project 1: Lexical Analyzer (Scanner)

C++ Language Tutorial

AP Computer Science Static Methods, Strings, User Input

A Simple Introduction to Game Programming With C# and XNA 3.1

Outline Basic concepts of Python language

Chapter 13: Program Development and Programming Languages

Informatica e Sistemi in Tempo Reale

The programming language C. sws1 1

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

The C Programming Language course syllabus associate level

McGraw-Hill The McGraw-Hill Companies, Inc.,

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

University of Twente. A simulation of the Java Virtual Machine using graph grammars

CS 40 Computing for the Web

Introduction to programming

Chapter 8. Living with Java. 8.1 Standard Output

CS 106 Introduction to Computer Science I

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Building Java Programs

Transcription:

CS 106 Introduction to Computer Science I 01 / 21 / 2014 Instructor: Michael Eckmann

Today s Topics Introduction Homework assignment Review the syllabus Review the policies on academic dishonesty and improper collaboration Introductory comments on programming languages An example of a simple Java program Fill out a questionnaire

Who is your instructor? I'm Mike Eckmann, an Assistant Professor in the Mathematics and Computer Science Dept., Skidmore College. This is my tenth year of teaching at Skidmore. Before coming to Skidmore I was at Lehigh University in PA. I studied Mathematics and Computer Engineering and Computer Science all at Lehigh University. I was employed as a programmer (systems analyst) for eight years.

1 st Homework Read Sections 1.1, 1.2 and 1.3 before Friday's class. I'll send everyone an email later today. Please reply to the email so I know you got it. In your reply, let me know if you want any additional email addresses for yourself added to the class list and what they are.

Syllabus Office hours Stop by / email Text book Class rooms Assignments Programs & HW Collaboration policy Grading scheme Attendance Workload Readings Note: The most up-to-date syllabus will be found on the course web page.

This semester we will... Be introduced to computer science. Learn programming (in Java)! Solve problems and learn to think like programmers. Hopefully have a fun learning experience.

Computer Science is... more than computer programming.

Programming Languages Machine Assembly High-level in no particular order Pascal, C, C++, Basic, Fortran, Java, Python, Ruby, many more... 4 th generation (e.g. visual) Procedural vs. Object-oriented

Syntax vs. semantics Time flies like an arrow 3 meanings (semantics) for 1 syntax That's why English is not a programming language! Errors Run time (crashes) Compile time (syntax errors) Logic (semantic errors) Have any of you encountered any of these with software that you use? Which kinds of errors? Examples?

Hello, World! program /* CS106 first program written by Michael Eckmann purpose: print the text Hello, World! to the screen */ public class HelloWorld { public static void main(string args[]) { System.out.println("Hello, World!"); } // end of main method } // end of HelloWorld class

Discussion of Hello, World! comments in source code basic structure of a Java program every Java program must have at least one class every Java program must have exactly one main method System.out.println the use of braces -- begin and end classes and method bodies. Braces are { and } I call them curly braces. keywords public, static, void, class, etc... see text book section 1.2

Some common programming mistakes (bugs) Case matters --- upper case letters in class names etc. are different than lower case letters --- e.g. String and string are understood differently by the compiler. Additionally the capitalized String is necessary in the main method argument list declaration. System must be capitalized when referring to something like: System.out because System is the name of the class/object, system with a lower case s is not. Spaces are not allowed in a class name or any identifier / variable name e.g. HelloWorld is legal, Hello World is not.

Some common programming mistakes (bugs) (continued) Double quotes must be paired up when giving the value of a String --- can t forget the second double quote. Curly braces must be paired correctly. An extra one dangling somewhere in the code will cause errors, same if one is forgotten. Class name must be the same as the main part of the file name. e.g. a public class named HelloWorld must reside in a file named HelloWorld.java --- not in something like HW.java or any other file. Java source code can only be in plain text files. If a Java source code file is stored as a Word document or any other file type besides plain text, the code will not be able to be compiled.

Some common programming mistakes (bugs) (continued) Multi-line comments starting with /* must end with */ and cannot contain a */ in between. The reason is, once the compiler sees /*, it considers everything a comment until the first */ that it sees, then starts interpreting as code once again. Note: Encountering bugs is a good thing because it helps the learning process --- e.g. when you encounter similar bugs in the future, you re likely to find the solution more quickly.

Comments Comments are ignored by the compiler Single line comments start with // and the comment continues until a newline Multi-line comments start with /* and end with */ Another type of multi-line comment starts with /** and ends with */ What s the purpose of comments if the compiler ignores them?

println vs. print println adds a newline character (carriage return) after the String is printed. print does not.

println vs. print System.out.println( I love java. ); System.out.println( It tastes good. ); If these two statements were inside the main method's curly braces, then the program would print: I love java. It tastes good.

println vs. print System.out.print( I love java. ); System.out.println( It tastes good. ); Will print: I love java.it tastes good.

println vs. print System.out.print( I love java. ); System.out.print( It tastes good. ); How will this print?

Phases of Java Programs create / compile the program Create / Edit the source code of the program Compile the source code into bytecodes After the.java file is compiled we get a.class file E.g. if we compile a program called HelloWorld.java the result is HelloWorld.class When we execute the program (via the java interpreter), the interpreter does the following: Loads the Classes Verifies the Bytecodes Interprets the Bytecodes All of these execution steps are done on the.class file(s)

Phases of Java Programs see figure 1.1 in your text on page 10. It contains a diagram showing the creation and execution of a Java program. terminology you need to understand source code compile, build bytecode execute, run

Phases of Java Programs terminology you need to understand source code (= the.java files) compile, build (the source code into bytecode) bytecode (= the.class files) execute, run (the.class file(s) using the Java bytecode interpreter)

Eclipse Eclipse is the integrated development environment that we will use for this course. You are free to use others but I will give instructions for Eclipse and show examples during labs, etc. in Eclipse Let's try it.

Escape sequences A String's value is specified between double quotes. Within the double quotes the backslash character \ is special. The backslash character followed by another character is called an escape sequence. Escape sequences are handled differently than regular characters. \n is a common escape sequence to embed a newline \t is a way to add a tab to a string E.g. System.out.println( Hello\nWorld ); prints: Hello World

Escape sequences \b is the backspace character There are only a total of eight valid escape sequences. Let s say you wanted to print a sarcastic message using double quotes. If we do System.out.println(''Hamilton has a ''great'' lacrosse team''); there will be a syntax error at compile time.

Escape sequences System.out.println(''Hamilton has a ''great'' lacrosse team''); The string will be Hamilton has a and then the compiler will see a g and will generate an error because the second double quote (the one before great) causes the string to end. What do we do?

Escape sequences There is an escape sequence to represent a ''. It is \'' System.out.println(''Hamilton has a \''great\'' lacrosse team''); The above line of code will do what we want now.

Escape sequences The \ character is special inside a String. Java will interpret the \ and the next character as an escape sequence (even if it is not one of the valid ones.) How might we add a backslash character to a String? Let s say we want a String like: C:\My Documents Will this work? System.out.println(''C:\My Documents'');

Let s try this problem Write an application that displays the numbers 1 to 4 on the same line, with each pair of adjacent numbers separated by one space. Write the program using the following methods: A) Using one System.out statement B) Using four System.out statements Now let's do the same thing but on 4 different lines. A) Using one System.out statement B) Using four System.out statements

String concatenation To concatenate two Strings together, Java provides the operator + e.g. System.out.println(''Hey '' + ''now.''); prints Hey now.

String concatenation Strings can only be concatenated to other Strings, but numbers are converted to Strings by Java automatically if the they are being concatenated to a String. e.g. System.out.println(''I am '' + 21 + '' years old.''); Will work the same as: System.out.println(''I am '' + ''21'' + '' years old.''); Will work the same as: System.out.println(''I am 21 years old.'');

Variables and Type A program often needs to be able to store and change data as it is running. For example, a program might need to store a user's name, or the user's age, or the air temperature, or whatever. Variables allow names to be given to data that can be stored and changed.

Variables and Type A variable is a location in memory (RAM) with the following properties. name --- given by the programmer --- try to be descriptive of the data the variable will hold type --- decided by the programmer from all available types, depending on what kind of values the programmer wants to store size --- amount of memory used, this is determined by the type value --- self-explanatory value can be stored / changed / read by a program.

Primitive Types for Variables byte, short, int, long --- these are integer types --- they hold whole numbers like 31, 4256, 56, 2632, 755, -901 (they hold different ranges) char --- this is a type that holds one character like a letter, digit, *, &, or an escape sequence boolean --- holds one of true or false float, double --- numeric types that hold numbers with decimal values like -5.5, 98.6, 1002.99995 etc. (they hold different ranges and to different precisions) We can look up the ranges/precision of the numeric types.

Variables and Type To be able to specify to our program that we want to use a variable, before we can use it we must declare the variable. That is we state the desire type as well as a descriptive name that we make up (must start with a letter and may contain letters, digits, and _ ). Example: If we wanted a place to store a person's age (years only e.g. 18, 21, 55 etc.), we might create a variable like this: int age; // declares the variable age of type int age is a name we made up, what does int do?

Variables and Type int age; // declares the variable age of type int age is a name we made up, what does int do? We chose int because we knew we wanted to only store whole numbers and the int type stores whole numbers. We could have chosen byte, short or long which also hold whole numbers but we typically use int unless we have a good reason to use one of those other three.

Variables and Type int area = 50; // This line of code not only // declares a variable but also initializes its value. name of variable is area type of variable is int size of variable is 32 bits (because an int has a size of 32 bits.) value of variable is currently 50 note the same thing can be done with 2 lines of code: int area; area = 50; // this is an assignment statement (it assigns // the value 50 to the variable area