Object Oriented Programming and Design in Java. Session 2 Instructor: Bert Huang

Similar documents
Introduction to Programming Using Java (98-388)

Fall 2017 CISC124 9/16/2017

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

CS 151. Exceptions & Javadoc. slides available on course website. Sunday, September 9, 12

CS 302: Introduction to Programming

PROGRAMMING FUNDAMENTALS

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

Announcements. 1. Forms to return today after class:

Getting started with Java

Array. Prepared By - Rifat Shahriyar

CS 11 java track: lecture 1

16-Dec-10. Consider the following method:

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

Exceptions and Libraries

CS 302: Introduction to Programming in Java. Lectures 17&18. It s insanely hot. People desperately need some snowflakes

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

MIDTERM REVIEW. midterminformation.htm

Computational Expression

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

Lec 3. Compilers, Debugging, Hello World, and Variables

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

CS1007: Object Oriented Design and Programming in Java. Lecture #2 Sept 8 Shlomo Hershkop Announcements

Computer Science is...

Oracle 1Z Java SE 8 Programmer I. Download Full Version :

Review: Array Initializer Lists

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

CS 11 java track: lecture 3

Dynamic Arrays. Fundamentals of Computer Science

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

Introduction to Java & Fundamental Data Types

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

EECS1710. Announcements. Labtests have been returned Term Test #01 marking is in progress. Next Labtest: Thu Oct 23/Fri Oct 24

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

Pace University. Fundamental Concepts of CS121 1

Chapter 6 Classes and Objects

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

CS 211: Using ArrayList, Implementing Arraylist

CMSC 331 Second Midterm Exam

double float char In a method: final typename variablename = expression ;

CS 231 Data Structures and Algorithms, Fall 2016

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

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012)

Introduction to Java

CS 161: Object Oriented Problem Solving

Lecture 22: Java. Overall Structure. Classes & Objects. Every statement must end with ';' Carl Kingsford, , Fall 2015

What did we talk about last time? Examples switch statements

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

First Java Program - Output to the Screen

BLM2031 Structured Programming. Zeyneb KURT

COMP-202: Foundations of Programming. Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015

EECS168 Exam 3 Review

last time in cs recitations. computer commands. today s topics.

COE318 Lecture Notes Week 8 (Oct 24, 2011)

Starting Out with Java: From Control Structures through Data Structures 3e (Gaddis and Muganda) Chapter 2 Java Fundamentals

JAVA REVIEW cs2420 Introduction to Algorithms and Data Structures Spring 2015

Arrays. CSE 142, Summer 2002 Computer Programming 1.

Full file at

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2014

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

CS112 Lecture: Primitive Types, Operators, Strings

Outline. Java Compiler and Virtual Machine. Why Java? Naming Conventions. Classes. COMP9024: Data Structures and Algorithms

S.E. Sem. III [CMPN] Object Oriented Programming Methodology

Chapter 8. Exception Handling. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Array. Array Declaration:

Final Exam Practice. Partial credit will be awarded.

About this exam review

Mr. Monroe s Guide to Mastering Java Syntax

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

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

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

EECS1710. Checklist from last lecture (Sept 9, 2014) " get an EECS account (if you don t have it already) " read sections

Loops. CSE 114, Computer Science 1 Stony Brook University

H212 Introduction to Software Systems Honors

Gaddis: Starting Out with Java: From Control Structures through Objects, 6/e

Chapter 15. Exception Handling. Chapter Goals. Error Handling. Error Handling. Throwing Exceptions. Throwing Exceptions

SE2205B - DATA STRUCTURES AND ALGORITHMS JAVA BASICS. Kevin Brightwell. Tuesday January 10th, Acknowledgements:Dr.

CSC Java Programming, Fall Java Data Types and Control Constructs

An overview of Java, Data types and variables

Program Fundamentals

Question: Total Points: Score:

Exception Handling. General idea Checked vs. unchecked exceptions Semantics of... Example from text: DataAnalyzer.

CS Week 5. Jim Williams, PhD

CS 455 Midterm Exam 2 Fall 2015 [Bono] Nov. 10, 2015

AP Computer Science Unit 1. Programs

CSCI 1103: File I/O, Scanner, PrintWriter

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

PRIMITIVE VARIABLES. CS302 Introduction to Programming University of Wisconsin Madison Lecture 3. By Matthew Bernstein

ECE 122. Engineering Problem Solving with Java

DM550 Introduction to Programming part 2. Jan Baumbach.

Dining philosophers (cont)

CS Programming I: ArrayList

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

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

4. If the following Java statements are executed, what will be displayed?

Week 7 - More Java! this stands for the calling object:

Transcription:

Object Oriented Programming and Design in Java Session 2 Instructor: Bert Huang

Announcements TA: Yipeng Huang, yh2315, Mon 4-6 OH on MICE clarification Next Monday's class canceled for Distinguished Lecture: Feb 1, 11 AM Davis Auditorium. Course survey due by next class for 1 point extra credit

Schedule Sun Mon Tue Wed Thu Fri John 1-3 Class 11-12:15 Bert 2-4 Yipeng 4-6 Lauren 5:30-7:30 Class 11-12:15

Three Rivers in Machine Learning: Data, Computation and Risk John Lafferty Carnegie Mellon University Abstract: Machine learning is a confluence of computer science and statistics that is empowering technologies such as search engines, robotics, and personalized medicine. Fundamentally, the goal of machine learning is to develop computer programs that predict well, according to some measure of risk or accuracy. The predictions should get better as more historical data become available. The field is developing interesting and useful frameworks for building such programs, which often demand large computational resources. Theoretical analyses are also being advanced to help understand the tradeoffs between computation, data, and risk that are inherent in statistical learning. Two types of results have been studied: the consistency and scaling behavior of specific convex optimization procedures, which have polynomial computational efficiency, and lower bounds on any statistically efficient procedure, without regard to computational cost. This talk will give a survey of some of these developments, with a focus on structured learning problems for graphs and shared learning tasks in high dimensions.

Review Course information Prerequisites Assignments and expectations Course goals

Today's Plan: Java Review basic syntax, javadoc, primitive types, references, importing packages, exceptions, input, Arrays, ArrayLists, declaration keywords, code style CUNIX and Eclipse demo

Compiling Java Java Code (text format) javac Java program Java is a compiled language Your code tells the compiler what machine code to produce

public class SyntaxTester { public static void main(string [] args) { for (int i = 0; i < MAX; i++) { System.out.println("Iteration " + i); if (i == 1) { System.out.println("i is 1"); } else { System.out.println("i is not 1"); } } } final static int MAX = 5; }

Javadoc Automated documentation generation Comment each class, each public method, its parameters and return value javadoc converts comments into organized html website

/** * This class doesn't do anything, * but demonstrates javadoc. * @author Bert Huang */ public class Nothing { /** * This method is commented with javadoc * @param x Whatever I write here is * added to the documentation * @param y Looking like a fool with * your pants on the ground. * @return If this were an actual method, * this should say something more helpful. */ public int donothing(int x, int y) { return 1000; } }

Primitive Types int, long, short, byte char boolean double, float Java allocates memory for each variable of these types

Object References All other variables are references to objects Running a constructor allocates memory for the variable String word; word = new String(); word word

Primitive Type Wrappers You can use primitive types as objects by using wrappers: Integer, Byte, Short, Long Double, Float Character Boolean

Reference Headaches to Avoid MyObject A = new MyObject(); MyObject B = A; A B String C = "hello"; String D = "hello"; C == D? C D Use foo.equals(bar) instead "hello" "hello"

Scope i = 5; for (int i = 0; i < 10; i++) { System.out.println(i); } System.out.println(i);

Scope public class Example { public Example(String name) { System.out.println(name); this.name = name; } private String name; }

Importing Packages To import built in (or 3rd party) packages: import java.util.random; Adds classes in package to your space You can always refer to classes by their full name: java.util.random rand = new java.util.random();

Scanners for Input import java.util.scanner; Scanner in = new Scanner(System.in); in.nextline(); // returns String until \n in.nextint(); // reads next token as int in.nextdouble(); // double in.usedelimiter(","); // sets token separator to, in.next(); // returns next token new Scanner(new File("fileToRead.txt"));

Handling Exceptions Methods can throw Exceptions to indicate runtime errors Methods are declared to throw exceptions, and javac complains if you don't handle them try { // something // dangerous } catch (Exception e) { // handle e }

Throwing Exceptions public void mymethod() throws IOException { // do something that might throw IOException } Transfers responsibility to caller of mymethod() Try to handle exception asap

Arrays Objects can be grouped into arrays of similar objects int [] A = new int[5]; 0 1 2 3 4 A[0] A[1] A[2] A[3] A[4] A[5] causes IndexOutOfBoundsException

ArrayLists ArrayList<String> list = new ArrayList<String>(); No fixed size, grows as needed Class in < > indicates what is stored in the ArrayList list.add("blah"); list.add("blah", 4);

Enhanced For Loop For each element of array or ArrayList Arraylist<String> list; //... put stuff into list... for (String current : list) { // do something with current String }

Declaration Keywords public - Available to all other objects private - Available only to this object static - Exists independently of instantiation. Behaves the same for each instance of the class. final - Will never change. Use this for constants

Code Style Consistency is most important; easily find code Some good suggestions by Horstmann: http://www.horstmann.com/bigj/style.html Some highlights: lowercase variable and methods class names Uppercase No magic numbers, use ALL_CAPS final constants Put spaces around all binary operations (==, +, >) Space after every comma

CUNIX and Eclipse

Reading Horstmann Ch. 2 for next two sessions Cunix info page http://www.cs.columbia.edu/~bert/ courses/1007/cunix.html