Unit 4: Classes and Objects Notes

Similar documents
Unit 4: Classes and Objects Exercises

AP Computer Science A

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

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science Unit 1. Programs

Introduction to Computer Science Unit 2. Exercises

AP Computer Science Unit 1. Writing Programs Using BlueJ

Introduction to Java Unit 1. Using BlueJ to Write Programs

Using Java Classes Fall 2018 Margaret Reid-Miller

AP CS Unit 3: Control Structures Notes

Unit 10: Sorting/Searching/Recursion

AP CS Unit 7: Interfaces Exercises Assume all code compiles unless otherwise suggested.

Getting started with Java

AP Computer Science A Unit 7. Notes on Arrays

Practice Midterm 1 Answer Key

Practice Midterm 1. Problem Points Score TOTAL 50

NATIONAL UNIVERSITY OF SINGAPORE

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

AP CS Unit 7: Arrays Exercises

Unit 5: More on Classes/Objects Exercises

Computer Programming, I. Laboratory Manual. Final Exam Solution

Oct Decision Structures cont d

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

University of Palestine. Mid Exam Total Grade: 100

More on variables and methods

Unit 10: Sorting/Searching/Recursion

AP CS Unit 7: Interfaces. Programs

Creating Strings. String Length

AP CS Unit 7: Interfaces Exercises 1. Select the TRUE statement(s).

Unit 5: More on Classes/Objects Notes

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

AP CS Unit 6: Inheritance Notes

Programming with Java

H212 Introduction to Software Systems Honors

Exam 1 - (20 points)

1 Short Answer (15 Points Each)

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

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

Chapter 5: Classes and Objects in Depth. Introduction to methods

Mr. Monroe s Guide to Mastering Java Syntax

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

Computational Expression

Computer Science II Data Structures

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

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

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

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

COMP 110/L Lecture 5. Kyle Dewey

Handout 7. Defining Classes part 1. Instance variables and instance methods.

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

Chapter 6 Lab Classes and Objects

Chapter 6 Lab Classes and Objects

Review questions. Review questions, cont d. Class Definition. Methods. Class definition: methods. April 1,

Introduction to Java (All the Basic Stuff)

Notes from the Boards Set BN19 Page

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

Introduction to Objects. James Brucker

CS 101 Exam 1 Spring 200 Id Name

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Place your name tag here

AP Computer Science A Unit 2. Exercises

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

Arrays and Strings. Arrays - homogeneous indexed collections Selection sort as array example. String class, revisited

Lecture Set 2: Starting Java

Java Coding 3. Over & over again!

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

Review. Single Pixel Filters. Spatial Filters. Image Processing Applications. Thresholding Posterize Histogram Equalization Negative Sepia Grayscale

Unit 10: Sorting/Searching/Recursion

Classes Basic Overview

Lecture Set 2: Starting Java

PROGRAMMING FUNDAMENTALS

"Hello" " This " + "is String " + "concatenation"

Programming: Java. Chapter Objectives. Chapter Objectives (continued) Program Design Including Data Structures. Chapter 7: User-Defined Methods

CSCI 111 Midterm Spring 2014

Introduction to Java. Handout-1d. cs402 - Spring

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

Flow Control. Boaz Kantor Introduction to Computer Science, Fall semester IDC Herzliya

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

CS 101 Fall 2005 Midterm 2 Name: ID:

Lecture Notes for CS 150 Fall 2009; Version 0.5

1 Short Answer (10 Points Each)

Fundamentals of Programming Data Types & Methods

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

Strings in Java String Methods. The only operator that can be applied to String objects is concatenation (+) for combining one or more strings.

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

CS 2334: Programming Structures and Abstractions: Exam 1 October 3, 2016

I. True/False: (2 points each)

Lecture 13 & 14. Single Dimensional Arrays. Dr. Martin O Connor CA166

Faculty of Science COMP-202A - Foundations of Computing (Fall 2012) - All Sections Midterm Examination

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Page 1 / 3. Page 2 / 18. Page 3 / 8. Page 4 / 21. Page 5 / 15. Page 6 / 20. Page 7 / 15. Total / 100. Pledge:

1 Short Answer (10 Points Each)

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

CS111: PROGRAMMING LANGUAGE II


8/25/17. Demo: Create application. CS2110, Recita.on 1. Arguments to method main, Packages, Wrapper Classes, Characters, Strings.

Introduction to Computer Science Unit 5. Programs: Strings

Transcription:

Unit 4: Classes and Objects Notes AP CS A Another Data Type. So far, we have used two types of primitive variables: ints and doubles. Another data type is the boolean data type. Variables of type boolean have a value of or. Wherever you can use a boolean expression, you may also use a boolean variable. Here is one way a boolean variable may be used. In this context it is called a because it signals when to keep playing and when to stop. boolean game_on = true; while ( game_on ){ int n = (int) (7*Math.random()) + 2; System.out.println( n ); if ( n == 4 ) game_on = false; The Not Operator. We have already covered the AND ( && ) and OR ( ) operators. A third logical operator is the NOT (! ) operator. Here are some examples: 1. What is displayed? boolean boo = true; boo =!boo; 2. Will the code execute if a = 4, b = 4, and c = 8? 3. Name values that will cause the loop to terminate. System.out.println( boo ); while (!( a == b && b == c ) ){ // code Classes and Objects. Usually, a class is written so that we can make objects from it. The class defines what data and behavior these objects have. For instance, using the Scanner class we can write: Scanner sue = new Scanner( System.in ); System.out.print( "Enter a number " ); double x = sue.nextdouble(); As the above example shows, we need to create the object first then we can call its methods. In this unit we will first learn about the String class to get used to creating objects and calling methods. Then we will write our own classes and create/use objects based on those classes. Page 1

The String Class. A String object represents a sequence of one or more characters where a character could be a letter, digit, or punctuation mark. Each character in a string has a unique index starting at. Anything in quotes is a and is an object of the String class. "jump now"; // the u is at index, the n is at index Classes have that are used to create objects. String a = new String( "easy 123" ); // String class more than 10 constructors Once the object is created, you can use its methods. The first line of a method is called its header. It tells you three things: return_type method_name( parameters ) For example, the header the nextdouble method from the Scanner class is: Some String Methods int length() String x = "Oct 2017"; int i = x.length(); System.out.println( i ); String substring(int start) String substring(int start, int stop) s String s1 = "phone"; String s2 = s1.substring(2); System.out.println( s2 ); String s3 = "phone"; String s4 = s3.substring(0, 2); System.out.println( s4 ); Page 2

String tolowercase() The above example prints 4 sale! String s1 = "4 SALE!"; s1 = s1.tolowercase(); System.out.println( s1 ); String touppercase() String s1 = "What?"; s1 = s1.touppercase(); System.out.println( s1 ); The above example prints WHAT? boolean equals(object obj) String s1 = "a"; String s2 = "A"; if ( s1.equals( s2 ) ) System.out.println( "ok" ); s int indexof(string s) String s1 = "bubbles"; int x = s1.indexof( "b" ); int y = s1.indexof( "bb" ); System.out.println( x + ", " + y ); int indexof(string s, int i) String s1 = "bubbles"; int x = s1.indexof( "b", 1 ); int y = s1.indexof( "bb", 3 ); System.out.println( x + ", " + y ); Page 3

Escape characters are generally used to represent non-printable characters or characters that have special meaning. Here are some examples. String a = "She said \"Hi\""; System.out.println( a ); System.out.println( a.length() ); String b = "up\ndown"; System.out.println( b ); System.out.println( b.length() ); String c = "\\\\"; System.out.println( c ); System.out.println( c.length() ); \t prints a tab and can be useful when displaying output. IMPORTANT. To properly use a class, you need to understand how to construct its objects and call its methods. You do NOT need to know how those methods work. For instance, you do not need to know how the String class stores those characters nor do you need to see the source code for all its methods - you simply need to know how to use those methods. Page 4

Writing Custom Classes. A class is generally used as a blueprint to create objects. The class determines what data (variables) and behaviors (methods) its objects will have. : public class Square { private int side; public Square( int x ) { side = x; public int area() { int a = side*side; return a; public void set( int x ){ side = x; A class consists of three parts: public class Runner { public static void main(string[] args) { Instance variables/attributes: Constructors An object is an of a class. A constructor is used to an object. Methods Visibility Modifiers. public means other classes can directly use it; private means that its access is restricted to its class. Methods have three parts: a,, and. Here are the two methods of the Square class. public int area() { int a = side*side; return a; public void set( int x ){ side = x; Page 5

A return statement causes the program to exit the method. Let s consider three examples: If we call method1 and n equals 3, what does public double method1( int n ) { the method return? if ( n < 0 ) { return 6; If we call method1 and n equals -22, what does the method return? int b = 4*n; return b; If we call method2 and the parameters are 3 and 5, what is displayed? Notice that this return statement (1) returns nothing and (2) causes the program to exit early. method3 causes this compiler error: unreachable statement What does this mean? public void method2( int a, int b ) { int c = a + b; if ( c % 2 == 0 ) { System.out.println( "hey" ); return; System.out.println( "joe" ); public int method3( int c ) { c = c + 2; return c; c = c + 3; In the context of a class, all variables fall into one of three categories: instance variables parameters local variables Name any instance variables Name any parameters Name any local variables Only one line generates the compiler error: variable might not have been initialized Which line is it? public void method4( int h ) { int g = h + k; public void method5( int n ) { // 1 int a; // 2 System.out.println( n ); // 3 System.out.println( a ); // 4 Two Common Terms. If a method changes the value of an instance variable, then it is called a or method. If a method returns the value of an instance variable, then it is called an or method. Page 6