Warm-up: Tuesday, March 11 In programming, what is the difference between an object and a class?

31
Warm-up: Tuesday, March 11 In programming, what is the difference between an object and a class?

Transcript of Warm-up: Tuesday, March 11 In programming, what is the difference between an object and a class?

Page 1: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

Warm-up: Tuesday, March 11

In programming, what is the difference between an object and a class?

Page 2: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

Objects and ClassesPAP Computer ScienceCycle 5

Page 3: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

Review: Output/Input Output

System.out.println( );

Input int number = console.nextInt( ); double decimal = console.nextDouble( ); String word = console.next( );

Page 4: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

Classes Characterize an object

Category of information used to collect and contain properties/methods for related pieces of information

Objects are one instance of that class

Page 5: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

Example Class: Superheroes Class properties

Name, costume, colors, sidekick, main villain, powers, universe (Marvel vs DC)

Class methods Fly, use a super power, talk, rescue a

damsel in distress Object instances

Superman, Spiderman, Wolverine, Flash

Page 6: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

Jeroo Connection Class properties

how many flowers is it holding, what direction is it facing

Class methods hop( ), plant( ), pick( ), toss( ), turn( ),

give( ) Object instances

Jeroo bobby = new Jeroo

Page 7: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

Classes We’ve Seen So Far String

String word = “some text”;

Scanner static Scanner console = new

Scanner(System.in)

Page 8: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

Creating an Object Recall…an object is an instance of a

class

Standard format:Class name = new Class(parameters)

Example:Jeroo bobby = new Jeroo(2);

Page 9: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

Keyword New The keyword new is used when creating

objects

It sets aside memory in the computer for the object

It only has to be used once per object creation, much like declaring a variable

Page 10: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

Using an Object – Dot Operator

To access the properties and methods associated with a class, you first type the name of your object, followed by a dot (.), then put the command you want to run

Jeroo bobby = new Jeroo( ); //create objectbobby.hop( ); //use the object

Page 11: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

Examples

static Scanner console = new Scanner(System.in);

int number = console.nextInt( );

String name = “Spring Break”;int length = name.length( );

float circumference = 2*radius*Math.PI

Page 12: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

Review Classes are a way to bundle the

properties and methods of related information.

Class objects are instances of a class. Use the operator new to create a class

object. In Java, the dot (.) separates the object

name from the property or method name

Page 13: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

Warm-Up: Wednesday, March 12

What is the difference between class properties and class methods?

Page 14: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

Class MathCycle 5Week 4

Page 15: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

Review Classes are a way to bundle the

properties and methods of related information.

Class objects are instances of a class. Use the operator new to create a class

object. In Java, the dot (.) separates the object

name from the property or method name

Page 16: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

Math Operators Basics

Addition (+) Subtraction (-) Multiplication (*) Division (/) Modulus (%)

Other math functions must be accessed using the Math class

Page 17: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

Using class Math The class Math is stored within the

java.lang package

At the beginning of each code, be sure to add the following line:

import static java.lang.Math.*

This allows you to drop the Math. prefix when using Math methods and constants

Page 18: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

Math Constants Math.E = 2.718281828459045

Math.PI = 3.141592653589793

Page 19: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

Math Methods abs(x) – absolute value

abs(-96.5) 95.6

ceil(x) – ceiling operatorceil(54.13) 55

floor(x) – floor operatorfloor(54.13) 54

exp(x) – returns ex

exp(3) e3 20.0855369232

Page 20: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

Math Methods log(x) – natural logarithm (base e) of x

log(2) ln(2) 0.69314718056

log10(x) – base-10 logarithm of xlog10(100) log10(100) 2.0

max(x,y) – maximum of two numbersmax(15, 25) 25

min(x,y) – minimum of two numbersmin(3, 4) 3

Page 21: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

Math Methods pow(x,y) – returns xy

pow(2,3) 23 8

round(x) – rounds x to the nearest whole #round(34.4) 34 round(34.7) 35

sqrt(x) – square root of a numbersqrt(121) √121 11

Page 22: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

import static java.lang.Math.*;public class MathProblems { public static void main(String[] args) {int x = pow(10, 3);System.out.println(“10^3 = “ + x);

int m = max(24, 67);System.out.println(“Max of 24 and 67 = “ + m);

double a = 2*PI*4;System.out.print(“Area of a circle with radius ”);System.out.print(“of 4 = “ + a); }}

Page 23: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

Warm-up: Thursday, March 13 What class are each of these packages

associated with?

a. java.util.*

b. javax.swing.*

c. java.lang.Math.*

Page 24: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

Warm-up: Thursday, March 13 What class are each of these packages

associated with?

a. java.util.*Scanner

b. javax.swing.*JOptionPane (pop-up boxes)

c. java.lang.Math.*Math

Page 25: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

Class StringCycle 5Week 4

Page 26: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

Strings - Review Class used to manipulate character

arrays, or words

Declared like variables String name = “Alexander”;

Can be combined using the + operator String break = “Spring ” + “break”;

Page 27: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

String Methods String.length( ) – returns the length of the

string (how many letters long it is) String word = “dog”; word.length( ) 3

String.toLowerCase( ) – changes all capital letters to lowercase String word = “HAHA”; word.toLowerCase( ) “haha”;

String.toUpperCase( ) – changes all lowercase letters to capital letters String word = “kitty”; word.toUpperCase( ) “KITTY”

Page 28: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

Strings as Lists Strings are essentially a list of letters Strings are indexed just like lists String indices begin counting at 0

String word = “happy”;word[0] = ‘h’word[1] = ‘a’word[2] = ‘p’word[3] = ‘p’word[4] = ‘y’

Page 29: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

More String Methods String.charAt(x) – returns the letter at

position x in the String String word = “happy”; word.charAt(2) ‘p’;

String.replace(x,y) – replaces each instance of character ‘x’ with character ‘y’ String word = “happy”; word.replace(‘p’,’ r’) “harry”

Page 30: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

indexOf( ) indexOf(char) – returns the index, or list

location, of the first instance of letter char String word = “happy”; word.indexOf(‘h’) 0

indexOf(str) – returns the index, or list location, of the first instance of a String str String name = “Alexander”; name.indexOf(“and”) 4

Note: Both of these commands will return -1 if the character or String is not found

Page 31: Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

String.substring( ) String.substring(x, y) – Converts the

characters between list location x of the String (inclusive) and list location y of the String (exclusive) into a new String

String word = “Spring Break”;

word.substring(0,6) “Spring” word.substring(7, 12) “Break” word.substring(3, 9) “ing Br”