CS 251 Intermediate Programming Methods and More

Similar documents
CS 251 Intermediate Programming Methods and Classes

Chapter 4 Defining Classes I

Introduction to Programming Using Java (98-388)

CS 251 Intermediate Programming Java Basics

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

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

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

Chapter 6 Introduction to Defining Classes

Object-Oriented Programming Concepts

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

This course supports the assessment for Scripting and Programming Applications. The course covers 4 competencies and represents 4 competency units.

CS 251 Intermediate Programming Inheritance

Defining Classes and Methods

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

CSE 142 Su 04 Computer Programming 1 - Java. Objects

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

9 Working with the Java Class Library

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

CST141 Thinking in Objects Page 1

Java for Non Majors. Final Study Guide. April 26, You will have an opportunity to earn 20 extra credit points.

Defining Classes and Methods

Methods and Data (Savitch, Chapter 5)

Java for Non Majors Spring 2018

Software Design and Analysis for Engineers

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

CS 152 Computer Programming Fundamentals Coding Standards

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

Midterm Exam CS 251, Intermediate Programming March 12, 2014

CS112 Lecture: Defining Instantiable Classes

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

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

CSC Java Programming, Fall Java Data Types and Control Constructs

Java Primer 1: Types, Classes and Operators

CS 139 Practice Midterm Questions #2

Object Oriented Programming in C#

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

CS 251 Intermediate Programming Coding Standards

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

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

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

CS 2340 Objects and Design - Scala

Midterm Exam CS 251, Intermediate Programming March 6, 2015

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

Java Object Oriented Design. CSC207 Fall 2014

CS 231 Data Structures and Algorithms, Fall 2016

CS162: Introduction to Computer Science II

Classes. Classes as Code Libraries. Classes as Data Structures

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

PIC 20A Number, Autoboxing, and Unboxing

Programming II (CS300)

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

Lecture 8 Classes and Objects Part 2. MIT AITI June 15th, 2005

Methods. Methods. Mysteries Revealed

CS 351 Design of Large Programs Coding Standards

Programming II (CS300)

BM214E Object Oriented Programming Lecture 8

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Creating an object Instance variables

Lecture 7: Classes and Objects CS2301

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

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

A First Object. We still have another problem. How can we actually make use of the class s data?

2. The object-oriented paradigm!

CS 251 Intermediate Programming More on classes

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

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

COP 3330 Final Exam Review

Assignment 1 due Monday at 11:59pm

More About Objects and Methods. Objectives. Outline. Chapter 6

More About Objects and Methods

CHAPTER 7 OBJECTS AND CLASSES

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

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types


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

CS1004: Intro to CS in Java, Spring 2005

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Java Fundamentals (II)

CHAPTER 7 OBJECTS AND CLASSES

ITI Introduction to Computing II

Use the scantron sheet to enter the answer to questions (pages 1-6)

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

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

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

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

Separate Compilation Model

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

Declarations and Access Control SCJP tips

Java Intro 3. Java Intro 3. Class Libraries and the Java API. Outline

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

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

EECS168 Exam 3 Review

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

Language Features. 1. The primitive types int, double, and boolean are part of the AP

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

Classes Classes 2 / 35

Java. Representing Data. Representing data. Primitive data types

Object-Oriented Programming in Java. Topic : Objects and Classes (cont) Object Oriented Design

More About Objects and Methods

Transcription:

CS 251 Intermediate Programming Methods and More Brooke Chenoweth University of New Mexico Spring 2018

Methods An operation that can be performed on an object Has return type and parameters Method with no return type (void) is often called procedure in other languages. Method that has a return type, may be called function in other languages. In Java - They are all called methods Can be overloaded. What does that mean? Many already available methods... (See JDK API)

What is Overloading? A method is identified by its name, and the types of its arguments. You can declare several methods with the same if the argument types differ. Argument order is important! It is not possible to declare two methods that only differ in the return type. (Why?) Can not declare one method circlearea with the radius as argument, and one circlearea with the circumference, unless radius and circumference have different types.

Overloading Example public class OverloadExample { public static void writetype ( int x) { System. out. println (" int "); public static void writetype ( char c) { System. out. println (" char "); public static void writetype ( String s, float f) { System. out. println (" String + float "); public static void main ( String [] args ) { writetype (1); writetype ( a ); writetype (" Hello ", 3.1415);

The static keyword Static methods and Static variables Mixing static and non static Programming style - use a small main Only contain method calls

Nonstatic from static Problems with calling non-static methods from a static method. Why? Solution 1: Instantiate an object and invoke method with it. Solution 2: Add a parameter to take object of object type and use it. (Won t work for main)

Put a main in any class! May be something new. Very useful for testing No need to compile whole program, just class Remember previous slide. Instantiate object to test it. Other reasons for multiple main methods?

Static variables Constants - public final static Variables - public static When do we use them? Why? Static vars in other classes. How to use them?

Static methods What is a static method? When do we want to use them? main is always static Example: Math class

Wrapper classes Integer, Character, & Double Has many useful static methods. Initialization and casting between primitive Autoboxing and unboxing

What is an object? Each object has certain data and behavior An example: student Data: age, endurance, intelligence,... Behavior: code, drink, workout, sleep,... Another example: car Data: power, top-speed, shape, color, etc... Behavior: start, accelerate, break, turn

What is a class? A class is a blueprint from which objects are created. An object created from a class is an instance of that class.

Example class public class Student { private int age, endurance, intelligence ; public Student ( int age, int endurance, int intelligence ) { this. age = age ; this. endurance = endurance ; this. intelligence = intelligence ; public void drink ( String what ) { if ( what == " milk " ) { endurance ++; else if ( what == " alcohol " ) { if ( age >= 21) { intelligence = intelligence - 5; else { System. out. println (" You are too young to drink!"); else { System. out. println (" Don t drink " + what + "!");

Find mistakes! What s wrong with the program on previous page?

The String trap Why shouldn t you compare two strings with the == operator? Reference types! A reference to a place in memory - a comparison with the == operator compares addresses of memory. Are the two references both refering to the same object? When comparing two objects, usually want to use equals method.

Example class revisited public class Student { private int age, endurance, intelligence ; public Student ( int age, int endurance, int intelligence ) { this. age = age ; this. endurance = endurance ; this. intelligence = intelligence ; public void drink ( String what ) { if ( what. equals (" milk ") ) { endurance ++; else if ( what. equals (" alcohol ") ) { if ( age >= 21) { intelligence = intelligence - 5; else { System. out. println (" You are too young to drink!"); else { System. out. println (" Don t drink " + what + "!");

Class vs Instance variables Instance variables Non-static fields Every object has its own Need instance to use Class Variables Static fields Associated with class, not a particular object Can be manipulated without an instance

Class and Instance Variable Example public class Student { // These are instance variables private String name ; private int id; // This is a class variable private static int numberofstudents = 0; public Student ( String name ) { this. name = name ; // Give each student a unique ID this. id = ++ numberofstudents ; // More methods here...

Access Modifiers public Accessible to all private Only this class protected Only this class and its subclasses package-private No modifier. This class and others in same package.

Access Modifier Tips Don t expose your guts! Use private unless you have a good reason not to. Avoid public fields except for constants. (Use getter/setter)

Encapsulation - Creating an API API = Application Programming Interface Define a set of rules for an object (i.e., what public methods should be available) A well defined API will help in large projects Will reduce time for redesign and integration Is what we will strive to achieve Will require a certain design component in later programming projects

How to think object orientation Look at problem description Identify the following: Verbs (possible methods) Nouns (possible objects - or instance variables) Think early on how these objects will interact - Diagrams! What information (possibly objects) will need to be passed between them Then what? Put the design to test - have someone else critique it. Revise your design - Start Implementation

Creating the API First part of implementation is to realize the API Create all classes, with method stubs only Write an initial documentation for each object and method - This way you clearly know what each method is supposed to do, and might find flaws in the design when you think about it more. Use Javadoc for your comments Creates nice webpages for the API automatically.

Encapsulation Guidelines 1. Place comment in front of class to define how to think about the class. 2. All instance variables should be declared private 3. Provide mutator and accessor methods for state change 4. Use comment before each method, describing it s use 5. Make all helper methods private 6. Use /* */ comments for API comments and // for implementation details

Writing javadoc comments. (Page 1) /** * A class to demonstrates the usage of javadoc documentation * features. * @author Brooke Chenoweth * @version 1.1 */ public class JavadocDemo { private String name ; // Name of the object private int desc ; // Description of object /** The constant number of the democonstant. */ public static final int DEMOCONSTANT = 5; /** * The default constructor. Defines the empty JavadocDemo * class with default values set. Typically implicitly * called by subclasses. */ public JavadocDemo () {

Writing javadoc comments. (Page 2) /** * Preferred constructor. * @param desc Describes the entity in the object. * @param name Names the entity in the object. */ public JavadocDemo ( int desc, String name ) { this. name = name ; this. desc = desc ; /** * Changes the name of the object and makes sure name is valid. * @param name Proposed new name for object * @return True if name accepted, false if not. */ public boolean changename ( String name ) { if ( name. equals ( this. name ) ) { return false ; this. name = name ; return true ;