programming in java unit 1. learning outcome: lo1: understand the principles of programming in java...

32
Programming in Java Unit 1

Upload: janice-flynn

Post on 25-Dec-2015

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

Programming in JavaUnit 1

Page 2: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

• Learning outcome: LO1: Understand the principles of programming in Java LO2: Be able to design Java solutions

• Assessment criteria: AC1.1: Discuss the principles, characteristics and features

of programming in Java. AC2.1: Critically evaluate the environmental flexibility of

programming in Java

• Learning objectives

• After studying this unit, you should be able to: Understand the principles of programming in Java Understand the Java environment Understand the role of Java in distributed networking applications

Page 3: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

Java development environment• The Java Virtual Machine:

• Java is platform independent.

• Programs written in Java can be compiled just once and be run on any operating system by making use of the Java Virtual Machine (JVM).

• JVM is part of the Java Development Kit (JDK) and the foundation of the Java platform.

• A virtual machine is a software application that that simulates a computer but hides the underlying operating system and hardware from the programs that interact with it.

• The JVM understands the environment and version of Java in which bytecode were compiled.

• The JVM is invoked by the java command.

• For example to execute a Java application called Mike, using the Command Prompt window you type javac Mike.java to use the Javac compiler to convert the java file Mike.java to a bytecode file Mike.class.

• Then you run the compiled file by typing java Mike, and this then executes the JVM and therefore will initiate all necessary steps to execute the program.

Page 4: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

5 Phases in programming1. Edit

This is where you create your program. It consists of editing a file with an editor program (editor). This means you type a Java program using the editor, you can therefore make any changes and save it on

any secondary storage device with file extension .java.  For example Mike.java.

2. Compile In this phase you use the command javac (Java compiler) to compile a program. For example javac Mike.java within the command prompt. This means that the Java compiler will translate Java source code into byte codes. Byte codes will be executed by the Java Virtual machine.

3. Load At this stage the JVM places the program in memory to execute it (loading). This basically means that the JVM’s class loader takes the .class files containing the byte code of the program

and puts them into primary memory.

4. Verify As the classes are loaded, the byte code verifier checks the byte code to ensure that they are valid and do

not violate security restrictions.

5. Execute JVM executes the program’s byte code i.e. performing actions specified by the program. Before JVM was an interpreter for Java byte codes. This had a problem of causing the programs to execute slowly. Nowadays they use Just In Time compilation (JIT).

Page 5: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment
Page 6: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

Java class libraries/API packages• These are the pre-defined classes that are grouped into categories of

related classes called packages.

• All together they form the Java Application Programming Interface or the Java class libraries.

•  A package is also defined as a namespace that organizes a set of related classes and interfaces.

• Conceptually you can think of packages as being similar to different folders on your computer.

• You might keep HTML pages in one folder, images in another, and scripts or applications in yet another.

• The next table shows some of the examples of packages and their descriptions.

Page 7: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

Package Description

java.appletThe Java Applet Package contains a class and several interfaces for creating Java applets - programs that execute in web browsers.

 

java.awt

The Java Abstract Window Toolkit Package contains the classes and interfaces required to create and manipulate GUIs in early versions of Java. In current versions of Java, the Swing GUI components of the javax.swing packages are typically used instead.

 java.awt.event

The Java Abstract Window Toolkit Event Package contains classes and interfaces that enable event handling for GUI components in both the java.awt and javax.swing packages.

java.awt.geomThe Java 2D Shapes Package contains classes and interfaces for working with Java’s advanced two-dimensional graphics capabilities.

java.ioThe Java Input/Output Package contains classes and interfaces that enable programs to input and output data

 java.lang

The Java Language Package contains classes and interfaces that are required by many Java programs. This package is imported by the compiler into all programs.

 java.net

The Java Networking Package contains classes and interfaces that enable programs to communicate via computer networks like the Internet.

 

java.text

The Java Text Package contains classes and interfaces that enable programs to manipulate numbers, dates, characters and strings. The package provides internationalisation capabilities that enable aprogram to be customized to locales.

 java.util

The Java Utilities Package contains utility classes and interfaces that enable such actions as date and time manipulations, random-number processing and the storing and processing of large amounts of data.

 javax.swing

The Java Swing GUI Components Package contains classes and interfaces for Java’s Swing GUI components that provide support for portable GUIs.

 javax.swing.event

The Java Swing Event Package contains classes and interfaces that enable event handling (e.g., responding to button clicks) for GUI components in package javax.swing

Page 8: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

Variables• In most programming languages, variables are strongly-typed.

• What this means is that you need a specific type of variable to store a specific type of value.

• You cannot, for example, store a name in a variable intended to hold numbers.

• The following represents the different types of variables in object oriented programming:

• Instance variables: These are variables that are used to store the state of an object (for example, color). Every object created from a class definition would have its own copy of the variable. They are variables within the class but declared outside any method. The variable is valid for and occupies storage for as long as the corresponding object is in

memory. Lines 7 and 8 in the code below show how instance variables to describe every Car object

are declared in a class called Car.

Page 9: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment
Page 10: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

• Class variables: These variables are explicitly defined within the class-level scope with a

static modifier. Because these variables are defined with the static modifier, there would

always be a single copy of these variables no matter how many times the class has been instantiated.

They live as long as the class is loaded in memory. Line 10 in the code below shows a declaration of a static variable model in a

class Car.

Page 11: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

• Parameters or Arguments: These are variables passed into a method signature (for example,

maxSpeed). They are not attached to modifiers i.e. public, private, protected or static. They can be used everywhere in the method. They are in memory during the execution of the method and can't be used

after the method returns. Line 12 in the code below shows how an argument maxSpeed is used in a

method called accelerate.

Page 12: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

• Local variables: These variables are defined and used specifically within the method-level

scope (for example, currentSpeed) but not in the method signature. They do not have any modifiers attached to it. They are destroyed when method finishes execution completely. Code below shows the method accelerate that has one argument maxSpeed. Line 14 shows a declaration of a local variable currentSpeed that can only be

used inside the method accelerate.

Page 13: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

Data types• During run time, variables and all the information they store are kept

in the computer's memory for access.

• Think of a computer's memory as a table of data — where each cell corresponds to a variable.

• Upon creating a variable, we basically create a new address space and give it a unique name.

• Java goes one step further and lets you define what you can place within the variable — in which is called variable data type.

• So, you essentially have to do two things in order to create a variable: Create a variable by giving it a unique name. Define a data type for the variable.

Page 14: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

• Line 14 below shows an example of a variable declaration.

• The name of the variable is currentSpeed.

• The variable has been declared as of type int, meaning currentSpeed is a location in the computer memory that is ready to hold any integer value assigned or placed in it during program execution.

Page 15: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

• Data types are divided into two - primitive and reference types.

• Every variable, whether it’s of a primitive type or of a class type, is implemented as a location in computer memory.

• For a variable of a primitive type, the value of the variable is stored in the memory location assigned to the variable.

• So, if an integer variable is declared as “int x = 3″, then when we look at the memory location of “x”, there will be a “3″ stored there just as expected.

Page 16: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

• However, a variable of a reference type only stores the memory address of where the object is located - not the values inside the object.

• So, if we have a class called “SomeClass”, when we create an object like this: “SomeClass anObject”, then when we look at “anObject” in memory, we will see that it does not store any of the variables that belong to that object in memory.

• Instead, the variable “anObject” just stores an address of another place in memory where all the details of “anObject” reside.

• This means that the object named by the variable is stored in some other location in memory and the variable contains only the memory address of where the object is stored.

• This memory address is called a reference to the object.

Page 17: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

Java data types

Page 18: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

Type Description Example

stringUsed to store a sequence of characters, suchas a word or sentence

String name =”Michael”;

intUsed to store an integer cannot be used tostore floating point numbers

int passMark = 60;

double Used to store a decimal number double pi = 3.14;

char Used to store a single character char gender=”m”;

bool Used to store a true or false value bool passed = true;

Page 19: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

OO Programming: Objects• Everywhere you look in the real world you see objects—people,

animals, plants, cars, planes, buildings, and computers and so on.

• Humans think in terms of objects.

• Telephones, houses, traffic lights, microwave ovens and water coolers are just a few more objects.

• An object has characteristics -Attributes and behaviors.

• Let us analyze a ball as an object.

• An attribute is a piece of information about an object; hence the ball has attributes color, size and weight.

• The ball also exhibits behaviors e.g. a ball can bounce, roll and inflate.

Page 20: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

• A computer program consists of a collection of objects interacting with each other by sending messages to achieve certain results.

• Object-oriented programming (OOP) involves programming using objects.

• An object is defined as an instance of a class.

• When we create a new object, we say that we have instantiated that object.

• Another word for instantiated is created.

• An object-oriented programming language combines many separate self-contained objects to form a complete program.

• Each object contains its own program logic which defines what the object is and how it can interact with other objects.

• In a nutshell, an object has a unique identity, state and behavior.

Page 21: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

• A state of an object, also known as properties or attributes is represented by data fields with their current value.

• For example, a rectangle object has data fields width and height, which are the properties that characterize a rectangle.

width

height

Page 22: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

Attributes• An attribute is a piece of information about an object.

• Attributes are a property of objects that helps describe an object, giving it state. e.g. a car object can be described using its color and year, with values red

and 2007 respectively (state of the object) used to describe the car object.

• Attributes are also referred to as instance variables.

• A variable represents a location in memory that stores a value used during run time.

• All variables are of a certain data type e.g. a variable name may contain a value “Mike” which is of type String.

Page 23: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

Classes• A class is a template for creating objects.

• A class contains characteristics i.e. attributes and behaviors that are common amongst all objects created by the same class.

• Code below shows an example of a class called Car used to create various car objects.

Page 24: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

• According to the Car class, each car object will have the following characteristics:

• Attributes: speed year model

• Methods/Behaviours: reverse() accelerate (int maxSpeed) brake()

Page 25: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

Methods• A method is also known as a ‘function’, ‘behavior’ or ‘operation’.

• We will be using the terms interchangeably throughout the context of the guide.

• Behavior is something which an object does.

• A behavior is also known as operation that is defined by methods.

• Methods are used to ask the object to perform an action.

• For example, a method called getArea() can be invoked on the rectangle class to return its area.

Page 26: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

Events• Any system or program that is created consists of events.

• An event is anything that occurs within the context of the program, for example a button being clicked.

Page 27: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

Event handlers• When an event occurs, the program reacts through methods known as

‘event handlers’.

• For example, if a user clicks on a login button, an event occurs to log the user into the account.

• The event handler is the back-end section of the program which controls the event and logs the user into the account.

Page 28: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

Object oriented programming principles• Abstraction:

An ‘abstraction’ is an idea or concept which is not associated with one specific object.

In programming languages, we create abstract templates (classes) which store the essential attributes and behaviors for a type of object.

For example, you could create an abstract Vehicle class which would define all the attributes and behaviors common to all vehicles.

Because the Vehicle class would be abstract, you would not be able to create objects from it.

It is more of a template from which you can create other classes through inheritance (which can then be used to create objects).

Page 29: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

• Encapsulation: Encapsulation, also known as information-hiding, is a principle of object

oriented programming which states that all the attributes and behaviors of an object should be grouped together in one data type or class.

This means objects may know how to communicate with one another across well-defined interfaces but are not allowed to know how other objects are implemented, thus implementation details are hidden within the objects themselves.

Page 30: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

• Inheritance: Inheritance is a core principle of object-oriented programming which allows

one class to inherit all the attributes and behaviors of another class. This allows you to specialize a class by inheriting from it and providing

additional functionality (attributes and behaviors).

Page 31: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

• Polymorphism: The principle of polymorphism allows us to override behaviors inherited from

a parent class such that the derived class (the class which inherits from the parent) has behaviors that work differently to the parent class, but share the same name.

In a nutshell it allows you to write code that process objects that share the same superclass.

Page 32: Programming in Java Unit 1. Learning outcome:  LO1: Understand the principles of programming in Java  LO2: Be able to design Java solutions Assessment

Test your knowledge• Complete Self review exercises from your e-book, p. 33 - 36.

•  Complete exercises from the Toolbox: Individual exercise: 2.2 & 2.3. Tutorial folder: Tutorial 1 and Configuring the Environment Lab exercise: 1