Ch 7 Designing Java Classes & Class structure

Similar documents
Ch 7 Designing Java Classes & Class structure. Methods: constructors, getters, setters, other e.g. getfirstname(), setfirstname(), equals()

Ch 7 Designing Java Classes & Class structure. Fields have data values define/describe an instance

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

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

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

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

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Object Oriented Programming in C#

Classes. Classes as Code Libraries. Classes as Data Structures

Programming II (CS300)

Introduction to Classes and Objects. David Greenstein Monta Vista High School

CS1004: Intro to CS in Java, Spring 2005

Recitation 3 Class and Objects

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

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

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

Answer ALL Questions. Each Question carries ONE Mark.

Chapter 4: Writing Classes

Programming II (CS300)

Principles of Object Oriented Programming. Lecture 4

ECOM 2324 COMPUTER PROGRAMMING II

Object Oriented Programming

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

COMP-202: Foundations of Programming. Lecture 26: Review; Wrap-Up Jackie Cheung, Winter 2016

Software and Programming 1

Java Identifiers, Data Types & Variables

Java Foundations Certified Junior Associate

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

Recommended Group Brainstorm (NO computers during this time)

CS 302 Week 9. Jim Williams

CS1083 Week 2: Arrays, ArrayList

Object Oriented Programming

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

CS 1302 Chapter 9 (Review) Object & Classes

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

Assignment 1 due Monday at 11:59pm

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

Chapter 4 Classes in the Java Class Libraries

Chapter 9. Objects and Classes

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

This exam is open book. Each question is worth 3 points.

Lecture Static Methods and Variables. Static Methods

Encapsulation. Inf1-OOP. Getters and Setters. Encapsulation Again. Inheritance Encapsulation and Inheritance. The Object Superclass

Lecture Static Methods and Variables. Static Methods

Inf1-OOP. Inheritance and Interfaces. Ewan Klein, Perdita Stevens. January 12, School of Informatics

Chapter 4 Defining Classes I

Anatomy of a Class Encapsulation Anatomy of a Method

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

Notes on Chapter Three

Methods and Data (Savitch, Chapter 5)

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

CS18000: Problem Solving And Object-Oriented Programming

Chapter 8 Objects and Classes. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

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

Distributed Systems Recitation 1. Tamim Jabban

More on variables and methods

CS Week 13. Jim Williams, PhD

SSE3052: Embedded Systems Practice

Java Classes. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

Software and Programming 1

CMP 326 Midterm Fall 2015

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

OO Programming Concepts

Methods. Methods. Mysteries Revealed

TaxiBot New attributes Variables Math! TaxiBot

Activity 11: Designing Classes

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

Introduction to Computer Science I

Introduction to Programming

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

Java Object Oriented Design. CSC207 Fall 2014

More Java Basics. class Vector { Object[] myarray;... //insert x in the array void insert(object x) {...} Then we can use Vector to hold any objects.

CS 101 Fall 2006 Midterm 3 Name: ID:

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

Final Exam CS 152, Computer Programming Fundamentals December 5, 2014

Ticket Machine Project(s)

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

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

Informatik II Tutorial 6. Subho Shankar Basu

Classes and Methods: Classes

Software and Programming 1

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Informatik II (D-ITET) Tutorial 6

PART 1. Eclipse IDE Tutorial. 1. What is Eclipse? Eclipse Java IDE

Programming with Java

Generating/Updating code from whole project

CS1004: Intro to CS in Java, Spring 2005

Module Contact: Dr Geoff McKeown, CMP Copyright of the University of East Anglia Version 2

Defining Classes and Methods

Programming II (CS300)

CSCI 1103: Object-Oriented Objects

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

Previous to Chapter 7 Files

ECE 122. Engineering Problem Solving with Java

Chapter 8 Objects and Classes Part 1

Programming II (CS300)

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

Chapter 4. Defining Classes I

Object-Oriented Programming Concepts

G51PGP Programming Paradigms. Lecture OO-4 Aggregation

Transcription:

Ch 7 Designing Java Classes & Class structure Classes comprise fields and methods Fields: Things that describe the class or describe instances (i.e. objects) e.g. student number, first name, last name, gender, Methods: constructors, getters, setters, other e.g. getfirstname(), setfirstname(), equals() a getter/accessor a setter/mutator ACS-1903 1

UML Diagram of a Class fields Class name A class has a name, Fields (called variables in Java code) methods (including constructors) Methods (A method may have local fields) ACS-1903 2

e.g. Math & Random classes A quick look at two classes we have used: Math and Random Math provides some useful utility methods. We use it without instantiating an object. double area = Math.PI * Math.pow(r,2); Random lets us use random sequences. To utilize this we must instantiate objects. Random die = new Random(); Int toss = die.next(6)+1; +E +PI Math -Math() +abs(double a) +abs(float a) + abs(int a) + max(double a, double b) + max(int a, int b) -seed -multiplier Random +Random() +Random(long seed) +nextboolean() +nextint() ACS-1903 3

e.g. Math class Math has two static fields Math has a private constructor You cannot instantiate a Math object Math has many static methods To use π you write Math.PI To use the static method max you write Math.max(n1, n2) +E +PI Math -Math() +abs(double a) +abs(float a) + abs(int a) + max(double a, double b) + max(int a, int b) Here we specify the name of the class ACS-1903 4

e.g. Random class We must instantiate an object to get a random sequence Random gen = new Random (); Random has some private instance fields seed multiplier = 0x5DEECE66DL constructors instance methods gen.nextboolean() gen.nextint() -seed -multiplier Random +Random() +Random(long seed) +nextboolean() +nextint() gen is an object an instance ACS-1903 5

Fields Fields may be primitive variables Or, they may be of some other type (e.g. String, ) May be public or private public anyone can use it private limited access ACS-1903 6

Methods Methods are either: value-returning must have a return statement e.g. getters naming convention is void no return statement e.g. setters naming convention is ACS-1903 7

Methods public vs private public - anyone can use it private - special cases Math constructor is private you cannot instantiate a Math object try to do it ACS-1903 8

Methods All classes should have equals( ) tostring() ACS-1903 9

equals Method equals( ) Value-returning Returns a boolean Usually an equals method is designed for a class. Designer must determine the condition when two objects are considered equal. E.g. String class has an equals method string1.equals(string2) abc.equals( xyz ) returns false abc.equals( abc ) returns true ACS-1903 10

tostring Method tostring() Value-returning Returns a string A method automatically called when an object is displayed E.g. System.out.println(myObject); The designer of a class determines what it returns E.g. ArrayList has a tostring() method result is of the form: [ object 1, object 2, object n ] ACS-1903 11

Consider a Word class Create Word in BlueJ and experiment. public class Word { String text; int frequency; } public Word(String w){ text = w; frequency = 0; } Example: public class ProcessWords { public static void main(string[] args) { // put your code here Word w = new Word("Java"); System.out.println(w); System.out.println(w.text); System.out.println(w.frequency); } } What should be public? What should be private? If we have two words, how do we know if they are equal, or the same? What happens if we try to print a word? ACS-1903 12

Text Example - Student Consider the student class in the text ACS-1903 13

Class Diagram for Student -id -firstname -lastname -gender -active show 1, 2, or 3 compartments/ info as needed getters + means there is public access to the method - means there is no public access to the field or method setters ACS-1903 14

Java code for Student - fields instance vs class e.g. consider Student class Which fields are class? Which fields are instance? ACS-1903 15

Java code for Student - fields instance vs class Instance object Static field class-level field There is only one lastid field. It is a class-level field that is shared by all Student instances There are id, firstname, lastname, gender, active, and major fields for each Student instance. So each student can have different values. ACS-1903 16

Java code for Student - fields private vs public private: only directly accessible from within the class/object, and from outside the class via getters/setters public : accessible from anywhere A design principle is to make fields private but give public access to the getters and setters ACS-1903 17

Java code for Student - constructors The no-arg constructor Constructor with 4 parameters Use as many constructors as your application requires. Constructors differ in the number and type of parameters. ACS-1903 18

Java code for Student - getters Notice Getters (also called accessors) for most private fields Naming convention: Start with get followed by the field name but this starts with a capital letter Naming convention for boolean: Start with is followed by the field name but this starts with a capital letter ACS-1903 19

Java code for Student - setters Notice Setters (also called mutators) for most private fields Naming convention: Start with set followed by the field name but this starts with a capital letter ACS-1903 20

Java code for Student other methods private method nextid Used to control the id assigned to a new student object tostring Executes when a student is printed equals Tests two student objects to see if they are equal ACS-1903 21

Java Classes Class is a template for objects How are these shown in UML? (Figures 7.2, 7.4, 7.5 ACS-1903 22

Objects Objects: instantiated/created via new lots of examples also called an instance so we can speak of instance fields/methods How are these shown in UML? (Figures 7.3, 7.4) object name followed by : followed by class name underlined field values ACS-1903 23

Objects Listing 8.1: Creates two students One using the no-arg constructors and setters The other using a 4-arg constructor ACS-1903 24

Objects /** * Create two student objects * using the two constructors */ public class UseConstructors { public static void main (String[] args){ // first, with the no-arg constructor Student jill = new Student(); // use setters to complete the student object jill.setfirstname("jill"); jill.setlastname("lee"); jill.setgender('f'); jill.setactive(true); // now with the other constructor Student sam = new Student("Samantha","Jones",'F',true); // display the students System.out.println(jill); System.out.println(sam); } } ACS-1903 25