2-1 Types of data  Java has two types of data  primitive types that store one value char boolean byte int long float double  reference types to store.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

4-1 Chapter 4 (a) Defining Classes. 4-2 The Contents of a Class Definition A class definition specifies the data items and methods that all of its objects.
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Introduction to Programming G51PRG University of Nottingham Revision 1
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Objects, Methods, Parameters, Input Lecture 5, Thu Jan
Chapter 41 Defining Classes and Methods Chapter 4.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
Chapter 9: Classes with Instance Variables or Classes=Methods+Variables Asserting Java © Rick Mercer.
Programming with Java standard classes. Java API Application Programming Interface Provides hundreds of standard classes that can be incorporated into.
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
Understanding class definitions Looking inside classes.
11 Chapter 5 METHODS. 22 INTRODUCTION TO METHODS A method is a named block of statements that performs a specific task. Other languages use the terms.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
The Java Programming Language
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
From C++ to Java A whirlwind tour of Java for C++ programmers.
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 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
Introduction to Java Java Translation Program Structure
Working With Objects Tonga Institute of Higher Education.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
24-Dec-15 Class Structure. 2 Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
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.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
CMSC 202 Java Primer 1. July 24, 2007 Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
3-July-2002cse142-D2-Methods © 2002 University of Washington1 Methods CSE 142, Summer 2002 Computer Programming 1
Java Programming Language Lecture27- An Introduction.
3.1 Objects. Data type. Set of values and operations on those values. Primitive/Built-In types. n Usually single values n Can build arrays but they can.
Functions + Overloading + Scope
Topic: Classes and Objects
3 Introduction to Classes and Objects.
String class.
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Methods Chapter 4: Methods Asserting Java ©Rick Mercer.
Lecture Note Set 1 Thursday 12-May-05
IFS410 Advanced Analysis and Design
Defining Classes and Methods
Presentation transcript:

2-1 Types of data  Java has two types of data  primitive types that store one value char boolean byte int long float double  reference types to store many values with one name  any of the hundreds of classes that come with Java String BufferedReader URLAddress JButton String BufferedReader URLAddress JButton  any domain types for specific applications BankAccount Student Jukebox CD BankAccount Student Jukebox CD  Classes that provide utility in a wide variety of applications Scanner String arrays JFrame Scanner String arrays JFrame

2-2 What's the difference  Primitive type variables that store the actual value of the variable int n = 0; // n stores 0 int n = 0; // n stores 0  Reference type variables store a special value for finding the object at runtime  the object is a bunch of bits on the heap (special part of memory that gets allocated at runtime)

2-3 Methods  Abstraction:  allows programmer to concentrate on essentials  how does a String's substring?  How does System.out.println send output to the console?  how does a JButton know it's been clicked?  What algorithm does the Math.sqrt method use?  However, programmers often create new methods  We will need to work out the details and know who to write the methods

2-4 Methods   A Java class typically contains many methods, each of which has a method heading   Method headings specify the number and types of arguments required  There are two major components to a method:  the method heading   the block (a set of curly braces with the code that completes the method's responsibility)

2-5 Method Headings  Sun uses method headings (and other things) to document methods so we know how to use them public String substring( int beginIndex, int endIndex ) Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex-1. Thus the length of the substring is endIndex-beginIndex. Examples: "hamburger".substring(4, 8) returns "urge" "smiles".substring(1, 5) returns "mile" Parameters: beginIndex - the beginning index, inclusive. endIndex - the ending index, exclusive. Returns: the specified substring.

2-6 What method headings tell us  Method headings provide the information needed to use it they show us how to send a message to instances of the class public String substring(int beginIndex, int endIndex) public String substring(int beginIndex, int endIndex)  What does the method evaluate to? String  What is the method name? substring  How many arguments are required? 2  What type of arguments are required? int  Example message String fullName = "Murphy, John"; String fullName = "Murphy, John"; String lastName = fullName.substring( 0, 6 ); String lastName = fullName.substring( 0, 6 );

2-7 Parameters = Arguments  The substring method requires two arguments in order to specify the portion of the string to return.  When the message is sent, the argument 0 is assigned to the parameter named beginIndex and the argument 6 to the parameter name endIndex fullName.substring( 0, 6 ); fullName.substring( 0, 6 ); public String substring( int beginIndex, int endIndex ) public String substring( int beginIndex, int endIndex ) Implementation of the method is not shown here Implementation of the method is not shown here

2-8 Arguments / Parameters  When a message is sent  The first argument is assigned to the first parameter,  second argument gets assigned to the second parameter,...  If you do not supply the correct number and type of arguments, you get compiletime errors  Examples of errors fullName.substring( "wrong type" ); fullName.substring( "wrong type" ); fullName.substring( 0, 6, fullName.length() ); fullName.substring( 0, 6, fullName.length() ); fullName.substring( ); fullName.substring( ); fullName.substring( 0.0, 6.0 ); fullName.substring( 0.0, 6.0 );

2-9 Method Headings  General form of a Java method heading public return-type method-name ( parameter-1, parameter-2, parameter-n,... ) parameter-2, parameter-n,... )  public says a method is known where objects are constructed  return-type may be any primitive type, any class, or void  A void method returns nothing  therefore, a void method can not be assigned to anything  also, you can not place a call to a void method in println  parameters, which are optional, specify the number and type of arguments required in the message

2-10 Parameters  General form of a parameter between ( and ) in method headings class-name identifier class-name identifier-or- primitive-type identifier primitive-type identifier  Example method headings: public int length( ) // class String public int length( ) // class String public int equals( String anotherString ) // class String public int equals( String anotherString ) // class String public void withdraw( double amount ) // class BankAccount public void withdraw( double amount ) // class BankAccount public double readDouble( ) // class TextReader public double readDouble( ) // class TextReader public void move( int spaces ) // class Grid public void move( int spaces ) // class Grid

2-11 The Method Body  The method body is Java code enclosed within a block { }  Curly braces contain the same things you saw in main  variable declarations and initializations: int creditOne = 0;  input and output messages: readInt, System.out.println  construct objects: Grid g= new Grid(8,6,1,1,Grid.EAST)  messages: g.turnLeft()  Method bodies have access to the state of the object  The "state" of an object is implemented as variables declared inside the class rather than a method body.