Docsity

OOP Bootcamp - Introduction to Computer Science and Programming - Lecture S, Lecture notes of Computer Security

Partial preview of the text

Download OOP Bootcamp - Introduction to Computer Science and Programming - Lecture S and more Lecture notes Computer Security in PDF only on Docsity!

OOP Bootcamp

What is OOP?

 (^) Object Oriented Programming

 (^) Write programs as collections of classes:

 (^) Each class is a type.  (^) Each class defines the behaviour of one or more objects of that class.  (^) Java starts the program by invoking the main method of the “driver” class.  (^) The driver's main method may then create other objects and execute other methods, and these methods may do the same, andsoforth ...

Object

 (^) Collection of data and methods.

 (^) Instance of a class.

 (^) Stored in memory, and only while program is running.

 (^) Is a data value (just like 1 is an int).

Test anObject = new Test();

Instance Variables

 (^) Variables defined in class.

 (^) Exist as part of object as long as object exists.

 (^) Can be accessed using dot-notation within methods of

class (or outside class if public).

class Test {

int mark; String name, subject;

}

Parameters and Return Values

 (^) Parameters send set of data values to method.

 (^) Return values get one value back, like a function.

class Test { public void setName ( String aName ) { name = aName; } public String getName () { return name; } }

this, toString, equals

 (^) this is a reference to the current object.  (^) no need to define - automatic  (^) toString is a special method that must return a String representation of the object.  (^) define if needed – needed sometimes  (^) equals is a special method that must return if an object is equal to the one passed in as a parameter.  (^) define if needed – needed sometimes  (^) Mutators (usually per instance variable) directly modify instance variables based on parameters.  (^) define if needed – needed most of the time  (^) Accessors (usually per instance variable) return the values of instance variables.  (^) define if needed – needed most of the time

Information Hiding

 (^) Declare all instance variables as private so they

CANNOT be accessed using dot-notation except from methods in the same class.

 (^) Declare some methods as public so they CAN be

accessed using dot-notation from methods in other classes.

Static Variables/Methods

 (^) Variables/Methods that can be used without an object.

 (^) Defined in class with “static” prefix.

 (^) Instance variables are 1-per-object, static variables

are 1-per-class class Test { private static int counter; public static addOne () { counter++ } }

References

 (^) Java stores objects indirectly by storing a their

memory locations – primitive data is stored directly.

 (^) Assignment causes 2 variables to reference the same

object.

Test t1 = new Test ();

Test t2 = t1;

t1.setName (“hussein”);

System.out.println (t2.getName());

null

 (^) Special value for any class type variable.

 (^) Equivalent to zero or nothing.

 (^) Used where variable does not reference anything

particular.

Copy Constructors

 (^) Initialise an object to be a copy of another object,

passed in as a parameter.

 (^) Results in a deep copy, instead of a shallow copy – a

completely new object, not just a reference.

public Test ( Test another ) { name = another.name; subject = another.subject; marks = another.marks; }

Problem

 (^) Write a program to manage a bank account using a

BankAccount class.

 (^) The BankAccount class should store information

about a single account (name, a/c number, balance).

 (^) The driver class must interact with the user for typical

operations (withdraw, deposit, query, etc.).

Problem

 (^) Write a program to manage the counting of votes in

an election.

 (^) The Election class must have variables to count votes

for ANC/DA/Cope/ID/etc. and associated methods.

 (^) The Vote class must store a single vote.

 (^) The main method must create an Election object, then

create a sequence of Vote objects and send those to the Election object to be counted.

Problem

 (^) Write a program that creates a family tree of Person

objects (defined/hard-coded in the driver class).

 (^) Each Person object should store a reference to a

spouse and links to up to 3 children, with appropriate accessors and mutators.

 (^) The tree should be printable using a single call to the

toString method of the head of the family tree.