COP 3330. Topics Java Basics - How to write & call your own method - Math methods (static methods) - String methods (instance methods) - Errors Classes.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Advertisements

Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design Lecture 6, Tue Jan
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Fundamental Programming Structures in Java: Strings.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Guide To UNIX Using Linux Third Edition
Arrays and Objects OO basics. Topics Basic array syntax/use OO Vocabulary review Simple OO example Instance and Static methods Static vs. instance vs.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Writing Classes You have already used classes –String, Random, Scanner, Math, Graphics, etc –To use a class: import the class or the package containing.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Session 7 Methods Strings Constructors this Inheritance.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Finalizers, this reference and static Sangeetha Parthasarathy 06/13/2001.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
CLASSES AND OBJECTS Object-Oriented Basics. Vocabulary Review – C++ Class Object Instance this new Constructor Default constructor Access (private/public/protected)
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Recitation 8 User Defined Classes Part 2. Class vs. Instance methods Compare the Math and String class methods that we have used: – Math.pow(2,3); – str.charAt(4);
Introduction to Object-Oriented Programming Lesson 2.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
CIS Intro to JAVA Lecture Notes Set 5 26-May-05.
Classes and Objects.
Chapter 7 User-Defined Methods.
Static data members Constructors and Destructors
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Chapter 4: Writing Classes
Java Review: Reference Types
CSC240 Computer Science III
Object Based Programming
Chapter 9 Inheritance and Polymorphism
Topics Introduction to File Input and Output
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Chapter 6 Methods: A Deeper Look
Phil Tayco Slide version 1.0 Created Oct 9, 2017
Polymorphism 28-Nov-18.
Object Oriented Programming
Namespaces, Scopes, Access privileges
elementary programming
Object Oriented Programming in java
Polymorphism 15-Apr-19.
Polymorphism 21-Apr-19.
Topics Introduction to File Input and Output
Visibilities and Static-ness
Chapter 11 Inheritance and Encapsulation and Polymorphism
Creating and Using Classes
Chapter 7 Objects and Classes
Presentation transcript:

COP 3330

Topics Java Basics - How to write & call your own method - Math methods (static methods) - String methods (instance methods) - Errors Classes Objects

Writing your own methods main() is a method you’ve all already used - public static void main(String[] args) To write your own methods, use the same syntax public static void myMethod() { // Do something here. } public static int myIntMethod() { // Do something here and return an int. }

Writing your own methods Order of methods doesn’t matter (Difference from C) Don’t forget static keyword or you’ll receive error “ Cannot make a static reference to the non-static method ” Write code showing method calls

Math Methods These are static methods. That means they are called on the Math class - abs() : Takes in an int and returns its absolute value - sin(), cos(), tan() : Take in doubles and perform trigonometry. These functions use radians, not degrees - PI : A static final variable that is closer to π than any other double.

Math Methods More Math methods - pow() : Takes in two doubles and returns the first raised to the power of the second - random() : Takes in no parameters and returns a random double in the range from 0.0 to 1.0, including 0, but excluding 1. - sqrt() : Takes in a double and returns its square root - Show example Math code

String Methods Java Strings are immutable. That means that once they're created, they can't be altered Calling methods on Strings will return new Strings, but leave the original intact

String Methods These are instance methods (that means they're called on String objects) - concat() : Takes in a String and returns a new String. Equivalent to + - replace() : Takes in two chars and returns a new String. The first char is the character to be replaced, and the second char is the character to replace it with

String Methods More methods - substring() : Takes in two ints and returns a new String. The first int is the beginning index and the second int is the ending index of the substring - length() : Takes in no parameters. Returns the length of the String - charAt() : Takes in an int and returns the character at that index

Simple grep Program Write a program that reads in a paragraph of text from a file called “ text.in ” and searches for all instances of a given word (pattern) input by the user from standard input, ignoring the case of the text or pattern. Print the line number and index (both 0-based) of each occurrence of the pattern, or print that the pattern was not found in the text. Use either the substring() method or the indexOf() method to search for the pattern.

Errors Compilation Errors Runtime Errors Logic Errors

Compilation Errors A compilation error can occur when the grammar of the language is broken Common examples - Missing semicolon - Unmatched curly brace - Misspelled keyword

Compilation Errors A compilation error can also occur when the syntax is basically valid, but a statement has an invalid meaning Examples - Using undeclared/out-of-scope variables - Not casting when casting is necessary - Using a double to index an array

Runtime Errors A runtime error is when the program crashes In Java, usually an unhandled exception - NullPointerException - ArithmeticException - ArrayIndexOutOfBoundsException - And much, much more… Also, virtual machine errors (e.g. out of memory)

Logical Errors Say what you mean, and mean what you say Logical errors are when the computer understands and can do what you’ve told it, but you’ve told it the wrong thing Logical errors are notoriously difficult to track down

Logical Errors Sources of logical errors - Typo - Programmer didn’t notice special cases - Programmer didn’t understand problem

Quiz #1 Returned Please come to the front of class to claim your Quiz #1 If you think you deserve more points, please see me during office hours - You must have a clear and valid reason to earn more points. “I need to get a better grade on this quiz” is not a valid reason!

A Good Design … features Encapsulation This is a key aspect of object-oriented design Encapsulation means grouping together related data and operations on that data into a unit

Class Basics A class defines a new data type A class is a blueprint for objects Classes are mostly made up of three basic components (members) - Instance variables (fields) - Constructors - Methods

A Good Design …features Information Hiding This is a key aspect of object-oriented design Information hiding means concealing unnecessary details This allows those using a system to more easily understand the big picture This makes it much more difficult to use the system in incorrect ways

Class Basics All members have access modifiers The most common are - public - private The other two will be discussed at a later point - protected - Default access (package)

Instance Variables Instance variables persist throughout the life of the object They can be of the same types as any other variables Example - private double realPart; - private double imaginaryPart;

Constructor A constructor is a method, but it is so important that it needs separate consideration A constructor creates, initializes, and returns a reference to a new object Creation and return are handled automatically The constructor always bears the name of its class

Constructor Example public class Complex { private double realPart; private double imagPart; public Complex() { realPart = imagPart = 0; }... }

Constructor Constructors are called by using the new keyword - Example: Complex foo = new Complex();

Garbage Collection In Java, when an object is no longer referenced, its memory is reclaimed automatically This is a big difference from C

Book Example Store information about a Book - Title - Publisher - ISBN - Number of pages - Number of chapters - The text of the book - Others?

Book Write a class to store this data and create a constructor for the class

Methods Non-static methods act on objects A method is laid out as follows: modifiers return-type identifier(parameters) { body } A method is essentially the same thing as a function in C

Methods The return type of a method can be any type If the method does not return anything, the return type is void

Methods Variables accessible from within a method - Instance variables - Formal parameters - Local variables

Methods Use object.methodName(parameters) to call the method on an object - Example: number = stdin.nextInt(); Add a method to the Book class to add a chapter

.toString() Java uses the toString method on objects to represent it as a string for purposes of string concatenation and such The toString method has the following signature: - public String toString() If you want to be able to print objects of your class to be able to be printed in a meaningful way, write this method in your class!

Overloading Multiple methods (and constructors) with the same name can exist in the same class The formal parameters MUST be different Why do this? - Use different types of information to construct an object (e.g. Scanner constructors) - Perform similar operations (e.g. print() method in PrintStream)

Static Ordinarily, methods and fields are associated with objects The static modifier makes them associate with the class Example - See the class Math in java.lang

Static To access a static member, use the class name instead of the object name - Example: number = Integer.parseInt( “ 5 ” ); Add a static method to the Book class to generate a new ISBN for a new book