Midterms Save the Dates!

Similar documents
Midterms Save the Dates!

Using Classes and Objects. Lecture 7. Midterms Save the Dates! Extra Credit Survey

PeerWise Study. Midterms Save the Dates! University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Constants Using Classes and Objects

Midterms Save the Dates!

Midterms Save the Dates!

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

! Rest of Chap 2 ! 2.3-4, ! Rest of Chap 4 ! ! Things that do not vary. ! unlike variables. ! will never change. !

Midterms Save the Dates!

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

Midterms Save the Dates!

Midterms Save the Dates!

Survey #2. Variable Scope. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings. Scope Static.

Midterms Save the Dates!

Midterms Save the Dates!

Survey #2. Teen Talk Barbie TM Reloaded. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Partially Filled Arrays ArrayLists

Lecture Set 2: Starting Java

Lecture Set 2: Starting Java

Survey #2. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Programming with Java

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

Using Java Classes Fall 2018 Margaret Reid-Miller

AP Computer Science Unit 1. Writing Programs Using BlueJ

Survey #2. Assignment #3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings. Static Interface Types.

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

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

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

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

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

AP Computer Science Unit 1. Writing Programs Using BlueJ

CS 101 Fall 2006 Midterm 1 Name: ID:

First Java Program - Output to the Screen

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Computer Science is...

Computational Expression

AP Computer Science Unit 1. Programs

Survey #2. Programming Assignment 3. Final Exam. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu.

Full file at

Introduction to Java Unit 1. Using BlueJ to Write Programs

Lesson 3: Accepting User Input and Using Different Methods for Output

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 6, Name:

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

Midterms Save the Dates!

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 1, Name: KEY A

Supplementary Test 1

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

COMP 202 Java in one week

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

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

Practice Midterm 1. Problem Points Score TOTAL 50

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

CS 112 Introduction to Programming

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

Faculty of Science Midterm. COMP-202B - Introduction to Computing I (Winter 2008)

COMP 401 Midterm. Tuesday, Oct 18, pm-3:15pm. Instructions

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6

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

H212 Introduction to Software Systems Honors

CS 231 Data Structures and Algorithms, Fall 2016

COMP 202 Java in one week

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

Chapter 1 Lab Algorithms, Errors, and Testing

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

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

Example Program. public class ComputeArea {

CS 106 Introduction to Computer Science I

! This week: Chapter 6 all ( ) ! Formal parameter: in declaration of class. ! Actual parameter: passed in when method is called

Programming Exercise 7: Static Methods

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

One of Mike s early tests cases involved the following code, which produced the error message about something being really wrong:

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

COMP-202 Unit 5: Basics of Using Objects

Using System.out.println()

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Kickstart Intro to Java Part I

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

4. Java Project Design, Input Methods

Lecture 8 " INPUT " Instructor: Craig Duckett

COMP 202. Java in one week

CS 170 Java Programming 1. Week 10: Loops and Arrays

CS 177 Recitation. Week 1 Intro to Java

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination

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

(First is used here)

CS 211: Existing Classes in the Java Library

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

AP Computer Science A: Java Programming

CS 11 java track: lecture 1

Final Exam Practice. Partial credit will be awarded.

Object-oriented programming in...

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

COMP 110 Project 1 Programming Project Warm-Up Exercise

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner

CS221: Algorithms and Data Structures. Asymptotic Analysis. Alan J. Hu (Borrowing slides from Steve Wolfman)

Transcription:

University of British Columbia CPSC 111, Intro to Computation Alan J. Hu (Using the Scanner and String Classes) Anatomy of a Java Program Readings This Week s Reading: Ch 3.1-3.8 (Major conceptual jump ) Lecture 9 Some slides borrowed from Kurt Eiselt, Tamara Munzner, and Steve Wolfman. Some learning goals from Beth Simon. Labs and Tutorials This week is Lab #3. Midterms Save the Dates! Midterm #1 is 5:30-6:30pm on February 10 (Tuesday) in Woodward IRC 2 Midterm #2 is 6-7pm on March 11 (Wednesday) in Woodward IRC 2 If you have a conflict with the first midterm, send me an email with your name, student ID number, and a brief explanation of your conflict, by noon, Tuesday, February 3! Extra Credit Survey Dr. Ben Yu is studying attitudes towards learning in introductory CS classes. Three surveys to complete during the term. Survey 1 is on WebCT now. Due January 28 These are completely optional. However, to encourage participation, I will give 1 pt of extra credit, applied to your labs, if you complete all three surveys in time. PeerWise Instructions For people participating in PeerWise, instructions are now available on WebCT. Your first questions/answers must be done by February 9. If you have questions/problems, please contact Paul Denny. 1

Learning Goals By the end of the next several lectures you will be able to Explain the concepts of class, object, and method as used in computer programming. Find pre-defined classes in the Java library, understand the documentation, and use the methods for basic classes. Write programs that use common Java classes like BigInteger, String, and Scanner. Learning Goals By the end of class today you will be able to Explain what syntax, runtime, and logical errors are. Write programs using (more features of) the Scanner and String classes. Navigate around a larger Java program and recognize basic building blocks, such as the class header and definition, methods definitions (bodies), and statements. Change of Gears Program Development Use an editor to create your Java program often called source used interchangeably with program or instructions in the computer world Another program, a compiler or an interpreter, translates source into target language or object, which is often machine language Finally, your computer can execute object Syntax Rules to dictate how statements are constructed. Example: open bracket needs matching close bracket If program is not syntactically correct, cannot be translated by compiler Different than humans dealing with natural languages like English. Consider statement t t with incorrect syntax (grammar) for weeks. rained in Vancouver it hasn t we still have pretty good shot at figuring out meaning Semantics What will happen when statement is executed Programming languages have well-defined semantics, no ambiguity Different than natural languages like English. Consider statement: Mary counted on her computer. How could we interpret this? Programming languages cannot allow for such ambiguities or computer would not know which interpretation to execute 2

Computers follows our instructions exactly If program produces the wrong result, it s the programmer s fault unless the user inputs incorrect data then cannot expect program to output correct results: Garbage in, garbage out (GIGO) Debugging: process of finding and correcting errors Unfortunately can be very time consuming! compile-time error Error at compile time (during translation) you did not follow syntax rules that say how Java elements must be combined to form valid Java statements run-time error compile-time error compile-time error run-time error logical error Error at run time (during execution) Source compiles Syntactically (structurally) correct But program tried something computers cannot do like divide a number by zero. Typically program will crash: halt prematurely Logical error Source compiles Object runs But program may still produce incorrect results because logic of your program is incorrect Typically hardest problems to find Questions? What about String? Is String a primitive type? Is it a class? String is a class, but it s a special class! Automatically imported Built-in literals, e.g., This is a String literal. + operator for concatenation But it also has many other methods, that you can call, just like for any ordinary class (Let s take a look ) 3

Some Available String Methods public String touppercase(); Returns a new String object identical to this object but with all the characters converted to upper case. public int length(); Returns the number of characters in this String object. public char charat( int index ); Returns the character at the given index. Note that the first character in the string is at index 0. More String Methods public String replace(char oldchar, char newchar); Returns a new String object where all instances of oldchar have been changed into newchar. public String substring(int beginindex); Returns new String object starting from beginindex position public String substring( int beginindex, int endindex ); Returns new String object starting from beginindex position and ending at endindex position up to but not including endindex char: substring(4, 7) o K H e l l o K e r m i t F r o g 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Keyboard Input Want to type on keyboard and have Java program read in what we type store it in variable to use later Want class to do this build our own? find existing i standard d Java class library? find existing library distributed by somebody else? Scanner class does the trick java.util.scanner nicer than System.in, the analog of System.out Import Scanner class from java.util package Declare string variable to store what user types in 4

Use Scanner constructor method to create new Scanner object named scan could be named anything, like keyboardstuff or foo Prompt user for input nextline method reads all input until end of line returns it as one long string of characters Print out the message on the display Scanner Class Methods Questions? The Scanner class has other methods to read other kinds of input, e.g., nextdouble() nextint() next() See online documentation or section 4.7 in your book for more. 5

Complex things have internal structure: books, countries, humans, etc. Java programs can be very complex! Java provides a lot of tools to help structure big programs. A Java program consists of a number of classes Each class lives in a file with the same name (.java) (In really large programs, classes can be grouped into packages.) Each of these class files declares or defines a new data type (a class). Each class usually groups together parts of a program to represent some concept or logical part of the program. A class starts with a class header and then stuff enclosed in curly braces A Simple Java Program // Our first Java program. /* Traditionally, one s first program in a new language prints out Hello, World! */ class HelloTester public static void main(string[] args) System.out.println( Hello, World! ); A Java program consists of a number of classes Each class lives in a file with the same name (.java) (In really large programs, classes can be grouped into packages.) Each of these class files declares or defines a new data type (a class). Each class usually groups together parts of a program to represent some concept or logical part of the program. A class starts with a class header and then stuff enclosed in curly braces A Java program consists of a number of classes. Each class can contain one or more methods: A method groups together some instructions to do some meaningful task for the class. A method starts with a method header and then some stuff in curly braces (Note that again, we are using the same word for when we create (declare, define) a method and for when we use (call, invoke) a method. There can be a special main method. If the class is run by the Java interpreter, then execution (fetchde-execute) starts with the main method. A Simple Java Program // Our first Java program. /* Traditionally, one s first program in a new language prints out Hello, World! */ class HelloTester public static void main(string[] args) System.out.println( Hello, World! ); 6

A Java program consists of a number of classes. Each class can contain one or more methods. A method contains a sequence of statements: Execution starts with the first statement, and goes step-by-step from there. (Fetch-de-execute) execute) (We ll learn ways to change that later.) Simple statements end with semi-colons. (We ll see more complicated statements later.) A Simple Java Program // Our first Java program. /* Traditionally, one s first program in a new language prints out Hello, World! */ class HelloTester public static void main(string[] args) System.out.println( Hello, World! ); A Java program consists of a number of classes: logical concept or part of program. Each class can contain one or more methods: does some task for the class A method contains a sequence of statements: little steps that do what is needed to make the method happen. A Java program consists of a number of classes: the things, like X-Men. Each class can contain one or more methods: what they can do, their powers A method contains a sequence of statements: how they do it. A bigger Java programs Questions? 7