Top Banner
Classes, Types and Classes, Types and Objects Objects Data Structures Data Structures Chapter 1 Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035 Java Programming Basics ECE, UPRM
39

Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

Dec 21, 2015

Download

Documents

Juliana Peters
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

Classes, Types and ObjectsClasses, Types and Objects

Data StructuresData StructuresChapter 1Chapter 1

Prof. Vidya ManianDept. of Electrical and Comptuer Engineering

ICOM 4035 Java Programming BasicsECE, UPRM

Page 2: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

Overview

• Objects, classes• expressions• Control flow• Arrays

ICOM 4035 ECE, UPRM 2

Page 3: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

Objects

• Objects –store data and provide methods for accessing and modifying data

• An object is an instance (member) of a class• Class – specifies type of objects, operations

that it performs• Data of objects are stored as instance

variables (fields) (of types e.g. integers, floating point numbers, Booleans)

ICOM 4035 ECE, UPRM 3

Page 4: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

Operations

• Methods :constructors, procedures, and functions

• Example: class declaration

ICOM 4035 ECE, UPRM 4

Page 5: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

• Counter class is public-other class can create and use a counter object

• Instance variable –count• Constructor method initializes it to 0• Accessor method, get Count• Update methods, incrementCount,

decrementCount

ICOM 4035 ECE, UPRM 5

Page 6: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

Class modifiers

• Abstract-empty methods with abstract keyword• Final – cannot have subclasses• Public – can be instantiated by anything that

imports the class• Base types (primitve types (boolean, char, byte,

short, int, long, float, double)• Int x, x=14 or x=195• Float, y=3.1415

ICOM 4035 ECE, UPRM 6

Page 7: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

Example of base types

ICOM 4035 ECE, UPRM 7

Page 8: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

• Comments: /** **/ or //• ‘new’ creates a new object and returns

reference to that object

ICOM 4035 ECE, UPRM 8

Page 9: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

new operator

• A new object is dynamically allocated in memory, initialized to default values ‘null’

• Constructor is called• The new operator returns a reference (a

memory address) to the newly created object

ICOM 4035 ECE, UPRM 9

Page 10: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

Number classes

• Byte n.byteValue()• Shortn.shortValue()• Integer n.intValue()• Long n.longValue()• Float n.floatValue()• Double n.doubleValue()

ICOM 4035 ECE, UPRM 10

Page 11: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

String objects

• String is a sequence of characters that comes form some alphabet (set of possible characters)

• P = “barney the bear”• Length 15• Concatenation• String s = “kilo” + “meters”

ICOM 4035 ECE, UPRM 11

Page 12: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

Object reference

• Reference variable is a pointer to some object

ICOM 4035 ECE, UPRM 12

Page 13: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

Dot operator• Can have many references to the same object• Dot operator”.”-access methods and instance variables

associated with an object• Examples of signatures (methods name with

parameters)• oven.cookDinner();• oven.cookDinner(food);• oven.cookDinner(food,seasoning);• Note: Java doesn not allow 2 methods with the same

signature to return different types

ICOM 4035 ECE, UPRM 13

Page 14: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

Instance Variables• Instance variables must have a type (int, float,

double) or a reference type : class such as String, an array

• A referrence variable v, points to object o, we can access any of the instance variables fo o that the access rules allow

• Public instance variables are accessible by everyone• Dot operator can get and set the value of any

instance variable (v.i)

ICOM 4035 ECE, UPRM 14

Page 15: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

• gnome.name =“professor smith”;• gnome.age=35;• gnome –reference to a Gnome object, with public

instance variables name and age• Variable modifiers• Public: anyone can access• Protected: only methods of the same package or of

its subclasses can access protected instance variables• Private: only methods of the same class can access it

ICOM 4035 ECE, UPRM 15

Page 16: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

• Static: variables are used to store “global” information about a class

• Final: a final instance variable is one that must be assigned an initial value, and can never be assigned a new value after that (MAX_HEIGHT)

ICOM 4035 ECE, UPRM 16

Page 17: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

ICOM 4035 ECE, UPRM 17

Page 18: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

• What are the base types ?• What is variable gnomeBuddy ?• What is variable name ?• Note: Constant values associated with a class

should be declared to be both static and final

ICOM 4035 ECE, UPRM 18

Page 19: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

Enum Types

• Take on values from a specified set of names• Modifier enum name {value_name0,

value_name1,….};• Modifer – black, public, protected or private• Name – any legal Java identifier• public enum Day

{MON,TUE,WERD,THU,FRI,SAT,SUN}

ICOM 4035 ECE, UPRM 19

Page 20: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

• Output:• Initially d is MON• Then it is WED• I say d and t are the same: true

ICOM 4035 ECE, UPRM 20

Page 21: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

Methods

• Similar to functions and procedures• Method has 2 parts: body and signaturemodifiers type name(type0 parameter0, …typen-1 parametern-1) {// method body…}

• type is the return type of the method, name – name of the method

• When a method of a class is called, it is invoked on a specific instance of the class and can change the state of the object

ICOM 4035 ECE, UPRM 21

Page 22: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

• Similar to instance variables, Method modifiers can be public, protected, private or abstract

• Note: All parameters are passed by value, changing the internal reference inside a method will not change the reference that was passed in

ICOM 4035 ECE, UPRM 22

Page 23: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

Constructors• A constructor is a special kind of method that

is used to initialize newly created objects• Note: name of constructor, name, must be

same as the name of the class it constructs• Return type not specified for constructor

ICOM 4035 ECE, UPRM 23

Page 24: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

• Constructor definition and invocation• Return statements are not allowed in a constructor

body• A new instance of this class is created and its

constructor is used to initialize its isntance variables• Must be called using new operator• Fish myFish = new Fish(y, “Wally”);• Classes that define Stand – alone Java program has

the main method

ICOM 4035 ECE, UPRM 24

Page 25: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

• Java Aquarium //system looks for compiled version of Aquarium class and invokes the special main method in that class

ICOM 4035 ECE, UPRM 25

Page 26: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

Statement blocks and local variables

• Two ways of declaring local variables• type name;• type name = initial_value;

ICOM 4035 ECE, UPRM 26

Page 27: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

Expressions• Expressions involve the use of literals, variables and

operators• Variables and constants are used expressions to define

new values/modify variables• Literal: is any “constant” value

– Null object reference– Boolean: true and false– Integer: type int, (32-bit integer), long iteger: 176L or -52l (64-

bit integer)– Floating point: default for floating point numbers is double,

3.14E2 or .19e10

ICOM 4035 ECE, UPRM 27

Page 28: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

– Character, ‘a’, ‘?’ are character constants– Special characters

– String literal: sequence of characters “have a nice day”

ICOM 4035 ECE, UPRM 28

Page 29: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

Operators• Assignment operator : variable=expression• i = j = 25;• Arithmetic operators: +, -, *, /, %• Increment and decrement operators

– int i=8; – int j=i++; – int k=++i; – int m=i--;– int n = 9+i++;

ICOM 4035 ECE, UPRM 29

Page 30: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

Logical operators• < , <=, ==, !=, >=, >• Boolean: ! Not• && conditional and, || conditional or• Bitwise operators: ~ bitwise complement, &

bitwise and, | bitwise or, ^ bitwise exclusive or, <<, >>, >>>

• Operational assignment operators: variable op = expression

• Variable = variable op expression

ICOM 4035 ECE, UPRM 30

Page 31: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

Arrays

ICOM 4035 ECE, UPRM 31

Page 32: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

ICOM 4035 ECE, UPRM 32

Page 33: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

Declaring arrays• Element_type[] array_name = {init_val_0,

init_value_1,.., init_val_N-1}• int[] primes = {2,3,5,7,11}• element_type[] array_name;• new element_type[length]

ICOM 4035 ECE, UPRM 33

Page 34: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

Arrays are objects

ICOM 4035 ECE, UPRM 34

Page 35: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

Cloning an array

ICOM 4035 ECE, UPRM 35

Page 36: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

// Example 1 // --------- // First declare//reference and then construct it. int[] ExampleArray1; ExampleArray1 = new int[24];

// Example 2 // --------- // This can be considered//the short form for declaring and construction.

int[] ExampleArray2 = new int[24]; ICOM 4035 ECE, UPRM 36

Page 37: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

// Example 3 // --------- // Construct and assign//an array using a single command. String[] ExampleArray3 = { "Sun Solaris" , "HP-

UX" , "Linux" , "MS Windows" , "Macintosh" }; int[] array = null;int[] arr = new int[] {1,2,3}; int[][] twoDimArray = { {1,2,3}, {4,5,6}, {7,8,9} }; int[][] myArray = new int[5][]; ICOM 4035 ECE, UPRM 37

Page 38: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

Testing 2D array

ICOM 4035 ECE, UPRM 38

class TestTwoDimArrays { // initialize # of rows static int [][] myArray = new int[3][]; public static void main(String[] args) { myArray[0] = new int[3]; // initialize # of cols myArray[1] = new int[4]; // in each row myArray[2] = new int[5]; for(int i=0; i<3; i++) // fill and print the array fillArray(i, i+3); System.out.println(); } // end main() private static void fillArray(int row, int col) { for( int i=0; i<col; i++) myArray[row][i] = i; for( int i=0; i<col; i++) System.out.print(myArray[row][i]); System.out.println(); } }

Page 39: Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,

Exercises in group

• Suppose that we create an array A of GameEntry objects, with integer scores field, we cloe A and store the result in an array B. If we set A[4].score =550, what is the score value of GameEntry object referenced by B[4]?

• Write a Java method that takes an array of int values and determines if all the numbers are different from each other.

ICOM 4035 ECE, UPRM 39