Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.

Slides:



Advertisements
Similar presentations
The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
Advertisements

Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Pointers Prasun Dewan Comp 114.
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.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
1 More on Classes Overview l Overloading l The this keyword l The toString method l The equals method.
Understanding class definitions Looking inside classes.
Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects Real and Java COMP.
UML Basics & Access Modifier
Intro to OOP with Java, C. Thomas Wu
Week 3 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
© A+ Computer Science - Inheritance © A+ Computer Science - Lab 20.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Introducing Objects. Structure  Objects have two parts: Instance Variables (attributes, adjectives) Instance Variables (attributes, adjectives) private.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
10-Nov-15 Java Object Oriented Programming What is it?
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
CSC 142 Computer Science II Zhen Jiang West Chester University
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Topics Instance variables, set and get methods Encapsulation
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
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.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java: Base Types All information has a type or class designation
Objects as a programming concept
Java: Base Types All information has a type or class designation
OOP Powerpoint slides from A+ Computer Science
Software Development Java Classes and Methods
Chapter 8 Classes and Objects
Creating Your OwnClasses
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
CS 302 Week 11 Jim Williams, PhD.
CS 106A, Lecture 22 More Classes
Chapter 4: Writing classes
Lesson A4 – Object Behavior
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Classes, Encapsulation, Methods and Constructors (Continued)
Implementing Non-Static Features
Unit-2 Objects and Classes
© A+ Computer Science - OOP © A+ Computer Science -
Today’s topics UML Diagramming review of terms
© A+ Computer Science - Classes And Objects © A+ Computer Science -
© A+ Computer Science - OOP Pieces © A+ Computer Science -
Handout-4b More on Classes
CIS 199 Final Review.
Dr. R Z Khan Handout-3 Classes
A+ Computer Science PARAMETERS
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course

public class Rectangle() { private int recHeight; private int recWidth; ………………. } Instance variables are used to store information for a given object. Each instance of an object has its own copy of each instance variable.

public Rectangle() { recHeight = 0; recWidth = 0; } Constructors are similar to methods. Constructors set the properties (instance variables) of an object to an initial state.

public Rectangle(int ht, int width) { recHeight = ht; recWidth = width; } Constructors are similar to methods. Constructors set the properties (instance variables) of an object to an initial state.

public void setDimensions(int h, int w) { recHeight = h; recWidth = w; } Modifier methods are methods that change the properties of an object. Modifier methods are sometimes referred to as Mutator methods.

public void setHeight(int h) { recHeight = h; } public void setWidth(int w) { recWidth = w; } Modifier methods are methods that change the properties of an object. Modifier methods are sometimes referred to as Mutator methods.

public int getHeight() { return recHeight; } public int getWidth() { return recWidth; } Accessor methods are methods that retrieve or grant access to the properties of an object, but do not make any changes.

public String toString() { return “Rectangle with height of “ + getHeight() + " and width of " + getWidth(); } The toString method is a special accessor method. It returns the string representation of the object. It does not modify the values of the instance variables.

All data members (instance variables) should have private access. The public constructors, accessor methods, and modifier methods should be used to manipulate and retrieve the data. All data is tucked away nicely inside the class.

The public methods give you access to an object’s private data / properties. getIt( ) setIt( ) toString( ) Class/ Object private data / instance variables / properties

Create a Movie class to store information about a movie and a MovieBox class that represents a kiosk to buy movies from.

In Java, all classes are automatically sub classes of the Object class. This adds greater flexibility when writing programs in Java. Object StringMovieMonster

public class Monster { ………… } The class automatically extends Object. You don’t have to explicitly extend it. extends Object

Because all classes are sub classes of Object, all classes start with the same methods..equals( ).toString( )..... and more

The equals() method is used to see if two objects have the same contents. String one = "comp"; String two = "sci"; if (one.equals(two)) System.out.print(“Strings are equal“);

public class Monster { private int height; private double weight; private String name; // constructor method and other methods not shown public boolean equals(Object obj) { Monster other = (Monster) obj; if (getHeight() == other.getHeight() ) return true; return false; } //test code in the main Monster one = new Monster(33, 200.0, “Stu”); Monster two = new Monster(12, 150.0, “Alice”); if (one.equals(two)) System.out.print(“heights are equal”); else System.out.print(“heights not equal”); OUTPUT heights not equal

The toString() method is used to return a version of your object in String form. The most common use of this method is when you are printing the object. System.out.print(Monster); Since the print() method requires a String data type, Java automatically calls the toString() method before printing the object.

public class Monster { private int height; private double weight; private String name; // constructor method and other methods not shown public String toString() { return name + “ is “ + height + “ ft tall and weighs “ + weight + “ lbs”; } // test code in the main Monster one = new Monster(10, 200.0, ”Sam”); System.out.println(one); OUTPUT Sam is 10 ft tall and weighs lbs

All members with public access can be accessed inside and outside of the class where they are defined.

All members with private access can only be accessed inside of the class where they are defined.

If you do not provide any constructors, Java will provide a default constructor. This allows you to instantiate the object, but its properties (instance variables) will not be initialized.

Overloading occurs when you have more than one method or constructor with the same name in the same class. Each method or constructor must have a different parameter list. The number of parameters and the data types of the parameters matter

class Monster { private int height; private double weight; private String name; public Monster(){ height=0; weight=0.0; name=“no name”; } public Monster(int ht){ height=ht; weight=0.0; name=“no name”; } public Monster(double wt){ height=0; weight=wt; name=“no name”; } public Monster(int ht, double wt, String nm){ height=ht; weight=wt; name=nm; }

class Monster { private int height; private double weight; private String name; //……constructors not shown public void setProperty(int ht) { height = ht; } public void setProperty(double wt) { weight = wt; } public void setProperty(String nm) { name = nm; }

getX( ) - accessors setX( ) - modifiers Monster() - constructors Monster toString( ) - accessor

public class Monster { //instance vars / data fields public Monster(){ //code } public void setX( params ){ //code } public int getX(){ //code } public String toString() { //code }

class Monster{ // instance variables public Monster(){ code } public Monster(int ht) { code } public Monster(int ht, int wt) { code } public Monster(int ht, int wt, String n) { code } //more methods }

Monster m = new Monster(); MONSTER Properties: height = 0, weight = 0, name = “no name” methods m m is a reference variable that refers to a Monster object.

Monster m = new Monster(23); m is a reference variable that refers to a Monster object. MONSTER Properties height = 23, weight = 0, name = “no name” methods m 0x234

Monster m = new Monster(23, 45); m is a reference variable that refers to a Monster object. MONSTER Properties height = 23, weight = 45, name = “no name” methods m 0x239

Monster m = new Monster(23, 45, “Gus”); m is a reference variable that refers to a Monster object. MONSTER Properties height = 23, weight = 45, name = “Gus” methods m 0x2B3