Beruflich Dokumente
Kultur Dokumente
25-Feb-14
Programming Languages
Programming languages allow programmers to code software. The three major families of languages are:
Machine languages Assembly languages High-Level languages
25-Feb-14
Machine Languages
Comprised of 1s and 0s The native language of a computer Difficult to program one misplaced 1 or 0 will cause the program to fail. Example of code:
1110100010101 10111010110100
3
111010101110 10100011110111
25-Feb-14
Assembly Languages
Assembly languages are a step towards easier programming. Assembly languages are comprised of a set of elemental commands which are tied to a specific processor. Assembly language code needs to be translated to machine language before the computer processes it. Example: ADD 1001010, 1011010
4 25-Feb-14
High-Level Languages
High-level languages represent a giant leap towards easier programming. The syntax of HL languages is similar to English. Historically, we divide HL languages into two groups:
Procedural languages Object-Oriented languages (OOP)
25-Feb-14
Procedural Languages
Early high-level languages are typically called procedural languages. Procedural languages are characterized by sequential sets of linear commands. The focus of such languages is on structure. Examples include C, COBOL, Fortran, LISP, Perl, HTML, VBScript
25-Feb-14
Object-Oriented Languages
Most object-oriented languages are high-level languages. The focus of OOP languages is not on structure, but on modeling data. Programmers code using blueprints of data models called classes. Examples of OOP languages include C++, Visual Basic.NET and Java.
25-Feb-14
25-Feb-14
25-Feb-14
10
25-Feb-14
OOP - Encapsulation
Incorporation into a class of data & operations in one package Data can only be accessed through that package Information Hiding
11 25-Feb-14
OOP - Inheritance
Allows programmers to create new classes based on an existing class Methods and attributes from the parent class are inherited by the newly-created class New methods and attributes can be created in the new class, but dont affect the parent classs definition
12
25-Feb-14
OOP - Polymorphism
Creating methods which describe the way to do some general function (Example: The drive method in the automobile class) Polymorphic methods can adapt to specific types of objects.
13 25-Feb-14
An object contains both data and methods that manipulate that data
An object is active, not passive; it does things An object is responsible for its own data
But: it can expose that data to other objects
15 25-Feb-14
And methods:
eat, hide, run, dig
17 25-Feb-14
18
25-Feb-14
The operations defined by the ADT are the only operations permitted on its data Example: a CheckingAccount, with operations deposit, withdraw, getBalance, etc. Classes enforce this bundling together
If all data values are private, a class can also enforce the rule that its defined operations are the only ones permitted on the data
19
25-Feb-14
Example of a class
class Employee { // Fields private String name; //Can get but not change private double salary; // Cannot get or set // Constructor Employee(String n, double s) { name = n; salary = s; } // Methods void pay () { System.out.println("Pay to the order of " + name + " $" + salary); } public String getName() { return name; } // getter }
20 25-Feb-14
Approximate Terminology
instance = object field = instance variable method = function sending a message to an object = calling a function These are all approximately true
21
25-Feb-14
22
25-Feb-14
Dialog FileDialog
C++ is different
In C++ there may be more than one root
but not in Java!
In C++ an object may have more than one parent (immediate superclass)
but not in Java!
24
25-Feb-14
25
25-Feb-14
Example of inheritance
class Person { String name; int age; void birthday () { age = age + 1; } } class Employee extends Person { double salary; void pay () { ...} }
Every Employee has name and age fields and birthday method as well as a salary field and a pay method.
26 25-Feb-14
It declares that n is an integer variable It allocates space to hold a value for n For a primitive, this is all that is needed
It declares that secretary is type Employee It allocates space to hold a reference to an Employee For an object, this is not all that is needed This allocate space to hold a value for the Employee Until you do this, the Employee is null
27 25-Feb-14
28
25-Feb-14
Outside a class, you need to say which object you are talking to
if (john.age < 75) john.birthday ();
If you don't have an object, you cannot use its fields or methods!
29
25-Feb-14
this is like an extra parameter to the method You usually don't need to use this
30
25-Feb-14
31
25-Feb-14
34
25-Feb-14
35
25-Feb-14
You can write your own constructors; but if you dont, Java provides a default constructor with no arguments
It sets all the fields of the new object to zero If this is good enough, you dont need to write your own
The syntax for writing constructors is almost like that for writing methods
36
25-Feb-14
37
25-Feb-14
A parameter overrides a field with the same name But you can use this.name to refer to the field class Person { String name; int age; Person (String name, int age) { this.name = name; this.age = age; }
If an Employee is a Person, and a Person is an Object, then when you say new Employee ()
The Employee constructor calls the Person constructor The Person constructor calls the Object constructor The Object constructor creates a new Object The Person constructor adds its own stuff to the Object The Employee constructor adds its own stuff to the Person
39
25-Feb-14
If you don't write a constructor for a class, Java provides one (the default constructor)
The one Java provides has no arguments
If you write any constructor for a class, Java does not provide a default constructor Adding a perfectly good constructor can break a constructor chain You may need to fix the chain
40 25-Feb-14
Now you can only create Employees with names This is fair, because you can only create Persons with names
42
25-Feb-14
There is exactly one copy of a class variable, not one per object Use the special keyword static to say that a field or method belongs to the class instead of to objects
45
25-Feb-14
Make as much as possible private Your class is responsible for its own data; dont allow other classes to screw it up!
47 25-Feb-14
Kinds of access
Java provides four levels of access:
public: available everywhere protected: available within the package (in the same subdirectory) and to all subclasses [default]: available within the package private: only available within the class itself
The default is called package visibility In small programs this isn't important...right?
49
25-Feb-14
The End
50
25-Feb-14