27
OOP&M - theory lectures 1 OOP&M – presentation •Team presentation •Lab Tove/Lenny •Theory (basics) Thomas •Theory (continuation) David •Personal presentation •26 – Spanish – driver license – no swedish yet, but learning •MSc. Telecommunication Engineering, University of Zaragoza/Spa •6 months working at an ISP (Internet Service Provider) •1 year working at the radio brunch - Teltronic – Management •9 months working at Infineon (Siemens Semicon) – Development •Several projects with Malmo Hogskola (sorry english keyboard)

OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab Tove/Lenny Theory (basics) Thomas Theory (continuation) David Personal presentation

  • View
    228

  • Download
    8

Embed Size (px)

Citation preview

Page 1: OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation

OOP&M - theory lectures 1

OOP&M – presentation

•Team presentation•Lab Tove/Lenny•Theory (basics) Thomas•Theory (continuation) David

•Personal presentation•26 – Spanish – driver license – no swedish yet, but learning•MSc. Telecommunication Engineering, University of Zaragoza/Spain•6 months working at an ISP (Internet Service Provider)•1 year working at the radio brunch - Teltronic – Management•9 months working at Infineon (Siemens Semicon) – Development•Several projects with Malmo Hogskola (sorry english keyboard)

Page 2: OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation

OOP&M - theory lectures 2

•Courses objectives•GUI, I/O, Exceptions, Event Handling, Threads•Reaching a minimum comprehension level (everybody)•Practical work at the laboratory•Satisfying the need of new knowledge (some)

•Description of the classes•There are two different levels of theory classes:

•Thomas leads the basic course (swedish)•David leads the normal course (english)

•Labs will consist in some explanation (code analysis) and in doing some exercises, that should be finished at the end of the Lab hour. You must give a form filled up IN THAT moment (english please) 1 form per LAB each!!

•We need: Pictures•one picture from each one / with name / end of this week / digital format

OOP&M – presentation

Page 3: OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation

OOP&M - theory lectures 3

•Calendar of the classes

OOP&M – presentation

Week Theory basics Theory LAB number

3 Data types, parts of a program, assignment, variables

4 Arrays, strings, arithmetics, type conversion

I/O: Out Monitor and files

5 Boolean, If..Else I/O: In Keyboard and files 1

6 Methods GUI: basics, window 2

7 Loops GUI: button, text field 3

8 ?? Event Handling: mouse I 4

9 Event Handling: mouse II 5

10 Exceptions 6

15 Exceptions 7

16 Threads 8

17 Threads 9

Page 4: OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation

OOP&M - theory lectures 4

OOP&M – presentation

Week Title LAB number

4 None

5 -streaming keyboard-showing information-streaming files

1

6 2

7 -placement of windows-placement of buttons-calculator layout

3

8 4

9 -capturing mouse clicks 5

10 6

15 -exceptions 7

16 -threads 8

17 9

•Calendar of the labs

LAB Objective:Roman number calculator

Page 5: OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation

OOP&M - theory lectures 5

•Information board•There is an information board at David’s studio, it contains several information. Some of that is referred to this course, other to deadlines for applications for conferences or design contests …

•Material (books, notes, web)•Everything that is needed will be published on the web as powerpoint files or adobe acrobat files

•Evaluation methods:•Presence at the courses: 80%•Presence at the lab: 80%•Lab work: 100% or additional work from the 80%•Extra work: “Game of life Seminar – first example for programming artificial life”•Final project collaborating with the MDI course

•Questions:•David will be Wednesdays at the studio: 9:00 – 12:00

OOP&M – presentation

Page 6: OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation

OOP&M - theory lectures 6

OOP&M – presentation

Monday Tuesday Wednesday Thursday Friday

LAB Questions LAB

Theory b Theory

•Your Java Week

•This makes a total of:•30 theory hours•~30 lab hours•~21 basics hours

Page 7: OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation

OOP&M - theory lectures 7

OOP&M – the very beginning

“…at the beginning there was nothing…”- the Hollybook -

Page 8: OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation

OOP&M - theory lectures 8

OOP&M – introduction

computer machine

has got no aim

does nothing by itself

if we want the machine to do somethingit should be PROPERLY feed before

the food is made of programs

the nutrients in the food are machine code

axiom

fact

possible solutions

Page 9: OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation

OOP&M - theory lectures 9

OOP&M – introduction

input output

-joystick-mouse-keyboard-microph.

humanbasedinput

-floppy-HD-CD-ZIP/JAZZ-…

prev.rec.

infor.as

input

-joystick-screen-speakers-lights

humanbasedoutput

-floppy-HD-CD-ZIP/JAZZ-…

infor.as

output

Page 10: OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation

OOP&M - theory lectures 10

OOP&M – a monitor

monitor object

•Most of the programs that we write show information on the monitor •Here we will learn how to use one of Java’s predefined objects to control a monitor

1 monitor 2 roles

•For a user it is a device to read information from •For a program it is a device to display information on

display read

program monitorBjorn

Page 11: OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation

OOP&M - theory lectures 11

OOP&M – the two monitors example

small monitor Bjornbig

monitorLisa

“turn up the brightness of the big monitor!”

-message: “turn up the brightness of the big one!”

-“big one” is a reference to an object

-the monitors are the objects

-“change brightness” is the behavior

-“up” is a part of the further details

Page 12: OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation

OOP&M - theory lectures 12

OOP&M – Mr. Monitor is an … object

Let’s see how a Java program is going to use the monitor for showing information. We want to show the message: “In winter Bjorn and Lisa eat potatoes.”

in Java the computer’s monitor is represented as a predefined object

the object is an instance of the PrintStream class

PrintStream class is one of Java’s predefined classes

Objects are instances of classes

Page 13: OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation

OOP&M - theory lectures 13

OOP&M – Mr. Monitor is an … object

If we want the program to show something on the screen, then we need to use a line like the following:

System.out.println(“ In winter Bjorn and Lisa eat potatoes.”)

We can now compare this situation with the other one that we saw for the other example:•reference

-Lisa: “big one”-Java: System.out

•behavior-Lisa: “turn … brightness”-Java: println

•further details-Lisa: “up”-Java: (“In winter Bj…toes”)

Page 14: OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation

OOP&M - theory lectures 14

OOP&M – Mr. Monitor is an … object

System.out.println(“ In winter Bjorn and Lisa eat potatoes.”)

println(“ In winter Bjorn and Lisa eat potatoes.”)

behavior details

reference to the receiver

message

Sending a message to an object is an action that the programmer specifies and that the computer carries out when the program runs. In Java all actions are specified in STATEMENTS.

Page 15: OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation

OOP&M - theory lectures 15

OOP&M – Mr. Monitor is an … object

Which form will have our program for writing the message to the screen?

import java.io.*class Potatis {

public static void main(String[] arg) {System.out.println(“In winter …”);

}}

Here is very easy to identify the order that makes the program use the library where the predefined classes for input/output of data using the standard interfaces:

import java.io.*

Page 16: OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation

OOP&M - theory lectures 16

OOP&M – my computer eats disks

Most of the mass-storage media organize the information into files. Floppies, HardDrives, ZIP disks, CDs … contain information in the form of files. For many processes we need to store information and not only to show it on the screen. Therefore we will study the use of files, because they are a transparent way of treating the storage of information.

Page 17: OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation

OOP&M - theory lectures 17

OOP&M – a file never lie … it’s so transparent

Advantages of files:•persistence: screen information last as long as it is

shown on the screen•capacity: it can be stored much more data in a file than

can be displayed on the screen or even printed•reusability: convertible between systems

Attributes of files:•contents (data): it can be any

information. From letters to shopping lists, anything.

•file name: the rules for choosing file names vary between operating systems. They are more liberal than the rules for governing identifiers.

SHOPPINGpotatismjolkagg…

shopping.lst

Page 18: OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation

OOP&M - theory lectures 18

OOP&M – a file never lie … it’s so transparent

Operations with files:•create: programs write data into files•delete: removes name and contents•rename: changes name but keeps contents•overwrite: keeps name but changes contents•read: reads data from the contents of the file

SHOPPINGpotatismjolkagg…

shopping.lst

Page 19: OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation

OOP&M - theory lectures 19

OOP&M – Java has got an affair with file

Java provides a predefined class for modeling disk files, called File

the constructor for File accepts the file’s name (a String reference) as its argument

new File (filename)

An example of this would be:

File f1, f2;f1 = new File(“Bjorn_receipt”);f2 = new File(“Lisa_receipt”);

Page 20: OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation

OOP&M - theory lectures 20

OOP&M – Java has got an affair with file

If the files exist the class provides us two methods for directly operate with the files:

DO NOT FORGET THAT:The creation of the instances f1 and f2 doesn’t mean that the files exist!!

File f;f = new File(“delete_me”);f.delete();

delete

File f1, f2;f1 = new File(“change_me”);f2 = new File(“take_my_name”);f1.renameTo(f2);

rename

Page 21: OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation

OOP&M - theory lectures 21

OOP&M – Java has got an affair with file

after this interlude about the files, we can continue talking about the use of the interfaces to Java programs

first we analyzed the screen as an output, now we are going to study the files as an output

for creating or overwriting a file in Java we need exactly the same resource

pathway stream

Page 22: OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation

OOP&M - theory lectures 22

OOP&M – Java streams into files

Java provides a predefined class for modeling a stream of output that goes to a file called FileOutputStream

the constructor for FileOutputStream accepts a reference to a File as its argument

new FileOutoutStream (file)

An example of this would be:

File f;f = new File(“Bjorn_nude_sauna”);FileOutputStream fs = new FileOutputStream(f);

Page 23: OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation

OOP&M - theory lectures 23

OOP&M – Java streams into files

Javaprogram

FileOutput

Stream

SHOPPINGpotatismjolkagg…

shopping.lst

bytes of data

ewsc24rfds53Hej20Hur20m76ar20du20?rsf

Page 24: OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation

OOP&M - theory lectures 24

OOP&M – Java streams into files

Java provides a tool for modeling a stream of bytes sent to a file as if they were sent to a screen

the constructor for PrintStream accepts a reference to a FileOutputStream as its argument

new PrintStream (fileoutputstream)

An example of this would be:

File f;FileOutputStream fs;PrintStream target;f = new File(“data.out”);fs = new FileOutputStream(f);target = new PrintStream(fs);target.println(“Lisa drinks a glass onions”);

Page 25: OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation

OOP&M - theory lectures 25

OOP&M – Java streams into files

summary: how to create or overwrite a file in a line

• Create a File object to represent the file and then use it to• Create a FileOutputStream object to represent the output

pathway to the file and use it to• Create a PrintStream object to provide a convenient output

pathway to the file• Use the print or println methods of PrintStream as

needed to write content to the file

PrintStream target = new PrintStream(new FileOutputStream(

new File(“data.out”)));

Page 26: OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation

OOP&M - theory lectures 26

OOP&M – Java streams into files

Class exercise: create a Java program that writes information both to the screen and to a file called: “Bjorn_Lisa.log”

Consider that Bjorn and Lisa did the following:1) 16:30 Met at Triangeln (she was late)2) 17:00 Drunk coffee at Expresso-House3) 19:15 Met with Lisa’s brother at the cinema4) 21:25 Had dinner at Lillatorg5) 23:00 Had some drinks at the Hippodromen6) 01:00 ?!?!

Answer the question: is that possible? How much costs a night like that?

Page 27: OOP&M - theory lectures1 OOP&M – presentation Team presentation Lab  Tove/Lenny Theory (basics)  Thomas Theory (continuation)  David Personal presentation

OOP&M - theory lectures 27

OOP&M – Java streams into files

import java.io.*class LogMyDay { public static void main(String[] arg) throws Exception{ PrintStream log; FileOutputStream logfileStream; File logfile;

logfile = new File(“Bjorn_Lisa.log”); logfileStream = new FileOutputStream(logfile); log = new PrintStream(logfileStream);

System.out.println(“16:30 Met at Triangeln …”); log.println(“16:30 Met at Triangeln …”);…

}}

one possible solution