COMP 110 Introduction to Programming. What did we discuss?

Similar documents
Basic Computation. Chapter 2

Chapter 2 Part 2 Edited by JJ Shepherd, James O Reilly

COMP 110 Introduction to Programming. What did we discuss?

COMP String and Console I/O. Yi Hong May 18, 2015

COMP 110 Introduction to Programming. What did we discuss?

9/11/08 (c) 2008 Matthew J. Rutherford Class (c) 2008 Matthew J. Rutherford Class

Introduction to Computers and Java. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

Introduction to Computers and Java

Introduction to Computers and Java

Introduction to Computers and Java. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

Basic Computation. Chapter 2

COMP 110 Introduction to Programming. What did we discuss?

Basic Computation. Chapter 2

COMP Introduction to Programming If-Else Statement, Switch Statement and Loops

Introduction to Computers and Java

Introduction to Computers and Java

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

Introduction to Computers and Java

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Table of Contents Date(s) Title/Topic Page #s. Abstraction

COMP 110 Introduction to Programming. What did we discuss?

Lecture Set 2: Starting Java

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Introduction to Computers and Java

Lecture Set 2: Starting Java

Section 2: Introduction to Java. Historical note

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

Section 2.2 Your First Program in Java: Printing a Line of Text

Darrell Bethea May 25, 2011

COMP Computer Basics. Yi Hong May 13, 2015

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

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

CSCE 145 Exam 1 Review. This exam totals to 100 points. Follow the instructions. Good luck!

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Problem Solving With C++ Ninth Edition

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Programming with Java

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Fundamentals of Programming Data Types & Methods

COMP 110 Project 1 Programming Project Warm-Up Exercise

Using Java Classes Fall 2018 Margaret Reid-Miller

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

CS 106 Introduction to Computer Science I

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

CSc Introduction to Computing

First Java Program - Output to the Screen

MIDTERM REVIEW. midterminformation.htm

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

B.V. Patel Institute of BMC & IT, UTU 2014

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Midterms Save the Dates!

Chapter 1 Lab Algorithms, Errors, and Testing

Full file at

A variable is a name for a location in memory A variable must be declared

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

An overview of Java, Data types and variables

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

AP Computer Science A

Elementary Programming

CMSC131. Introduction to your Introduction to Java. Why Java?

We now start exploring some key elements of the Java programming language and ways of performing I/O

STUDENT LESSON A12 Iterations

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

COMP Introduction to Programming Primitive Types, Strings and Console I/O

CSE 1223: Introduction to Computer Programming in Java Chapter 1 Computer Basics

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Tutorials. Tutorial every Friday at 11:30 AM in Toldo 204 * discuss the next lab assignment

Introduction to Software Development (ISD) David Weston and Igor Razgon

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

when you call the method, you do not have to know exactly what those instructions are, or even how the object is organized internally

Chapter. Let's explore some other fundamental programming concepts

STUDENT LESSON A7 Simple I/O

Section 2.2 Your First Program in Java: Printing a Line of Text

CS 106 Introduction to Computer Science I

Software and Programming 1

Lecture 2 Tao Wang 1

4 WORKING WITH DATA TYPES AND OPERATIONS

BEFORE WE GET STARTED. BASIC TERMS TO KNOW JavaScript Python Basic Syntax Object String Functions Arguments Property While- Loops Varibles

COMP 202 Java in one week

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

CSCE 145 Exam 1 Review Answers. This exam totals to 100 points. Follow the instructions. Good luck!

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

CSc 2010 Principles of Computer Science, Fall 2013 Practice Problems for Midterm 3* * 3 17 % 9-20 % (26 / 7) "2"

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

Getting started with C++ (Part 2)

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

CS 1110, LAB 1: EXPRESSIONS AND ASSIGNMENTS First Name: Last Name: NetID:

4. Inputting data or messages to a function is called passing data to the function.

Full file at

2: Basics of Java Programming

Section 003 Fall CS 170 Exam 1. Name (print): Instructions:

Week 2: Data and Output

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

Basic Programming Language Syntax

Fundamentals of Programming Session 4

Transcription:

COMP 110 Introduction to Programming Fall 2015 Time: TR 9:30 10:45 Room: AR 121 (Hanes Art Center) Jay Aikat FB 314, aikat@cs.unc.edu Previous Class What did we discuss? COMP 110 Fall 2015 2 1

Today Announcements Lab1: due Wed, Sep 9 at 11:55 PM Midterm is on Thu, Oct 8 More If else COMP 110 Fall 2015 3 Some Terminology follows COMP 110 Fall 2015 4 2

Class Loader A Java program typically consists of several pieces called classes. Each class may have a separate author and each is compiled (translated into byte code) separately. A class loader (called a linker in other programming languages) automatically connects the classes together. COMP 110 Fall 2015 5 Programmer, User, Package The person who writes a program is called the programmer. The person who interacts with the program is called the user. A package is a library of classes that have been defined already. import java.util.scanner; COMP 110 Fall 2015 6 3

Arguments, Variables The item(s) inside parentheses are called argument(s) and provide the information needed by methods. A variable is something that can store data. An instruction to the computer is called a statement; it ends with a semicolon. The grammar rules for a programming language are called the syntax of the language. COMP 110 Fall 2015 7 Programming Programming is a creative process. Programming can be learned by discovering the techniques used by experienced programmers. These techniques are applicable to almost every programming language, including Java. COMP 110 Fall 2015 8 4

Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, dogs, etc.). Objects have state and behavior. A car has state (model, color, fuel level, etc), and behavior (start, change gear, brake, etc). An object s behavior can affect its state and the state of other objects. Object oriented programming (OOP) treats a program as a collection of objects, each with behaviors and state. COMP 110 Fall 2015 9 OOP Terminology Behaviors are included as methods. State is contained in a set of attributes. A class defines the methods and attributes. An object is an instance of a class. A program may have several instances of a class. (mycar, yourcar, hercar) Each object (instance of a class) has the same set of methods, but its own attribute values. (state) COMP 110 Fall 2015 10 5

Scanner Class Scanner keyboard = new Scanner(System.in); keyboard.nextline(); Scanner is a class keyboard is an instance of the Scanner class nextline() is a method (behavior) of the Scanner class. The Scanner has an internal attribute (state) which stores the delimiter. COMP 110 Fall 2015 11 Scanner Class The object performs an action when you invoke or call one of its methods objectname.methodname(argumentsthemethodneeds); COMP 110 Fall 2015 12 6

Algorithms By designing methods, programmers provide actions for objects to perform. An algorithm describes a means of performing an action. Once an algorithm is defined, expressing it in Java (or in another programming language) usually is easy. COMP 110 Fall 2015 13 Algorithms An algorithm is a set of instructions for solving a problem. An algorithm must be expressed completely and precisely. Algorithms usually are expressed in English or in pseudocode. COMP 110 Fall 2015 14 7

The Class String We've used constants of type String already. "Enter a whole number from 1 to 99." A value of type String is a Sequence of characters Treated as a single item. COMP 110 Fall 2015 15 String Methods An object of the String class stores data consisting of a sequence of characters. Objects have methods as well as data The length() method returns the number of characters in a particular String object. Try this: String greeting = "Hello"; int n = greeting.length(); System.out.println("Length of the string is " + n); COMP 110 Fall 2015 16 8

The Method length() The method length() returns an int. You can use a call to method length() anywhere an int can be used. int count = command.length(); System.out.println("Length is " + command.length()); count = command.length() + 3; COMP 110 Fall 2015 17 String Indices Positions start with 0, not 1. The 'J' in "Java is fun." is in position 0 A position is referred to an an index. The 'f' in "Java is fun." is at index 8. COMP 110 Fall 2015 18 9

String Methods Methods and their return types COMP 110 Fall 2015 19 String Methods COMP 110 Fall 2015 20 10

String Methods COMP 110 Fall 2015 21 String Methods COMP 110 Fall 2015 22 11

Putting Quotes in a String What do you do if you want to output How do I put quotes in my string? This won t work! System.out.println( How do I put quotes in my string? ); You have to let the computer know that you want the quote marks to be in the String System.out.println( How do I put \ quotes\ in my string? ); COMP 110 Fall 2015 23 Backslashes Backslash in a String means: the next character is special System.out.println( How do I put a \\ in my string? ); It will print: How do I put a \ in my string? COMP 110 Fall 2015 24 12

Escape Characters \ Double quote \ Single quote \\ Backslash \n New line \t Tab Each escape sequence is a single character even though it is written with two symbols. COMP 110 Fall 2015 25 Escape Characters How would you print "Java" refers to a language.? The compiler needs to be told that the quotation marks (") do not signal the start or end of a string, but instead are to be printed. System.out.println( "\"Java\" refers to a language."); COMP 110 Fall 2015 26 13

Examples try these System.out.println("abc\\def"); abc\def System.out.println("new\nline"); new line char singlequote = '\''; System.out.println (singlequote); ' COMP 110 Fall 2015 27 If and Else You can use only one if statement if (boolean expression) { statements; } other statements; Other statements will always be executed You can also use an if else statement if (boolean expression) { statement 1; } else { statement 2; } If the expression is true, run statement 1, otherwise run statement 2 COMP 110 Fall 2015 28 14

Using If and Else Use if else statement Do not use two if statements Always pay attention to boundaries Is it > or >=? Is it < or <=? Do you need a ==? COMP 110 Fall 2015 29 Using If-Else Pay attention to the brackets {} You can discard them if there is only one statement if (inputint > 0) { System.out.println( Positive ); } else { System.out.println( Negative or zero ); } if (inputint > 0) System.out.println( Positive ); else System.out.println( Negative or zero ); COMP 110 Fall 2015 30 15

Using If-Else Pay attention to the brackets {} if there is only one statement in {}, discard them If multiple statements, discarding {} will cause problems if (inputint > 0) System.out.println( Positive ); else System.out.println( Negative or zero ); System.out.println( What s happening? ); // will always be executed COMP 110 Fall 2015 31 Using If-Else Pay attention to the brackets {} As a good habit, don t discard them, even if you have only one statement in it The only exception: multibranch if else COMP 110 Fall 2015 32 16

Using If-Else Never put a semicolon after if or else if (inputint > 0); System.out.println( What s happening now? ); Compiler will interpret it as if (inputint > 0) { ;} System.out.println( What s happening now? ); COMP 110 Fall 2015 33 Next class More if else Reading Assignment: Chapter 2 COMP 110 Fall 2015 34 17