Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS-0401 INTERMEDIATE PROGRAMMING USING JAVA Prof. Dr. Paulo Brasko Ferreira Fall 2014.

Similar presentations


Presentation on theme: "CS-0401 INTERMEDIATE PROGRAMMING USING JAVA Prof. Dr. Paulo Brasko Ferreira Fall 2014."— Presentation transcript:

1 CS-0401 INTERMEDIATE PROGRAMMING USING JAVA Prof. Dr. Paulo Brasko Ferreira Fall 2014

2

3 Chapter 6 Presentation Outline Classes and Objects Instance Fields and Methods Constructors Overloading Methods and Constructors Scope of Instance Fields Getters and Setters Packages and Import Statements

4

5 The concepts of class and object Java is an OOP language Object-oriented programming language Tries to mimic the way we see the world Makes it easier to program and maintain a code A program based on objects? How?

6

7 Similar houses in my neighborhood

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

9 Create objects is similar to create primitive variables double distance = 10.5; “Variable” type “Variable” name “Variable” value House paulosHouse = new House(); “Object” type “Object” name “Object” value

10 Similarities when creating objects int number = 3; int daysOfTheWeek = 7; House paulosHouse = new House(); House marysHouse = new House(); int number; number =3; House johnsHouse; johnsHouse = new House();

11 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();

12 Other Examples of Classes

13 Humans

14 Components of a Class Attributes: Channel number Volume level Close-caption off What are the attributes/char acteristics? Operations: Change Channel Increase Volume Turn Close-caption on What are the operations you can perform?

15

16 Class Major Components Fields (characteristics): Internal variables (int, double) or objects (wheels on a car) Example: Class Bike Gear Number Cadence Color Methods (what operations you can perform): Functions that uses the internal variables/objects to perform some type of action paintBike(Red) switchToLowerGear() estimateActualVelocity()

17 Example of a Bike Class Topics to discuss while programming in class: Creating a Bike class with fields and methods Class default and non-default constructors Creating a test program that creates objects of the Bike class Different objects have different attribute values How to call methods for a given object Differences between public and private methods Differences between public and private fields (why is encapsulation important) Getters and setters: Standard names for getters and setters How NetBeans can help you creating them

18 Visual Aid on Encapsulation

19 Class Fields Class fields are global variables for the class methods The fields can be public or private For class encapsulation, they are normally private Example: changing bike gears The concept of getters and setters They are normally placed at the top of the class

20 Class Methods Methods can be private or public Public methods can be accessed from other classes Math.sqrt() is a public method from Math class getters and setters methods Private methods can be only accessed from other methods of the same class Internal calculations or verifications that no one cares besides the class itself

21 Creating a class What Am I suppose to put inside a class? First imagine the object that will be created by this class What are its main characteristics? What methods would you think it should have?

22 Questions to ask when creating a class What variables (fields) a rectangle class would have? How could we set the values of those fields? In what ways could I create an object of this class? Rectangle myBedroom = new Rectangle(); Rectangle myBedroom = new Rectangle(5, 7);

23 The Rectangle Class Class Name Goes Here Public(+) or Private(-) NameField Type Or Method input arguments Method returning type Fields Methods UML diagram for the Rectangle Class

24

25 Any Questions? 25


Download ppt "CS-0401 INTERMEDIATE PROGRAMMING USING JAVA Prof. Dr. Paulo Brasko Ferreira Fall 2014."

Similar presentations


Ads by Google