Object-Oriented Programming Concepts and Class Design

slide1 n.w
1 / 9
Embed
Share

Learn about object-oriented programming concepts, class design, and implementation in Java through examples of creating classes like Circle and BMW_Z4. Understand constructors, methods, and properties in object-oriented programming, and see how to utilize them in practical scenarios.

  • Java Programming
  • Object-Oriented
  • Class Design
  • Constructors
  • Methods

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Ps Classes Classes & Objects & Objects OBJECT OBJECT- -ORIENTED PROGRAMMING AND ORIENTED PROGRAMMING AND CLASS DESIGN CLASS DESIGN

  2. Class Circle Pseudocode CLASS Circle BEGIN CREATE Radius = 1.0 CONSTRUCTOR Circle //called default constructor BEGIN END CONSTRUCTOR CONSTRUCTOR Circle (NewRadius) //called constructor BEGIN Radius = NewRadius END CONSTRUCTOR METHOD getArea () //compute and return circle area BEGIN RETURN (Radius * Radius * 3.14159) END METHOD END CLASS Ps

  3. Class Circle public class Circle { double radius = 1.0; Circle() { //default constructor } Circle(double newRadius) { radius = newRadius; } public double getArea() //calculates area //another constructor { return (radius*radius*3.14159); } }

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

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

  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

  7. 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; licensePlate = "DEALER"; } }

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

  9. 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; } }

More Related Content