WarmUp What is a class in Java What
- Slides: 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?
Classes and Objects Pre-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() char. At() index. Of() compare. To() to. Upper()
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 find. Area() new. Length()
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. next. Int();
Example: Dog Lillipup = new Dog(); Lillipup. breed = “mutt”; Lillipup. weight = 42; Lillipup. run(); Lillipup. bark();
Example: the Sims Sim Bella. Goth = new Sim(); Bella. Goth. hair. Color = “black”; Bella. Goth. gender = “female”; Bella. Goth. greet(); Bella. Goth. chat();
Example: Square my. Square = new Square(); my. Square. new. Length(25); double area = my. Square. find. Area(); 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 Classes Pre-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 { instance. Variables; constructor() {} constructor(parameters) return. Type method 1() {} return. Type method 2() {} } {}
Example: Class Square class Square { double length; Square() { length = 1. 0; } Square(double new. Length) { length = new. Length; } double area() { return length*length; } }
Instance Variables class Square { double length; Declaring variables that define Square() { length = 1. 0; } Square(double new. Length) { length = new. Length; } double area() { return length*length; } } aspects of our object
Constructors class Square { double length; Square() { length = 1. 0; } Square(double new. Length) { length = new. Length; } 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 my. Square = 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 Parameters class Square { double length; Square() { length = 1. 0; } Square(double new. Length) { length = new. Length; } 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 my. Square = 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 my. Square 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 Methods class Square { double length; Square() { length = 1. 0; } Square(double new. Length) { length = new. Length; } 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 void change. Length(double new) { length = new; } double area() { return length*length; } double find. Prism. Volume(double height) { double base=area(); return base*height; double perimeter() { } return 4*length; }
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 Objects Pre-AP Computer Science, Cycle 6
Review: Class Square class Square { double length; Square() { length = 1. 0; } Square(double new. Length) { length = new. Length; } double area() { return length*length; } }
Using Classes: Object Creation Objects are created by referencing the class name and constructor, using the new keyword Square sq 1 = 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 sq 1 = new Square(); sq 1. new. Length(14);
More on Class Methods Class methods that DO NOT return information can be written as a single statement: sq 1. new. Length(14); Class methods that DO return information must be set equal to a storage variable: double area = sq 1. 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 sq 1. length; Note: NO PARENTHESES
Using Instance Variables if (sq 1. length < 4) System. out. println(“Small square”); else System. out. println(“Big square”);
- Java warmup
- Warmup ratio
- Warmup 65
- Gmass warmup
- Status vs class
- Pyramid warmup
- Monorhyme
- Multiplying exponents
- Define:warmup
- Pathos story
- Tinman endurance coaching
- 65 mins
- Warmup end
- Import java.lang.*
- Import java.util.*
- Import java.awt.* import java.applet.*
- Import java.util
- Java 讀檔
- Java gcd
- Str2str 使い方
- What is readline in java
- Java import java.util.*
- Java import java.io.*
- Pengertian awt dan swing pada java
- Import java.awt.* import java.awt.event.*
- Java interpreter
- Ejb remote vs local
- Hierarchy of thread class in java
- Math class function in java
- Uml private public protected
- Iterator inner class java
- Diagram example
- Applet class in java
- Factory singleton
- Java runtime code generation