© A+ Computer Science - www.apluscompsci.com. public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

JAVA Revision Lecture Electronic Voting System Marina De Vos.
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.
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.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
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.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.
© A+ Computer Science - Inheritance © A+ Computer Science - Lab 20.
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!
Introducing Objects. Structure  Objects have two parts: Instance Variables (attributes, adjectives) Instance Variables (attributes, adjectives) private.
GCOC – A.P. Computer Science A. College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose,
ICS 201 Introduction to Computer Science
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.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
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.
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
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.
© 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.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
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.
Objects Real and Java COMP T1 3
Objects as a programming concept
OOP Powerpoint slides from A+ Computer Science
Haidong Xue Summer 2011, at GSU
Software Development Java Classes and Methods
Intro To Classes Review
Chapter 8 Classes and Objects
CS 302 Week 11 Jim Williams, PhD.
CSC240 Computer Science III
Multiple Choice -answer the easiest question 1st
Lesson A4 – Object Behavior
CSC 113: Computer programming II
Creating Objects in a Few Simple Steps
Inheritance, Polymorphism, and Interfaces. Oh My
Classes, Encapsulation, Methods and Constructors (Continued)
More on Classes and Objects
© A+ Computer Science - OOP © A+ Computer Science -
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
© A+ Computer Science - Classes And Objects © A+ Computer Science -
© A+ Computer Science - OOP Pieces © A+ Computer Science -
See requirements for practice program on next slide.
Handout-4b More on Classes
Instance Method – CSC142 Computer Science II
CIS 199 Final Review.
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
A+ Computer Science PARAMETERS
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
A+ Computer Science AP Review 2019 AP CS A EXAM
ITE “A” GROUP 2 ENCAPSULATION.
Presentation transcript:

© A+ Computer Science -

public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties of an object to an initial state.

© A+ Computer Science - public Triangle(int a, int b, int c) { setSides(a,b,c); } Constructors are similar to methods. Constructors set the properties of an object to an initial state.

© A+ Computer Science - public void setSides(int a, int b, int c) { setSideA(a); //more of the same } Modifier methods are methods that change the properties of an object.

© A+ Computer Science - public void setSideA(int a) { sideA=a; } Modifier methods are methods that change the properties of an object.

© A+ Computer Science - public int getSideA() { return sideA; } Accessor methods are methods that retrieve or grant access to the properties of an object, but do not make any changes.

© A+ Computer Science - public String toString() { return "" + getSideA() + //more get calls } Accessor methods are methods that retrieve or grant access to the properties of an object, but do not make any changes.

© A+ Computer Science - All data members should have private access. The public constructors, accessor methods, and modifier methods should be used to manipulate the data. All data is tucked away nicely inside the class.

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

© A+ Computer Science -

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

© A+ Computer Science - public class Monster extends Object { public void print( ) { out.println("Monster"); }

© A+ Computer Science - Because all classes are sub classes of Object, all classes start with the same methods..equals( ).toString( )..... and more

© A+ Computer Science -

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

© A+ Computer Science - All members with private access can only be accessed inside of the class where they are defined.

© A+ Computer Science -

If you do not provide any constructors, Java will provide a default constructor.

© A+ Computer Science -

The equals() method is used to see if two objects have the same contents. String one = "comp"; String two = "sci"; out.println(one.equals(two));

© A+ Computer Science - class Monster { private int height; //methods public boolean equals(Object obj){ Monster other = (Monster)obj; if(getHeight()==other.getHeight()) return true; return false; } //methods } //test code in the main Monster one = new Monster(33); Monster two = new Monster(12); out.println(one.equals(two)); OUTPUT false

© A+ Computer Science -

Overloading occurs when you have more than one method or constructor with the same name. Each method or constructor must have a different parameter list. # of parameters && data types matter

© A+ Computer Science - class Monster{ private int height;//default assinged to 0 private double weight;//default assinged to 0 public Monster(){ height=0; weight=0.0; } public Monster(int ht){ height=ht; weight=0.0; } public Monster(double wt){ height=0; weight=wt; } public Monster(int ht, double wt){ height=ht; weight=wt; }

© A+ Computer Science -

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

© A+ Computer Science - class Monster { //instance vars / data fields public Monster(){ //code } public void setX( params ){ //code } public int getX(){ //code } public String toString() { //code }

© A+ Computer Science - 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, int age) { code } //more methods }

© A+ Computer Science - Monster m = new Monster(); MONSTER Properties – height – 0 weight - 0 age - 0 methods m m is a reference variable that refers to a Monster object.

© A+ Computer Science - Monster m = new Monster(23); m is a reference variable that refers to a Monster object. MONSTER Properties – height – 23 weight – 0 age - 0 methods m 0x234

© A+ Computer Science - Monster m = new Monster(23, 45); m is a reference variable that refers to a Monster object. MONSTER Properties – height – 23 weight – 45 age - 0 methods m 0x239

© A+ Computer Science - Monster m = new Monster(23, 45, 11); m is a reference variable that refers to a Monster object. MONSTER Properties – height – 23 weight – 45 age - 11 methods m 0x2B3

© A+ Computer Science -