CS 200 Using Objects. Jim Williams, PhD

Similar documents
CS Week 2. Jim Williams

CS Week 2. Jim Williams, PhD

Week 6 CS 302. Jim Williams, PhD

CS Week 5. Jim Williams, PhD

Welcome to the Using Objects lab!

Welcome to the Using Objects lab!

AP CS Unit 3: Control Structures Notes

Fundamentals of Programming Data Types & Methods

Introduction to Computer Science Unit 2. Notes

Computational Expression

H212 Introduction to Software Systems Honors

CS 302: Introduction to Programming

Supplementary Test 1

Introduction to Computer Science Unit 2. Notes

Welcome to the Primitives and Expressions Lab!

CS111: PROGRAMMING LANGUAGE II

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

AP Computer Science Unit 1. Programs

Objectives of CS 230. Java portability. Why ADTs? 8/18/14

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

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

CS111: PROGRAMMING LANGUAGE II

Object Oriented Programming. Java-Lecture 1

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Programming with Java

Introduction to Programming Using Java (98-388)

Classes and Objects Miscellany: I/O, Statics, Wrappers & Packages. CMSC 202H (Honors Section) John Park

Formatting Output & Enumerated Types & Wrapper Classes

CS 200 Objects and ArrayList Jim Williams, PhD

Java API: Math, Wrapper Classes, StringBuffer Random Data Quiz2

CSCI 355 Lab #2 Spring 2007

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

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

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

Computational Expression

CS1083 Week 2: Arrays, ArrayList

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Getting started with Java

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

CS-201 Introduction to Programming with Java

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

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

Using Java Classes Fall 2018 Margaret Reid-Miller

Chapter 2: Objects and Primitive Data

Java Classes: Math, Integer A C S L E C T U R E 8

Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 45 Obtained Marks:

Java Assignment 3: Loop Practice Ver 3.0 Last Updated: 12/1/2015 8:57 AM

Lecture Set 2: Starting Java

CS110: PROGRAMMING LANGUAGE I

Chapter 5: Methods. by Tony Gaddis. Starting Out with Java: From Control Structures through Objects. Fourth Edition

Lecture Set 2: Starting Java

Practice Midterm 1 Answer Key

CS Week 11. Jim Williams, PhD

Introduction to Java Applications

CSE20 : Lab #4 - Data Types

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

COMP 202 Java in one week

CS 180. Recitation 8 / {30, 31} / 2007

Full file at

CSCI 355 LAB #2 Spring 2004

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

Reading Input from Text File

CS141 Programming Assignment #6

Computational Expression

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

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

CS 200 File Input and Output Jim Williams, PhD

Question: Total Points: Score:

Programming Language Basics

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

Computer Science II Data Structures

Practice Midterm 1. Problem Points Score TOTAL 50

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

Midterm Examination (MTA)

Arrays OBJECTIVES. In this chapter you will learn:

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

AP Computer Science Unit 1. Writing Programs Using BlueJ

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

Data Structure and Programming Languages

Programming Exercise 7: Static Methods

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Control Statements: Part 1

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

ITERATION WEEK 4: EXMAPLES IN CLASS

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Introduction to Java

Assignment 2.4: Loops

Unit 4: Classes and Objects Notes

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

CS 177 Spring 2010 Exam I

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

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

DM550 Introduction to Programming part 2. Jan Baumbach.

Object-Based Programming. Programming with Objects

Installing Java. Tradition. DP Computer Science Unit 4.3: Intro to programming 1/17/2018. Software & websites

CS61BL. Lecture 1: Welcome to CS61BL! Intro to Java and OOP Testing Error-handling

Final Exam Practice. Partial credit will be awarded.

Transcription:

CS 200 Using Objects Jim Williams, PhD

This Week Notes By Friday Exam Conflict and Accommodations Install Eclipse (version 8) Help Queue Team Lab 2 Chap 2 Programs (P2): Due Thursday Hours Spent Week? Lecture: Using Objects

Review of Key Concepts in Methods Call Definition Parameter, Parameter list Argument Header Body Return data type, value Variable Scope

Is this mprint Call or Definition? static void mprint() { } System.out.println("my print"); call definition

Defining Methods public class M { //method definition static void mprint() { System.out.println("my print"); } public static void main(string []args) { mprint(); // method call. } }

Argument vs. Parameter public static void main(string []args) { int num = 10; printcount( 23); //argument is value passed in printcount( num+3); } static void printcount(int count) { //parameter System.out.println( count); }

Local Variables Local variables are those declared within a method. Variables declared within a method are only available within that method.

What prints out? public static void calc(int num) { num = num + 3; } public static void main(string []args) { int num = 5; calc( num); System.out.println( num); } 3 5 35 error

Returning a Value from a Method public static void main(string []args) { int value = 5; int result = triple( value); } public static int triple(int num) { return num * 3; }

Which is called first: calc or println? public static int calc(int num) { num -= 33; return num; } public static void main(string []args) { int n = 55; System.out.println( calc( n)); } calc println error

Using Objects Lots of existing Java code! How do we build off it rather than having to write it ourselves? API - Application Programming Interface

Calling Class (static) Methods double numints = Math.pow( 2, 32); double root = Math.sqrt( 16); int num1 = 16; int num2 = 3; double result; result = num2 + Math.sqrt( num1); API

Instance vs. Class (static) Methods Class (static) Methods method definition has static modifier use name of class when calling Math.max( 10, 20); Instance (non-static) Methods method definition does Not have static modifier use instance of class when calling Random randgen = new Random(); randgen.nextint( 5);

Primitive vs Reference Data Types Primitive Store value int i = 5; i 5 Reference Store a reference to another location in memory that contains value Integer num; num = new Integer(9); num 9

Thought Experiment Imagine: Someone asks you to teach an introductory programming course this summer. Are you preparing yourself? Are you simply looking for an "A" or are you preparing yourself to teach programming to others?

The Study Cycle Check Am I using study methods that are effective? Do I understand the material enough to explain it to others? http://students.lsu.edu/academicsuccess/studying/strategies/tests/studying

What kind of methods? Math.max( 10, 20); Random randgen = new Random(); randgen.nextint( 5); max: class nextint: instance max: instance nextint: class max: class nextint: class max: instance nextint: instance

Wrapper Classes Primitive type Wrapper class boolean Boolean Wrapper classes contain helpful methods. byte char float int long short double Byte Character Float Integer Long Short Double

Data Type Conversions String weeknum = 3; String weeknum = "" + 3; String numstr = Integer.toString( 4); int num = Integer.parseInt( numstr);

Wrapper Classes Primitive type Wrapper class boolean Boolean byte Byte char Character float Float int Integer long Long Boxing: Putting primitive into instance of wrapper class Unboxing: retrieve primitive from instance auto-boxing/unboxing: when compiler inserts code to box/unbox. short double Short Double

Primitive vs Reference Variables int num1; num1 = 5; Integer num2; num2 = new Integer(7); Integer num3 = 9; int num4 = num3; //use Java Visualizer, options Show String/Integer objects checkbox

What will show when d1 is printed? Double d1 = new Double(10); double d2 = d1; d1 = 14.1; Double d3 = d1; d1 = d2; System.out.println( d3); 10 14.1 a reference error

String class A reference (class), not a primitive data type. Frequently used final String TITLE_PREFIX = "Welcome to Week "; int week = 2; System.out.println( TITLE_PREFIX + week);

String String name2 = "Alex"; name2.tolowercase(); String name3 = name2 + " Johnson"; System.out.print( name3); Alex Johnson alex johnson alex Johnson error instances of String are immutable (cannot change) methods in String class that appear to change strings actually return new strings.

Calling String methods String stra = "This is a string"; char achar = stra.charat( 3); achar: 'i' achar: 's' achar: ' '

What is the answer? String s1 = "An important programming tool."; String s2 = s1.substring( 9, 10); String s4 = new String( "?"); if ( s1.contains( "gram")) { s4 = s1.substring( 2, 4).trim(); } char c3 = s1.charat( s1.indexof('g') -3); String answer = (s2 + c3 + s4).touppercase(); AP? api ANP IM API

Read In Values Recall: import java.util.scanner; Scanner input = new Scanner( System.in); int age = input.nextint(); String name = input.nextline();

What are values of variables? String note = "1 2\nAlex two words"; Scanner input = new Scanner( note); int num1 = input.nextint(); int num2 = input.nextint(); String name = input.next(); String words = input.nextline();

What are values of variables? String note = "Minsub\n22\nCS"; Scanner input = new Scanner( note); String name = input.nextline(); int age = input.nextint(); String major = input.nextline(); name: Minsub\n age: 22\nCS major: name: Minsub\n22\CS age: major: name: Minsub age: 22 major:

What are values of variables? String note = "Minsub\n22\nCS"; Scanner input = new Scanner( note); String name = input.nextline(); int age = input.nextint(); String major = input.nextline(); name: Minsub\n22\CS age: major: name: Minsub age: 22 major: name: Minsub age: 22 major: CS

What are values of variables? String note = "20 25\nscores"; Scanner input = new Scanner( note); int score1 = input.nextint(); int score2 = input.nextint(); String title = input.nextline(); score1: 20 score2: 25 title: scores score1: 20 score2: 25 title: score1: 20 25 score2: scores title:

java.util.random Random randgen; //Declare reference variable randgen = new Random(); //create instance // randgen.setseed( 123); //set state int valuea = randgen.nextint( 5); //get a value int valueb = randgen.nextint( 5); int valuec = randgen.nextint( 5);

What is the answer? What expression to get a value between 2 and 10, inclusive of both 2 and 10? Assume: Random rand = new Random(); int x; x = rand.nextint(10)-2; x = rand.nextint(9)+1; x = rand.nextint(8)+2; x = rand.nextint(9)+2; Other

Debugging with Print statements See what is going on. Divide and conquer.