Each object belongs to a class

Slides:



Advertisements
Similar presentations
Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
Advertisements

Using Classes Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Not Glasses, Classes!!!
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 2 – Using Objects.
Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
Strings Testing for equality with strings.
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 2 Using Objects. These slides for CSE 110 Sections are based in part on the textbook-authors ’ slides, which are copyright 2003 by Cay Horstmann.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class.
The Class String. String Constants and Variables  There is no primitive type for strings in Java.  There is a class called String that can be used to.
Chapter 2  Using Objects 1 Chapter 2 Using Objects.
Chapter 2 types, variables, methods Pages Horstmann.
Strings.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter One: Introduction.
CHAPTER 2 Using Objects. Basic Programming Terminology  Computer program process values.  Numbers (digits)  Words (Strings)  These values are different.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Two: Using Objects.
Chapter Two: Using Objects. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about parameters.
Chapter 2 – Using Objects Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 1 – Introduction ( ปรับปรุง )
ICOM 4015: Advanced Programming Lecture 2 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Reading: Chapter Two: Using.
Chapter 2 Using Objects. Chapter Goals To learn about variables To understand the concepts of classes and objects To be able to call methods To be able.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. int: integers, no fractional part: 1, -4, 0 double : floating-point.
Week 2 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 2 - Using Objects.
String and Scanner CS 21a: Introduction to Computing I First Semester,
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A class represents a single concept from the problem domain, or a.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 2 – Using Objects.
Programming Kickstart Monday – Friday 10:50am – 12:35pm Chalmers 304 June 17-July 5 (not July 4) 2013 Ms. Paula Evans Jake Saferstein.
Chapter 2 Using Objects. Chapter Goals To learn about variables To understand the concepts of classes and objects To be able to call methods To be able.
Array length = maximum number of elements in array Usually, array is partially filled Need companion variable to keep track of current size Uniform naming.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Declaring variables The type could be: int double char String name is anything you want like lowerCaseWord.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 2 – Using Objects.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A string is a sequence of characters Strings are objects of the String.
A Simple Object Oriented Program public class Simple { public static void main (String [] args) { System.out.println(“howdy”); } System.out is an object.
Slides by Evan Gallagher
Slides by Evan Gallagher
Chapter Goals To learn about variables
Chapter 2 Not Glasses, Classes!!! Using Classes
A final variable is a constant
John Woodward A Simple Program – Hello world
Lecture 3 John Woodward.
Data Structures and Algorithms revision
Console Output, Variables, Literals, and Introduction to Type
Chapter 4 – Fundamental Data Types
String class.
Chapter 1 – Introduction
Chapter 2 – Using Objects
Chapter Three - Implementing Classes
Chapter 2 Using Objects.
Chapter 4 – Fundamental Data Types
Wednesday 09/23/13.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Use a variable to store a value that you want to use at a later time
Chapter 5 – Decisions Big Java by Cay Horstmann
1.7 Errors Compile-time error: A violation of the programming language rules that is detected by the compiler Example: System.ou.println("Hello, World!);
6.2 for Loops Example: for ( int i = 1; i
Parameter: an input to a method
Presentation transcript:

Each object belongs to a class 2.4 Objects and Classes Object: entity that you can manipulate in your programs (by calling methods) Each object belongs to a class Example: System.out belongs to the class PrintStream Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Method: sequence of instructions that accesses the data of an object You manipulate objects by calling its methods Class: declares the methods that you can apply to its objects Class determines legal methods: String greeting = "Hello"; greeting.println() // Error greeting.length() // OK Public Interface: specifies what you can do with the objects of a class Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Overloaded Method Overloaded method: when a class declares two methods with the same name, but different parameters Example: the PrintStream class declares a second method, also called println, as public void println(int output) Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

A Representation of Two String Objects Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

length: counts the number of characters in a string: String Methods length: counts the number of characters in a string: String greeting = "Hello, World!"; int n = greeting.length(); // sets n to 13 toUpperCase: creates another String object that contains the characters of the original string, with lowercase letters converted to uppercase: String river = "Mississippi"; String bigRiver = river.toUpperCase(); // sets bigRiver to "MISSISSIPPI" When applying a method to an object, make sure method is defined in the appropriate class: System.out.length(); // This method call is an error Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Self Check 2.9 How can you compute the length of the string "Mississippi"? Answer: river.length() or "Mississippi".length()

Self Check 2.10 How can you print out the uppercase version of "Hello, World!"? Answer: System.out.println(greeting.toUpperCase());

Self Check 2.11 Is it legal to call river.println()? Why or why not? Answer: It is not legal. The variable river has type String. The println method is not a method of the String class.