Post on 14-Oct-2019
Informatica 2e semester: HOC 3
Informatica
Les 3
Elektronica – Interfaces -Overschrijven
Jan Lemeire
Informatica 2e semester
februari – mei 2018
Vandaag1. Deel III: hoofdstuk 1 – binair rekenen
2. Deel III: hoofdstuk 2
3. Bibliotheekklassen Set & Map
4. Interfaces
5. Oefening
6. Overerving en overschrijven
Leibniz’ droom
De “Calculus ratiocinator”
Een logisch denkend apparaat
Informatica 2e semester: HOC 3
1646 – 1716
Go digital! Go binary!
Van analoog naardigitaal…
Informatica 2e semester: HOC 3
Binair rekenen?
1 input => 1 outputMaar 1 niet-triviale functie: NOT
2 inputs => 1 output
Genoeg voor een basis: AND, OR, XOR, NOT
Alle functies (met meer inputs) zijn samen testellen uit deze!!
0 1
0 ? ?
1 ? ?
24 = 16 mogelijkheden
Informatica II: les 2
0 1
0 0 0
1 0 0
0 1
0 0 1
1 0 1
0 1
0 1 1
1 1 1
0 1
0 1 0
1 1 0
0 1
0 1 1
1 0 0
0 1
0 0 0
1 1 1
0 1
0 0 1
1 1 0
0 1
0 1 0
1 0 1
0 1
0 0 1
1 1 1
0 1
0 1 0
1 0 0
0 1
0 0 0
1 0 1
0 1
0 1 1
1 1 0
0 1
0 0 1
1 0 0
0 1
0 1 0
1 1 1
0 1
0 0 0
1 1 0
0 1
0 1 1
1 0 1
triviaal exclusive or (xor)
or
and
2 inputs => 1 output: 16 mogelijkheden
basisoperaties op bits en bytes
a=60 0 0 1 1 1 1 0 0
b=13 0 0 0 0 1 1 0 1
~a Not 1 1 0 0 0 0 1 1
a & b And 0 0 0 0 1 1 0 0
a | b Or 0 0 1 1 1 1 0 1
a ^ b Exclusive or 0 0 1 1 0 0 0 1
a << 3 Shift left 1 1 1 0 0 0 0 0
a >> 3 Shift right 0 0 0 0 0 1 1 1
Zie programma BitwiseOperators in package voorbeelden
0 0
0 1
0 1ab0
10 1
1 1
0 1ab0
1
0 1
1 0
0 1ab0
1
And Or Exclusive or
Programma van vorige tabel
public class BitwiseOperators {public static void main(String[] args) {
int a = 60;/* 60 = 0011 1100 */int b = 13;/* 13 = 0000 1101 */int c = 0;
c = a & b; /* 12 = 0000 1100 and */System.out.println("a & b = " + c );
c = a | b; /* 61 = 0011 1101 or */System.out.println("a | b = " + c );
c = a ^ b; /* 49 = 0011 0001 exclusive or (xor) */System.out.println("a ^ b = " + c );
c = ~a; /*-61 = 1100 0011 not */System.out.println("~a = " + c );
c = a << 2; /* 240 = 1111 0000 shift left */System.out.println("a << 2 = " + c );
c = a >> 2; /* 15 = 1111 shift right */System.out.println("a >> 2 = " + c );
}}
Enkele toepassingenGegeven: integers x & i
int x = 85, i = 4;
1. “Is de (i+1)-de bit van x gelijk aan 1?”
int y = 1 << i; // y wordt een masker genoemd
if ((x & y) > 0)
System.out.println("De "+(i+1)+"de bit van” +x+" is 1.");
else
System.out.println("De "+(i+1)+"de bit van” + x +" is 0.");
2. “Zet de (i+1)-de bit van x op 1”
i = 1;
y = 1 << i; // een masker met een 1 op plaats i
x |= y; // is identiek aan x = x | y;
System.out.println("x = "+x+": "+(i+1)+ "de bit is nu 1.");
3. “Wat zijn de waarden van de 4e tot en met de 6e bit?”
int z = (x >> 4) & 7; // 7 is hier ook een masker: 111
System.out.println("Bits 4 tot en met 6 van “ +Integer.toBinaryString(x)+"zijn"+Integer.toBinaryString(z))
;
Zie programma
BitwiseOperators in
package voorbeelden
Beide lijnen verbeterd tov cursus
Hoofdstuk 2: Getallenstelsels en codes
Voorstelling van digitale informatie: Binaire getallen
Om de notatie van binaire getallen te verkorten worden ook
de hexadecimale notatie gebruikt. Een hexadecimaal cijfer beschrijft 4
bits en wordt voorgesteld door 0, 1, ..., 9, A, ..., E.
Vb: omzetting van 1110001101
1 1 1 0 0 0 1 1 0 1
3 8 D
Om aan te duiden dat een getal in het hexadecimaal talstelsel is (bvb
tijdens programmeren), voegen we de prefix ‘0x’ toe: 0x38D.
0x11 is dus gelijk aan 17
Hexadecimaal talstelsel
Binair Hex.
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
Binair Hex.
1000 8
1001 9
1010 A
1011 B
1100 C
1101 D
1110 E
1111 F
Tabel staat niet in cursus
Voorstelling van karakters
De huidige computers gebruiken gestandaardiseerde tabellen waarin de alfanumerieke tekens die op het toetsenbord staan geassocieerd worden met binaire getallen. De meestgebruikte is de ASCII tabel.
De 128 tekens van hiernaastkunnen voorgesteld worden met 7 bits (27=128)
Informatica 2e semester: HOC 3
(1) Digitaal & binair
(4) Hardware architectuur
(5) Efficiënt productieproces
(6) Computatietheorie & Software
Electronica: (2) ‘relais’-schakeling,
(3)geheugen
(7) Operating system
(8) Communicatie &
internet
(9) Artificiële
intelligentie
Informatica deel III: technologie, historiek en economische aspecten
Waarmaken van Leibniz’s droom
(0) Het idee
Jan Lemeire 13Pag. / 72
Hoofdstuk 2: De elektronische relais
Hoe elektrisch maken?
1. Geleider
1. Weerstand
2. Spoel
3. Verbinden van componenten
2. Isolator (dielectric)
1. Condensator
2. Scheiden van componenten
Extra component nodig…
Jan Lemeire 15Pag. / 72
Nodig: de relais
Electro-mechanisch
Informatica II: les 3
Naast de geleider, weerstand, capaciteit en spoel hebben we een component nodig met de werking zoals de elektromechanische relais. De relais zorgt ervoordat een signaal een ander signaal kan beïnvloeden.Het inputsignaal bepaalt of de output ‘aan’ of ‘uit’ is . Met deze component kunnen we elektrische schakelingen maken die binair kunnen rekenen. Gebaseerd op de elektromechanische relais bouwde de duitser Zuse een volledig werkende computer die gebruikt werd tijdens de tweede wereldoorlog.
Jan Lemeire 16Pag. / 72
Binair rekenen
Informatica II: les 3
or
and A B C = A and B
C = A or B
A
B
Met deze componenten kan je alles berekenen!
Complexe functies
Algemeen: van n inputs naar m outputs2-naar-m kan je ontbinden als m keer 2-naar-1
n-naar-1 kan je ontbinden door 2 bij 2 samen te nemen
– Bvb {a, b, c, d} => x door {a, b} => e, {c, d} => f en {e, f} => x
Beide samen geeft n-naar-m
– is misschien wel niet het efficientste
Som van bits
Som S van 2 bits A en B. De carriage (C) geeft de bit voor de volgende orde.
Rekening houdend met de carriage bit (Cin) van de vorige orde.
Half adder
Full adder
Som van getallen
Twee getallen (A en B) van 3 bits bij elkaar tellen (S)
Ripple carry adder
Overflow bit
Zie http://parallel.vub.ac.be/education/java/applets.html
Least-significant bits (bits van laagste orde)
WOII: de duitsers
Een werkende electro-mechanischecomputer Konrad Zuse
Door de mechanische componenten beperkt tot vijftien à
twintig operaties per seconde
Jan Lemeire 22Pag. / 72
De Vacuümbuis: elektronische relais
Vrije elektronen in vacuüm
diode versterker
grid
inout
gloeilamp
De spanning (in) van het grid van de vacuümbuis bepaalt de geleidbaarheid tussenanode en kathode. Door een grote spanning op out te zetten, zal een kleininputsignaal versterkt worden. Een signaal met een klein vermogen wordt omgezetin een signaal met een groot vermogen.
Vacuümbuizen
Jan Lemeire 24Pag. / 72Informatica II: les 3
Jan Lemeire 25Pag. / 72
De eerste computer: ENIAC
John Mauchly and John Eckert, 1945
De ENIAC maakte gebruik van vacuümbuizen voor het rekenen. Deze zijn echterduur, groot, onhandig en gaan gemakkelijk stuk.
Jan Lemeire 26Pag. / 72
Van vacuümbuis naar transistor
grid
inout
halfgeleider
Maar de echte doorbraak kwam met de uitvinding van de transistor. 10 jaar (!) werd er in Bell Labs (van AT&T - American Telephone & Telegraph Company) actiefgezocht naar een handige vervanger van de vacuümbuis. Het labo bestond uitpraktische technici, theoretici (fysica, wiskunde, chemie) en creatieve geesten. De eerste succesvolle toepassing was de draagbare transistorradio.
Jan Lemeire 27Pag. / 72
Transistor (MOSFET)
De spanning VG bepaalt de geleidbaarheid van de halfgeleider(semiconductor) tussen Source en Drain
Te gebruiken als versterker
Te gebruiken als relais, voor binaire operaties
halfgeleider
halfgeleider
De halfgeleider gedraagt zich als een isolator als VG=0 en als geleider bij VG>1V. We hebben een elektronische relais!
http://www-
g.eng.cam.ac.uk/mmg/teaching/
linearcircuits/mosfet.html
Jan Lemeire 28Pag. / 72Informatica II: les 4
transistors
Informatica 2e semester: HOC 3
Analoge electronica
Sommeren, integreren, afleiden
http://www.falstad.com/circuit/
1.2.4 Verzameling
Set
Jan Lemeire 31Pag. / 72
De Set
Een wiskundige verzameling dus...
In Java gedefinieerd als interface
Definitie van documentatie:
“A collection that contains no duplicate elements. More formally,
sets contain no pair of elements e1 and e2 such that e1.equals(e2),
and at most one null element.”
Informatica 2e semester: HOC 3
p. 31
Method Summary
booleanadd(E e)
Adds the specified element to this set if it is not already present.
booleanaddAll(Collection<? extends E> c)
Adds all of the elements in the specified collection to this set if they're not already
present (optional operation).
voidclear()
Removes all of the elements from this set.
booleancontains(Object o)
Returns true if this set contains the specified element.
booleancontainsAll(Collection<?> c)
Returns true if this set contains all of the elements of the specified collection.
booleanequals(Object o)
Compares the specified object with this set for equality.
booleanisEmpty()
Returns true if this set contains no elements.
booleanremove(Object o)
Removes the specified element from this set if it is present (optional operation).
booleanremoveAll(Collection<?> c)
Removes from this set all of its elements that are contained in the specified collection.
booleanretainAll(Collection<?> c)
Retains only the elements in this set that are contained in the specified collection.
intsize()
Returns the number of elements in this set (its cardinality).Informatica 2e semester: HOC 3
Jan Lemeire 33Pag. / 72
Bewerkingen
bewerking methode
Unie a U b AddAll()
Doorsnede a ∩ b RetainAll()
Verschil a \ b RemoveAll()
Symmetrische
verschil
(a \ b) U (b \ a) Combinatie van
bovenstaande
Informatica 2e semester: HOC 3
Interfaces
public interface Set<E> extends Collection<E> {
int size();boolean isEmpty();boolean contains(Object o);Iterator<E> iterator();Object[] toArray();<T> T[] toArray(T[] a);boolean add(E e);boolean remove(Object o);boolean containsAll(Collection<?> c);boolean addAll(Collection<? extends E> c);boolean retainAll(Collection<?> c);boolean removeAll(Collection<?> c);void clear();boolean equals(Object o);int hashCode();
}
Set.java
Jan Lemeire 36Pag. / 72
Interfaces
Alle methodes zijn abstract: enkel header, geen implementatie
Klasse die interface implementeert moet alle methodes implementeren
Informatica 2e semester: HOC 3
p. 5
Filosofie:
een interface definieert hoe je met een object communiceert.
Je hoeft de implementatie niet te kennen
Inwisselbare implementatie, efficiëntste implementatie hangt af van situatie
p. 7Pijlers van object-georiënteerde
programmeertalen
Jan Lemeire 38Pag. / 72
Set Object: implementatie
Implementaties voldoen aan het “Set-kontrakt”
Gebruik: TreeSet en HashSetKomen we op terug in hoofdstuk 8
Informatica 2e semester: HOC 3
1.2.5 Mappen
Jan Lemeire 41Pag. / 72
Mappen of dictionaries
Linken object van type A aan objecten van type B.Opzoeken gebeurt op A-objecten, dus A best gesorteerd
Voorbeeld:
Informatica 2e semester: HOC 3
Ida, Jennifer Oprolbare brug
Laurent, Maarten Ultragrabber
Manuka, Stani Moving Tower Ahmedabad
Mitichashvili, Tornike piramide
Nguyen, Kim trap Villa Savoye
Pelicaen, Erik achthoekstructuur
Perez Sotomayor, Lucia Origami
student projectkey value
p. 33
Jan Lemeire 42Pag. / 72
Java map
Interface Map<K,V>Met Type Parameters:
K - the type of keys maintained by this map
V - the type of mapped values
Informatica 2e semester: HOC 3
Map<Student, String> map;
Map methodesvoidclear()
Removes all of the mappings from this map (optional operation).
booleancontainsKey(Object key)
Returns true if this map contains a mapping for the specified key.
booleancontainsValue(Object value)
Returns true if this map maps one or more keys to the specified value.
Set<Map.Entry<K,V
>>
entrySet()
Returns a Set view of the mappings contained in this map.
booleanequals(Object o)
Compares the specified object with this map for equality.
Vget(Object key)
Returns the value to which the specified key is mapped, or null if this map contains no
mapping for the key.
inthashCode()
Returns the hash code value for this map.
booleanisEmpty()
Returns true if this map contains no key-value mappings.
Set<K>keySet()
Returns a Set view of the keys contained in this map.
Vput(K key, V value)
Associates the specified value with the specified key in this map (optional operation).
voidputAll(Map<? extends K,? extends V> m)
Copies all of the mappings from the specified map to this map (optional operation).
Vremove(Object key)
Removes the mapping for a key from this map if it is present (optional operation).
intsize()
Returns the number of key-value mappings in this map.
Collection<V>values()
Informatica 2e semester: HOC 3
Jan Lemeire 44Pag. / 72
Implementaties van Java map
TreeMap en HashMapTree of Hashtabel (zien er later!)
Map<String, String> map;
map = new TreeMap<String, String>();
of
map = new HashMap<String, String>();
Cf. TreeSet en HashSet implementaties van de Set interface
Informatica 2e semester: HOC 3
Klasse-oefening
p. 63
/** Gegeven de volgende klassen: Trapezium, Rechthoek, Vierkant
1. Maak een object aan van elke klasse. Kies je eigen waarden
voor de parameters.
2. Duidt aan welke constructors worden opgeroepen en welke
superconstructors.
3. Geef voor elk object de waarde van de attributen.
4. Bereken de oppervlakte van de trapezium en print deze af.
5. Voeg een methode toe aan Rechthoek die de oppervlakte
berekent.
6. Maak van de rechthoek een subklasse van het trapezium. Dan
zijn de attributen 'breedte' en 'lengte' in feite niet meer
nodig, die mag je dus schrappen. De oppervlaktemethode ook.
7. Vierkant erft alle attributen over, maar heeft er in feite
slechts 1 nodig. Schrap de 'extends Rechthoek', voeg het nodige
attribuut toe en de methode voor oppervlaktebrekening. */
1.2.6 Generics
TER INFO
p. 35
1.3
Vriendenvoorbeeld
Jan Lemeire 50Pag. / 72
Objecten
Informatica 2e semester: HOC 3
p. 36
attributen van
moederklasse
Jan Lemeire 51Pag. / 72
Overerving
Informatica 2e semester: HOC 3
p. 39
Informatica 2e semester: HOC 3
p. 37-38
Jan Lemeire 53Pag. / 72Informatica 2e semester: HOC 3
Jan Lemeire 54Pag. / 72
Ontkenner
Informatica 2e semester: HOC 3
Maak van Elke een ontkenner
Overerving (Inheritance)
met overschrijven van methode
Jan Lemeire 55Pag. / 72
Java’s spelregels
Informatica 2e semester: HOC 3
p. 5
Jan Lemeire 56Pag. / 72
Leugenaar
Informatica 2e semester: HOC 3
Maak van Piet een leugenaar