AP COMPUTER SCIENCE A

Similar documents
Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

ITI Introduction to Computing II

ITI Introduction to Computing II

CS 302 Week 9. Jim Williams

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

CS1150 Principles of Computer Science Objects and Classes

Programming II (CS300)

CS Week 13. Jim Williams, PhD

CSCE145 Test 2-Review 03/29/2015 Hongkai Yu

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

CSCI 161 Introduction to Computer Science

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

Today s Agenda. Quick Review

COMP Information Hiding and Encapsulation. Yi Hong June 03, 2015

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

Programming in the Large II: Objects and Classes (Part 1)

Chapter 15: Object Oriented Programming

Abstraction in Software Development

AP COMPUTER SCIENCE A

System.out.print(); Scanner.nextLine(); String.compareTo();

2. [20] Suppose we start declaring a Rectangle class as follows:

Defining Classes and Methods

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

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

Introduction to Classes

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

Chapter 6 Lab Classes and Objects

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

Computer Science is...

Chapter 6 Class and Method

Java Review. Fundamentals of Computer Science

Methods and Data (Savitch, Chapter 5)

Programming Language. Functions. Eng. Anis Nazer First Semester

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Classes. Classes as Code Libraries. Classes as Data Structures

Principles of Object Oriented Programming. Lecture 4

Classes and Objects. CGS 3416 Spring 2018

COMP 401: THE DUAL ROLE OF A CLASS. Instructor: Prasun Dewan (FB 150,

Classes and Methods: Classes

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

CS1004: Intro to CS in Java, Spring 2005

Understanding class definitions. Looking inside classes (based on lecture slides by Barnes and Kölling)

public class TicketMachine Inner part omitted. public class ClassName Fields Constructors Methods

Ticket Machine Project(s)

University of Massachusetts Amherst, Electrical and Computer Engineering

Objects. say something to express one's disapproval of or disagreement with something.

Programming II (CS300)

CS Week 14. Jim Williams, PhD

Programming II (CS300)

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Object Oriented Programming COP3330 / CGS5409

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

Software and Programming 1

Question 1 [20 points]

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

TaxiBot New attributes Variables Math! TaxiBot

Chapter 6 Lab Classes and Objects

Encapsulation. Mason Vail Boise State University Computer Science

APCS Semester #1 Final Exam Practice Problems

Programming by Delegation

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

Lecture #1. Introduction to Classes and Objects

5. Defining Classes and Methods

Understanding class definitions

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation

More About Objects and Methods

Chapter 4 Defining Classes I

CSCE 156 Computer Science II

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

CS-202 Introduction to Object Oriented Programming

Defining Classes and Methods

ECOM 2324 COMPUTER PROGRAMMING II

Recap of OO concepts. Objects, classes, methods and more. Mairead Meagher Dr. Siobhán Drohan. Produced by:

Implementing non-static features

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

EECS 1001 and EECS 1030M, lab 01 conflict

Programming Basics. Digital Urban Visualization. People as Flows. ia

COMP-202: Foundations of Programming. Lecture 14: static, private, public Jackie Cheung, Winter 2015

Assignment 1 due Monday at 11:59pm

Department of Networks College of Bardarash Technical Institute DUHOK Polytechnic University Subject: Programming Fundamental by JAVA Course Book

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5

Java Tutorial. Saarland University. Ashkan Taslimi. Tutorial 3 September 6, 2011

G52CPP C++ Programming Lecture 9

Chapter 4: Writing Classes

Chapter 4. Defining Classes I

Intro. Classes Beginning Objected Oriented Programming. CIS 15 : Spring 2007

Slide 1 CS 170 Java Programming 1

Java Object Oriented Design. CSC207 Fall 2014

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

CS24 Week 4 Lecture 1

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

Computer Science II Data Structures

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Transcription:

AP COMPUTER SCIENCE A CLASSES AND OBJECTS (1) Sep 11 2017 Week 4 http://apcs.cold.rocks 1

One More Class public static int a=1; System.out.println(a); 1 public static int a=2; http://apcs.cold.rocks 2

One More Class public static int a=1; System.out.println(ClassTwo.a); 2 public static int a=2; http://apcs.cold.rocks 3

One More Class public static int a=1; ClassTwo.a = 3 System.out.println(ClassTwo.a); 3 public static int a=2; http://apcs.cold.rocks 4

One More Class printhello(); public static void printhello() { System.out.println( Hello Class One! ); Hello Class One! public static void printhello() { System.out.println( Hello Class Two! ); http://apcs.cold.rocks 5

One More Class ClassTwo.printHello(); public static void printhello() { System.out.println( Hello Class One! ); Hello Class Two! public static void printhello() { System.out.println( Hello Class Two! ); http://apcs.cold.rocks 6

One More Class We can make more than one class Fields, methods are NOT shared between classes. But, we can access other classes with ClassName. (dot) Only public elements can be accessed by other classes http://apcs.cold.rocks 7

Public vs Private private static int a=1; System.out.println(a); 1 private static int a=2; http://apcs.cold.rocks 8

Public vs Private private static int a=1; System.out.println(ClassTwo.a); ERROR (variable NOT VISIBLE) private static int a=2; http://apcs.cold.rocks 9

Public vs Private ClassTwo.printHello(); private static void printhello() { System.out.println( Hello Class One! ); Hello Class One! private static void printhello() { System.out.println( Hello Class Two! ); http://apcs.cold.rocks 10

Public vs Private ClassTwo.printHello(); private static void printhello() { System.out.println( Hello Class One! ); ERROR (variable NOT VISIBLE) private static void printhello() { System.out.println( Hello Class Two! ); http://apcs.cold.rocks 11

Public vs Private Only public elements can be accessed by other classes In AP Computer Science A All fields are set to private All methods are set to public Then, how do we deal with fields from other classes? ACCESSORS (getters) MUTATORS (setters) http://apcs.cold.rocks 12

Accessors System.out.println(ClassTwo.getSomethingPrivate()); 1 private static int somethingprivate = 1; public static int getsomethingprivate() { return somethingprivate; http://apcs.cold.rocks 13

Mutators ClassTwo.setSomethingPrivate(200); System.out.println(ClassTwo.getSomethingPrivate()); 200 private static int somethingprivate = 1; public static int getsomethingprivate() { return somethingprivate; public static void setsomethingprivate(int sp) { somethingprivate = sp; http://apcs.cold.rocks 14

Accessors and Mutators Accessors access and mutators mutate. (of course ) Accessors should return the value of a private field Return type MUST match the type of the field Some special accessors may not return the exact value. In this case, the types do not have to match Accessors look like : public int getsomething() { return something; http://apcs.cold.rocks 15

Accessors and Mutators Mutators should set the value of field(s) as we want There should be parameter(s), which will be the value to be set Type must match Like accessors, some mutators may not take parameters or take different type of parameter. Mutators look like : public void setsomething(type s) { something = s; http://apcs.cold.rocks 16

Accessors and Mutators System.out.println(ClassTwo.isValuePositive()); ClassTwo.setValueZero(); System.out.println(ClassTw0.isValuePositive()); true false private int value = 1; public static boolean isvaluepositive() { return value>0; public static void setvaluezero() { value=0; http://apcs.cold.rocks 17

Class as a Type ClassTwo ct1; ClassTwo ct2; NO ERROR! private static int value = 1; public static int getvalue() { return somethingprivate; public static void setvalue(int v) { value=v; http://apcs.cold.rocks 18

Classes and Objects Class also was type The value that goes inside the class is called object Making new object : using keyword new ClassTwo ct = new ClassTwo(); http://apcs.cold.rocks 19

Classes and Objects ClassTwo ct1 = new ClassTwo(); ClassTwo ct2 = new ClassTwo(); System.out.println(ct1.getValue()); System.out.println(ct2.getValue()); 1 1 private static int value = 1; public static int getvalue() { return somethingprivate; public static void setvalue(int v) { value=v; http://apcs.cold.rocks 20

Static keyword ClassTwo ct1 = new ClassTwo(); ClassTwo ct2 = new ClassTwo(); System.out.println(ct1.getValue()); ct1.setvalue(2); System.out.println(ct2.getValue()); 1 1 private int value = 1; public int getvalue() { return somethingprivate; public void setvalue(int v) { value=v; http://apcs.cold.rocks 21

Static Keyword static elements (fields, methods) are shared across all objects and class itself When calling class name, all elements related to it should be static Other elements belong to each object Class is a blueprint, or a prototype of objects Objects are, the real object. http://apcs.cold.rocks 22

Classes and Objects Public Class SomeClass { private int intvalue; private static double doublevalue; has value 3.14 public getintvalue();... SomeClass sc1 intvalue = 1; doublevalue = 3.14; public getintvalue(); returns 1... SomeClass sc2 SomeClass SomeClass sc2 SomeClass sc2 sc2 intvalue = 2; intvalue doublevalue = 3.14; intvalue = 2; doublevalue = 3.14; intvalue = 2; = 2; doublevalue doublevalue = 3.14; = 3.14; public getintvalue(); returns 2 public getintvalue(); returns 2... public getintvalue(); returns 2... public getintvalue(); returns 2...... http://apcs.cold.rocks 23

Class as a Type Rectangle r1 = new Rectangle(); Rectangle r2 = new Rectangle(); r1.setheightandwidth(3,5); r2.setheightandwidth(5,7); System.out.println(r1.getArea()); System.out.println(r2.getArea()); public class Rectangle { 15 35 private int height; private int width; public int getarea() { return height*width; public void setheightandwidth(int h, int w) { height=h; width=w; http://apcs.cold.rocks 24

Your turn Design a class of CSIA student http://apcs.cold.rocks 25