1 / 64

Chapter 14 Slides

Exposure Java. Chapter 14 Slides. Serious OOP. PowerPoint Presentation created by: Mr. John L. M. Schram. From Materials Created by Mr. Leon Schram. Important Java Reality. A Java program consists of one or more class declarations.

alea-durham
Download Presentation

Chapter 14 Slides

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. Exposure Java Chapter 14 Slides Serious OOP PowerPoint Presentation created by: Mr. John L. M. Schram From Materials Created by Mr. Leon Schram

  2. Important Java Reality A Java program consists of one or more class declarations. Every program statement must be placed inside a class. public class Java0202 { public static void main (String args[ ]) { System.out.println("Plain Simple Text Output"); } }

  3. Program Modules Program development requires that large programs are divided into smaller program modules to be manageable. This is the principle of divide and conquer.

  4. Structured Programming Structured programming is an organized style of programming that places emphasis on modular programming, which aids testing, debugging and modifying. In structured programming, modules are procedures and functions that process external data passed by parameters.

  5. Casual OOP Definition Object Oriented Programming is a programming style with heavy emphasis on program reliability through modular programming. In object oriented programming, modules contain both the data and subroutines that process the data. In Java the modules are called classes and the process-subroutines are smaller modules called methods.

  6. Formal OOP Definition Object Oriented Programming is a style of programming that incorporates program development in a language with the following three OOP traits:  Encapsulation  Polymorphism  Inheritance

  7. Encapsulation Encapsulation is the process of placing a data structure’s data (attributes) with the methods (actions) that act upon the data inside the same module, called a class in Java.

  8. Inheritance Inheritance is the process of using features (both attributes and actions) from an established higher class. The higher class is called the superclass. The lower class is called the subclass.

  9. Polymorphism Polymorphism allows a single accessing feature, such as an operator, method or class identifier, to have many forms. This is hardly a clear explanation. Since the word polymorphism is used in the formal OOP definition, a brief explanation is provided, but details of this powerful OOP concept will come in a later chapter.

  10. Class A class is a user-defined data type that encapsulates both data and the methods that act upon the data.

  11. Object or Instance An object is one instance of a class. A class is a type and an object is a variable. Cat is a class and Fluffy is an object or one instance of the Cat class. Objects can be discussed in a general sense, such as in Object Oriented Programming. This causes confusion between Object, the concept and object, the instance of a class. There is a tendency to use instance when referring to one variable example of a class.

  12. Attributes or Instance Variables The data components of a class are the class attributes and they are also called instance variables. Instance variables should only be accessed by methods of the same class.

  13. Methods Methods are action modules that process data. In other languages such modules may be called subroutines, procedures and functions. In Java the modules are called methods. They are declared inside a class module and process the instance variables.

  14. Instantiation Instantiation is the moment or instance that memory is allocated for a specific object of a class. Statements like the construction of an object, the definition of an object, the creation of an object all have the same meaning as the instantiation of an object.

  15. // Java1401.java // Stage-1 of the Person class. // Only the Person class data attributes are declared. // Each stage of the Person class will have its own number to // distinguish between the different stages. public class Java1401 { public static void main(String args[]) { System.out.println("Person Class, Stage 1\n"); Person01 p = new Person01(); System.out.println(); } } class Person01 { String name; int yearBorn; int education; } Java1401.java Output Person Class, Stage 1

  16. Class Declaration Location In most cases a class declaration is located outside any other class declaration. It is possible to declare one class inside another class (inner class), which will be explained later.

  17. Instantiation and Construction An object is created with the new operator. The creation of a new object is called: instantiation of an object construction of an object The special method that is called during the instantiation of a new object is called a constructor.

  18. // Java1402.java // Stage-2 of the Person class. // This stage adds a default "no-parameter" constructor to the Person class. public class Java1402 { public static void main(String args[]) { System.out.println("Person Class, Stage 2\n"); Person02 p = new Person02(); System.out.println(); } } class Person02 { String name; int yearBorn; int education; Person02() { System.out.println("Calling Default Constructor"); name = "John Doe"; yearBorn = 1980; education = 0; } } Java1402.java Output Person Class, Stage 2 Calling Default Constructor

  19. Constructor Notes A constructor is a method with the same identifier as the class. Constructors are neither void nor return methods. A constructor is called during the instantiation of an object. Constructors without parameters are default constructors.

  20. // Java1403.java // Stage-3 of the Person class. // This stage accesses Person data directly, which is very poor OOP design // by violating encapsulation, which may cause side effects. public class Java1403 { public static void main(String args[]) { System.out.println("Person Class, Stage 3\n"); Person03 p = new Person03(); System.out.println("Name: " + p.name); System.out.println("Born: " + p.yearBorn); System.out.println("Education: " + p.education); } } class Person03 { String name; int yearBorn; int education; Person03() { System.out.println("Calling Default Constructor"); name = "John Doe"; yearBorn = 1980; education = 0; } } Java1403.java Output Person Class, Stage 3 Calling Default Constructor Name: John Doe Born: 1980 Education: 0

  21. // Java1404.java // Stage-4 of the Person class. // In this program direct access to Person data is denied by declaring all class data private. // This program will not compile. public class Java1404 { public static void main(String args[]) { System.out.println("Person Class, Stage 4\n"); Person04 p = new Person04(); System.out.println("Name: " + p.name); System.out.println("Year Born: " + p.yearBorn); System.out.println("Education: " + p.education); System.out.println(); } } class Person04 { private String name; private int yearBorn; private int education; public Person04() { System.out.println("Calling Default Constructor"); name = "John Doe"; yearBorn = 1980; education = 0; } } Java1404.java Output Java1404.java:28: Name has private access in Person04 System.out.println("Name: " + P.Name); ^ Java1404.java:29: YearBorn has private access in Person04 System.out.println("Year Born: " + P.YearBorn); ^ Java1404.java:30: Education has private access in Person04 System.out.println("Education: " + P.Education); ^ 3 errors

  22. // Java1405.java // Stage-5 of the Person class. // The private data of the Person class is now properly accessed with // public member "get methods" of the Person class. public class Java1405 { public static void main(String args[]) { System.out.println("Person Class, Stage 5\n"); Person05 p = new Person05(); System.out.println("Name: " + p.getName()); System.out.println("Year Born: " + p.getYearBorn()); System.out.println("Education: " + p.getEducation()); } } class Person05 { private String name; private int yearBorn; private int education; public Person05() { System.out.println("Calling Default Constructor"); name = "John Doe"; yearBorn = 1980; education = 0; } public int getYearBorn() { return yearBorn; } public String getName() { return name; } public int getEducation() { return education; } } Java1405.java Output Person Class, Stage 5 Calling Default Constructor Name: John Doe Year Born: 1980 Education: 0

  23. Private and Public Class Members The principle of encapsulation requires that object data members are only accessed by methods of the same object. Private class members of some object x can only be accessed by methods of the same x object. Public class members of some object x can be accessed by any client of object x. Data attributes of a class should be declared private. Methods of a class are usually declared public, but there are special helper methods that may also be declared private. Helper methods will be demonstrated later in this chapter.

  24. // Java1406.java Stage-6 of the Person class. This stage adds a second "parameter-constructor" to the Person class. public class Java1406 { public static void main(String args[]) { System.out.println("Person Class, Stage 6\n"); Person06 p1 = new Person06(); System.out.println("Name: " + p1.getName()); System.out.println("Year Born: " + p1.getYearBorn()); System.out.println("Education: " + p1.getEducation()); System.out.println(); Person06 p2 = new Person06("Sue",1972,16); System.out.println("Name: " + p2.getName()); System.out.println("Year Born: " + p2.getYearBorn()); System.out.println("Education: " + p2.getEducation()); } } class Person06 { private String name; private int yearBorn; private int education; public Person06() { System.out.println("Calling Default Constructor"); name = "John Doe"; yearBorn = 1980; education = 0; } public Person06(String n, int y, int e) { System.out.println("Calling Parameter Constructor"); name = n; yearBorn = y; education = e; } public int getYearBorn() { return yearBorn; } public String getName() { return name; } public int getEducation() { return education; } } Java1406.java Output Person Class, Stage 6 Calling Default Constructor Name: John Doe Year Born: 1980 Education: 0 Calling Parameter Constructor Name: Sue Year Born: 1972 Education: 16 As programs get bigger, files can get quite large and tedious. The next slide shows how to handle this.

  25. // Java1407.java // Stage-7 of the Person class. // This program shows how a program can be separated into multiple files, // which in this case are the Person07.java and Java1407 files. // The result is the same as the Java1406.java program. public class Java1407 { public static void main(String args[]) { System.out.println("Person Class, Stage 7\n"); Person07 p1 = new Person07(); System.out.println("Name: " + p1.getName()); System.out.println("Year Born: " + p1.getYearBorn()); System.out.println("Education: " + p1.getEducation()); System.out.println(); Person07 p2 = new Person07("Sue",1972,16); System.out.println("Name: " + p2.getName()); System.out.println("Year Born: " + p2.getYearBorn()); System.out.println("Education: " + p2.getEducation()); System.out.println(); } } Java1407.java Output Person Class, Stage 6 Calling Default Constructor Name: John Doe Year Born: 1980 Education: 0 Calling Parameter Constructor Name: Sue Year Born: 1972 Education: 16

  26. // Person07.java This file supports the executable Java1407.java file. public class Person07 { private String name; private int yearBorn; private int education; public Person07() { System.out.println("Calling Default Constructor"); name = "John Doe"; yearBorn = 1980; education = 0; } public Person07(String n, int y, int e) { System.out.println("Calling Parameter Constructor"); name = n; yearBorn = y; education = e; } public int getYearBorn() { return yearBorn; } public String getName() { return name; } public int getEducation() { return education; } } Java1407.java Output Person Class, Stage 6 Calling Default Constructor Name: John Doe Year Born: 1980 Education: 0 Calling Parameter Constructor Name: Sue Year Born: 1972 Education: 16

  27. Working with Multiple Files Create a separate file for each class in the program or at least a file that is manageable in size with several small classes. Make sure there is only one file with a main method, known as the main file. Place all the files in the same folder and compile each file separately. Execute the entire program by executing the main file.

  28. // Java1408.java Stage-8 of the Person class. // This stage adds "setMethods" that alter private data of a class during // program execution. It also adds a showData method to display all the data with a single call. import java.io.*; public class Java1408 { public static void main (String args[]) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Person Class, Stage 8\n"); Person08 p = new Person08(); p.showData(); System.out.print("Enter name ===>> "); p.setName(input.readLine()); System.out.print("Enter year born ===>> "); p.setYearBorn(Integer.parseInt(input.readLine())); System.out.print("Enter education ===>> "); p.setEducation(Integer.parseInt(input.readLine())); System.out.println(); p.showData(); } } Java1408.java Output Person Class, Stage 8 Calling Default Constructor Name: John Doe Year Born: 1980 Education: 0 Enter name ===>> "Byron Derry" Enter year born ===>> 1966 Enter education ===>> 20 Name: "Byron Derry" Year Born: 1966 Education: 20

  29. // Person08.java This file supports the Java1408.java executable file. public class Person08 { ///// PRIVATE PERSON ATTRIBUTES //////////////////////////////////////////////////// private String name; private int yearBorn; private int education; ///// CONSTRUCTORS //////////////////////////////////////////////////////////////// public Person08() { System.out.println("Calling Default Constructor"); name = "John Doe"; yearBorn = 1980; education = 0; } public Person08(String n, int y, int e) { System.out.println("Calling Parameter Constructor"); name = n; yearBorn = y; education = e; } ///// GET (ACCESSOR) METHODS //////////////////////////////////////////////////////// public String getName() { return name; } public int getYearBorn() { return yearBorn; } public int getEducation() { return education; } public void showData() { System.out.println("Name: " + getName()); System.out.println("Year Born: " + getYearBorn()); System.out.println("Education: " + getEducation()); System.out.println(); } ///// SET (MODIFIER) METHODS //////////////////////////////////////////////////////// public void setYearBorn(int y) { yearBorn = y; } public void setName(String n) { name = n; } public void setEducation(int e) { education = e; } } Java1408.java Output Person Class, Stage 8 Calling Default Constructor Name: John Doe Year Born: 1980 Education: 0 Enter name ===>> "Byron Derry" Enter year born ===>> 1966 Enter education ===>> 20 Name: "Byron Derry" Year Born: 1966 Education: 20

  30. // Java1409.java // Stage-9 of the Person class. // This stage attempts to create a copy with an existing object // at the parameter for the constructor of the second object. public class Java1409 { public static void main (String args[]) { System.out.println("Person Class, Stage 09\n"); Person09 p1 = new Person09("Seymour",1975,18); p1.showData(); Person09 p2 = new Person09(p1); p2.showData(); } } Java1409.java Output Java1409.java:14: cannot resolve symbol symbol : constructor Person09 (Person09) location: class Person09 Person09 p2 = new Person09(p1); ^ 1 error

  31. // Java1410.java // Stage-10 of the Person class. // This stage attempts to create an copy with an existing object // at the parameter for the constructor of the second object. // This time a "copy constructor" has been added to the Person class. public class Java1410 { public static void main (String args[]) { System.out.println("Person Class, Stage 10\n"); Person10 p1 = new Person10("Seymour",1975,18); p1.showData(); Person10 p2 = new Person10(p1); p2.showData(); } } Java1410.java Output Person Class, Stage 10 Calling Default Constructor Name: Seymour Year Born: 1975 Education: 18 Calling Copy Constructor Name: Seymour Year Born: 1975 Education: 18

  32. // Person10.java // This file supports the Java1410.java executable file. // A portion of the class is shown. The entire class is in your book or on your computer. public class Person10 { ///// PRIVATE PERSON ATTRIBUTES //////////////////////////////////////////////////// private String name; private int yearBorn; private int education; ///// CONSTRUCTORS //////////////////////////////////////////////////////////////// public Person10() { System.out.println("Calling Default Constructor"); name = "John Doe"; yearBorn = 1980; education = 0; } public Person10(Person10 p) { System.out.println("Calling Copy Constructor"); name = p.name; yearBorn = p.yearBorn; education = p.education; } Java1410.java Output Person Class, Stage 10 Calling Default Constructor Name: Seymour Year Born: 1975 Education: 18 Calling Copy Constructor Name: Seymour Year Born: 1975 Education: 18

  33. Copy Constructor public Person10(Person10 p) { System.out.println("Calling Copy Constructor"); name = p.name; yearBorn = p.yearBorn; education = p.education; } A copy constructor is a constructor that instantiates a new object as a copy of an existing object. A copy constructor uses a single parameter, which is an object of the same class as the copy constructor.

  34. // Java1411.java Stage-11 of the Person class. // The Person class has been simplified to demonstrate scope of an object. // The program will only compile with several statements commented out. Remove the // comments and the program does not compile, because the objects are no longer in scope. public class Java1411 { public static void main (String args[]) { System.out.println("Person Class, Stage 11\n"); System.out.println("MAIN, LEVEL 1"); Person11 p1 = new Person11("Kathy"); p1.showData("p1"); { System.out.println("\nMAIN, LEVEL 2"); Person11 p2 = new Person11("Joseph"); p2.showData("p2"); { System.out.println("\nMAIN, LEVEL 3"); p1.showData("p1"); p2.showData("p2"); Person11 p3 = new Person11("Elizabeth"); p3.showData("p3"); } System.out.println("\nMAIN, LEVEL 2"); p1.showData("p1"); p2.showData("p2"); // p3.showData("p3"); } System.out.println("\nMAIN, LEVEL 1"); p1.showData("p1"); // p2.showData("p2"); // p3.showData(""p3"); } } Java1411.java Output Person Class, Stage 11 MAIN, LEVEL 1 showData for: p1 Name: Kathy MAIN, LEVEL 2 showData for: p2 Name: Joseph MAIN, LEVEL 3 showData for: p1 Name: Kathy showData for: p2 Name: Joseph showData for: p3 Name: Elizabeth MAIN, LEVEL 2 showData for: p1 Name: Kathy showData for: p2 Name: Joseph MAIN, LEVEL 1 showData for: p1 Name: Kathy Press any key to continue. . .

  35. // Java1412.java // This program demonstrates the side effect caused by the p2 = p1; // statement. Note that p1 is altered after p2 is altered. public class Java1412 { public static void main (String args[]) { System.out.println("\nJava1412.java\n"); Person p1 = new Person("Kathy"); p1.showData("p1"); Person p2 = new Person("Tom"); p2.showData("p2"); p2 = p1; p2.showData("p2"); p2.setName("George"); p2.showData("p2"); p1.showData("p1"); System.out.println(); int num1 = 15; System.out.println("num1: " + num1); int num2 = 25; System.out.println("num2: " + num2); num2 = num1; System.out.println("num2: " + num2); num2 = 100; System.out.println("num2: " + num2); System.out.println("num1: " + num1); System.out.println(); } } Java1412.java Output Java1412.java showData for: p1 has name Kathy showData for: p2 has name Tom showData for: p2 has name Kathy showData for: p2 has name George showData for: p1 has name George num1: 15 num2: 25 num2: 15 num2: 100 num1: 15

  36. // This class is used with program Java1412.java class Person { private String name; public Person(String n) { name = n; } public void showData(String s) { System.out.println("showData for: " + s + " has name " + name); } public void setName(String n) { name = n; } } Java1412.java Output Java1412.java showData for: p1 has name Kathy showData for: p2 has name Tom showData for: p2 has name Kathy showData for: p2 has name George showData for: p1 has name George num1: 15 num2: 25 num2: 15 num2: 100 num1: 15

  37. // Java1413.java // This program demonstrates that an object stores the memory address where the // object data is located. It does not store the data. When one object is assigned // to another object, the address is copied, not the data. public class Java1413 { public static void main (String args[]) { System.out.println("\nJava1413.java\n"); Person p1 = new Person(); Person p2 = new Person(); System.out.println("p1 value: " + p1); System.out.println("p2 value: " + p2); p2 = p1; System.out.println("\np2 value: " + p2); System.out.println(); } } class Person { private String name; public Person() { name = "John Doe"; } } Java1413.java Output Java1413.java p1 value: Person@df6ccd p2 value: Person@601bb1 p2 value: Person@df6ccd

  38. What is going on? Part 1 Figure 14.18 shows 2 strings and 2 integers are stored.

  39. What is going on? Part 2 Figure 14.19 shows the result of making the two assignment statements: p2 = p1; num2 = num1;

  40. Objects and Simple Data TypesStore Information Differently Simple (primitive) data types store assigned values directly in their allocated memory locations. Objects store memory references of the memory locations allocated during the construction of the object.

  41. What is going on? Part 3 Figure 14.20 shows the result of making the two assignment statements: p2 = George; num2 = 100;

  42. Aliasing Aliasing occurs when two or more variables reference the same memory location. Any change in the value of one variable brings about a change in the value of the other variable(s).

  43. // Java1414.java // This program demonstrates the difference between simple, primitive data and objects. // Simple data passes a value to a method and objects pass a memory address reference to a method. public class Java1414 { public static void main (String args[]) { System.out.println("\nJava1414.java\n"); Person p1 = new Person("Tom"); Person p2 = new Person("Liz"); int intList1[] = {1,2,3,4,5}; int intList2[] = {5,4,3,2,1}; System.out.println("\np1 value: " + p1); System.out.println ("intList1 value: " + intList1); System.out.println("\np2 value: " + p2); System.out.println ("intList2 value: " + intList2); System.out.println("\nCalling parameterDemo method"); parameterDemo(7,p1,intList1); System.out.println("\nCalling parameterDemo method"); parameterDemo(20,p2,intList2); System.out.println(); } public static void parameterDemo(int years, Person p, int list[]) { System.out.println("Parameter years value: " + years); System.out.println("Parameter p value: " + p); System.out.println("Parameter list value: " + list); } } class Person { private String name; public Person(String n) { name = n; } } Java1414.java Output Java1414.java p1 value: Person@df6ccd intList1 value: [I@601bb1 p2 value: Person@ba34f2 intList2 value: [I@ea2dfe Calling parameterDemo method Parameter years value: 7 Parameter p value: Person@df6ccd Parameter list value: [I@601bb1 Calling parameterDemo method Parameter years value: 20 Parameter p value: Person@ba34f2 Parameter list value: [I@ea2dfe

  44. // Java1415A.java // This program demonstrates that the simple data type variable used in parameter // passing is not altered by any method process. It tries to show that the object // is altered, but since myName is an object of the String class it remains unaltered. public class Java1415A { public static void main (String args[]) { System.out.println("\nJava1415A.java\n"); int intNum = 100; String myName = "Bob"; System.out.println("main method values"); System.out.println("intNum: " + intNum); System.out.println("myName: " + myName); qwerty(intNum,myName); System.out.println("\nmain method values"); System.out.println("intNum: " + intNum); System.out.println("myName: " + myName); System.out.println(); } Java1415A.java Output Java1415A.java main method values intNum: 100 myName: Bob qwerty method values num: 200 name: Joe main method values intNum: 100 myName: Bob public static void qwerty(int num, String name) { num += 100; name = "Joe“ System.out.println("\nqwerty method values"); System.out.println("num: " + num); System.out.println("name: " + name); }

  45. // Java1415B.java // This program PROPERLY demonsrates that the simple data type variable // used in parameter passing is not altered by any method process. The object is altered. public class Java1415B { public static void main (String args[]) { System.out.println("\nJava1415a.java\n"); int intNum = 100; myClass myObject = new myClass(); System.out.println("main method values"); System.out.println("intNum: " + intNum); System.out.println("myObject.getName(): " + myObject.getName()); qwerty(intNum,myObject); System.out.println("\nmain method values"); System.out.println("intNum: " + intNum); System.out.println("myObject.getName(): " + myObject.getName()); System.out.println(); } public static void qwerty(int num, myClass myObject) { num += 100; myObject.alterName("Joe"); System.out.println("\nqwerty method values"); System.out.println("num: " + num); System.out.println("myObject.getName(): " + myObject.getName()); } } Java1415a.java Output Java1415a.java main method values intNum: 100 myName: Bob qwerty method values num: 200 name: Joe main method values intNum: 100 myName: Joe class myClass { String name; myClass() { name = "Bob"; } void alterName(String newName) { name = newName; } String getName() { return name; } }

  46. Java Parameters Java passes information to a method with parameters. A copy of the calling parameter is assigned to the receiving parameter in the method. Altering a simple data type parameter inside a method does not alter the value of the calling parameter. Altering an object parameter inside a method alter the object value of the calling parameter, because objects pass the memory reference where object information is stored. It is true that strings are objects. However, strings are a special exception. Chapter 16 will deal with exactly what is going on with Strings. For now think of Strings as simple data types.

  47. // Java1416.java // This program intentionally uses identical parameter identifiers in the constructor method // as the private attribute identifiers of the Car class. This has an undesirable result. public class Java1416 { public static void main (String args[]) { System.out.println("\nJava1416.java\n"); Car ford = new Car("Explorer",2002,30000.0); ford.showData(); System.out.println(); } } class Car { private String make; private int year; private double cost; public Car(String make, int year, double cost) { make = make; year = year; cost = cost; } public void showData() { System.out.println("make: " + make); System.out.println("year: " + year); System.out.println("cost: " + cost); } } Java1416.java Output Java1416.java make: null year: 0 cost: 0.0

  48. // Java1417.java // The problem of the previous program is solved by using the "this" reference // to explicitly indicate the meaning of an identifier. public class Java1417 { public static void main (String args[]) { System.out.println("\nJava1417.java\n"); Car ford = new Car("Explorer",2002,30000.0); ford.showData(); System.out.println(); } } class Car { private String make; private int year; private double cost; public Car(String make, int year, double cost) { this.make = make; this.year = year; this.cost = cost; } public void showData() { System.out.println("make: " + make); System.out.println("year: " + year); System.out.println("cost: " + cost); } } Java1417.java Output Java1417.java Make: Explorer Year: 2002 Cost: 30000.0

  49. // Java1418.java // This program introduces the Engine class, which will be used in the next // program to demonstrate composition. public class Java1418 { public static void main(String args[]) { System.out.println("\nJava1418.java\n"); Engine iForce = new Engine(8,4.7,265); System.out.println("Cylinders: " + iForce.getCylinders()); System.out.println("Size: " + iForce.getSize()); System.out.println("HorsePower: " + iForce.getHP()); System.out.println(); } } class Engine { private int cylinders; // number of cylinders in the engine private double liters; // displacement of the engine in liters private int horsePower; // horsepower of the engine public Engine(int cyl, double size, int hp) { cylinders = cyl; liters = size; horsePower = hp; } public int getCylinders() { return cylinders; } public double getSize() { return liters; } public int getHP() { return horsePower; } } Java1418.java Output Java1418.java Cylinders: 8 Size: 4.7 HorsePower: 265

  50. // Java1419.java // This program demonstrates "composition", which uses multiple classes in // such a way that an object of one class become a data member of another class. public class Java1419 { public static void main(String args[]) { System.out.println("\nJava1419.java\n"); Car toyota = new Car("Tundra",4,"Red",8,4.7,265); toyota.displayCar(); } } class Engine // shown on previous slide class Car { private String carMake; private int carDoors; private String carColour; private Engine carEngine; public Car(String cm, int cd, String cc, int cyl, double size, int hp) { carMake = cm; carDoors = cd; carColour = cc; carEngine = new Engine(cyl,size,hp); System.out.println("Car Object Finished\n"); } public void displayCar() { System.out.println("carMake: " + carMake); System.out.println("carDoors: " + carDoors); System.out.println("carColour: " + carColour); System.out.println("cylinders: " + carEngine.getCylinders()); System.out.println("size: " + carEngine.getSize()); System.out.println("horsePower: " + carEngine.getHP()); } } Java1419.java Output Java1419.java Engine Object Finished Car Object Finished carMake: Tundra carDoors: 4 carColour: Red cylinders: 8 size: 4.7 horsePower: 265

More Related