classes/objects. creating objects much like creating variables of primitive types –string name;...

22
Classes/Objects

Post on 20-Dec-2015

229 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values

Classes/Objects

Page 2: Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values

Creating Objects

• Much like creating variables of primitive types– String name;– type name;

• Object variables hold references, not values– can be initialized to null

• Use new to instantiate new objects/initialize object variables– String name = new String(“Mickey”);– invokes Constructor of String class

Page 3: Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values

Example

int num = 5;

String name = new String(“Mickey”);

String name2 = “Minnie”; //String shortcut

5num

name Mickey

name2 Minnie

Page 4: Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values

Dot Operator

• Use dot operator to access methods of an

object

– name.length()

Page 5: Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values

Aliases

• Two object variables can point to the same

object

• With primitive types, a copy is made

int a = 5;

int b = 12;

a = b;

12a

12b

Page 6: Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values

Aliases

String name = “Mickey”;

String name2 = “Mickey”;

name Mickey

name2 Mickey

Page 7: Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values

Aliases

name = name2;

name

name2 Mickey

Page 8: Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values

Strings

• Strings are immutable

• String methods

– char charAt(int index)

– String replace(char oldChar, char newChar)

Page 9: Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values

Strings

String s = “Hello”

String t = s.replace('l', 'p');

s Hello

t Heppo

Page 10: Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values

Packages

• Classes in the standard Java library are

grouped into packages

– java.util

– java.math

– java .text

• Using the API

Page 11: Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values

import

• To use a class from the library you must

either use the fully-qualified name or import

the classjava.util.Scanner s = new java.util.Scanner(System.in)

import java.util.Scanner;

import java.util.*; //import all classes in java.util.

Page 12: Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values

Exercises

• Complete PP 3.1

– Write an application that prompts for and reads

the user's first and last name (separately).

Then, print a string composed of the first letter

of the user's first name, followed by (no more

than) the first five characters of the user's last

name, followed by a random number in the

range 10 to 99.

• Hint: You will need to use java.util.Random

Page 13: Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values

Java Classes

• Contain data and methods

• Methods enable user to access and modify

data

Page 14: Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values

Encapsulation

• Data should be hidden from the user

– Access to/modification of data is controlled by

methods

• Modifiers

– Enable programmer to specify which data and

methods are private and which are public

• private are accessible within the class

• public are accessible from anywhere

Page 15: Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values

Name.javapublic class Name {

private String first;

private String last;

//Constructor

public Name(String thefirst, String thelast) {

first = thefirst;

last = thelast;

}

public void printName() {

System.out.println("First: " + first + " Last: " + last);

}

}

Page 16: Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values

Driver Classes

• The driver typically contains the main

method

– this is the starting point for the program

• The driver creates instances of other

classes

Page 17: Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values

NameTest.java

public class NameTest {

public static void main(String[] args) {

Name n = new Name("Sami", "Rollins");

n.printName();

}

}

Page 18: Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values

Methods

public void printName() {

...

}

• modifier return_type name(type name, ...)

• You must specify the return type and the

type of each parameter

Page 19: Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values

More Method Headers

• For each data member, consider whether you

should create setter and getter methods

public String getFirstName()

public void setFirstName(String newname)

public String getLastName()

public void setLastName(String newname)

Page 20: Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values

Constructors• Called when a new object is created

• Like a method with no return type

• Should initialize all relevant data

• Should take as input initial values for any variables that do

not have a logical default value

• A default constructor takes no parameters

public Name(String thefirst, String thelast) {

first = thefirst;

last = thelast;

}

Page 21: Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values

Scope

public Name(String first, String last) {

this.first = first;

this.last = last;

}

• this provide access to class variable

• first/last are local to the constructor

Page 22: Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values

Exercises

• Complete exercise PP4.4

– Design and implement a class called Book that

contains instance data for the title, author, publisher,

and copyright date. Define the Book constructor to

accept and initialize this data. Include setter and getter

methods for all instance data. Include a toString

method that returns a nicely formatted , multi-line

description of the book. Create a driver class called

BookShelf, whose main method instantiates and

updates several Book objects.