Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

Size: px
Start display at page:

Download "Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9"

Transcription

1 Chapter 7 Objects and Classes Chapter 6 Arrays Chapter 7 Objects and Classes Chapter 8 Strings and Text I/O Chapter 9 Inheritance and Polymorphism GUI can be covered after 10.2, Abstract Classes Chapter 12 GUI Basics 10.2, Abstract Classes Chapter 13 Graphics 10.4, Interfaces Chapter 14 Event-Driven Programming Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9 Chapter 17 Exceptions and dassertions Chapter 18 Binary I/O 1

2 Objectives To understand objects and classes and use classes to model objects ( 7.2). To learn how to declare a class and how to create an object of a class ( 7.3). To understand the roles of constructors and use constructors to create objects ( 7.3). To use UML graphical notations to describe classes and objects ( 7.3). To distinguish between object reference variables and primitive data type variables ( 7.4). To use classes in the Java library ( 7.5). To declare private data fields with appropriate get and set methods to make class easy to maintain ( ). To develop methods with object arguments ( 7.9). To understand the difference between instance and static variables and methods ( 7.10). To determine the scope of variables in the context of a class ( 7.11). To use the keyword this as the reference to the current object that invokes the instance method ( 7.12). To store and process objects in arrays ( 7.13). 2

3 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity in the real world that can be distinctly i identified. ifi d For example, a student, a desk, a circle, a button, and even a loan can all be viewed as objects. An object has a unique identity, i state, and dbehaviors. The state of an object consists of a set of data fields (also known as properties) with their current values. The behavior of an object is defined ed by a set of methods. 3

4 Objects Class Name: Circle Data Fields: radius is Methods: getarea A class template Circle Object 1 Circle Object 2 Circle Object 3 Three objects of the Circle class Data Fields: radius is 10 Data Fields: radius is 25 Data Fields: radius is 125 An object has both a state and behavior. The state defines the object, and the behavior defines what the object does. 4

5 Classes Classes are construction that define objects of the same type. A Java class uses variables to define data fields and methods to define behaviors. Additionally, a class provides a special type of methods, known as constructors, which are invoked to construct objects from the class. 5

6 Classes class Circle { /** The radius of this circle */ double radius = 1.0; /** Construct a circle object */ Circle() { /** Construct t a circle object */ Circle(double newradius) { radius = newradius; Data field Constructors /** Return the area of this circle */ double getarea() { return radius * radius * ; Method 6

7 UML Class Diagram UML Class Diagram Circle Class name radius: double Data fields Circle() Circle(newRadius: double) Constructors and Mthd Methods getarea(): double circle1: Circle radius: 10.0 circle2: Circle radius: 25.0 circle3: Circle radius: UML notation for objects UML=Unified U Modeling Language 7

8 Constructors Circle() { Constructors are a special kind of methods that are invoked to construct objects. Circle(double newradius) { radius = newradius; Different signature: order, number, type overload constructor 8

9 Constructors, cont. A constructor with no parameters is referred to as a no-arg constructor. Constructors must have the same name as the class itself. Constructors do not have a return type not even void. Constructors are invoked using the new operator when an object is created. Constructors play the role of fiiilii initializing objects. 9

10 Creating Objects Using Constructors ClassName objname = new ClassName(); Example: Circle cir1 = new Circle(); Circle cir2 =new Circle(5.0); 10

11 Default Constructor A class may be declared without constructors. In this case, a no-arg constructor with an empty body is implicitly declared in the class. This constructor, called a df default constructor, is provided automatically only if no constructors are explicitly declared in the class. ยกต วอย างประกอบ ยกตวอยางประกอบ 11

12 Declaring Object Reference Variables To reference an object, assign the object to a reference variable. To declare a reference variable, use the syntax: ClassName objectrefvar; objectrefvar = new ClassName(); Example: Circle mycircle; mycircle = new Circle(); 12

13 Declaring/Creating Objects in a Single Step ClassName objectrefvar = new ClassName(); Example: Assign object reference Create an object Circle mycircle = new Circle(); mycircle mycircle: Circle radius:

14 Accessing Objects Referencing the object s data fields: objectrefvar.data e.g., mycircle.radius Invoking the object s method: objectrefvar.methodname(arguments) e.g., mycircle.getarea() Occasionally, an object does not need to be referenced later, you can create an object without explicitly assigning it to a variable System.out.println( Area is + new Circle(5).getArea() ).getarea()); Call Anonymous object 14

15 public class TestCircle { /** Main method */ public static void main(string[] args) { // Create a circle with radius 5.0 Circle mycircle = new Circle(5.0); System.out.println("The area of the circle of radius " + mycircle.radius + " is " + mycircle.getarea()); System.out.println(myCircle); t yc e); // Create a circle with radius 1 Circle yourcircle = new Circle(); System.out.println("The area of the circle of radius " + yourcircle.radius + " is " + yourcircle.getarea()); // Modify circle radius yourcircle.radius = 100; System.out.println("The area of the circle of radius " + yourcircle.radius + " is " + yourcircle.getarea()); // Define the circle class with two constructors class Circle { double radius; /** Construct a circle with radius 1 */ Circle() { radius = 1.0; /** Construct a circle with a specified radius */ Circle(double newradius) { radius = newradius; /** Return the area of this circle */ double getarea() { return radius * radius * Math.PI; Circle radius: double Circle() Circle(newRadius: double) getarea(): double TestCircle main(args : String[]) TestCircle1 15

16 animation Trace Code Declare mycircle Circle mycircle = new Circle(5.0); Circle yourcircle = new Circle(); yourcircle.radius = 100; mycircle no value 16

17 animation Trace Code, cont. Circle mycircle = new Circle(5.0); Circle yourcircle = new Circle(); yourcircle.radius = 100; mycircle :Circle no value radius: 5.0 Create a circle 17

18 animation Trace Code, cont. Circle mycircle = new Circle(5.0); Circle yourcircle = new Circle(); mycircle reference value yourcircle.radius = 100; Assign object reference to mycircle radius: 5.0 :Circle 18

19 animation Trace Code, cont. Circle mycircle = new Circle(5.0); Circle yourcircle = new Circle(); yourcircle.radius = 100; mycircle reference value : Circle radius: 5.0 yourcircle no value Declare yourcircle 19

20 animation Trace Code, cont. Circle mycircle = new Circle(5.0); Circle yourcircle = new Circle(); yourcircle.radius = 100; mycircle reference value : Circle radius: 5.0 yourcircle no value Create a new radius: 0.0 Circle object : Circle 20

21 animation Trace Code, cont. Circle mycircle = new Circle(5.0); Circle yourcircle = new Circle(); yourcircle.radius = 100; mycircle reference value : Circle radius: 5.0 yourcircle reference value Assign object reference to yourcircle : Circle radius:

22 animation Trace Code, cont. Circle mycircle = new Circle(5.0); Circle yourcircle = new Circle(); yourcircle.radius = 100; mycircle reference value : Circle radius: 5.0 yourcircle reference value : Circle Change radius in radius: yourcircle 22

23 1 public class Circle { 2 /** Main method */ 3 public static void main(string[] args) { C 4 // Create a circle with radius Circle mycircle = new Circle(5.0); 6 System.out.println("The area of the circle of radius " 7 + mycircle.radius + " is " + mycircle.getarea()); System.out.println(myCircle); t yc e); // Create a circle with radius 1 Circle yourcircle = new Circle(); System.out.println("The area of the circle of radius " + yourcircle.radius + " is " + yourcircle.getarea()); // Modify circle radius yourcircle.radius = 100; System.out.println("The area of the circle of radius " + yourcircle.radius + " is " + yourcircle.getarea()); Circle() { // Define the circle class with two constructors double radius; /** Construct a circle with radius 1 */ radius = 1.0; /** Construct a circle with a specified radius */ Circle(double newradius) { radius = newradius; /** Return the area of this circle */ double getarea() { return radius * radius * Math.PI; Circle radius: double main(args : String[]) Circle() Circle(newRadius: double) getarea(): double -สร างเมธอด main ไว ในคลาสเพ อทดสอบการท างานของคลาส -ส วน fields วางไว ส วนใดก ได แต ให อย ใน scope ของคลาส 23

24 Caution Recall that you use Math.methodName(arguments) (e.g., Math.pow(3, 2.5)) to invoke a method in the Math class. Can you invoke getarea() using Circle1.getArea()? The answer is no. All the methods used before this chapter are static methods, which are defined using the static keyword. However, getarea() is non-static. It must be invoked from an object using objectrefvar.methodname(arguments) (e.g., mycircle.getarea()). More explanations will be given in Section 7.7, Static Variables, Constants, and Methods. 24

25 Reference Data Fields The data fields can be of reference types. For example, the following Student class contains a data field name of the String type. public class Student { String name; // name has default value null int age; // age has default value 0 boolean issciencemajor; // issciencemajor has default value false char gender; // c has default value '\u0000' If a data field of a reference type does not reference any object, the data field holds a special literal value, null. Student name: String age : int issciencemajor : boolean gender : char 25

26 Default Value for a Data Field The default value of a data field is null for a reference type 0.0 for a float, double type 0 for a numeric type, (byte, short, int, long) false for a boolean type, and '\u0000' for a char type. Student name: String age : int issciencemajor : boolean gender : char However, Java assigns no default value to a local variable inside a method. public class Test { public static void main(string[] args) { Student student = new Student(); System.out.println("name? " + student.name); //null System.out.println("age? " + student.age); //0 System.out.println("isScienceMajor? " + student.issciencemajor); //false System.out.println("gender? " + student.gender);//space character System.out.println(student.name.length());//NullPointerException Student std = null; std.age = 20; //NullPointerException is a common runtime error 26

27 Example Java assigns no default value to a local variable inside id a method. public class Test { public static void main(string[] args) { int x; // x has no default value String y; // y has no default value System.out.println("x is " + x); System.out.println("y is " + y); Compilation error: variables not initialized 27

28 Differences between Variables of Primitive Data Types and Object Types Pi Primitive iti type iti int i = 1 i 1 Created using new Circle() Object type Circle c c reference c: Circle radius =

29 Copying Variables of Primitive Data Types and Object Types Before: Primitive type assignment i = j After: Circle radius: double Circle() Circle(newRadius: double) getarea(): double i 1 i 2 j 2 j 2 int i=1; int j=2; i=j; c1 Before: Circle c1 = new Circle(5); Circle c2 = new Circle(9); c1=c2; Object type assignment c1 = c2 After: c1 c2 c2 c1: Circle c2: Circle c1: Circle c2: Circle radius = 5.0 radiu s = 9.0 radius = 5.0 radius =

30 Garbage Collection As shown in the previous figure, after the assignment statement c1 = c2, c1 points to the same object referenced by c2. The object previously referenced by c1 is no longer referenced. This object is known as garbage. Garbage is automatically collected by JVM. The JVM detects garbage and automatically reclaims the space it occupies. This process is called garbage collection 30

31 Garbage Collection, cont TIP: If you know that an object is no longer needed, you can explicitly assign null to a reference variable for the object. The JVM will automatically collect the space if the object is not referenced by any variable. Circle c1 = new Circle(5); Circle c2 = new Circle(9); c1=null; c1=c2; 31

32 Using Classes from the Java Library Example 7.1 declared the Circle1 class and created objects from the class. Often you will use the classes in the Java library to develop programs. You learned to obtain the current time using System.currentTimeMillis() in Example 2.5, Displaying Current Time. You used the division and remainder operators to extract current second, minute, and hour. 32

33 Using Classes from the Java Library :The Date Class Java provides a system-independent encapsulation of date and time in the java.util.date class. You can use the Date class to create an instance for the current date and time and use its tostring method to return the date and time as a string. The + sign indicates public modifer java.util.date +Date() +Date(elapseTime: long) +tostring(): String +gettime(): long +settime(elapsetime: long): void Constructs a Date object for the current time. Constructs a Date object for a given time in milliseconds elapsed since January 1, 1970, GMT. Returns a string representing the date and time. Returns the number of milliseconds since January 1, 1970, GMT. Sets a new elapse time in the object. java.util.date d = new java.util.date(24*60*60*1000); System.out.println(d.toString());// Fri Jan 02 07:00:00 ICT

34 The Date Class Example For example, the following code java.util.date date = new java.util.date(); System.out.println(date.toString()); //เวลาป จจ บ น displays a string like: Mon Jul 04 14:42:18 ICT 2011 ต อง import java.util.date; //หร อไม 34

35 The Random Class You have used Math.random() to obtain a random double value between 0.0 and 1.0 (excluding 1.0). A more useful random number generator is provided in the java.util.random class. java.util.random +Random() Constructs a Random object with the current time as its seed. +Random(seed: long) Constructs a Random object with a specified seed. +nextint(): int Returns a random int value. +nextint(n: int): int Returns a random int value between 0 and n (exclusive). +nextlong(): long Returns a random long value. +nextdouble(): double Returns a random double value between 0.0 and 1.0 (exclusive). +nextfloat(): float Returns a random float value between 0.0F and 1.0F (exclusive). +nextboolean(): boolean Returns a random boolean value. 35

36 The Random Class Example If two Random objects have the same seed, they will generate e identical sequences of numbers. For example, the following code creates two Random objects with the same seed 3. Random random1 = new Random(3); System.out.print("From random1: "); for (int i = 0; i < 10; i++) System.out.print(random1.nextInt(1000) + " "); Random random2 = new Random(3); System.out.print("\nFrom random2: "); for (int i = 0; i < 10; i++) System.out.print(random2.nextInt(1000) + " "); From random1: From random2: Random random3 = new Random(9); System.out.print("\nFrom random3: "); for (int i = 0; i < 10; i++) System.out.print(random3.nextInt(1000) ( ( ) + " "); o/p: From random3:

37 Instance Variables, and Methods Instance variables belong to a specific instance, it is not shared among objects of the same class double radius; Instance methods are invoked by an instance of the class. void getarea(){st; t Instance methods and instance variables belong to instances and can only be used after the instances are created. They are accessed via a reference variable Circle obj = new Circle(); obj.getarea(); obj.radius; 37

38 UML Class Diagram UML Class Diagram Circle Class name radius: double Data fields Circle() Circle(newRadius: double) Constructors and Mthd Methods getarea(): double circle1: Circle radius: 10.0 circle2: Circle radius: 25.0 circle3: Circle radius: UML notation for objects Circle cir1 = new Circle(10); cir1.getarea(); cir1.radius; 38

39 Static Variables, Constants,and and Methods Static variables are shared by all the instances of the class. static int numberofobjects;//class variable Static ti methods are not tied to a specific object. static void getnumberofobjects(){st;//class method Static constants are final variables shared by all the instances of the class. final static int MAX = 100; To declare static variables, constants, and methods, use the static modifier. Static ti methods and static ti variables can be accessed from a reference variable or from their class name. 39

40 Static Variables, Constants, and Methods, cont. instantiate circle1 Memory Circle radius = 1.0 numberofobjects = radius radius: double numberofobjects: int getnumberofobjects(): int +getarea(): double instantiate circle2 2 numberofobjects UML Notation: underline: static variables or methods radius = 5.0 numberofobjects = radius Circle circle1 = new Circle(1); Circle circle2 = new Circle(5); circle1.numberofobjects = 2; System.out.println(circle2.getNumberOfObjects()); ถ าเป นแบบ static ควรใช คลาสอ างด กว าเพ อให ทราบว าค อต วแปรและ เมธอดแบบ static Circle.numberOfObjects; Circle.getNumberOfObjects() Access by using class name 40

41 static variable final static variable obj1 obj2... obj1 objn obj2... objn static int numobj; final static int MAX; final variable final variable final variable... public class A { obj1 obj2 objn final int MIN; final int MIN; public A(int val){ MIN = val; 41

42 Example of Using Instance and Class Variables and dmethod Objective: Demonstrate the roles of instance and class variables and their uses. This example adds a class variable numberofobjects to track the number of Circle objects created. Circel2, TestCircle2 42

43 Static Variables, Constants, and Methods, cont. Static variables and methods can be used from instance or static methods in the class Instance variables and methods can only be used from instance methods, not from static method public class Foo { int i = 5; static int k = 2; public static void main(string[] args) { int j = i; // wrong because i is an instance var m1(); // wrong because m1 is an instance method public void m1(){ public static void m() Can call -static vars, methods i = i + k + m2(i, k); public void m2() // correct since instance and static var and methods // can be used in an instance method public void m2(int m, int n){ return(int)(math.pow(m, n)); Can call -static vars,methods -instance vars,methods 43

44 JDK 1.5:import static variables, methods New JDK 1.5 feature to directly import static variables and methods from a class The imported data and methods can be referenced or called without specifying a class import static packagename.classname.*; * represent all static variables and static methods import static java.lang.math.*; import static java.lang.math.*; public class Test { public static void main(string[] args) { System.out.println(abs( outprintln(abs(-7.8)); System.out.println(max(9,100)); System.out.println(PI); If use: import static java.lang.math.abs; 44

45 instance one or a static one? How do you decide whether a variable or method should be an instance one or a static one? A variable or method that is dependent on a specific instance of fthe class should ldbe an instance variable or method. A variable or method that is not dependent on a specific instance of the class should be a static variable or method. radius, //specific use Math class : pow(), abs() // common use Student -studentid -tuitionfees getstudentid() gettuitionfees() instance or static? 45

46 Visibility Modifiers If public or private is not used, then by default the classes, methods and data fields They are accessible by any class in the same package, known packageaccess double radius; public The class, data, or method is visible to any class in any package. private The data or methods can be accessed only by the declaring class. private class Test { //compile error, outer class 46

47 package p1; public class C1 { public class C2 { public int x; void amethod() { int y; C1 o = new C1(); private int z; can access o.x; can access o.y; public void m1() { void m2() { private void m3() { cannot access o.z; o can invoke o.m1(); can invoke o.m2(); cannot invoke o.m3(); ต วแปร ตวแปร z เล นได ท ไหนบ าง? เลนไดทไหนบาง? package p2; import p1.c1; public class C3 { void amethod() { C1 o = new C1(); can access o.x; cannot access o.y; cannot access o.z; can invoke o.m1(); cannot invoke o.m2(); cannot invoke o.m3(); package p1; package p2; class C1 { import p1.c2; public class C2 {... can access C1 public class C3 { cannot access C1; can access C2; The private modifier restricts access to within a class, the default modifier restricts access to within a package, and the public modifier enables unrestricted t access. 47

48 NOTE An object cannot access its private members, as shown in (b). It is OK, however, if the object is declared in its own class, as shown in (a). public class Foo { private boolean x; public static void main(string[] args) { Foo foo = new Foo(); System.out.println(foo.x); System.out.println(foo.convert()); ()) public class Test { public static void main(string[] i args) { Foo foo = new Foo(); System.out.println(foo.x); System.out.println(foo.convert(foo.x)); private int convert(boolean b) { return x? 1 : -1; (a) This is OK because object foo is used inside the Foo class (b) This is wrong because x and convert are private in Foo. 48

49 NOTE Visibility modifiers are used for the members of the class, not local variables inside the method Cause compile error private constructor If you want to prohibit the user from creating an instance of a class private Math() { public class Test{ public static void main(string[] args) { static int m = 5; public int n = 10; private int p = 15; final int x = 100; // ok 49

50 Why Data Fields Should Be private? To protect data. To make class easy to maintain. Accessor/Mutator Methods The get and set methods are used to read and modify private properties. Accessor : public returntype getpropertyname() { Mutator : public void setpropertyname() { Return type boolean : public void ispropetyname() { 50

51 Example of Data Field Encapsulation The - sign indicates private modifier Circle -radius: double The radius of this circle (default: 1.0). -numberofobjects: int The number of circle objects created. +Circle() +Circle(radius: double) +getradius(): double +setradius(radius: double): void +getnumberofobject(): int +getarea(): double Constructs a default circle object. Constructs a circle object with the specified radius. Returns the radius of this circle. Sets a new radius for this circle. Returns the number of circle objects created. Returns the area of this circle. Circle3, TestCircle3 51

52 Immutable Objects and Classes Its class is called an immutable class. If you delete the set methods in the Circle class in the preceding example, the class would be immutable because radius is private and cannot be changed without a set method. If the contents of an object cannot be changed once the object is created, the object is called an immutable object and A class with all private data fields and without mutators t is not necessarily immutable. For example, the following class Student has all private data fields and no mutators (พวกเมธอด set, but it is mutable.) 52

53 Example public class Student t { private int id; private BirthDate birthdate; public class BirthDate { private int year; private int month; private int day; public Student(int ssn, int year, int month, int day) { id = ssn; birthdate = new BirthDate(year, month, day); public int getid() { return id; public BirthDate getbirthdate() { return birthdate; public BirthDate(int newyear, int newmonth, int newday) { year = newyear; month = newmonth; day = newday; public void setyear(int newyear) { year = newyear; public class Test { public static void main(string[] args) { Student student = new Student( , 1970, 5, 3); BirthDate date = student.getbirthdate(); date.setyear(2010); // Now the student birth year is changed! For a class to be immutable, it must mark all data fields private and provide no mutator methods 53 and no accessor methods that would return a reference to a mutable data field object.

54 Passing Objects to Methods Passing by value for primitive type value (the value is passed to the parameter) Passing by value for reference type value (the value is the reference to the object) TestPassObject 54

55 1 public class TestPassObject { 2 /** Main method */ 3 public static void main(string[] args) { 4 // Create a Circle object with radius 1 5 Circle mycircle = new Circle(1); 6 7 // Print areas for radius 1, 2, 3, 4, and 5. 8 intn=5; n printareas(mycircle, n); // See mycircle.radius and times 12 System.out.println("\n" t tl " + "Radius is " mycircle.getradius()); System.out.println("n is " + n); 17 /** Print a table of areas for radius */ 18 public static void printareas(circle c, int times) { 19 System.out.println("Radius \t\tarea"); 20 while (times >= 1) { System.out.println(c.getRadius() + "\t\t" + c.getarea()); c.setradius(c.getradius() + 1); times--; Radius Area

56 Passing Objects to Methods, cont. Stack Space required for the printareas method int times: 5 Circle c: reference Space required for the main method it int n: 5 mycircle: reference Pass by value (here the value is 5) Pass by reference for the object Heap A circle object 56

57 Scope of Variables ab The scope of instance and static variables is the entire class. They can be declared d anywhere inside id a class. public class Circle{ public double getarea(){ return radius*radius*math.pi; private double radius = 1; The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. A local variable must be initialized explicitly before it can be used. 57

58 Local variable same name as class s variable The local variable takes precedence and the class s variable with the same name is hidden 1 public class Foo { 2 int x = 19; 3 int y = 20; 4 public void p(){ 5 int x = 1; 6 System.out.println( x= + x); 7 System.out.println( y= + y); x= 1 public static void main(string[] args) { Foo obj = new Foo(); obj.p(); y= 20 58

59 The this Keyword this.instance_method(); or this.instance_data filed Use this to refer to the object that invokes the instance method. Use this to refer to an instance data field. this(); this(args); Use this to invoke an overloaded constructor of the same class. 59

60 Serving as Proxy to the Calling Object class Foo { int i = 5; static double k = 0; void seti(int i) { this.i = i; static void setk(double k) { Foo.k = k; //Suppose that f1 and f2 are two objects of Foo. Foo f1 = new Foo(); Foo f2 = new Foo(); //Invoking f1.seti(10) is to execute f1.i = 10;// where this is replaced by f1 //Invoking f2.seti(45) is to execute f2.i = 45;// where this is replaced by f2 -ห ามใช -หามใช this ใน static method, compile error this.k = k ; -หล กเล ยงการต งช อซ าก บ fields 60

61 Calling Overloaded Constructor public class Circle { private double radius; public Circle(double radius) { this.radius = radius; public Circle() { this(1.0); this must be explicitly used to reference the data field radius of the object being constructed this is used to invoke another constructor public double getarea() { return this.radius * this.radius * Math.PI; Every instance variable belongs to an instance represented by this, which is normally omitted public class Test{ public static void main(string[] args){ Circle obj = new Circle(); 61

62 Array of Objects Circle[] circlearray = new Circle[10]; An array of objects is actually an array of reference variables. So invoking circlearray[1].getarea() involves two levels of referencing as shown in the next figure. circlearray references to the entire array. circlearray[1] references to a Circle object. 62

63 Array of Objects, cont. Circle[] circlearray = new Circle[10]; circlearray[0] = new Circle(4); circlearray[9] = new Circle(11); circlearray reference circlearray[0] Circle object 0 circlearray[1] Circle object 1 Summarizing the areas of the circles circlearray[9] Circle object 9 TotalArea 63

64 Example: The Loan Class Loan -annualinterestrate: double -numberofyears: int -loanamount: double -loandate: Date +Loan() +Loan(annualInterestRate: double, numberofyears: int, loanamount: double) +getannualinterestrate(): double +getnumberofyears(): int +getloanamount(): double +getloandate(): Date +setannualinterestrate( annualinterestrate: double): void +setnumberofyears( numberofyears: int): void +setloanamount( loanamount: double): void +getmonthlypayment(): double +gettotalpayment(): double The annual interest rate of the loan (default: 2.5). The number of years for the loan (default: 1) The loan amount (default: 1000). The date this loan was created. Constructs a default Loan object. Constructs a loan with specified interest rate, years, and loan amount. Returns the annual interest rate of this loan. Returns the number of the years of this loan. Returns the amount of this loan. Returns the date of the creation of this loan. Sets a new annual interest rate to this loan. Sets a new number of years to this loan. Sets a new amount to this loan. Returns the monthly payment of this loan. Returns the total payment of this loan. Loan,TestLoanClass 64

65 Example: The Course Class Course -name: String -students: String[] -numberofstudents: int +Course(name: String) +getname(): String +addstudent(student: String): void +getstudents(): t String[] +getnumberofstudents(): int The name of the course. The students who take the course. The number of students (default: 0). Creates a Course with the specified name. Returns the course name. Adds a new student to the course list. Returns the students for the course. Returns the number of students for the course. Course,TestCourse 65

66 Example: The StackOfIntegers Class StackOfIntegers -elements: int[] -size: int +StackOfIntegers() +StackOfIntegers(capacity: int) +empty(): boolean +peek(): int +push(value: int): int +pop(): int +getsize(): int An array to store integers in the stack. The number of integers in the stack. Constructs an empty stack with a default capacity of 16. Constructs an empty stack with a specified capacity. Returns true if the stack is empty. Returns the integer at the top of the stack without removing it from the stack. Stores an integer into the top of the stack. Removes the integer at the top of the stack and returns it. Returns the number of elements in the stack. TestStackOfIntegers t 66

67 Implementing StackOfIntegers Class elements[capacity 1] elements[size-1]... top capacity elements[1] elements[0]... bottom size StatckOfIntegers 67

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9 Chapter 6 Arrays Chapter 7 Objects and Classes Chapter 8 Strings and Text I/O Chapter 9 Inheritance and Polymorphism GUI can be covered after 10.2, Abstract Classes Chapter 12 GUI Basics 10.2, Abstract

More information

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. Chapter 9 Objects and Classes 1 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections, loops, methods, and arrays. However, these Java

More information

OO Programming Concepts

OO Programming Concepts Chapter 8 Objects and Classes 1 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections, loops, methods, and arrays. However, these Java

More information

Chapter 9 Objects and Classes. OO Programming Concepts. Classes. Objects. Motivations. Objectives. CS1: Java Programming Colorado State University

Chapter 9 Objects and Classes. OO Programming Concepts. Classes. Objects. Motivations. Objectives. CS1: Java Programming Colorado State University Chapter 9 Objects and Classes CS1: Java Programming Colorado State University Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections, loops,

More information

Chapter 8 Objects and Classes. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Chapter 8 Objects and Classes. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. Chapter 8 Objects and Classes 1 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections, loops, methods, and arrays. However, these Java

More information

OO Programming Concepts. Classes. Objects. Chapter 8 User-Defined Classes and ADTs

OO Programming Concepts. Classes. Objects. Chapter 8 User-Defined Classes and ADTs Chapter 8 User-Defined Classes and ADTs Objectives To understand objects and classes and use classes to model objects To learn how to declare a class and how to create an object of a class To understand

More information

Chapter 9. Objects and Classes

Chapter 9. Objects and Classes Chapter 9 Objects and Classes 1 OO Programming in Java Other than primitive data types (byte, short, int, long, float, double, char, boolean), everything else in Java is of type object. Objects we already

More information

ECOM 2324 COMPUTER PROGRAMMING II

ECOM 2324 COMPUTER PROGRAMMING II ECOM 2324 COMPUTER PROGRAMMING II Object Oriented Programming with JAVA Instructor: Ruba A. Salamh Islamic University of Gaza 2 CHAPTER 9 OBJECTS AND CLASSES Motivations 3 After learning the preceding

More information

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. Chapter 9 Objects and Classes rights reserved. 1 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections, loops, methods, and arrays. However,

More information

Chapter 8 Objects and Classes Part 1

Chapter 8 Objects and Classes Part 1 Chapter 8 Objects and Classes Part 1 1 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity in the real world that can be distinctly

More information

Chapter 8 Objects and Classes

Chapter 8 Objects and Classes Chapter 8 Objects and Classes 1 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections, loops, methods, and arrays. However, these Java

More information

UFCE3T-15-M Object-oriented Design and Programming

UFCE3T-15-M Object-oriented Design and Programming UFCE3T-15-M Object-oriented Design and Programming Block1: Objects and Classes Jin Sa 27-Sep-05 UFCE3T-15-M Programming part 1 Objectives To understand objects and classes and use classes to model objects.

More information

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. Chapter 8 Objects and Classes Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk 1 Motivations After learning the preceding chapters,

More information

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Objects and Classes (contd.) Course Lecture Slides 19 May 2010 Ganesh Viswanathan Objects and Classes Credits: Adapted from CIS3023 lecture

More information

Chapter 8 Objects and Classes

Chapter 8 Objects and Classes Chapter 8 Objects and Classes 1 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections, loops, methods, and arrays. However, these Java

More information

Chapter 3 Objects and Classes

Chapter 3 Objects and Classes Chapter 3 Objects and Classes Topics OO Programming Concepts Creating Objects and Object Reference Variables Constructors Modifiers Instance and Class Variables and Methods Scope of Variables OO Programming

More information

CS 170, Section /3/2009 CS170, Section 000, Fall

CS 170, Section /3/2009 CS170, Section 000, Fall Lecture 18: Objects CS 170, Section 000 3 November 2009 11/3/2009 CS170, Section 000, Fall 2009 1 Lecture Plan Homework 5 : questions, comments? Managing g g Data: objects to make your life easier ArrayList:

More information

OOP Part 2. Introduction to OOP with Java. Lecture 08: Introduction to OOP with Java - AKF Sep AbuKhleiF -

OOP Part 2. Introduction to OOP with Java. Lecture 08: Introduction to OOP with Java - AKF Sep AbuKhleiF - Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017 Lecture 08: OOP Part 2 Instructor: AbuKhleif, Mohammad Noor Sep 2017 AbuKhleiF - 1 Instructor AbuKhleif, Mohammad Noor Computer

More information

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes Based on Introduction to Java Programming, Y. Daniel Liang, Brief Version, 10/E 1 Creating Classes and Objects Classes give us a way of defining custom data types and associating data with operations on

More information

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017 Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017 Lecture 07: OOP Part 1 Instructor: AbuKhleif, Mohammad Noor Sep 2017 Instructor AbuKhleif, Mohammad Noor Computer Engineer (JU

More information

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. Chapter 9 Objects and Classes 1 Objectives Classes & Objects ( 9.2). UML ( 9.2). Constructors ( 9.3). How to declare a class & create an object ( 9.4). Separate a class declaration from a class implementation

More information

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles, Chapter 11 Inheritance and Polymorphism 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Inheritance and Polymorphism Dr. M. G. Abbas Malik Assistant Professor Faculty of Computing and IT (North Jeddah Branch) King Abdulaziz University, Jeddah, KSA mgmalik@kau.edu.sa www.sanlp.org/malik/cpit305/ap.html

More information

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber Chapter 10 Inheritance and Polymorphism Dr. Hikmat Jaber 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the

More information

Chapter 8 Objects and Classes Dr. Essam Halim Date: Page 1

Chapter 8 Objects and Classes Dr. Essam Halim Date: Page 1 Assignment (1) Chapter 8 Objects and Classes Dr. Essam Halim Date: 18-3-2014 Page 1 Section 8.2 Defining Classes for Objects 1 represents an entity in the real world that can be distinctly identified.

More information

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Chapter 1 Introduction to Computers, Programs, and Java Chapter 2 Primitive Data Types and Operations Chapter 3 Selection

More information

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects Islamic University of Gaza Faculty of Engineering Computer Engineering Dept Computer Programming Lab (ECOM 2114) ABSTRACT In this Lab you will learn how to describe objects and classes and how to define

More information

Computer Science II (20073) Week 1: Review and Inheritance

Computer Science II (20073) Week 1: Review and Inheritance Computer Science II 4003-232-01 (20073) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Hardware and Software Hardware Physical devices in a computer system

More information

2 What are the differences between constructors and methods?

2 What are the differences between constructors and methods? 1 Describe the relationship between an object and its defining class. How do you define a class? How do you declare an object reference variable? How do you create an object? How do you declare and create

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Chapter 10. Object-Oriented Thinking

Chapter 10. Object-Oriented Thinking Chapter 10 Object-Oriented Thinking 1 Class Abstraction and Encapsulation Class abstraction is the separation of class implementation details from the use of the class. The class creator provides a description

More information

Programming in the Large II: Objects and Classes (Part 1)

Programming in the Large II: Objects and Classes (Part 1) Programming in the Large II: Objects and Classes (Part 1) 188230 Advanced Computer Programming Asst. Prof. Dr. Kanda Runapongsa Saikaew (krunapon@kku.ac.th) Department of Computer Engineering Khon Kaen

More information

Inf1-OOP. Data Types. Defining Data Types in Java. type value set operations. Overview. Circle Class. Creating Data Types 1.

Inf1-OOP. Data Types. Defining Data Types in Java. type value set operations. Overview. Circle Class. Creating Data Types 1. Overview Inf1-OOP Creating Data Types 1 Circle Class Object Default Perdita Stevens, adapting earlier version by Ewan Klein Format Strings School of Informatics January 11, 2015 HotelRoom Class More on

More information

Chapter 6 Introduction to Defining Classes

Chapter 6 Introduction to Defining Classes Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of

More information

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Lecture Notes Chapter #9_b Inheritance & Polymorphism Lecture Notes Chapter #9_b Inheritance & Polymorphism Inheritance results from deriving new classes from existing classes Root Class all java classes are derived from the java.lang.object class GeometricObject1

More information

Inheritance and Polymorphism. CSE 114, Computer Science 1 Stony Brook University

Inheritance and Polymorphism. CSE 114, Computer Science 1 Stony Brook University Inheritance and Polymorphism CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Motivation Model classes with similar properties and methods: Circles, rectangles

More information

Constructor. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Constructor. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. Constructor Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design these classes so to avoid redundancy? The

More information

OBJECTS AND CLASSES Objectives To describe objects and classes, and use classes to model objects ( 7.2). To define classes with data fields and methods ( 7.2.1). To construct an object using a constructor

More information

Object Oriented System Development Paradigm. Sunnie Chung CIS433 System Analysis Methods

Object Oriented System Development Paradigm. Sunnie Chung CIS433 System Analysis Methods Object Oriented System Development Paradigm Sunnie Chung CIS433 System Analysis Methods OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents

More information

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;

More information

CH. 2 OBJECT-ORIENTED PROGRAMMING

CH. 2 OBJECT-ORIENTED PROGRAMMING CH. 2 OBJECT-ORIENTED PROGRAMMING ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016) OBJECT-ORIENTED

More information

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9 Chapter 6 Arrays Chapter 7 Objects and Classes Chapter 8 Strings Chapter 9 Inheritance and Polymorphism GUI can be covered after 10.2, Abstract Classes Chapter 12 GUI Basics 10.2, Abstract Classes Chapter

More information

Chapter 11 Inheritance and Polymorphism

Chapter 11 Inheritance and Polymorphism Chapter 11 Inheritance and Polymorphism 1 Motivations OOP is built on three principles: Encapsulation (classes/objects, discussed in chapters 9 and 10), Inheritance, and Polymorphism. Inheritance: Suppose

More information

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism rights reserved. 2 Motivation Suppose you want to define classes to model circles, rectangles, and

More information

Lecture 6 Introduction to Objects and Classes

Lecture 6 Introduction to Objects and Classes Lecture 6 Introduction to Objects and Classes Outline Basic concepts Recap Computer programs Programming languages Programming paradigms Object oriented paradigm-objects and classes in Java Constructors

More information

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance Contents Topic 04 - Inheritance I. Classes, Superclasses, and Subclasses - Inheritance Hierarchies Controlling Access to Members (public, no modifier, private, protected) Calling constructors of superclass

More information

Chapter 10 Thinking in Objects. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved.

Chapter 10 Thinking in Objects. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. Chapter 10 Thinking in Objects 1 Motivations You see the advantages of object-oriented programming from the preceding chapter. This chapter will demonstrate how to solve problems using the object-oriented

More information

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding Java Class Design Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding eugeny.berkunsky@gmail.com http://www.berkut.mk.ua Objectives Implement encapsulation Implement inheritance

More information

COE318 Lecture Notes Week 4 (Sept 26, 2011)

COE318 Lecture Notes Week 4 (Sept 26, 2011) COE318 Software Systems Lecture Notes: Week 4 1 of 11 COE318 Lecture Notes Week 4 (Sept 26, 2011) Topics Announcements Data types (cont.) Pass by value Arrays The + operator Strings Stack and Heap details

More information

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide AP Computer Science Chapter 10 Implementing and Using Classes Study Guide 1. A class that uses a given class X is called a client of X. 2. Private features of a class can be directly accessed only within

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 02: Using Objects MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Using Objects 2 Introduction to Object Oriented Programming Paradigm Objects and References Memory Management

More information

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class. Name: Covers Chapters 1-3 50 mins CSCI 1301 Introduction to Programming Armstrong Atlantic State University Instructor: Dr. Y. Daniel Liang I pledge by honor that I will not discuss this exam with anyone

More information

Practice Questions for Chapter 9

Practice Questions for Chapter 9 Practice Questions for Chapter 9 MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) An object is an instance of a. 1) A) program B) method C) class

More information

Chapter 2 Primitive Data Types and Operations. Objectives

Chapter 2 Primitive Data Types and Operations. Objectives Chapter 2 Primitive Data Types and Operations Prerequisites for Part I Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Chapter 1 Introduction to Computers, Programs,

More information

Questions Answer Key Questions Answer Key Questions Answer Key

Questions Answer Key Questions Answer Key Questions Answer Key Benha University Term: 2 nd (2013/2014) Class: 2 nd Year Students Subject: Object Oriented Programming Faculty of Computers & Informatics Date: 26/4/2014 Time: 1 hours Exam: Mid-Term (A) Name:. Status:

More information

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub Lebanese University Faculty of Science Computer Science BS Degree Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub 2 Crash Course in JAVA Classes A Java

More information

Inf1-OP. Creating Classes. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics

Inf1-OP. Creating Classes. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics Inf1-OP Creating Classes Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein School of Informatics February 26, 2018 Creating classes Last time we saw how to use a class: create a

More information

Chapter 2 Elementary Programming

Chapter 2 Elementary Programming Chapter 2 Elementary Programming Part I 1 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from this chapter, you will learn how to solve practical

More information

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and Classes CS 251 Intermediate Programming Methods and Classes Brooke Chenoweth University of New Mexico Fall 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

CS 251 Intermediate Programming Methods and More

CS 251 Intermediate Programming Methods and More CS 251 Intermediate Programming Methods and More Brooke Chenoweth University of New Mexico Spring 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

Class Foo. instance variables. instance methods. Class FooTester. main {

Class Foo. instance variables. instance methods. Class FooTester. main { Creating classes Inf1-OP Creating Classes Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein School of Informatics February 26, 2018 Last time we saw how to use a class: create a

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 03: Creating Classes MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Creating Classes 2 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation

More information

Chapter 10 Thinking in Objects. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

Chapter 10 Thinking in Objects. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited Chapter 10 Thinking in Objects Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited 2015 1 Motivations You see the advantages of object-oriented programming

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 02: Using Objects MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Using Objects 2 Introduction to Object Oriented Programming Paradigm Objects and References Memory Management

More information

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview Introduction to Visual Basic and Visual C++ Introduction to Java Lesson 13 Overview I154-1-A A @ Peter Lo 2010 1 I154-1-A A @ Peter Lo 2010 2 Overview JDK Editions Before you can write and run the simple

More information

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring Java Outline Java Models for variables Types and type checking, type safety Interpretation vs. compilation Reasoning about code CSCI 2600 Spring 2017 2 Java Java is a successor to a number of languages,

More information

CS1150 Principles of Computer Science Objects and Classes

CS1150 Principles of Computer Science Objects and Classes CS1150 Principles of Computer Science Objects and Classes Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Colorado Springs Object-Oriented Thinking Chapters 1-8

More information

Lecture 7 Java SE Multithreading

Lecture 7 Java SE Multithreading Lecture 7 Java SE Multithreading presentation Java Programming Software App Development Cristian Toma D.I.C.E/D.E.I.C Department of Economic Informatics & Cybernetics www.dice.ase.ro Cristian Toma Business

More information

Chapter 1-9, 12-13, 18, 20, 23 Review Slides. What is a Computer?

Chapter 1-9, 12-13, 18, 20, 23 Review Slides. What is a Computer? Chapter 1-9, 12-13, 18, 20, 23 Review Slides CS1: Java Programming Colorado State University Original slides by Daniel Liang Modified slides by Chris Wilcox rights reserved. 1 What is a Computer? A computer

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 03: Creating Classes MOUNA KACEM mouna@cs.wisc.edu Spring 2019 Creating Classes 2 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation

More information

Lecture 7: Classes and Objects CS2301

Lecture 7: Classes and Objects CS2301 Lecture 7: Classes and Objects NADA ALZAHRANI CS2301 1 What is OOP? Object-oriented programming (OOP) involves programming using objects. An object represents an entity in the real world that can be distinctly

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 20, 2014 Abstract

More information

Object Oriented Programming

Object Oriented Programming Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2114) Lab 11 Object Oriented Programming Eng. Mohammed Alokshiya December 16, 2014 Object-oriented

More information

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. and Java. Chapter 2 Primitive Data Types and Operations

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. and Java. Chapter 2 Primitive Data Types and Operations Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Chapter p 1 Introduction to Computers, p, Programs, g, and Java Chapter 2 Primitive Data Types and Operations Chapter

More information

Chapter 2. Elementary Programming

Chapter 2. Elementary Programming Chapter 2 Elementary Programming 1 Objectives To write Java programs to perform simple calculations To obtain input from the console using the Scanner class To use identifiers to name variables, constants,

More information

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

IST311. Advanced Issues in OOP: Inheritance and Polymorphism IST311 Advanced Issues in OOP: Inheritance and Polymorphism IST311/602 Cleveland State University Prof. Victor Matos Adapted from: Introduction to Java Programming: Comprehensive Version, Eighth Edition

More information

1 Shyam sir JAVA Notes

1 Shyam sir JAVA Notes 1 Shyam sir JAVA Notes 1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write

More information

What is Inheritance?

What is Inheritance? Inheritance 1 Agenda What is and Why Inheritance? How to derive a sub-class? Object class Constructor calling chain super keyword Overriding methods (most important) Hiding methods Hiding fields Type casting

More information

COMPUTER APPLICATIONS

COMPUTER APPLICATIONS COMPUTER APPLICATIONS (Theory) (Two hours) Answers to this Paper must be written on the paper provided separately. You will not be allowed to write during the first 15 minutes. This time is to be spent

More information

What is a Computer? Chapter 1-9, 12-13, 18, 20, 23 Review Slides

What is a Computer? Chapter 1-9, 12-13, 18, 20, 23 Review Slides Chapter 1-9, 12-13, 18, 20, 23 Review Slides What is a Computer? A computer consists of a CPU, memory, hard disk, floppy disk, monitor, printer, and communication devices. CS1: Java Programming Colorado

More information

Object-Oriented Programming Concepts

Object-Oriented Programming Concepts Object-Oriented Programming Concepts Real world objects include things like your car, TV etc. These objects share two characteristics: they all have state and they all have behavior. Software objects are

More information

Thinking in Objects. CSE 114, Computer Science 1 Stony Brook University

Thinking in Objects. CSE 114, Computer Science 1 Stony Brook University Thinking in Objects CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Immutable Objects and Classes 2 Immutable object: the contents of an object cannot be changed

More information

Motivations. Chapter 2: Elementary Programming 8/24/18. Introducing Programming with an Example. Trace a Program Execution. Trace a Program Execution

Motivations. Chapter 2: Elementary Programming 8/24/18. Introducing Programming with an Example. Trace a Program Execution. Trace a Program Execution Chapter 2: Elementary Programming CS1: Java Programming Colorado State University Original slides by Daniel Liang Modified slides by Chris Wilcox Motivations In the preceding chapter, you learned how to

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 21, 2013 Abstract

More information

3.1 Class Declaration

3.1 Class Declaration Chapter 3 Classes and Objects OBJECTIVES To be able to declare classes To understand object references To understand the mechanism of parameter passing To be able to use static member and instance member

More information

Introduction to Programming Using Python Lecture 4. Dr. Zhang COSC 1437 Fall, 2018 October 11, 2018

Introduction to Programming Using Python Lecture 4. Dr. Zhang COSC 1437 Fall, 2018 October 11, 2018 Introduction to Programming Using Python Lecture 4 Dr. Zhang COSC 1437 Fall, 2018 October 11, 2018 Chapter 7 Object-Oriented Programming Object-oriented programming (OOP) involves programming using objects.

More information

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java. Classes Introduce to classes and objects in Java. Classes and Objects in Java Understand how some of the OO concepts learnt so far are supported in Java. Understand important features in Java classes.

More information

Computer Science II (20082) Week 1: Review and Inheritance

Computer Science II (20082) Week 1: Review and Inheritance Computer Science II 4003-232-08 (20082) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Syntax and Semantics of Formal (e.g. Programming) Languages Syntax

More information

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension. Java How to Program, 9/e Education, Inc. All Rights Reserved. } Covered in this chapter Classes Objects Methods Parameters double primitive type } Create a new class (GradeBook) } Use it to create an object.

More information

Java Classes, Objects, Inheritance, Abstract and Interfaces Recap

Java Classes, Objects, Inheritance, Abstract and Interfaces Recap Java Classes, Objects, Inheritance, Abstract and Interfaces Recap CSE260, Computer Science B: Honors Stony Brook University http://www.cs.stonybrook.edu/~cse260 1 Objectives To recap classes, objects,

More information

JAVA: A Primer. By: Amrita Rajagopal

JAVA: A Primer. By: Amrita Rajagopal JAVA: A Primer By: Amrita Rajagopal 1 Some facts about JAVA JAVA is an Object Oriented Programming language (OOP) Everything in Java is an object application-- a Java program that executes independently

More information

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors Agenda

More information

Full download all chapters instantly please go to Solutions Manual, Test Bank site: testbanklive.com

Full download all chapters instantly please go to Solutions Manual, Test Bank site: testbanklive.com Introduction to Java Programming Comprehensive Version 10th Edition Liang Test Bank Full Download: http://testbanklive.com/download/introduction-to-java-programming-comprehensive-version-10th-edition-liang-tes

More information

COMP 250 Winter 2011 Reading: Java background January 5, 2011

COMP 250 Winter 2011 Reading: Java background January 5, 2011 Almost all of you have taken COMP 202 or equivalent, so I am assuming that you are familiar with the basic techniques and definitions of Java covered in that course. Those of you who have not taken a COMP

More information

Object Oriented Programming in C#

Object Oriented Programming in C# Introduction to Object Oriented Programming in C# Class and Object 1 You will be able to: Objectives 1. Write a simple class definition in C#. 2. Control access to the methods and data in a class. 3. Create

More information

COP 3330 Final Exam Review

COP 3330 Final Exam Review COP 3330 Final Exam Review I. The Basics (Chapters 2, 5, 6) a. comments b. identifiers, reserved words c. white space d. compilers vs. interpreters e. syntax, semantics f. errors i. syntax ii. run-time

More information

CS171:Introduction to Computer Science II

CS171:Introduction to Computer Science II CS171:Introduction to Computer Science II Department of Mathematics and Computer Science Li Xiong 1/24/2012 1 Roadmap Lab session Pretest Postmortem Java Review Types, variables, assignments, expressions

More information

CMSC 132: Object-Oriented Programming II

CMSC 132: Object-Oriented Programming II CMSC 132: Object-Oriented Programming II Java Support for OOP Department of Computer Science University of Maryland, College Park Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation

More information

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012)

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012) COE318 Lecture Notes: Week 3 1 of 8 COE318 Lecture Notes Week 3 (Week of Sept 17, 2012) Announcements Quiz (5% of total mark) on Wednesday, September 26, 2012. Covers weeks 1 3. This includes both the

More information

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail. OOP in Java 1 Outline 1. Getting started, primitive data types and control structures 2. Classes and objects 3. Extending classes 4. Using some standard packages 5. OOP revisited Parts 1 to 3 introduce

More information