Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITIP © Ron Poet Lecture 12 1 Finding Out About Objects.

Similar presentations


Presentation on theme: "ITIP © Ron Poet Lecture 12 1 Finding Out About Objects."— Presentation transcript:

1 ITIP © Ron Poet Lecture 12 1 Finding Out About Objects

2 ITIP © Ron Poet Lecture 12 2 What Methods Does A Console Understand  We have used Console objects for a while.  What methods do they support.  Is there a way of finding out all of them?  Eclipse will show a class outline when we open a.java file.  The Outline view  This lists all the methods, together with their parameters.

3 ITIP © Ron Poet Lecture 12 3 eclipse Methods Explained  A green circle means a public method.  A red square means a private helper method.  You will notice that the FormatIO classes contain quite a lot of methods.  One of the Console method names even has a spelling mistake!

4 ITIP © Ron Poet Lecture 12 4 The char Type

5 ITIP © Ron Poet Lecture 12 5 A Single Character  It is sometimes useful to be able to process single characters.  Character literals use single quotes ‘a’, ‘\n’.  Character variables use the char type.  Different alphabets can be represented by char types.  English, German, Thai, Chinese etc.

6 ITIP © Ron Poet Lecture 12 6 Operations With Characters  char values can be appended to strings.  It is possible to get the individual characters in a string by the charAt method. String greeting = “Hello”; char c = greeting.charAt(0); // c has the value ‘H’ // Strings start with character position 0.

7 ITIP © Ron Poet Lecture 12 7 The switch Statement

8 ITIP © Ron Poet Lecture 12 8 A Specialised Series of Tests  The switch statement is used when we have a special form of chaining.  We test the same variable or expression for different values.  A switch is more efficient and easier to read.  There are some pitfalls to be avoided.

9 ITIP © Ron Poet Lecture 12 9 A daysInMonth Helper Method  Let us assume that months are represented by integers between 1 and 12.  The helper method has the header. private static int daysInMonth(int month) { // code here }

10 ITIP © Ron Poet Lecture 12 10 Chain Version if (month == 4 || month == 6 || month == 9 || month == 11) return 30; else if (month == 2) if (isLeap(year)) return 29; else return 28; else return 31;

11 ITIP © Ron Poet Lecture 12 11 isLeap Helper Method  The previous code assumes we have an isLeap helper method. private static boolean isLeap(int year) { return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; }

12 ITIP © Ron Poet Lecture 12 12 switch Version switch (month) { case 4: case 6: case 9: case 11: return 30; case 2: if (isLeap(year)) return 29; else return 28; default: return 31; }

13 ITIP © Ron Poet Lecture 12 13 switch Syntax  Any expression that will produce an integer or a char can be inside the () after the word switch.  In this case the variable month produces an integer value.  The switch part is followed by a compound statement.  There are a number of cases, each of which must be an integer or char literal.  The programs jumps to the appropriate case.  The statements following are executed until a break, return or end of case is encountered.

14 ITIP © Ron Poet Lecture 12 14 switch Actions  The expression, month in this case, will produce an actual number.  3 if the month is March.  Control will go to the appropriate case if there is one.  Otherwise it will go to the default.

15 ITIP © Ron Poet Lecture 12 15 Fall Through  In the example, the cases 4, 6, 9 and 11 all have the same action.  Control falls through the case until it finds a statement.  Return 30 in this case.  Cases 4, 6, 9 and 11 all have the same sequence of statements.  Accidental fall through is a common error.  In the example the fall through is deliberate.

16 ITIP © Ron Poet Lecture 12 16 switch and break  If we do not want to return from a method then we exit the case with a break.  The switch statement is left as soon as a break is encountered.  Normally the action for each case is different.  Each case must then be terminated by a break before the next case is encountered.

17 ITIP © Ron Poet Lecture 12 17 Example, A Simple Menu  A Console based menu program could use single letters to decide on a series of options.  ‘q’ for quit, ‘s’ for save file, ‘r’ for read file.  We can read them in as single characters with con.getChar().  Then write a switch statement to do the appropriate thing.

18 ITIP © Ron Poet Lecture 12 18 Menu Code con.print(“What next “); char option = con.getChar(); switch (option) { case ‘q’: System.exit(0); break; case ‘s’: saveFile(); break; case ‘r’: readFile(); break; case ‘ ‘: case ‘\t’: case ‘\n’: break; // ignore, user will enter newlines, maybe spaces and tabs default: con.print(“Illegal option”); break; }

19 ITIP © Ron Poet Lecture 12 19 The ?: Statement

20 ITIP © Ron Poet Lecture 12 20 A Specialised Form of if  Many if statements use the test to compute two different values.  The actual value chosen is then used in the same way regardless of whether the test was true or false. if (a > b) max = a; else max = b;

21 ITIP © Ron Poet Lecture 12 21 Using ?:  The previous if statement could be written as: max = (a > b) ? a : b;  There are 3 parts, a test, the true value and the false value.  The part after ? is the true value.  The part after : is the false value.  The above is easier to read (after a while!)

22 ITIP © Ron Poet Lecture 12 22 Months In February  Instead of writing if (isLeap(year)) return 29; else return 28;  We could write return (isLeap(year)) ? 29 : 28;  Both have the same effect.

23 ITIP © Ron Poet Lecture 12 23 Optional Brackets  The brackets around the test are optional.  They show that a test is being made.  The previous statements could be written as max = a > b ? a : b; return isLeap(year) ? 29 : 28;


Download ppt "ITIP © Ron Poet Lecture 12 1 Finding Out About Objects."

Similar presentations


Ads by Google