Top Banner
Warm-Up What is a class in Java? What is an example of a class we’ve used in Java so far this semester?
34

Warm-Up What is a class in Java? What is an example of a class we’ve used in Java so far this semester?

Dec 14, 2015

Download

Documents

Saige Stallman
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: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Warm-Up

What is a class in Java?

What is an example of a class we’ve used in Java so far this semester?

Page 2: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Classes and ObjectsPre-AP Computer Science, Cycle 6

Page 3: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Classes

Collection of related information/methods

Define objects

Objects are created from classes

Page 4: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Example: class String

Includes a collection of methods related to working with Strings length() charAt() indexOf() compareTo() toUpper()

Page 5: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Example: Class dog

If you were creating a class to define a “dog”, what might you include in the class definition? Think about things that characterize, or

describe, the dog Think about things that define what the

dog can do

Page 6: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Example: Class dog

Descriptors gender, breed, size, weight, color,

markings, eye color, name, owner, etc

Actions walk, run, wag tail, fetch, bark, whine,

eat, poop, lick, play, pant, drool, etc

Page 7: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Example: the Sims

Page 8: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Example: class Square

Variables side length

Methods findArea() newLength()

Page 9: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Objects and Classes

Create objects from classes using the new operator Jeroo bobby = new Jeroo( ); Scanner console = new Scanner(System.in);

Access class methods with the dot operator bobby.hop(); console.nextInt();

Page 10: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Example: Dog

Dog Lillipup = new Dog();

Lillipup.breed = “mutt”;

Lillipup.weight = 42;

Lillipup.run();

Lillipup.bark();

Page 11: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Example: the Sims

Sim BellaGoth = new Sim();

BellaGoth.hairColor = “black”;

BellaGoth.gender = “female”;

BellaGoth.greet();

BellaGoth.chat();

Page 12: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Example: Square

Square mySquare = new Square();

mySquare.newLength(25);

double area = mySquare.findArea();

System.out.print(“Area: “ + area);

Page 13: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Warm-Up: Thursday, May 8

You are writing a class to describe a car. List 2 variables that you might use to describe the car, as well as 3 methods that would describe things the car could do

Page 14: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Writing Custom ClassesPre-AP Computer Science, Cycle 6

Page 15: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Review

Classes are templates for objects

Classes contain instance variables that describe objects (settings)

Classes contain methods that code for actions the object can do

Page 16: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Creating a Class: Template

class Name {

instanceVariables;

constructor() {}

constructor(parameters) {}

returnType method1() {}

returnType method2() {}

}

Page 17: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Example: Class Squareclass Square {

double length;

Square() {

length = 1.0;

}

Square(double newLength) {

length = newLength;

}

double area() {

return length*length;

}

}

Page 18: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Instance Variablesclass Square {

double length; Declaring variables that define

Square() { aspects of our object

length = 1.0;

}

Square(double newLength) {

length = newLength;

}

double area() {

return length*length;

}

}

Page 19: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Constructorsclass Square {

double length;

Square() {

length = 1.0;

}

Square(double newLength) {

length = newLength;

}

double area() {

return length*length;

}

}

Special methods used to create an object

Specifically, this constructor has no parameter, which means it is used for creating an object with DEFAULT settings

MUST ALWAYS BE INCLUDED IN A CLASS DEFINTION

Page 20: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

More on Constructors

Square mySquare = new Square();

The reference to the class name, followed by the parenthesis, indicates usage of a class constructor Special method used to create objects ONLY JOB IS TO SET VALUES TO YOUR

INSTANCE VARIABLES

If the parenthesis are left empty (as with this case), default settings are used for all instance variables

Page 21: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Constructors with Parametersclass Square {

double length;

Square() {

length = 1.0;

}

Square(double newLength) {

length = newLength;

}

double area() {

return length*length;

}

}

Special methods used to create an object

This constructor has a parameter included. It allows the user to set the value of the length whenever a new Square object is declared

Page 22: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

More on Constructors w/ Parameters

Square mySquare = new Square(25);

The blue/bolded piece of code is still a class constructor, but this time, the parenthesis are NOT left empty

Instead, when the object mySquare is created, it will be created using the settings described in the parenthesis Side length of 25 NOT the default settings

Page 23: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

More on Constructors w/ Parameters

Constructor methods must be defined in the class definition for each possible parameter situation the user may choose

Jeroo connection: Jeroo bobby = new Jeroo(); //default settings Jeroo bobby = new Jeroo(5); //with 5 flowers Jeroo bobby = new Jeroo(3, 4); //starting

position Jeroo bobby = new Jeroo(3, 4, 5); //start &

flowers

Page 24: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

class Jeroo {

int row, column, flowers;

Jeroo() {

row=0; column=0; flowers=0; }

Jeroo(int a) {

flowers=a; }

Jeroo(int a, int b) {

row=a; column=b; }

Jeroo(int a, int b, int c) {

row=a; column=b; flowers=c); }

}

Page 25: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Class Methodsclass Square {

double length;

Square() {

length = 1.0;

}

Square(double newLength) {

length = newLength;

}

double area() {

return length*length;

}

}

Class methods do NOT share the same name as the class

This distinguishes them from constructors

Class methods are written by writing the return type, followed by the method name and any required parameters

Page 26: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Examples of Methods for Class Square

double area() {

return length*length;

}

double perimeter() {

return 4*length;

}

void changeLength(double new) { length = new; }

double findPrismVolume(double height) { double base=area(); return base*height; }

Page 27: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Warm-up: Friday, May 9

What is a constructor?

What are constructors used for?

When you are finished, TURN IN YOUR WARMUP SHEET

Page 28: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Using Classes and ObjectsPre-AP Computer Science, Cycle 6

Page 29: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Review: Class Squareclass Square {

double length;

Square() {

length = 1.0;

}

Square(double newLength) {

length = newLength;

}

double area() {

return length*length;

}

}

Page 30: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Using Classes: Object Creation

Objects are created by referencing the class name and constructor, using the new keyword

Square sq1 = new Square( );

Objects MUST be given a unique name

When referencing objects later, you MUST use the object name

Page 31: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Accessing Class Methods

To access class methods, you must first have created an object

Reference the method by writing the name of the object you wish to use, followed by the dot operator, then the method name

Square sq1 = new Square();

sq1.newLength(14);

Page 32: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

More on Class Methods

Class methods that DO NOT return information can be written as a single statement:

sq1.newLength(14);

Class methods that DO return information must be set equal to a storage variable:

double area = sq1.area();

Page 33: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Referencing Instance Variables

To access an object’s instance variables, write the name of the object followed by the name of the instance variable to reference

sq1.length;

Note: NO PARENTHESES

Page 34: Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?

Using Instance Variables

if (sq1.length < 4)

System.out.println(“Small square”);

else

System.out.println(“Big square”);