In Java there are three types of data values:

Similar documents
Data & Functional Abstraction ( (Parts of Chaps 4 & 6) Objects. Classes. In Java there are three types of data values:

Class. Chapter 6: Data Abstraction. Example. Class

Class. Chapter 6: Data Abstraction. Example. Class

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Encapsulation in C++

9 Working with the Java Class Library

Chapter 4 Defining Classes I

Introduction to Programming Using Java (98-388)

Selected Questions from by Nageshwara Rao

Objects: Data Abstraction

BM214E Object Oriented Programming Lecture 8

Primitive vs Reference

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

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

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

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

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Week 6: Review. Java is Case Sensitive

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Inf1-OOP. Data Types. Defining Data Types in Java. type value set operations. Overview. Circle Class. Creating Data Types 1.

Java Review. Fundamentals of Computer Science

SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177. Programming

OO Programming Concepts. Classes. Objects. Chapter 8 User-Defined Classes and ADTs

Points To Remember for SCJP

CSC1322 Object-Oriented Programming Concepts

Chapter 4: Writing Classes

THE CONCEPT OF OBJECT

ECOM 2324 COMPUTER PROGRAMMING II

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

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

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

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

You must declare all variables before they can be used. Following is the basic form of a variable declaration:

Full file at

Fundamental Concepts and Definitions

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

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.

Chief Reader Report on Student Responses:

Anatomy of a Class Encapsulation Anatomy of a Method

Object Oriented Programming in C#

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

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

Brief Summary of Java

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Java Foundations Certified Junior Associate

Chapter 4. Defining Classes I

PIC 20A The Basics of Java

CHAPTER 7 OBJECTS AND CLASSES

Chapter 9 Objects and Classes. OO Programming Concepts. Classes. Objects. Motivations. Objectives. CS1: Java Programming Colorado State University

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

Chapter 6 Introduction to Defining Classes

Pace University. Fundamental Concepts of CS121 1

Creating an object Instance variables

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

CS-202 Introduction to Object Oriented Programming

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

Java Primer 1: Types, Classes and Operators

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

CS 231 Data Structures and Algorithms, Fall 2016

Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B

Class Foo. instance variables. instance methods. Class FooTester. main {

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

CS111: PROGRAMMING LANGUAGE II

Inf1-OP. Creating Classes. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics

Defining Classes and Methods

CHAPTER 7 OBJECTS AND CLASSES

Fundamentos de programação

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Objects and Classes. Basic OO Principles. Classes in Java. Mark Allen Weiss Copyright 2000

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Java Object Oriented Design. CSC207 Fall 2014

CH. 2 OBJECT-ORIENTED PROGRAMMING

Chapter 7 User-Defined Methods. Chapter Objectives

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

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

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

Chapter 1 Getting Started

PROGRAMMING FUNDAMENTALS

Methods and Data (Savitch, Chapter 5)

Chapter 6. Classes revisited. Objects: Data Abstraction. Data Abstraction

Assignment 1 due Monday at 11:59pm

Binghamton University. CS-140 Fall Data Types in Java

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Sri Vidya College of Engineering & Technology

Chapter 9. Objects and Classes

EECS168 Exam 3 Review

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition

Objects and Classes -- Introduction

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

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

Instructor: SIR MUHAMMAD NAVEED Created by: ARSLAN AHMED SHAAD ( ) MUHAMMAD BILAL ( ) ISIT:

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

COE318 Lecture Notes Week 3 (Week of Sept 15, 2014)

Index COPYRIGHTED MATERIAL

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

Introduction to Java

MIDTERM REVIEW. midterminformation.htm

Transcription:

In Java there are three types of data values: primitive data values (int, double, boolean, etc.) arrays (actually a special type of object) objects An object might represent a string of characters, a planet, a type of food, a student, an employee, a piece of email,... anything that can't be (easily) represented by a primitive value or an array. Just as 3 is a primitive value of type int, every object must also have a type. These types are called classes. A class describes a set of objects. It specifies what information will be used to represent an object from the set (e.g. name and salary for an employee). It also specifies what operations can be performed on such an object (get the name of the student, send an email message). String is a standard Java class. Values from the class String are called objects, so "hello" is an object from the class String. We can also say "hello" is an instance of the class String. The class String has instance methods that operate on an instance of class String. For example: length() and charat().

Write a program that determines if a String is a palindrome. What is an algorithm? public class Palindrome { String str1 = "eye", str2 = "bye"; System.out.println("Palindrome detection"); System.out.println(str1 + " " + ispalindrome(str1)); System.out.println(str2 + " " + ispalindrome(str2)); static boolean ispalindrome(string s) { int left = 0; int right = s.length() - 1; while (left < right) { if (s.charat(left)!= s.charat(right)) return false; left++; right--; return true; How to program it recursively? Because strings are so common, Java has special two pieces of special syntax for the class String. There is syntactic support for string concatenation. There is syntactic support for creating string literals. The operator + is overloaded to implement concatenation of strings. "hello, " + "world is equivalent to "hello, ".concat("world")

String literals are supported. String s = "hello" is equivalent to char[] temp={ h, e, l, l, o ; String s = new String(temp); Instances of the class String are immutable. This means once created, a String object cannot be changed. One implication of this is that in the following code fragment: String s = "some string";... somefunction(s)... we know for certain that when the function returns, s will still be "some string". StringBuffer is another standard Java class for representing strings. Unlike String, instances of the class StringBuffer are mutable. The class StringBuffer has mutator methods -operations (instance methods) that actually change the object. class StringBufferInsert { StringBuffer sbuf = new StringBuffer ("some string"); sbuf.insert (sbuf.length() / 2, "mutable "); System.out.println (sbuf); Execution results : some mutable string Person class class Person { String name; //data member or field String phonenumber; //data member or field To actually create instances, we need to use the new operation Person p; //declare that p will refer to a Person object p = new Person(); // create a Person object Person p = new Person(); //declare and construct The variable p is a reference variable and has as its value a memory address. The operator new gets from the system memory, called a heap, the number of bytes needed to store a Person object. The resulting value of p is the memory address of where the data fields are stored.

A member of a class object can be accessed by using dot(. ) operator. classvariable.membername If we want to assign some values to the Person variable p is referring to. p.name = "Jane Programmer"; p.phonenumber = "(831) 459-1234"; System.out.println(p.name + "'s phone number is " + p.phonenumber); Each instance of a class will have its own copy of each data member. Person jane = new Person(); Person john = new Person(); jane.name = "Jane Programmer"; john.name = "John Programmer"; // SameName.java - demonstration of class Pair public class SameName { Person personone = new Person(); Person persontwo = new Person(); personone.name = "Jane Programmer"; personone.phonenumber = "(831) 459-1234"; persontwo.name = "Jane Programmer"; persontwo.phonenumber = "(651) 999-1234"; print(personone); print(persontwo); if (samename(personone, persontwo)) System.out.println("They are the same person."); else System.out.println("They are different people."); static void print(person p) { System.out.println(p.name + "'s phone number is " + p.phonenumber); static boolean samename(person p, Person q) { return p.name.equals(q.name); A class describes the data values used to represent an object and any operations that can be performed on that object. The data values are stored in instance variables, also known as fields, or data members. The operations are described by instance methods, sometimes called procedure members. Suppose that we needed a class Counter: It counts sequentially from 0 through 99. Each time the counter is clicked it should increase by 1. When the counter reaches 99 and is clicked it returns to 0. // CounterTest.java - demonstration of class Counter class CounterTest { Counter c1 = new Counter(); //create a Counter Counter c2 = new Counter(); //create another c1.click(); // increment Counter c1 c2.click(); // increment Counter c2 c2.click(); // increment Counter c2 again System.out.println("Counter1 value is " + c1.get()); System.out.println("Counter2 value is " + c2.get()); c1.reset(); System.out.println("Counter1 value is " + c1.get());

Counter class implementation //Counter.java - a wrap around counter class Counter { int value; //0 to 99 void reset() { value = 0; int get() { return value; //current value void click() { value = (value + 1) % 100; Objects in Memory An abstract data type (ADT) is a user-defined type with a set of methods to manipulate objects of that type. In the case of Counter, we ve methods such as get() and click(). There is also an implementation of the values needed to describe the object s state in this case, the instance variable value. Decoupling implementation from manipulation by accessor has important advantages. we can use a Counter without knowing whether the internal value is represented as an int, long, byte, or possibly some other class. This capability allows the implementer to develop an optimal implementation. It allows a client, or user of the implementation, the opportunity to develop an application using Counter without being concerned about the internal details of Counter. It is desirable to hide the inner details of a class (abstract data type) from the users of the class. We want to be able to determine the correctness of a class without examing the entire program of which it is a part. With our current class Counter we wish to assert that the value is always between 0 and 99. Accessing instance variables from outside the class breaks data hiding. class CounterTest2 { Counter c1 = new Counter(); //create a Counter c1.value = 100; // breaks assumption about Counter System.out.println("Counter1 value is " + c1.get()); public class Counter { private int value; // instance variable public void reset() { value = 0; // mutator method public int get() { return value; // accessor method public void click() { value = (value + 1) % 100; This implementation makes certain access to the field value, such as the assignment c.value = 100, a compiler error if attempted from outside the methods in class Counter. The use of private fields for implementation is a common technique in building ADTs and is referred to as the principle of data hiding.

private methods/fields cannot be accessed from outside of the class. public methods/fields can be accessed from anywhere. Default (no modifier) methods/fields have package access. They can be accessed from other classes in the same package. If you don't specify a package, all classes in the same directory are part of the same, default - unnamed package. Objects are created with new ClassName() This allocates space for the object in the heap (memory), and initializes the object by invoking the constructor for the class if there is one. If there is no constructor, by default all fields are initialized (boolean fields are false, all other primitives are 0, and everything else is initialized to null). A constructor is like a method with two special characteristics: It has no return type. The method name is the same as the class name. The purpose of a constructor is initialization of an object. Constructors are frequently overloaded because there are many useful ways to initialize objects. public class Counter { private int value; // instance variable public Counter () { value = 0; public Counter (int v) { value = v; public void reset() { value = 0; // mutator method public int get() { return value; // accessor method public void click() { value = (value + 1) % 100;

class CounterTest3 { Counter c1 = new Counter(); //a Counter starting at 0 Counter c2 = new Counter(50); //one starting at 50 c1.click(); c2.click(); System.out.println("Counter1 value is " + c1.get()); System.out.println("Counter2 value is " + c2.get()); tostring() must be public. A full explanation must wait until Chapter 7. Every class has a default tostring() that is public. When you give your class a tostring() you can't undo the already public status of the method. By providing every class with a tostring() method, we can use System.out.println() to print ANY object value. class Person { private int age; private String name; private char gender; Problems to solve in Chapter 6 3, 4, 6, 9, 13, 14, 15 public String tostring () { return "Name: " + name + "\nage: " + age + "\ngender: " + gender; class PersonTest { Person jane = new Person (); Person john = new Person ();... System.out.println (jane); System.out.println (john);

Static methods don't operate (implicitly) on an instance of the class containing the method. Likewise, static fields are not part of an object, they are instead part of the class, hence also called class variables. public class Counter { //instance variables -fields -- hidden private int value; private static int howmany = 0; //methods -- exposed public Counter(){ howmany++; public void reset() { value = 0; public int get() { return value; public void click() { value = (value + 1) % 100; public static int howmany() { return howmany; Unlike the field value, the static field howmany is universal to the class. // CounterTest2.java - demonstration of static field class CounterTest2 { System.out.println(Counter.howMany()); Counter c1 = new Counter(); Counter c2 = new Counter(); c1.click(); c2.click(); c2.click(); System.out.println("Counter1 value is " + c1.get()); // value is 1 System.out.println("Counter2 value is " + c2.get()); // value is 2 System.out.println(Counter.howMany());// prints 2 Calling methods in the same class just use the name works for instance method to instance method instance method to static method but NOT static method to instance method Calling instance methods objectreference.methodname() Calling class methods ClassName.methodName()

We could implement click() in Counter with void click() { value = (get() + 1) % 100; This call to get() is operating on the same Counter object as the one used to invoke click(). When calling one instance method in the same class from another in the same class, they both operate on the same, implicit object. When executing a static method there is NO implicit object being operated on, hence calling an instance method in the same class using only the method names, doesn t specify what object to operate on. When we want to define a constant for a class, the keyword final is used. The keyword final indicates that the variable cannot be changed once it has been initialized. public class Math { public static final double PI = 3.14159265358979323846; You can use Math.PI, but cannot change the value explicitly.