aalborg media lab 21-jun-15 software design lecture 1 “ introduction to java and oop”

23
Aalborg Media Lab Mar 14, 2022 Software Design Lecture 1 “ Introduction to Java and OOP”

Post on 20-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Aalborg Media LabApr 18, 2023

Software Design

Lecture 1

“ Introduction to Java and OOP”

Aalborg Media LabApr 18, 2023

Lecture Overview

• Chapter 1.4 -1.6 of “Java Software Solutions”

• Slides and exercises can be found on: http://www.media.aau.dk/sd

Aalborg Media LabApr 18, 2023

Java Fundamentals

• Java is a object oriented language• Java is accompanied by the

“Java standard library ”• Java can be executed using the web• Is constantly under development

Chapter 1.4

Aalborg Media LabApr 18, 2023

A simple Java Application//**************************************************// Application printing a single line in console//**************************************************class HelloDan{ public static void main(String args[]) //entry point { System.out.println (“What’s the frequency Kenneth?”); }}

Aalborg Media LabApr 18, 2023

Java’s Structure

• Every Java application consists of class definitions• In the Java programming language:

– A program is made up of one or more classes

– A class contains one or more methods

– A method contains program statements

• A Java application always contains a method called main (entry point)

Aalborg Media LabApr 18, 2023

Using Objects

• The System.out object represents a destination to which we can send output

• In the HelloDan program, we invoked the println method of the System.out object:

System.out.println ("What’s the frequency Kenneth?");

object methodinformation provided to the method(parameters)

Aalborg Media LabApr 18, 2023

Using Comments

• Comments allow programmers to communicate their thoughts independent of the code

• Serves as documentation, VERY IMPORTANT !!

• Comments are “inline documentation”

// this is a single line comment

/* This is a block comment

*/

Aalborg Media LabApr 18, 2023

Identifiers and Reserved Words

• Language consists of identifiers– Words we make up

– Words another programmer did choose(for example when using the Java standard library)

– Reserved words

• Java is case sensitive– Class start with capital letter

– Methods start with small letter

Aalborg Media LabApr 18, 2023

Code Style

• Use descriptive names

• Make your code readable by using blank spaces and new lines(Listing 1.2 & 1.3)

Aalborg Media LabApr 18, 2023

About languages

• All programs must be translated to machine language in order to be executed and each type of CPU has its own specific binary machine language (01101…)

• There are four programming language levels:– machine language– assembly language– high-level language– fourth-generation language

Chapter 1.5

Aalborg Media LabApr 18, 2023

Compiler & Interpreters 1/2

• Compilers are small programs translating a language (source code) into another language (target language)

• Interpreters interweaves translation and execution(one line at a time)

Aalborg Media LabApr 18, 2023

Compiler

Aalborg Media LabApr 18, 2023

Java Using Compiler & Interpreter

Aalborg Media LabApr 18, 2023

Syntax & Semantic

• The syntax rules of a language define how we can put together symbols, reserved words, and identifiers to make a valid program

• The semantics of a program statement define what that statement means (its purpose or role in a program)

• A program that is syntactically correct is not necessarily logically (semantically) correct

• A program will always do what we tell it to do, not what we meant to tell it to do

Aalborg Media LabApr 18, 2023

Errors

• The compiler will find syntax errors and other basic problems (compile-time errors)

– If compile-time errors exist, an executable version of the program is not created

• A problem can occur during program execution, such as trying to divide by zero, which causes a program to terminate abnormally (run-time errors)

• A program may run, but produce incorrect results, perhaps using an incorrect formula (logical errors)

Aalborg Media LabApr 18, 2023

Environment

• JDK– compile and execute

• BlueJ (editor)– writing the code

Aalborg Media LabApr 18, 2023

Compiling and Execution

• The JDK and a simple Editor (Notepad) are enough for coding, compiling and execution

Aalborg Media LabApr 18, 2023

BlueJ Environment

• Java based development environment for Java

• BlueJhttp://www.bluej.org/download/download.html

Aalborg Media LabApr 18, 2023

Object-Oriented Programming

• Object is fundamental entity in Java

• Developing software by defining objects that interact with each other

• OOP is mapping real life situations into a program– Mapping objects and their behavior

Chapter 1.6

Aalborg Media LabApr 18, 2023

Problem Solving

• Life cycle of program development– Understand the problem– Design a solution– Considering alternatives / refining– Implementing the solution– Testing / fixing / refining the solution

Aalborg Media LabApr 18, 2023

Concepts/Terminology of OOP

• Object (real object of the problem domain)

• Attribute (characteristics of objects, state)

• Method (objects behavior)

• Class (objects are defined trough classes, concept)– Multiple object can be created from a class

• Encapsulation– Only object itself can change his state (manages itself)

• Inheritance– Reuse of class concepts for similar classes

• Polymorphism

Aalborg Media LabApr 18, 2023

Inheritance

Bank Account

Account

Charge Account

Savings Account

Checking Account

•One class can be used to derive another via inheritance•Classes can be organized into inheritance hierarchies

Aalborg Media LabApr 18, 2023

Exercises

• Use BlueJ for following exercises– JSS 1.1

– JSS 1.3

• Try out the Hello application (BlueJ demos)– Add method stop, printing “Bye, world”.