1 / 23

CS-0401 INTERMEDIATE PROGRAMMING USING JAVA

Understand the concept of classes and objects in Java, including fields, constructors, methods, data encapsulation, and more. Learn to create, access, and manipulate objects in Java programming.

Download Presentation

CS-0401 INTERMEDIATE PROGRAMMING USING JAVA

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Chapter 6 A First Look at Classes

  3. 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

  4. The Concept ofClass and Object

  5. 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?

  6. The General Concept of a Class

  7. Similar houses in my neighborhood How is that possible?

  8. 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” • HousepaulosHouse = new House(); • House marysHouse = new House();

  9. 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();

  10. Major Componentsof a Class

  11. 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)

  12. Example public class House { // class fields private String address; private double price; public intnumberOfBedrooms; private booleanhasSwimmingPool; // class methods public double getPrice() { return price; } public void setPrice(double updatedValue) { price = updatedValue; } }

  13. 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?

  14. 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! }

  15. 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”

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

  17. 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

  18. public class House { // class fields private String address; private double price; private intnumberOfBedrooms; private booleanhasSwimmingPool; // 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; }

  19. Class Constructors

  20. 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

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

  22. 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

  23. Any Questions?

More Related