CS-0401 INTERMEDIATE PROGRAMMING USING JAVA

Slides:



Advertisements
Similar presentations
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
Advertisements

1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)
UML Basics & Access Modifier
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 2 Intro to Classes Richard Salomon and Umesh Patel Centre for Information.
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA Prof. Dr. Paulo Brasko Ferreira Fall 2014.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
10-Nov-15 Java Object Oriented Programming What is it?
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
CourseOutline Example & JavaBeans Lec Start with Example Displaying Course Outlines User will select either course “web design & development” or.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
Classes and Objects - Part I. What is an Object? An Object has two primary components: state – properties of the object behavior – operations the object.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
Classes - Intermediate
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Comp1004: Building Better Objects II Encapsulation and Constructors.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
MIT AITI 2004 Lecture 7 Classes and Objects - Part I.
CE-105 Spring 2007 By: Engr. Faisal ur Rehman
Andrew(amwallis) Classes!
Creating Your Own Classes
Concepts of Object Oriented Programming
Module Road Map Refactoring Why Refactoring? Examples
3 Introduction to Classes and Objects.
Classes and OOP.
Chapter 5 Classes.
Creating Your OwnClasses
HKCT Java OOP Unit 02 Object Oriented Programming in Java Unit 02 Methods, Classes, and Objects 1.
Chapter 7 Classes & Objects.
Classes In C#.
Inheritance Basics Programming with Inheritance
OOP Paradigms There are four main aspects of Object-Orientated Programming Inheritance Polymorphism Abstraction Encapsulation We’ve seen Encapsulation.
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
Chapter 9 Classes & Objects.
Encapsulation and Constructors
Chapter 9 Objects and Classes
Multiple if-else boolean Data
Computer Programming with JAVA
CS2011 Introduction to Programming I Objects and Classes
Chapter 7 Classes & Objects.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Session 2: Introduction to Object Oriented Programming
Chapter 7 Classes & Objects.
Object Oriented Programming in java
Review of Previous Lesson
Review of Previous Lesson
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
ITE “A” GROUP 2 ENCAPSULATION.
Encapsulation.
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

CS-0401 INTERMEDIATE PROGRAMMING USING JAVA Prof. Dr. Paulo Brasko Ferreira Spring 2018

Chapter 6 A First Look at Classes

Chapter 6 Presentation Outline The General Idea about Class and Object Instance Fields and Methods Constructors Overloading Methods and Constructors Scope of Instance Fields Data Encapsulation Packages and Import Statements Java API classes

The Concept of Class and Object

The concepts of class and object Java is an Object-Oriented Programming language Also known as OOP Tries to mimic the way we see the world Makes it easier to develop, debug, and maintain a code A program based on objects? How?

The General Concept of a Class

Similar houses in my neighborhood How is that possible?

The Class and Object Concepts Class Concept: A Class is like a blueprint of something, like the blue print of a house A Class is NOT the house itself, but it is the “instruction manual” that tells how to build a house (one or more of them) Object Concept: The object is the actual thing (e.g., the house) The object is constructed based on the blueprint (the Class) An object is created by using the word “new” House paulosHouse = new House(); House marysHouse = new House();

You create objects based on your class: The main idea You create a class: Put the functionality you want Put some data/parameters you want You create objects based on your class: Home majorHouse = new Home(); majorHouse.getAddress();

Major Components of a Class

Class Major Components Fields: Internal variables or objects Example: Class House Address Zip Code Price Methods: Functions that uses the internal variables/objects to perform some type of task getAddress() changeAskingPrice() getNumberOfBedrooms() All methods can see the class fields (global scope)

Example public class House { // class fields private String address; private double price; public int numberOfBedrooms; private boolean hasSwimmingPool; // class methods public double getPrice() { return price; } public void setPrice(double updatedValue) { price = updatedValue;

Questions and more questions How do I call the class methods? How do I create one or more houses from this class? How do I access the class fields? Any difference in the way we access public and private fields?

Using your class public class MyProgram { // creating a house House myHouse = new House(); myHouse.setPrice(150000); House yourHouse = new House(); yourHouse.setPrice(300000); System.out.println(“My house worths: $” + myHouse.getPrice()); System.out.println(“Your house worths: $” + yourHouse.getPrice()); myHouse.setZipCode(15432); // since numberOfBedrooms has public access…. myHouse.numberOfBedrooms = 3; // it can be accessed directly! System.out.println(“Number of bedrooms: “ + myHouse.numberOfBedrooms); myHouse.numberOfBedrooms = 100; // even though it is possible, it is bad! }

How can we avoid a class field to be set to an invalid value??? (e.g., numberOfBedrooms = 1000) Solution: Have “something” to validate the request before taking the action of changing a field value Dealer: “Sorry, we have only in black, silver, and white” Car: “what is he thinking???? Customer: “I want a green car”

Data Encapsulation Done Change the number of bedrooms to 1000 Private Fields Change the number of bedrooms to 1000 Public fields (and methods) bedrooms Change the price to $50.00 setZip() Zip price getPrice() setPrice() address getZip() setAddr() Not a chance! Valid prices are $150K to $200K

Data Encapsulation Data encapsulation is achieve by: Declaring the class fields as “private” Using setters to validate the data change request before changing it Using getters to get the current values of encapsulated fields It is a great way to keep invalid data away

public class House { // class fields private String address; private double price; private int numberOfBedrooms; private boolean hasSwimmingPool; // class methods public double getPrice() { return price; } public void setPrice(double updatedValue) { if(updatedValue < 150000 || updatedValue > 200000) { System.out.println(“Invalid price value, please try again); } else { price = updatedValue; public void setPrice(double updatedValue) { price = updatedValue; }

Class Constructors

Who builds your house (the object) from the blueprint (class)? Default order Special order A Class can have multiple constructors: a default one (that takes no input arguments) and others more specialized ones

Can we have a full example, please???? Class, Objects, Fields, Methods, Encapsulation, Getters, Setters, Constructors… GOT IT! Can we have a full example, please????

Bike Retailer Fields (attributes): Color (red, blue, white) Size (kids, adult) Model (Cruizer, …) Methods for changing: color size model Red text means that it is the default value used to display the bike on the web browser. The user can change it though later on

Any Questions?