230 likes | 395 Views
Software Design. Lecture 1 “ Introduction to Java and OOP”. Lecture Overview. Chapter 1.4 -1.6 of “Java Software Solutions” Slides and exercises can be found on: http://www.media.aau.dk/sd. Java Fundamentals. Java is a object oriented language
E N D
Software Design Lecture 1 “ Introduction to Java and OOP” Aalborg Media Lab
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 Lab
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 Lab
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 Lab
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 Lab
object method information provided to the method (parameters) 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?"); Aalborg Media Lab
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 Lab
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 Lab
Code Style • Use descriptive names • Make your code readable by using blank spaces and new lines(Listing 1.2 & 1.3) Aalborg Media Lab
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 Lab
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 Lab
Compiler Aalborg Media Lab
Java Using Compiler & Interpreter Aalborg Media Lab
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 Lab
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 Lab
Environment • JDK • compile and execute • BlueJ (editor) • writing the code Aalborg Media Lab
Compiling and Execution • The JDK and a simple Editor (Notepad) are enough for coding, compiling and execution Aalborg Media Lab
BlueJ Environment • Java based development environment for Java • BlueJhttp://www.bluej.org/download/download.html Aalborg Media Lab
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 Lab
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 Lab
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 Lab
Account Charge Account Bank Account Savings Account Checking Account Inheritance • One class can be used to derive another via inheritance • Classes can be organized into inheritance hierarchies Aalborg Media Lab
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”. Aalborg Media Lab