Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Warm-Up  What is a class in Java?  What is an example of a class we’ve used in Java so far this semester?"— Presentation transcript:

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?

2 Classes and Objects Pre-AP Computer Science, Cycle 6

3 Classes  Collection of related information/methods  Define objects  Objects are created from classes

4 Example: class String  Includes a collection of methods related to working with Strings  length()  charAt()  indexOf()  compareTo()  toUpper()

5 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

6 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

7 Example: the Sims

8 Example: class Square  Variables  side length  Methods  findArea()  newLength()

9 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();

10 Example: Dog Dog Lillipup = new Dog(); Lillipup.breed = “mutt”; Lillipup.weight = 42; Lillipup.run(); Lillipup.bark();

11 Example: the Sims  Sim BellaGoth = new Sim();  BellaGoth.hairColor = “black”;  BellaGoth.gender = “female”;  BellaGoth.greet();  BellaGoth.chat();

12 Example: Square Square mySquare = new Square(); mySquare.newLength(25); double area = mySquare.findArea(); System.out.print(“Area: “ + area);

13 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

14 Writing Custom Classes Pre-AP Computer Science, Cycle 6

15 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

16 Creating a Class: Template class Name { instanceVariables; constructor() {} constructor(parameters) {} returnType method1() {} returnType method2() {} }

17 Example: Class Square class Square { double length; Square() { length = 1.0; } Square(double newLength) { length = newLength; } double area() { return length*length; }

18 Instance Variables class 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; }

19 Constructors class 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

20 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

21 Constructors with Parameters class 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

22 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

23 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

24 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); } }

25 Class Methods class 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

26 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; }

27 Warm-up: Friday, May 9  What is a constructor?  What are constructors used for? When you are finished, TURN IN YOUR WARMUP SHEET

28 Using Classes and Objects Pre-AP Computer Science, Cycle 6

29 Review: Class Square class Square { double length; Square() { length = 1.0; } Square(double newLength) { length = newLength; } double area() { return length*length; }

30 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

31 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);

32 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();

33 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

34 Using Instance Variables if ( sq1.length < 4) System.out.println(“Small square”); else System.out.println(“Big square”);


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

Similar presentations


Ads by Google