Object-Based Programming. Programming with Objects

Similar documents
Chapter 4 Classes in the Java Class Libraries

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

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

1 class Lecture5 { 2 3 "Methods" / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199

COMP6700/2140 Std. Lib., I/O

CSCI 2010 Principles of Computer Science. Basic Java Programming. 08/09/2013 CSCI Basic Java 1

1001ICT Introduction To Programming Lecture Notes

Using Classes and Objects

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

Methods (Deitel chapter 6)

Methods (Deitel chapter 6)

Java Libraries. Lecture 6 CGS 3416 Fall September 21, 2015

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.

The Math Class. Using various math class methods. Formatting the values.

Introduction to Computer Science Unit 2. Notes

Methods: A Deeper Look

Peer Instruction 1. Elementary Programming

Python. Olmo Zavala R. Python Exercises. Center of Atmospheric Sciences, UNAM. August 24, 2016

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

RANDOM NUMBER GAME PROJECT

C Functions. 5.2 Program Modules in C

Using Classes and Objects. Chapter

Introduction to Java Applications

CT 229 Java Syntax Continued

Introduction to Computer Science Unit 2. Notes

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

Using the um-fpu with the Javelin Stamp

Using API in Java. EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG

Variables. location where in memory is the information stored type what sort of information is stored in that memory

The return Statement

Exercise. Write a program which allows the user to enter the math grades one by one (-1 to exit), and outputs a histogram.

H212 Introduction to Software Systems Honors

CS-201 Introduction to Programming with Java

Method Invocation. Zheng-Liang Lu Java Programming 189 / 226

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

Chapter 5 Lab Methods

ing execution. That way, new results can be computed each time the Class The Scanner

Data Conversion & Scanner Class

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

AP Computer Science Unit 1. Writing Programs Using BlueJ

COMP 110 Programming Exercise: Simulation of the Game of Craps

Formatting Output & Enumerated Types & Wrapper Classes

Methods: A Deeper Look

CS 211: Existing Classes in the Java Library

Module 01: Introduction to Programming in Python

The Math Class (Outsource: Math Class Supplement) Random Numbers. Lab 06 Math Class

static int min(int a, int b) Returns the smaller of two int values. static double pow(double a,

Chapter 5 C Functions

Mentor Graphics Predefined Packages


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

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

Chapter 5 Lab Methods

Question 2. [5 points] Given the following symbolic constant definition

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University


C Program Structures

Cloning Arrays. In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example,

Built-in Types of Data

PyCUDA. Continued...

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

Programming with Java

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point?

(created by professor Marina Tanasyuk) FUNCTIONS

Using Classes and Objects

Using Java Classes Fall 2018 Margaret Reid-Miller

DM536 / DM550 Part 1 Introduction to Programming. Peter Schneider-Kamp.

Computational Expression

IVOA Astronomical Data Query Language Version 0.6

Lecture 1: What is MATLAB?

Classes and Objects Part 1

Lab 9: Creating a Reusable Class

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

Introduction to MATLAB

Fall Semester (081) Dr. El-Sayed El-Alfy Computer Science Department King Fahd University of Petroleum and Minerals

Variable Scope. The variable scope is the range of the program where the variable can be referenced.

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

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

Chapter 6. Arrays. Java Actually: A Comprehensive Primer in Programming

Python Lists: Example 1: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2]

Math 2250 MATLAB TUTORIAL Fall 2005

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Object Oriented Programming in C#

Functions. Systems Programming Concepts

Supplementary Test 1

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

CS111: PROGRAMMING LANGUAGE II

CS 302: Introduction to Programming

Java Programming. U Hou Lok. Java Aug., Department of Computer Science and Information Engineering, National Taiwan University

Introduction to Computer Programming

Chapter 2: Data and Expressions

Input and Expressions Chapter 3 Slides #4

ECET 264 C Programming Language with Applications

Functions and Recursion

Downloaded from Chapter 2. Functions

BLOCK STRUCTURE. class block main method block do-while statement block if statement block. if statement block. Block Structure Page 1

Chap. 3. Creating Objects The String class Java Class Library (Packages) Math.random() Reading for this Lecture: L&L,

CSC Algorithms and Data Structures I. Midterm Examination February 25, Name:

Building Java Programs

Transcription:

ITEC1620 Object-Based Programming g Lecture 8 Programming with Objects

Review Sequence, Branching, Looping Primitive datatypes Mathematical operations Four-function calculator Scientific calculator Don t program the functions yourself 2

Math Class Java set of pre-defined mathematical functions and constants Constants (E, PI) Basic functions round, floor, ceil, min, max, abs Trigonometric functions sin, cos, tan, asin, acos, atan Exponential funcions pow, log 3

Functions Blocks of JAVA code that provide a specific, reusable functionality static double sqrt (double a) static double pow (double a, double b) 4

Functions and APIs Application Programmer Interfaces (APIs) list the available functionality of a class An API includes Constants, constructors, and methods Datatypes Argument lists with datatypes Descriptions of functionality 5

Constants static double PI The double value that t is closer than any other to pi, the ratio of the circumference of a circle to its diameter. static global variable double datatype PI variable name (identifier) 6

Functions static double sqrt (double a) Returns the correctly rounded d positive square root of a double value. static stateless function double datatype of result sqrt name (identifier) of method double a datatype of parameter 7

Sample Program I Calculate the radius or a circle with area = 1 area = πr 2 r = sqrt(area/π) 8

Program public class Radius { public static void main (String[] args) { double temp = 1/Math.PI; double radius = Math.sqrt(temp); } } 9

Keyboard Input Scanner class Provides functionality in Java to get user (keyboard) input Constructors Build new Scanner objects Methods Read d data 10

Methods to Read Data int nextint() Scans the next token of the input as an int. int datatype of result nextint name (identifier) of method Not static! 11

static vs. non-static methods I static Same result for same parameters, every time stateless non-static Result depends on state 12

static vs. non-static methods II nextint() Should this method always return the same result? Which input source? Which item in input series? Proper operation of method requires state information State information stored in objects 13

Creating Objects Scanner (InputStream source) Constructs t a new Scanner that t produces values scanned from the specified input stream. Scanner type of object being created source state information for new object 14

Example import java.util.scanner; public class Input { public static void main (String[] args) { Scanner scan = new Scanner (System.in); int input = scan.nextint(); } } 15

Random Numbers Random class Provides functionality in Java to generate (pseudo) random numbers Constructors Build new Random objects Methods Generate t (pseudo) random numbers 16

Creating Objects Random (long seed) Creates a new random number generator using a single long seed Random type of object being created seed random number sequence Two Random objects with the same seed will produce the same sequence of random numbers 17

Example import java.util.random; public class RandomNumbers { public static void main (String[] args) { Random generator = new Random (1); int randomint = generator.nextint(); } } 18

Function Names int input = scan.nextint(); Gets next input from user int randomint = generator.nextint(); nextint(); Generates next random number The meaning of nextint() depends on context 19

Sample Program I Simulate the rolling of two six-sided dice 20

Program import java.util.*; public class TwoDice { public static void main (String[] args) { Random generator = new Random (); } } 21

22

Sample Program II Generate a random Pick3 or Pick4 number 23

Data 24

Processes 25

Program import java.util.*; public class Lotto { public static void main (String[] args) { Scanner scan = new Scanner (System.in); Random generator = new Random (); } } 26

27

Sample Program III Generate a random number between 1 and d10, and dtake user guesses until they guess the number. Output the number of guesses. 28

Data 29

Processes 30

Program import java.util.*; public class GuessingGame { public static void main (String[] args) { Scanner scan = new Scanner (System.in); Random generator = new Random (); } } 31

32

Readings and Assignments Text sections (5 th, 6 th, or 7 th edition) 2.6, 3.4, 3.5 Tutorials Branching Programs in JAVA Tutorials Looping Programs in JAVA Mid-term Sample 1 Lab Assignment 3 33