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.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Using Processing Stream. Predefined Streams System.in InputStream used to read bytes from the keyboard System.out PrintStream used to write bytes to the.
Introduction to Computers and Programming Lecture 7:
BASIC JAVA. Hello World n // Hello world program public class MyFirstJavaProgram { public static void main(String args[]) { char c = 'H'; String s =
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered.
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.
Chapter 3b Standard Input and Output Sample Development.
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.
March 2005Java Programming1. March 2005Java Programming2 Why Java? Platform independence Object Oriented design Run-time checks (fewer bugs) Exception.
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.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 Course Lectures Available on line:
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.
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
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.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
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.
From C++ to Java A whirlwind tour of Java for C++ programmers.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Based on OOP with Java, by David J. Barnes Input-Output1 The java.io Package 4 Text files Reader and Writer classes 4 Byte stream files InputStream, FileInputStream,
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
CS100A, Fall 1997, Lecture 91 CS100A, Fall 1997 Lecture 9, Tuesday, 30 September Input/Output & Program Schema System.in, class Text, Some basic data processing,
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CS 11 C++ track: lecture 1 Administrivia Need a CS cluster account sysadmin/account_request.cgi Need to know UNIX (Linux)
Lab 2 Point List. OVERVIEW In this laboratory, you explore lists in which each element is a two-dimensional point or (x,y) pair. We refer to this type.
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.
Working With Objects Tonga Institute of Higher Education.
Java for C++ Programmers A Brief Tutorial. Overview Classes and Objects Simple Program Constructors Arrays Strings Inheritance and Interfaces Exceptions.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology- George Koutsogiannakis 1.
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.
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
I/O Basics Java does provide strong, flexible support for I/O related to files and networks. Java’s console based interaction is limited since in real.
CSCI 1100/1202 January 23, Class Methods Some methods can be invoked through the class name, instead of through an object of the class These methods.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Object-Oriented Programming Fundamentals. Comp 241, Fall Example Colored points on the screen What data goes into making one? Coordinates Color.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
Keyboard and Screen I/O (Input/Output). Screen Output  System.out is an object that is part of the Java language. (Don’t worry yet about why there is.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
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.:
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Object Oriented Programming Lecture 2: BallWorld.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Information and Computer Sciences University of Hawaii, Manoa
Objects as a programming concept
Introduction to Computer Science / Procedural – 67130
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II
I/O Basics.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Basic Elements of Java
Java Programming Language
Presentation transcript:

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 on an object importing classes input/output

objects and primitive types (1) everything in java is either an object a primitive type differences? objects take up more memory objects allocated in main computer memory (heap) objects have methods manipulating primitive types is faster

objects and primitive types (2) primitive types: boolean, byte, char, int, long, float, double char is actually 16-bits (Unicode) object types: String, arrays, user-defined classes

classes are a template for creating objects define the types of the data (fields) define the code that acts on the data (methods) can define methods that don't act on data static methods like global functions in most languages main() was an example of this

example class (1) public class Point2d { private double xcoord; private double ycoord; // Constructor: public Point2d(double x, double y) { this.xcoord = x; this.ycoord = y; } // continued on next slide...

example class (2) // no-argument constructor: public Point2d() { this.xcoord = 0.0; this.ycoord = 0.0; } // accessors: public double getX() { return xcoord; } public double getY() { return ycoord; } // not done yet...

example class (3) public void setX(double value) { xcoord = value; } public void setY(double value) { ycoord = value; } } // end of class Point2d

public and private what do public and private mean, anyway? private: can only be used by methods of this class public: can be used anywhere usually fields are private and methods are public (some exceptions) also protected (for a later lecture)

using the class Creating new objects of the class: Point2d a = new Point2d(2.0, 3.0); Point2d b = new Point2d(2.0, 3.0); System.out.println(a == b); // false! Calling methods: a.setY(10.0); n.b. methods have direct access to all of an object's fields

input/output (I/O) (1) input/output is very powerful and flexible in java downside: simple tasks are much more complicated than you'd expect first need to import the java i/o classes: // import all i/o classes import java.io.*; // can now say "Foo" instead of "java.io.Foo"

input/output (I/O) (2) printing: System.out.println() prints a data value + a newline System.out.print() prints a data value without a newline + is used for string concatenation (overloading) // this prints "foo10": System.out.println("foo" + 10);

input/output (I/O) (3) input is more complicated... need to know the following classes: InputStreamReader : converts bytes to characters StreamTokenizer : converts characters to "tokens" a token is a string of characters separated by space nextToken() method – gets next token nval field (double) – contains the numeric value of the next token (if it has one)

input/output (I/O) (4) sample input code: // System.in is the terminal input InputStreamReader isr = new InputStreamReader(System.in); StreamTokenizer tokens = new StreamTokenizer(isr); // Repeat for every token you need: tokens.nextToken(); // token value is in tokens.sval (and also in // tokens.nval if it's a number)

input/output (I/O) (5) nextToken() can throw an IOException if something goes wrong this is our first encounter with "exception handling" in java we'll deal with this in later lectures for now, have to change main() to be public static void main(String[] args) throws IOException { //... }

next week exception handling in depth more on OOP documentation (javadoc)