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

Slides:



Advertisements
Similar presentations
Introduction to Programming with Java, for Beginners dynamic vs. static static variables and methods.
Advertisements

Building Java Programs Interactive Programs w/ Scanner What is a class? What is an object? What is the difference between primitive and object variables?
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
CS 106 Introduction to Computer Science I 03 / 19 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
Constructors A constructor is a method that creates an object in Java. It does not return anything and it is not void. It’s only purpose is to create an.
MIT AITI 2003 Lecture 7 Class and Object - Part I.
Warm-Up: April 21 Write a loop (type of your choosing) that prints every 3 rd number between 10 and 50.
Java Unit 9: Arrays Declaring and Processing Arrays.
COMP More About Classes Yi Hong May 22, 2015.
Writing Classes (Chapter 4)
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Instructions To use this template: –for each slide write the correct answer on the orange bar first –choose which option (A,B,C or D) and make sure you.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Introduction to Programming with Java. Overview What are the tools we are using – What is Java? This is the language that you use to write your program.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CS 106 Introduction to Computer Science I 03 / 19 / 2007 Instructor: Michael Eckmann.
Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
IB Computer Science Unit 1 – Introduction to Java Variables.
ROUND 1 Name a method associated with class String 1.) 15 compareTo() 26 indexOf() 34 length() 2.) 3.) 4.) 3 toUpper() 7 substring() 11 charAt() 5.)
CS1201: Programming Language 2 Classes and objects By: Nouf Aljaffan Edited by : Nouf Almunyif.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Warm-Up: Monday, March 24 List as many commands in Java as you can remember (at least 10)
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress.
CS 106 Introduction to Computer Science I 10 / 29 / 2007 Instructor: Michael Eckmann.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
Software development For large and complex software use divide and conquer rule. Software design methods: Structured design Object-Oriented design.
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
Classes - Intermediate
Topics Instance variables, set and get methods Encapsulation
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
What About Alice? In Alice, we have seen that we have:  Objects (skater, snowman, snowwoman, camera)  Methods (move, turn, roll, move toward)  Properties.
 An object is basically anything in the world that can be created and manipulated by a program.  Examples: dog, school, person, password, bank account,
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
3-July-2002cse142-D2-Methods © 2002 University of Washington1 Methods CSE 142, Summer 2002 Computer Programming 1
Objects as a programming concept
Objects as a programming concept
Objects as a programming concept
Intro To Classes Review
Selenium WebDriver Web Test Tool Training
JavaScript: Functions.
Classes A class is a blueprint of an object
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Defining Classes and Methods
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Chapter 4 Writing Classes.
Which best describes the relationship between classes and objects?
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Object Oriented Programming in java
February , 2009 CSE 113 B.
Day 11 The Last Week!.
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

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()  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 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 { instanceVariables; constructor() {} constructor(parameters) {} returnType method1() {} returnType method2() {} }

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

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

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

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 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

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 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

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 Objects Pre-AP Computer Science, Cycle 6

Review: Class Square class 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”);