Date post: | 14-Dec-2015 |
Category: |
Documents |
Upload: | saige-stallman |
View: | 220 times |
Download: | 2 times |
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
Classes
Collection of related information/methods
Define objects
Objects are created from classes
Example: class String
Includes a collection of methods related to working with Strings length() charAt() indexOf() compareTo() toUpper()
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
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
Example: the Sims
Example: class Square
Variables side length
Methods findArea() newLength()
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();
Example: Dog
Dog Lillipup = new Dog();
Lillipup.breed = “mutt”;
Lillipup.weight = 42;
Lillipup.run();
Lillipup.bark();
Example: the Sims
Sim BellaGoth = new Sim();
BellaGoth.hairColor = “black”;
BellaGoth.gender = “female”;
BellaGoth.greet();
BellaGoth.chat();
Example: Square
Square mySquare = new Square();
mySquare.newLength(25);
double area = mySquare.findArea();
System.out.print(“Area: “ + area);
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
Writing Custom ClassesPre-AP Computer Science, Cycle 6
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
Creating a Class: Template
class Name {
instanceVariables;
constructor() {}
constructor(parameters) {}
returnType method1() {}
returnType method2() {}
}
Example: Class Squareclass Square {
double length;
Square() {
length = 1.0;
}
Square(double newLength) {
length = newLength;
}
double area() {
return length*length;
}
}
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;
}
}
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
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
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
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
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
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); }
}
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
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; }
Warm-up: Friday, May 9
What is a constructor?
What are constructors used for?
When you are finished, TURN IN YOUR WARMUP SHEET
Using Classes and ObjectsPre-AP Computer Science, Cycle 6
Review: Class Squareclass Square {
double length;
Square() {
length = 1.0;
}
Square(double newLength) {
length = newLength;
}
double area() {
return length*length;
}
}
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
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);
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();
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
Using Instance Variables
if (sq1.length < 4)
System.out.println(“Small square”);
else
System.out.println(“Big square”);