Object-Oriented Programming and class Design

Slides:



Advertisements
Similar presentations
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Advertisements

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.
Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors.
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.
 Setter methods ◦ public methods that set the value of instance variables to a value specified by the call to the argument ◦ Setter methods do not violate.
1. 2 Introduction to Methods  Type of Variables  Static variables  Static & Instance Methods  The toString  equals methods  Memory Model  Parameter.
Liang Chapter 6 Variable scope and the keyword this.
Advanced Java and Android Day 1 Object-Oriented Programming in Java Advanced Java and Android -- Day 11.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)
Introduction to Objects A way to create our own types.
1 Objects and Classes. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
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!
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
CSE 1301 Lecture 5 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Objects and Classes Mostafa Abdallah
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
A: A: double “4” A: “34” 4.
Packages. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 2.
Chapter 7 Objects and Classes. OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
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.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Introduction to Programming using Java Day 3 and 4 Java Language Basics Review The “For” loop Subroutines The “String” class.
Chapter 7 Objects and Classes
Lecture 3: Introduction to Object and Classes
Introduction to Programming G50PRO University of Nottingham Unit 9 : Classes and objects Paul Tennent
Elementary Programming
Chapter 2 Elementary Programming
Interface.
TK1114 Computer Programming
CS 302 Week 10 Jim Williams.
Lecture 13 Writing Classes Richard Gesick.
Classes & Objects: Examples
CSE 1030: Implementing Non-Static Features
Chapter 8 Objects and Classes Part 1
Unit-2 Objects and Classes
Chapter 8 Objects and Classes
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)
Objects and Classes Creating Objects and Object Reference Variables
Chapter 9 Objects and Classes
Module 2 - Part 1 Variables, Assignment, and Data Types
Module 3 Selection Structures 4/3/2019 CSE 1321 Module 3.
Methods and Data Passing
JAVA An Introduction to Java OOP Basics
Introduction to Objects & Classes
CSE Module 1 A Programming Primer
Module 3 Selection Structures 4/5/2019 CSE 1321 Module 3.
Chapter 6 Objects and Classes
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Chapter 8 Objects and Classes
Object-Oriented Programming
Object-Oriented Programming and class Design
Chapter 9 Objects and Classes Part 01
OO Programming Concepts
Object-Oriented Programming and class Design
Methods/Functions.
Object-Oriented Design AND CLASS PROPERTIES
Chapter 9 Objects and Classes
Object-Oriented Programming and class Design
Object-Oriented Programming and class Design
Object-Oriented Design AND CLASS PROPERTIES
Chapter 7 Objects and Classes
Day 11 The Last Week!.
Object-Oriented Design AND CLASS PROPERTIES
Presentation transcript:

Object-Oriented Programming and class Design Ps Module 6 Object-Oriented Programming and class Design 4/7/2019 CSE 1321 Module 5

Class Circle Pseudocode BEGIN CREATE Radius ← 1.0 CONSTRUCTOR Circle //called default constructor BEGIN END CONSTRUCTOR CONSTRUCTOR Circle (NewRadius) //called constructor Radius ← NewRadius END CONSTRUCTOR METHOD getArea () //compute and return circle area RETURN (Radius * Radius * 3.14159) END METHOD END CLASS Ps 4/26/2018 CSE 1321 Module 6 5

Class Circle public class Circle { double radius = 1.0; Circle() //default constructor { } Circle(double newRadius) //another constructor { radius = newRadius; } public double getArea() //calculates area { return (radius*radius*3.14159); } } 6 4/26/2018 CSE 1321 Module 6

Ps Driver in Pseudocode CLASS TestCircle BEGIN CREATE circle1, circle2 AS Circle // create two variables circle1 ← NEW circle() // create circle1 object circle2 ← NEW circle(25.0) // create circle2 object PRINT ("Circle 1 area = " + circle1.getArea()) PRINT ("Circle 2 area = " + circle2.getArea()) END TestCircle Outputs: Circle 1 area = 3.14159 Circle 2 area = 1963.4937499999999 Ps 4/26/2018 CSE 1321 Module 6 5

Driver in Java class Test_Circle { public static void main (String args[]) { Circle c1 = new Circle(); System.out.println("The area of the c1 is " + c1.getArea()); Circle c2 = new Circle(25); System.out.println("The area of c2 is " + c2.getArea()); } } 4/26/2018 CSE 1321 Module 6 6

Example CLASS BMW_Z4 BEGIN CONSTRUCTOR BMW_Z4() // constructor #1 BEGIN ModelYear ← 2004 TopUp ← false LicensePlate ← "DEALER" END CONSTRUCTOR CONSTRUCTOR BMW_Z4(parameter: year) // constructor #2 BEGIN ModelYear ← year TopUp ← false LicensePlate ← "DEALER" END CONSTRUCTOR END CLASS Ps 4/7/2019 CSE 1321 Module 6 6

Java - Constructor Example class BMW_Z4 { int modelYear; String licensePlate; boolean topUp; BMW_Z4 () { modelYear = 2004; topUp = false; licensePlate = "DEALER"; } BMW_Z4 (int year) modelYear = year; topUp = false; } 4/7/2019 CSE 1321 Module 6 7

Properties Example Pseudocode CLASS BMW_Z4 BEGIN PRIVATE ModelYear PRIVATE LicensePlate PUBLIC METHOD SetModelYear (Year) BEGIN ModelYear ← Year END PUBLIC METHOD GetModelYear () BEGIN RETURN ModelYear END PUBLIC METHOD SetLicensePlate (value) BEGIN LicensePlate ← value END PUBLIC METHOD GetLicensePlate () BEGIN RETURN LicensePlate END END CLASS Ps 4/26/2018 CSE 1321 Module 6 5

Class BMW_Z4 public class BMW_Z4 { private int ModelYear; private String LicensePlate; private boolean TopUp; public void SetModelYear(int Year) { ModelYear = Year; } public int getModelYear() { return ModelYear; } public void SetLicensePlate(String value) {LicensePlate = value;} public String getLicensePlate(){ return LicensePlate; } public void SetTopUp(boolean value) { TopUp = value; } public boolean getTopUp() { return TopUp; } } 6 4/26/2018 CSE 1321 Module 6