1710f14 c11 2014 10 14€¦ · 1 1 click to edit master text styles second level third level fourth...

20
1 1 Click to edit Master text styles Second level Third level Fourth level Fifth level EECS1710 Week 06, Lecture 11 Fall 2014 ! Tuesday, Oct 14, 2014 2 Announcements ePost updated, please check online Next Labtest: Thu Oct 23/Fri Oct 24 covers Ch 2 & 3 Next Term Test: Tue Oct 28 covers Ch 3 & 4

Upload: others

Post on 23-Sep-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1710f14 C11 2014 10 14€¦ · 1 1 Click to edit Master text styles Second level Third level Fourth level Fifth level EECS1710 Week 06, Lecture 11 Fall 2014 ! Tuesday, Oct 14, 2014

1  

1

Click to edit Master text styles Second level

Third level Fourth level

Fifth level

EECS1710 Week 06, Lecture 11

Fall 2014 ! Tuesday, Oct 14, 2014

2

Announcements

•  ePost updated, please check online

•  Next Labtest: Thu Oct 23/Fri Oct 24 •  covers Ch 2 & 3

•  Next Term Test: Tue Oct 28 •  covers Ch 3 & 4

Page 2: 1710f14 C11 2014 10 14€¦ · 1 1 Click to edit Master text styles Second level Third level Fourth level Fifth level EECS1710 Week 06, Lecture 11 Fall 2014 ! Tuesday, Oct 14, 2014

2  

3

Checklist from prev lecture (Oct 9, 2014)

What you should be doing to prepare for what comes next…

"  read section 3.2 “A Development Walk-Through”

"  review Ch 3 KC’s 7-14

"  do Ch 3 RQ’s 13-25

"  do Ch 3 Ex’s 3.12-3.16

Have  a  Happy  Thanksgiving  Holiday  

4

Plan for today

•  §3.2.1 The Development Process

•  §3.2.2 The Mortgage App – students review on their own

•  §3.2.3 Output Formatting

•  §3.2.4 Relational Operators and Input Validation

•  §3.2.5 Input Validation

•  §3.2.6 Assertions - students review on their own

Page 3: 1710f14 C11 2014 10 14€¦ · 1 1 Click to edit Master text styles Second level Third level Fourth level Fifth level EECS1710 Week 06, Lecture 11 Fall 2014 ! Tuesday, Oct 14, 2014

3  

5

§3.2.1 The Development Process

•  A methodology is a system of methods used in a particular activity

•  A software development methodology is a system of methods used in in order to develop software that is correct, usable, robust, … (and other desirable characteristics)

•  The development process described in the textbox is called the iterative methodology. It is the core technology-centered design methodology

•  Please be aware that the this technology-centered approach stands in contrast with Human-Centered Design (HCD), which is an approach that is better suited when designing for usability.

6

§3.2.1 The Development Process

REQUIREMENTS

DESIGN

IMPLEMENTATION

TESTING

DEPLOYMENT

can  you  describe  each  of  these  phases?  what  is  the  pur:ose  of  each  one?  

Page 4: 1710f14 C11 2014 10 14€¦ · 1 1 Click to edit Master text styles Second level Third level Fourth level Fifth level EECS1710 Week 06, Lecture 11 Fall 2014 ! Tuesday, Oct 14, 2014

4  

7

§3.2.1 The Development ProcessComprehension Questions •  What is the difference between the requirements analysis phase and the

design phases?

•  At what stage does coding take place?

•  At what stage would the designer create UML class diagrams?

•  In the implementation stage, how does the implementer know what functionality should be implemented?

•  In the testing phase, a set of test cases are uses to assess correctness. What is the basis for the creation of these test cases?

8

§3.2.2 The Mortgage App

•  Students should review this material on their own and be prepared to be able to answer related questions on the test

Page 5: 1710f14 C11 2014 10 14€¦ · 1 1 Click to edit Master text styles Second level Third level Fourth level Fifth level EECS1710 Week 06, Lecture 11 Fall 2014 ! Tuesday, Oct 14, 2014

5  

9

§3.2.3 Output Formatting:Available Services By now, we have employed println, print, and printf :

•  Which class(s) offers these services?

•  Discuss overloading: •  is printf an overloaded method? •  is print an overloaded method? •  is println an overloaded method?

•  How are print and println similar? How are they different?

•  Is printf more similar to print or to println?

10

§3.2.3 Output Formatting:Comprehension •  What is the return of print and println?

•  How are print and println similar?

•  How are print and println different?

Page 6: 1710f14 C11 2014 10 14€¦ · 1 1 Click to edit Master text styles Second level Third level Fourth level Fifth level EECS1710 Week 06, Lecture 11 Fall 2014 ! Tuesday, Oct 14, 2014

6  

11

§3.2.3 Output Formatting: printf

•  Signature of printf is as follows: printf(String format, Object... args)

•  This means at least one arg, format and then zero or more additional args

•  Now let's discuss this format string further….

12

§3.2.3 Output Formatting: printf

The format string is a String which may contain fixed text and zero or more embedded format specifiers

Idea #1: The format specifiers are optional and the args are optional

Consider what happens when you leave out the format specifiers and only make use of fixed text stdOut.printf("hello"); stdOut.printf("56"); stdOut.printf("56\n"); stdOut.printf("\t56\n");

printf(String format, Object... args) only  the  for;at  st<ing  &    no  args  

Page 7: 1710f14 C11 2014 10 14€¦ · 1 1 Click to edit Master text styles Second level Third level Fourth level Fifth level EECS1710 Week 06, Lecture 11 Fall 2014 ! Tuesday, Oct 14, 2014

7  

13

§3.2.3 Output Formatting: printf

The most basic conversion for the format specifier is %s.

Idea #2: there must be an arg for each format specifier

Consider the following stdOut.printf(“hello%s”,56f);

stdOut.printf(“56%s”,4L);

stdOut.printf(“56%s%s%s\n”,33,88.7,“hi”); stdOut.printf(“%s\t56\n”, 33.567);

stdOut.printf(“%s”, "hello");

14

§3.2.3 Output Formatting: printf

The “\n” string is equivalent to the format specifier %n.

Consider the following: •  stdOut.printf(“hello\n”); •  stdOut.printf(“hello%n”);

Page 8: 1710f14 C11 2014 10 14€¦ · 1 1 Click to edit Master text styles Second level Third level Fourth level Fifth level EECS1710 Week 06, Lecture 11 Fall 2014 ! Tuesday, Oct 14, 2014

8  

15

§3.2.3 Output Formatting: printf

The d conversion character takes int/long args

The f conversion character takes float/double args

Idea #4: Use these conversion characters

Consider the following: stdOut.printf(“ans: %d%n”,56); stdOut.printf(“ans: %d%n”,56L); stdOut.printf(“ans: %f%n”,56.67); stdOut.printf(“ans: %f%n”,56.67f);

In case of mismatch, what happens?

16

§3.2.3 Output Formatting: printf

Idea #5: The width component specifies the number of characters for the output. The width component can be used with %s, %d, %f. The output is right justified.

Consider the following: •  stdOut.printf("ans: %10s%n", "hi");

•  stdOut.printf("ans: %10d%n", 56);

•  stdOut.printf("ans: %10f%n", 56.6798);

Page 9: 1710f14 C11 2014 10 14€¦ · 1 1 Click to edit Master text styles Second level Third level Fourth level Fifth level EECS1710 Week 06, Lecture 11 Fall 2014 ! Tuesday, Oct 14, 2014

9  

17

§3.2.3 Output Formatting: printf

Idea #6: The precision component specifies the number of decimal digits for %f with float/double. It cannot be used with the%s for or the %d specifiers

Consider the following: •   stdOut.printf("ans: %10.2s%n", "hi"); •  stdOut.printf("ans: %10.2d%n", 56); •  stdOut.printf("ans: %10.2f%n", 56.6798);

18

§3.2.3 Output Formatting: printf

Idea #7: The flag component can be , and/or 0. It can be used only with numerical types

Consider the following: stdOut.printf("ans: %010d%n", 5666); stdOut.printf("ans: %,10d%n", 5666); stdOut.printf("ans: %0,10d%n", 5666); stdOut.printf("ans: %010.2f%n", 98956.6798); stdOut.printf("ans: %,10.2f%n", 98956.6798); stdOut.printf("ans: %0,10.2f%n", 98956.6798);

Page 10: 1710f14 C11 2014 10 14€¦ · 1 1 Click to edit Master text styles Second level Third level Fourth level Fifth level EECS1710 Week 06, Lecture 11 Fall 2014 ! Tuesday, Oct 14, 2014

10  

19

§3.2.3 Output Formatting: printf

The general form of each specifier is: %[flags][width][.precision]conversion

conversion letter can be: d : int/long f : float/double s : string n : new line

width: how many characters to be allocated flags can be: , and/or 0 precision: # of decimal digits (for f conversion letter only)

20

§3.2.3 Output Formatting: printf

•  In what crucial way is the signature of printf different from the signatures of print and println?

•  Is printf more similar to print or to println?

•  Is printf an overloaded method?

•  How can printf be used to mimic print or println? •  Replicate the behaviour of print(67.8) •  Replicate the behaviour of println(“hello”)

Page 11: 1710f14 C11 2014 10 14€¦ · 1 1 Click to edit Master text styles Second level Third level Fourth level Fifth level EECS1710 Week 06, Lecture 11 Fall 2014 ! Tuesday, Oct 14, 2014

11  

21

§3.2.4 Relational Operators

•  Numeric operands: < <= > >=

•  Numeric/boolean operands (any type): == !=

•  A special infix operator: instanceof

•  All relational operators violate closure (*except boolean equality operator)

•  No matter what the operand type is, the result type is always boolean.

22

Operator Precedence

Precedence Operator Operands Syntax true if

< numeric x < y x is less than y

<= numeric x <= y x is less than or equal to y

> numeric x > y x is greater than y

>= numeric x >= y x is greater than or equal to y

-7

instanceof x instanceof C is true if object reference x points at an instance of class C or a subclass of C.

== any type x == y x is equal to y -8

!= any type x != y x is not equal to y

#

#

precedence  level  of  the  assig>ment  operator  =  is  -15  (p.33,  p.110)  precedence  levels  of  arithmetic  operators  can  be  found  on  p.  27  

Page 12: 1710f14 C11 2014 10 14€¦ · 1 1 Click to edit Master text styles Second level Third level Fourth level Fifth level EECS1710 Week 06, Lecture 11 Fall 2014 ! Tuesday, Oct 14, 2014

12  

23

§3.2.4 The equality operator

•  Be certain that you understand the difference between the following:

•  the assignment operator =

•  the equality operator ==

24

§3.2.4 The equality operator

•  Be certain that you understand the behaviour of the equality operator with primitive operands

boolean isEqual1 = 7812 == 567.34; boolean isEqual2 = 'a'=='b'; boolean isEqual2 = 'a'+1=='b';

Page 13: 1710f14 C11 2014 10 14€¦ · 1 1 Click to edit Master text styles Second level Third level Fourth level Fifth level EECS1710 Week 06, Lecture 11 Fall 2014 ! Tuesday, Oct 14, 2014

13  

25

§3.2.4 The equality operator

•  Be certain that you understand the behaviour of the equality operator with non-primitive operands

Rectangle r1 = new Rectangle(10, 10); Rectangle r2 = new Rectangle(10, 10); Rectangle r3 = new Rectangle(20, 20); boolean isEqual1 = r1==r2; boolean isEqual2 = r1==r3;

26

§3.2.6 Assertions

•  Students should review this material on their own and be prepared to be able to answer related questions on the test

•  A key point: assertions are intended to be used only during development; once testing is completed and the app is going into production, all of the assert statements are disabled

Page 14: 1710f14 C11 2014 10 14€¦ · 1 1 Click to edit Master text styles Second level Third level Fourth level Fifth level EECS1710 Week 06, Lecture 11 Fall 2014 ! Tuesday, Oct 14, 2014

14  

27

§3.2.5 Input Validation

•  Students will have noticed that we have already been performing input validation during the lab exercises; all of this material should already be familiar!!

28

Topic: Working with files

•  We will cover some basic material about files during lecture and then do some exercises on the lab exercises (see §5.3.2)

•  Our motivation is that we will start to work with manipulating images, which requires working with the file system •  working with images also requires working with the operating

system’s window manager and the platform’s graphics hardware, understanding colour models and colour representation formats and being able to iterate and construct conditions

•  we will cover each of these topics over the next few lectures

Page 15: 1710f14 C11 2014 10 14€¦ · 1 1 Click to edit Master text styles Second level Third level Fourth level Fifth level EECS1710 Week 06, Lecture 11 Fall 2014 ! Tuesday, Oct 14, 2014

15  

29

Topic: Working with files

•  Any file on the hard drive has a pathname and the details of this path name depend on the operating system

•  Here are some examples Windows Local File System (LFS) C:\USER\DOCS\LETTER.TXT Windows Uniform Naming Convention (UNC)\\Server\Volume\File Unix-like OS/home/user/docs/Letter.txt"

30

Topic: Working with files

•  The format of a pathname is consistent across the different OS's, but the details differ

C:\USER\DOCS\LETTER.TXT \\Server\Volume\File /home/user/docs/Letter.txt"

"

•  Here are the consistent features •  the system prefix (e.g., C:\, \\, /) #•  the separator (e.g., \,/) #File.separator

Page 16: 1710f14 C11 2014 10 14€¦ · 1 1 Click to edit Master text styles Second level Third level Fourth level Fifth level EECS1710 Week 06, Lecture 11 Fall 2014 ! Tuesday, Oct 14, 2014

16  

31

Topic: Working with files

•  The class java.io.File provides services for working with the file system on your hard drive

•  For this class, a file can be either •  a directory •  a “normal file” (i.e., not a directory)

32

Topic: Working with files

•  The class java.io.File is NOT a utility class

•  The class has several constructors. We will be using the constructor that takes a String parameter: File(String)

•  The passed String is a pathname •  It can be a pathname to a file that doesn't exist yet (and that you

will create) •  It can be a pathname to a file that already exists (and from

which you want to read)

•  The File class encapsulates services for both potentially-existing files and already-existing files.

Page 17: 1710f14 C11 2014 10 14€¦ · 1 1 Click to edit Master text styles Second level Third level Fourth level Fifth level EECS1710 Week 06, Lecture 11 Fall 2014 ! Tuesday, Oct 14, 2014

17  

33

Topic: Working with files

•  How can we specify a pathname so that it is compatible for any operating system?

•  The class File has a static field that provides the separator that is in use on the system: File.separator

•  The VM will ensure the field is assigned a String value that reflects the current OS •  on Windows, it will be "\" •  on Unix-like OS's (including Mac OS X), it will be "/"

34

UML Class Diagram for File

java::io::File!

+separator: String"⋮"

File(String)"⋮"+exists(): boolean"+lastModified(): long"+length(): long"+getPath(): String"⋮ "

just because an object is instantiated for a pathname doesn’t necessarily mean that there is an actual file corresponding to that pathname!

Page 18: 1710f14 C11 2014 10 14€¦ · 1 1 Click to edit Master text styles Second level Third level Fourth level Fifth level EECS1710 Week 06, Lecture 11 Fall 2014 ! Tuesday, Oct 14, 2014

18  

35

Services provided by the File class"

!  provides constructor for object creation, given a pathname#!  demo: File_Example01#

!  provides delegation of file-related tasks:#!  does this file exist?#!  is this file a directory or a normal file?#!  what is the size of this file?#

!  can I write to this file? #!  which files are in this directory, if any?#

!  assumes this file is a directory#!  make a directory, as specified by this file#

!  assumes pathname is not already in use and operation is allowed##

35#

36

Services provided by the File class"

!  Additional services#!  can I write to this file? #!  which files are in this directory, if any?#

!  assumes this file is a directory#!  make a directory, as specified by this file#

!  assumes pathname is not already in use and operation is allowed##

36#

Page 19: 1710f14 C11 2014 10 14€¦ · 1 1 Click to edit Master text styles Second level Third level Fourth level Fifth level EECS1710 Week 06, Lecture 11 Fall 2014 ! Tuesday, Oct 14, 2014

19  

37

The encapsulation of a File…"

##!  provides services to ask whether the file is writable!

!  this tells you whether the permissions and other conditions are favourable#

!  the File class does not provide the means to write to the file object $#!  for this, you need the services of FileWriter"!  a FileWriter object encapsulates all of the working of writing content

to a File object#!  defer this aspect for the time being#

37#

38

Another way to interface with the file system "

##!  Use the services of the Swing package, which has a class called

JFileChooser

!  let the user specify one for you#!  FileChooser_Example01

38#

Page 20: 1710f14 C11 2014 10 14€¦ · 1 1 Click to edit Master text styles Second level Third level Fourth level Fifth level EECS1710 Week 06, Lecture 11 Fall 2014 ! Tuesday, Oct 14, 2014

20  

39

the JFileChooser class#

39#

java::swing::JFileChooser!

+APPROVE_OPTION: int"+CANCEL_OPTION: int"

JFileChooser()"⋮"+showOpenDialog(null): int"+getSelectedFile(): File"⋮ "

JFileChooser encapsulates information about and operations on a file choice dialogue.#

getSelectedFile() will always return something, #even before the dialog is even opened#

40

Checklist (for next time, Lecture 13)

What you should be doing to prepare for what comes next…

"  read section 3.3

"  review Ch 3 all key concepts, all review questions, remainder of exercises

"  do Ch 3 Exercises (up to 3.17 definitely, 3.18-3.22)

"  Exercise 3.23 is a practise test (link to textbook website can be found on course website)