Class API. Class API. Constructors. CS200: Computer Science I. Module 19 More Objects

Similar documents
Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

More Data Types. The Char Data Type. Variable Declaration. CS200: Computer Science I. Module 14 More Data Types

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

Flow of Control. Conditional Statements. Conditional Statements. CS256 Computer Science I Kevin Sahr, PhD. Lecture 15: The If-Statement

Method. Why Write Methods? Overview. CS256 Computer Science I Kevin Sahr, PhD. Lecture 21: Writing Class Methods. ClassName.methodName(arguments)

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

ECE 122. Engineering Problem Solving with Java

Lab5. Wooseok Kim

Programming Language. Machine Code. CS200: Computer Science I. Module 3 Programming Languages

Lab5. Wooseok Kim

A variable is a name for a location in memory A variable must be declared

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

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

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

COMP 250 Winter 2011 Reading: Java background January 5, 2011

CompSci 125 Lecture 05. Programming Style, String Class and Literals, Static Methods, Packages

Introduction to Programming Using Java (98-388)

Java Classes and Objects

Example packages from Java s standard class library:

Using Classes and Objects

Arrays Data structures Related data items of same type Remain same size once created Fixed-length entries

Computer Science II (20082) Week 1: Review and Inheritance

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Using Classes and Objects. Chapter

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Final Exam CS 152, Computer Programming Fundamentals May 9, 2014

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

CSCE3193: Programming Paradigms

Formatting Output & Enumerated Types & Wrapper Classes

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

Java Intro 3. Java Intro 3. Class Libraries and the Java API. Outline

Using Classes and Objects

Using Classes and Objects

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

Notes from the Boards Set BN19 Page

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Chief Reader Report on Student Responses:

CT 229 Fundamentals of Java Syntax

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

TEST (MODULE:- 1 and 2)

Department of Computer Science Purdue University, West Lafayette

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

Packages & Random and Math Classes

Nesting. Abstraction & Nesting. Example. if x is less than y output x is smaller else output y is smaller. CS256 Computer Science I Kevin Sahr, PhD

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

AP Computer Science. Strings. Credit: Slides are modified with permission from Barry Wittman at Elizabethtown College

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

c) And last but not least, there are javadoc comments. See Weiss.

Classes and Objects Part 1

Chapter 4: Writing Classes

Selected Questions from by Nageshwara Rao

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

CT 229 Java Syntax Continued

Chapter 4 Defining Classes I

JAVA WRAPPER CLASSES

OBJECT ORIENTED PROGRAMMING IN JAVA

Lecture 5: Methods CS2301

CS313D: ADVANCED PROGRAMMING LANGUAGE

Utilities (Part 2) Implementing static features

Unit 14. Passing Arrays & C++ Strings

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

CS 106 Introduction to Computer Science I

Introduction to Computer Science Unit 2. Notes

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Gettysburg College Department of Computer Science

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

Lab #7 Library Classes and JUnit Testing. Daniel Amyot, Diana Inkpen, Alan. Agenda. In this lab, you are going to create your own

String is one of mostly used Object in Java. And this is the reason why String has unique handling in Java(String Pool). The String class represents

Activity 9: Object-Oriented

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com

Creating Java Programs with Greenfoot

9 Working with the Java Class Library

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

Place your name tag here

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

CS162: Introduction to Computer Science II


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

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Chapter 7 User-Defined Methods. Chapter Objectives

Industrial Programming

What is an Object. Industrial Programming. What is a Class (cont'd) What is a Class. Lecture 4: C# Objects & Classes

CS111: PROGRAMMING LANGUAGE II

York University Fall 2001 / Test #1 Department of Computer Science

Chapter 3: Using Classes and Objects

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Functions. Arash Rafiey. September 26, 2017

In Java there are three types of data values:

Data Representation Classes, and the Java API

AP CS Unit 3: Control Structures Notes

CS1150 Principles of Computer Science Methods

CSCI 355 Lab #2 Spring 2007

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

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

Topic 5 Polymorphism. " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.

( &% class MyClass { }

Objectives. Introduce static keyword examine syntax describe common uses

Transcription:

CS200: Computer Science I Module 19 More Objects Kevin Sahr, PhD Department of Computer Science Southern Oregon University 1 Class API a class API can contain three different types of methods: 1. constructors 2. class methods - have keyword static in method header 3. instance methods - method header exactly like class methods except no static 2 Class API a class API can contain three different types of methods: 1. constructors 2. class methods - have keyword static in method header 3. instance methods - method header exactly like class methods except no static 3 Constructors purpose is to initialize the object give initial values to the object attributes invoked using the new operator when an object is instantiated method name is the same as the class name have no return type (not even void) 4

Default Constructor the default constructor is a constructor that takes no arguments initializes object attributes to default values 5 Square Class API Constructors EX: Square Class public Square() Default constructor to create a Square that is red, 30 pixels in size, is located at (60, 50), and is not initially visible. 6 Invoking Constructors invoked with the new operator EX: Square s1 = new Square(); invokes default Square constructor 7 Multiple Constructors a class may have more than one constructor the constructors must take different arguments Java invokes the correct constructor based on the arguments you give it 8

Person Class API Constructors EX: Person Class public Person() Constructor for a Person named John Doe who is 0 years old. Initially this Person is alive. public Person(String name, int age) Constructor for a Person with the specified name and age. Initially this Person is alive. 9 Invoking Constructors invokes default EX: Person constructor Person p1 = new Person(); Person p2 = new Person( Mary, 20); invokes second Person constructor 10 Person Class API, compiled class file Person.class, and an example program PersonApp.java are on the class website spend some time looking at this example note, for instance, that some Person instance methods return values work just like class methods, except require an object to invoke 11 Standard Class Library the Java Standard Class Library provides hundreds of useful classes for your use comes free with Java FYI: a link to the class APIs is on the website Related Links page 12

Packages the Java Standard Class Library classes are organized into packages classes in the java.lang package are available to all programs by default includes the classes Math and System 13 String Class String is actually a class in the java.lang package, not a data type every String is an object an abbreviated String class API is on the website 14 String Class because Strings are so important in writing programs, Java gives Strings two unique capabilities concatenation using the + operator abbreviated instantiation syntax (see next slide) no other classes have these capabilities 15 String Instantiation we have initialized Strings using the following syntax: String s = abc ; this is really special abbreviated syntax for: String s = new String( abc ); these statements are equivalent both create a String object containing abc 16

Length Method the length instance method returns how many characters are in a String object it has the method header: public int length() EX: String str = new String( ab ); int n; n = str.length(); n would have the value 2 17 CharAt Method the charat instance method takes a single integer argument, and returns the character at that index in the String it has the method header: public char charat(int index) 18 CharAt Method the first character in a String is at index 0 the last character is at length() - 1 EX: String s = new String( HELLO ); char c; c = s.charat(1); 01234 c would have the value E 19 Comparing Objects objects cannot be compared using the regular relational operators (==, >, etc.) classes usually provide instance methods for comparing objects of that class 20

public import Equals Method the String class provides the equals instance method that returns whether or not two Strings have the same value it has the method header: boolean equals(string s) 21 Equals Method EX: String a = new String( joe ); String b = new String( mary ); if (a.equals(b)) Output.showMessage( same ); else Output.showMessage( diff ); would output diff note that a.equals(b) and b.equals(a) have the same value Class Availability there are three ways to make a class available to use in your programs: 1. put compiled.class file in same folder with your program EX: Output, Square, Person 2. classes in java.lang package are automatically available EX: Math, String 3. classes in all other packages require an import statement 22 23 Import Statement syntax: packagename.classname; Java keyword dot operator put import statements just below your header comments, and just above your class header 24

DecimalFormat Class the DecimalFormat class in the java.text package lets you format doubles for output requires the following import statement: import java.text.decimalformat; an abridged DecimalFormat class API is given on the website 25 DecimalFormat Class to use, you must first create a DecimalFormat object with the desired format the constructor has the method header: public DecimalFormat (String formatstr) the format string argument specifies how the object will format doubles a format string of 0.00 indicates a format of two digits to the right of the decimal place 26 Format Method the format instance method has the following method header: public String format (double num) the method takes a single real number as its argument, and returns that number as a formatted String 27 DecimalFormat EX DecimalFormat df = new DecimalFormat("0.00"); String s; s = df.format(1.123456789); Output.showMessage(s); will output 1.12 28

DecimalFormat EX the program Format.java contains another DecimalFormat example spend some time looking at this example note the required import statement above the class 29 Module 19 Vocabulary constructor default constructor Java Standard Class Library package import statement 30 Questions? Email Kevin at sahrk@sou.edu 31