12.07.2015 Views

MAS-02-en [tryb zgodności] - pjwstk

MAS-02-en [tryb zgodności] - pjwstk

MAS-02-en [tryb zgodności] - pjwstk

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Castingo Why?o An examplevoid casts( ) {int i = 200;long l = (long)i;long l2 = (long)200;}Design and Analysis of Information Systems (<strong>MAS</strong>), lecture <strong>02</strong> 12


o IfA Control Flowif(Boolean-expression)statem<strong>en</strong>telsestatem<strong>en</strong>to returno Iterationwhile(Boolean-expression)statem<strong>en</strong>tDesign and Analysis of Information Systems (<strong>MAS</strong>), lecture <strong>02</strong> 13


o do-whiledostatem<strong>en</strong>tA Control Flow (2)while(Boolean-expression);o break, continueo forfor(initialization; Booleanexpression;step)statem<strong>en</strong>tDesign and Analysis of Information Systems (<strong>MAS</strong>), lecture <strong>02</strong> 14


A Control Flow (3)o switchswitch(integral-selector) {}case integral-value1 : statem<strong>en</strong>t; break;case integral-value2 : statem<strong>en</strong>t; break;case integral-value3 : statem<strong>en</strong>t; break;case integral-value4 : statem<strong>en</strong>t; break;case integral-value5 : statem<strong>en</strong>t; break;// ...default: statem<strong>en</strong>t;Design and Analysis of Information Systems (<strong>MAS</strong>), lecture <strong>02</strong> 15


InterfacesooA purposeAn exampleinterface Instrum<strong>en</strong>t {// A constant:// static & finalint I = 5;// Declaration only// a public methodvoid play(Note n);String what();}void adjust();Design and Analysis of Information Systems (<strong>MAS</strong>), lecture <strong>02</strong> 16


Interfaces(2)oImplem<strong>en</strong>tation of the interfaceclass Wind implem<strong>en</strong>ts Instrum<strong>en</strong>t {// windpublic void play(Note n) {System.out.println("Wind.play() " + n);}public String what() { return "Wind"; }public void adjust() {}}class Brass ext<strong>en</strong>ds Wind {public void play(Note n) {System.out.println("Brass.play() " + n);}public void adjust() {System.out.println("Brass.adjust()");}}class Woodwind ext<strong>en</strong>ds Wind {public void play(Note n) {System.out.println("Woodwind.play() " + n);}public String what() { return "Woodwind"; }}Design and Analysis of Information Systems (<strong>MAS</strong>), lecture <strong>02</strong> 17


o Errors:l Compilation time,l Runtimeo Old approachesErrors handlingl A special global variable,l Returning a special value (which one?)o The curr<strong>en</strong>t approach – exceptionsl Prosl Cons (?)Design and Analysis of Information Systems (<strong>MAS</strong>), lecture <strong>02</strong> 19


o The classExceptionso Inheritance from the Exception classo Attributeso Catching and processingtry {// a code which may cause n exception} catch(Type1 id1) {// processing of the exception Type1} catch(Type2 id2) {// processing of the exception Type2}Design and Analysis of Information Systems (<strong>MAS</strong>), lecture <strong>02</strong> 20


Exceptions (2)o A method which does not throw exceptionsoutside but processes them insidevoid f( ) { // ...o A method which warns that it may throw anexceptionvoid f( ) throws TooBigException,DivZeroException{ //...o Which approach is better?Design and Analysis of Information Systems (<strong>MAS</strong>), lecture <strong>02</strong> 21


Exceptions (3)o Catching all exceptionscatch(Exception e) {System.err.println("Caught an exception");}o Rethrowing the exceptioncatch(Exception e) {System.err.println("An exception was thrown");throw e;}o Special case: NullPointerExceptionperson.showName();Design and Analysis of Information Systems (<strong>MAS</strong>), lecture <strong>02</strong> 22


Exceptions (4)o Cleaining up with finallytry {// „Suspicious region”: we expect some exceptions:// A, B or C} catch(A a1) {// processing for A} catch(B b1) {// processing for B} catch(C c1) {// processing for C} finally {}// processed every time!Design and Analysis of Information Systems (<strong>MAS</strong>), lecture <strong>02</strong> 23


o Differ<strong>en</strong>t needsllConstants’ UtilizationA constant value (i.e. a iteration number)A classification of differ<strong>en</strong>t items• Old-school solution (not recomm<strong>en</strong>ded!)public class MainM<strong>en</strong>u {}public static final int MENU_FILE = 0;public static final int MENU_EDIT = 1;public static final int MENU_FORMAT = 2;public static final int MENU_VIEW = 3;• A new apporach (<strong>en</strong>um type)public <strong>en</strong>um MainM<strong>en</strong>u {FILE, EDIT, FORMAT, VIEW};Design and Analysis of Information Systems (<strong>MAS</strong>), lecture <strong>02</strong> 24


To be continued…Design and Analysis of Information Systems (<strong>MAS</strong>), lecture <strong>02</strong> 25

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!