CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects.

Slides:



Advertisements
Similar presentations
Lecture 15: I/O and Parsing
Advertisements

Chapter 1 Writing a Program Fall Class Overview Course Information –On the web page and Blackboard –
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Program development.
1 Fall 2009ACS-1903 Methods – Ch 5 A design technique referred to as stepwise refinement (or divide and conquer, or functional decomposition) is used to.
ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
Datalogi A 1: 8/9. Book: Cay Horstmann: Big Java or Java Consepts.
OOP&M - laboratory lectures1 OOP&M – LAB2 LABzwei: the Input.
Programming in Java; Instructor:Alok Mehta Objects, Classes, Program Constructs1 Programming in Java Objects, Classes, Program Constructs.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
28-Jun-15 Using Objects. 2 Overview In this presentation we will discuss: Classes and objects Methods for objects Printing results.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java I/O – what does it include? Command line user interface –Initial arguments to main program –System.in and System.out GUI Hardware –Disk drives ->
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
Computer Science A 1: 3/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Java Programming: I/O1 Java I/O Reference: java.sun.com/docs/books/tutorial/essential/io/
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
1 Course Lectures Available on line:
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
CompSci 42.1Intro to Java Anatomy of a Class & Terminology Running and Modifying a Program.
Lecture 61 CS110 Lecture 6 Thursday, February 12, 2004 Announcements –hw2 due today Agenda –questions –testing –modeling text files –declarations (classes,
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Console Input. So far… All the inputs for our programs have been hard-coded in the main method or inputted using the dialog boxes of BlueJ It’s time to.
The Java Programming Language
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Input & Output In Java. Input & Output It is very complicated for a computer to show how information is processed. Although a computer is very good at.
CS61B L03 Building Objects (1)Garcia / Yelick Fall 2003 © UCB  Dan Garcia ( Kathy Yelick  (
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
Chapter 2: Java Fundamentals
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
File and Stream I/O, Redirection Privacy and Super
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
File IO Basics By Dan Fleck Coming up: Data Streams.
Lecture 101 CS110 Lecture 10 Thursday, February Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
CS 11 java track: lecture 2 This week: more on object-oriented programming (OOP) objects vs. primitive types creating new objects with new calling methods.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Department of Computer Engineering Using Objects Computer Programming for International Engineers.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
CSC Java Programming, Fall, 2008 Week 3: Objects, Classes, Strings, Text I/O, September 11.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Java Input and Output. Java Input  Input is any information needed by your program to complete its execution  So far we have been using InputBox for.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Java Input / Output l a modular approach to input/output: - different stream objects are connected/wrapped to handle I/O l a data stream object: a flow.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
I/O in java and Revision. 2 I/O in Java Many packages and libraries associated with Java provide sophisticated ways to input and output information, e.g.:
Chapter 2 Basic Computation
CS0007: Introduction to Computer Programming
Introduction to Computer Science / Procedural – 67130
Objects, Classes, Program Constructs
I/O Basics.
I/O Streams- Basics Byte Streams and Character Streams
Introduction to Computing Using Java
LCC 6310 Computation as an Expressive Medium
Presentation transcript:

CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects

CS61B L02 Using Objects (2)Garcia / Yelick Fall 2003 © UCB Design Problem Revisited Want to build a web search engine like Google How can we break this into smaller problems? –Design a data structure to store keyword/urls –Design interface to respond to keyword queries –Build a web crawler to collect words on each page »Open a connection »Process strings on that page Find title Eliminate html junk Eliminate useless words (the, and, is, …) »Save results in some files –And much more

CS61B L02 Using Objects (3)Garcia / Yelick Fall 2003 © UCB How Long Does It Take? Factoid: 23% of web pages change daily How long does it take to crawl the full web? a)< 2 minutes b)< 2 hours c)< 2 days d)< 2 weeks How long did it take for the 61B web page changes to show up in Google’s own copy? a)< 2 minutes b)< 2 hours c)< 2 days d)< 2 weeks

CS61B L02 Using Objects (4)Garcia / Yelick Fall 2003 © UCB Design by Wishful Thinking Approach a problem using top-down design –Break it into pieces –Assume you can build most of them –Concentrate on one at a time –When you’re sure you can build one, go onto the next Rule: don’t write any code until you know how to build the pieces –We won’t follow this rule in lecture Three parts of Java we’ll need in this design: –Strings, Web connections, Files (later)

CS61B L02 Using Objects (5)Garcia / Yelick Fall 2003 © UCB Strings are Objects Strings are objects: What does that mean? –We can give them a name that will refer to the object –We can send them messages to have them do things »Tell me your length »What is your first character? »In Java these are called methods: ~40 on strings –It belongs to a class (the “factory” that created it) »We also call this the type of the object »All string objects are of type String s “Hello World”

CS61B L02 Using Objects (6)Garcia / Yelick Fall 2003 © UCB Box and Pointer Diagrams To understand complicated data structures draw pictures! A variable is shown as a box with a name –A variable may contain a primitive value (3, 2.5, etc.) –Or it may contain a reference to an object »Drawn as a pointer »The variable “refers to” or “points to” the object An object is also a box –So far we have seen only string objects –We will see that there may be variables inside objects s

CS61B L02 Using Objects (7)Garcia / Yelick Fall 2003 © UCB Variables and Objects Declare a variable String s1; s1 Create an object by calling a constructor s1 = new String(); “” Do both together s2 String s2 = new String(“white”); “white” Assign one to another (swing pointer) s1 = s2; X

CS61B L02 Using Objects (8)Garcia / Yelick Fall 2003 © UCB Invoking Methods on Strings Can invoke a string method using “.” Strings are immutable: cannot modify string objects Setting one does not affect the other s1 “white” s2 s2 = s2.concat(“house"); x “whitehouse” s2 = s2.substring(0,5); “white” x Wait: “house” is a String object, but it was not created with “new String…” Why? Because Strings are special

CS61B L02 Using Objects (9)Garcia / Yelick Fall 2003 © UCB Equality on Strings Two notions of equality: == and.equals Same objectDifferent object, same value s1 “white” s2 s1 “white” s2 “white” (s1 == s2) is true s1.equals(s2) is true (s1 == s2) is false s1.equals(s2) is true s2 = s1 s2 = new String(s1)

CS61B L02 Using Objects (10)Garcia / Yelick Fall 2003 © UCB Printing Strings To output strings, use “println” –Can concatenate them using “+” –Can also use + on string with another type System.out.println("s1 is: " + s1); System.out.println("the length of s1 is " + s1.length()); System.out.println(s1 + s2 + s1); Do you feel you know enough about strings in Java to write a program that finds keywords in a string representing a line on a web page?

CS61B L02 Using Objects (11)Garcia / Yelick Fall 2003 © UCB Scheme vs. Java: Differences SchemeJava Punctuation Comments ; // or /* */ Separators ( ),;.{ } ( ) Arithmeticprefixinfix Assignment set!= Types Implicit: list, atom, function Explicit int, char, boolean, …

CS61B L02 Using Objects (12)Garcia / Yelick Fall 2003 © UCB Scheme to Java SchemeJava Kinds of Values Primitive atom int, char, boolean … Non-primitive built-in ListsString,URL,… Non-primitive user defined Trees, etc. NewTree,… Invoking Functions With objects (ask o ‘m arg …)o.m( arg …) Without objects (f arg …)f( arg …)

CS61B L02 Using Objects (13)Garcia / Yelick Fall 2003 © UCB Announcements Lab 1 and homework 1 are online –Homework 1 is due at 11:59 Tuesday night –Homeworks will be auto-graded Textbooks were at Ned’s yesterday –Reading assignments online (do not need to know html) The CS61B Course Reader –Available at Copy Central on Euclid/Hearst –Contains “Scheme to Java”, tool documentation, etc. Labs and discussions –You must attend your assigned lab (change through Telebear if you must) –You may attend a different discussion, although the one that matches your lab has the same TA

CS61B L02 Using Objects (14)Garcia / Yelick Fall 2003 © UCB Input/Output (I/O) in Java To build a web crawler we need: Read from files across the web. We’ve seen one simple form of output –System.out.println(“some string”); –System.out is a PrintStream that prints to the screen What about input? Does System have anything? – –System.in is an InputStream from the keyboard. –And there’s a URL class with an openStream method, which creates an InputStream to the web –But you can’t read a line of text from an InputStream

CS61B L02 Using Objects (15)Garcia / Yelick Fall 2003 © UCB Input/Output (I/O) in Java We know how to construct an InputStream An InputStreamReader is one kind of Reader We want to construct a BufferedReader Need to have a Reader to call the constructor To construct an InputStreamReader we can use an InputStream

CS61B L02 Using Objects (16)Garcia / Yelick Fall 2003 © UCB Why So Complicated? InputStream objects (like System.in) –read raw data, but don't format the data. InputStreamReader objects –compose the raw data into characters. BufferedReader objects –compose the characters into lines of text. Why are these tasks divided among three different classes? –So one can reimplement part (for efficiency) –Can reuse some parts of code for different kinds of I/O (file, network, user, …)

CS61B L02 Using Objects (17)Garcia / Yelick Fall 2003 © UCB Code for Accessing the Web class WHWWW { public static void main(String[] arg) throws Exception { URL u = new URL(" InputStream ins = u.openStream(); InputStreamReader isr = new InputStreamReader(ins); BufferedReader whiteHouse = new BufferedReader(isr); System.out.println(whiteHouse.readLine()); }

CS61B L02 Using Objects (18)Garcia / Yelick Fall 2003 © UCB Summary Break large designs into pieces –Top-down design –Concentrate on one piece at a time Java and Scheme –Many syntactic differences –Many semantic similarities Strings and I/O Streams –Java Strings and Streams are objects –Use the online reference to the Java library Understanding objects –Draw pictures!