Java Classes - Using your classes. How the classes you write are being used

Similar documents
Classes - Testing your classes. Make sure your classes do what you think

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

Rules and syntax for inheritance. The boring stuff

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Chapter 4 Defining Classes I

BM214E Object Oriented Programming Lecture 8

Object Oriented Programming

The Java Main Method

Exceptions. Examples of code which shows the syntax and all that

Computer Programming, I. Laboratory Manual. Final Exam Solution

Objects and Classes. Amirishetty Anjan Kumar. November 27, Computer Science and Engineering Indian Institue of Technology Bombay

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

A simple map: Hashtable

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

COMP-202 More Complex OOP

Chapter 6 Classes and Objects

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

COMP 215: INTRO TO PROGRAM DESIGN. Prof. Chris Jermaine Chris Prof. Chris Dr. Chris

Survey #2. Teen Talk Barbie TM Reloaded. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Partially Filled Arrays ArrayLists

Chapter Two Bonus Lesson: JavaDoc

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

A Bad Idea Monday, April 16, :23 AM

Options for User Input

Lecture 5: Implementing Lists, Version 1

1.00 Lecture 32. Hashing. Reading for next time: Big Java Motivation

RMI Case Study. A Typical RMI Application

Lesson 12: OOP #2, Accessor Methods (W03D4)

! Two questions when we design an OO system : ! Our Initial goal: learn to design and implement simple objects.

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

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

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

Example Program. public class ComputeArea {

this keyword in java javatpoint

5.6.1 The Special Variable this

JAVA: A Primer. By: Amrita Rajagopal

public Twix() { calories = 285; ingredients = "chocolate, sugar, cookie, caramel"; }

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4

School of Informatics, University of Edinburgh

The Singleton Pattern. Design Patterns In Java Bob Tarr

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

MEMORIZE the Java App pick: public class { public static void main(string[] a) {

2. JAVA OOP R E F E R E N C E : O B J E C T O R I E N T E D D E S I G N A N D P R O G R A M M I N G ( H O R S T M A N N )

CS162: Introduction to Computer Science II. Primitive Types. Primitive types. Operations on primitive types. Limitations

The Singleton Pattern. Design Patterns In Java Bob Tarr

Object-Oriented Programming in Processing

Handout 9 OO Inheritance.

The compiler is spewing error messages.

Lecture 7 Objects and Classes

coe318 Lab 1 Introduction to Netbeans and Java

1.00 Lecture 8. Using An Existing Class, cont.

The Decorator Pattern. Design Patterns In Java Bob Tarr

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Lecture 02, Fall 2018 Friday September 7

Client Code - the code that uses the classes under discussion. Coupling - code in one module depends on code in another module

Annotation Hammer Venkat Subramaniam (Also published at

Design Patterns: State, Bridge, Visitor

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

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

Relationship of class to object

CS1004: Intro to CS in Java, Spring 2005

Methods and Data (Savitch, Chapter 5)

AP CS Unit 4: Classes and Objects Programs

Goals for Today. CSE1030 Introduction to Computer Science II. CSE1030 Lecture #9. Review is-a versus has-a. Lecture #9 Inheritance I

1.00 Lecture 13. Inheritance

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

Intro. Scheme Basics. scm> 5 5. scm>

CS 315 Software Design Homework 1 First Sip of Java Due: Sept. 10, 11:30 PM

CSE 142/143 Unofficial Commenting Guide Eric Arendt, Alyssa Harding, Melissa Winstanley

Object-Oriented Programming Concepts

Week 7 - More Java! this stands for the calling object:

Equality for Abstract Data Types

Part 2: The Material PART 2

Primitive vs Reference

PRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT 00. WELCOME TO OBJECTVILLE. Speaking the Language of OO

CIS 110: Introduction to Computer Programming

Outline. CIS 110: Introduction to Computer Programming. Any questions? My life story. A horrible incident. The awful truth

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

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

A set, collection, group, or configuration containing members regarded as having certain attributes or traits in common.

Final Examination Semester 3 / Year 2008

Utilities (Part 2) Implementing static features

Advanced Object Oriented Programming EECS2030Z

Tiers (or layers) Separation of concerns

First IS-A Relationship: Inheritance

Software Concepts. 2-8 Explain the process of translating and executing a Java program. How might the World Wide Web be involved?

Class and Functions. Reusable codes

CSSE 220. Intro to Java Graphics. Check out IntroToJavaGraphics and BiggestFan projects from SVN

Java Programming. What is a program? Programs Recipes. Programs as Recipes 8/16/12. Pancakes In one bowl. mix: 1½ cup flour

Method Overriding in Java

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

Binghamton University. CS-140 Fall Functional Java

CS 211 Programming Practicum Spring 2018

Midterms Save the Dates!

Computer Science 210: Data Structures

COMP-202 Unit 5: Basics of Using Objects

University of Palestine. Mid Exam Total Grade: 100

Instance Method Development Demo

PRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT 03. REQUIREMENTS CHANGE. I Love You, You re Perfect... Now Change!

Transcription:

Java Classes - Using your classes How the classes you write are being used

What s the use of classes? So, you have been writing a few classes by now... What for? The programs you will write will use objects to represent data and behavior The objects will be defined by various classes: Classes from the Java standard API Classes from other people Classes you have written yourself You write classes when there is no predefined class available which suits your needs

Suppose you are writing a system for Members You are writing a membership management system for a club. You ended up designing a class representing a member in the registry:

The class Member Two private instance variables name, email a private static final String separator Two constructors Some methods

The class Member package net.supermegacorp.orgmanager; import java.io.serializable; public class Member implements Serializable { private String name; private String email; private static final String separator = ";"; public Member(String name, String email) { this(name); setemail(email); } public Member(String name) { this.name = name; } //... etc

The use of the Member class The Member class was written (by e.g. you) in order to have a class for objects in your system which represents members of the club. In some module you might, for instance, have a method which adds a new Member to the club s registry: public void addmember(member newmember) { memberlist.add(newmember); save(); } That s one use of the Member class. You wrote it so that you could represent a member and pass it around in your system.

Using your own classes Typically this is the steps: You analyse the problem (requirements etc) You see that Member is a class which is used a lot in the Membership system You design a solution (design the classes etc needed to write the program) You design the Member class so that it can fulfill its purpose in the system You write the code for the Member class You write the code for the classes and methods which need to use Member objects

Possible confusion for students new to Java In Java, everything seems to be written inside a class. Sometimes the classes seem to simply represent some entity in the real world (e.g. a Member). Other times classes are the units which starts the program (e.g. the class which contains the main() method) Other times still, classes seem to represent stuff needed for the functionality of the system (e.g. some storage class, some GUI class etc) So far in the book/course, we ve mostly seen the first two types of classes: Classes that represents objects we use in the program to be Classes with the main() method

Possible confusion for students new to Java One thing that might be confusing to you, is that when you write a simple class like the Member class, it contains a lot of code (constructors and methods) but you can t really see the code which triggers those chunks of code! Classes are in this sense passive descriptors. The contain the code for possible future objects, but those objects are created in some other class! And the methods aren t executed until someone has a reference to e.g. a Member object, and invokes a method via the reference. That too, is done from a different class.

Simple example showing client code and a class // TestMember.java - client code using the Member class // to create a Member object, save a reference to it // and invoke a method via the reference public static void main(string[] args) { Member testmember = new Member("Bengt", " bengan@klubben.se "); // hard coded values for testing System.out.println("The member email address: " + testmember.email() ); } ----------------------------------In a totally different file----------------------------------- // Member.java - Model class - passive until some client code creates an object from it public class Member { private String name; private String email; public Member(String name, String email) { this.name = name; this.email = email; // better yet, check that it seems like a real email first } //... etc etc

Summary The simple classes you write during this book/course typically represent objects needed in some application (a Member, a Car, a Contact, a Product...) The code which actually does something, starts in the main() method The main method creates a few objects and calls methods on those objects via references Often, an object s method calls other methods (methods in the same object or methods of other objects (which might have been passed to the method as arguments) A class modelling a real-world thing is more of a blueprint for future objects which may be created by some code at some point Think of the class java.lang.string - it just sits there and does nothing until you or someone writes a program creating and using String objects!