Answers for seminar exercises: Week 2

carenextSoftware and s/w Development

Nov 18, 2013 (3 years and 6 months ago)

158 views

Answers for seminar exercises: Week 2


Exercise: Noughts and crosses


Here is a possible set of instructions for Player 1.


Player 1 (X)


MOVE 1:

Go in a free corner

MOVE 2:

IF there is an O on the side THEN Go in a corner away from that side

OTHERWISE IF

there is an X in a corner with the opposite corner a space

THEN Go in that space

OTHERWISE Go in an adjacent corner

MOVE 3:

IF there are 2Xs and a space in a line THEN Go in that space

OTHERWISE IF there are 2Os and a space in a line THEN Go in that space

OTHERWISE Go in a free corner

MOVE 4:

IF there are 2Xs and a space in a line THEN Go in that space

OTHERWISE IF there are 2Os and a space in a line THEN Go in that space

OTHERWISE Go in a free corner

MOVE 5:

Go in any free space


The aim of this exercise
is to prepare you for using a restricted, structured notation to express solutions
to problems, which is an important part of developing computer programs.



Exercise: Problem solving


Both solutions are written in pseudocode


a mixture of structured prog
ramming constructs and English


Solution 1


Select the first box as the box to be opened


REPEAT

Open the box

Compare the name in the box with the name being searched for

IF The names are the same



Say “I’ve found it!”

ELSE


Select the next box along

UN
TIL you find the name OR you have looked in all the boxes














Solution 2


Select the whole row as the section of the boxes to be considered


REPEAT

Open the middle box in the section to be considered

Compare the name in the box with the name being

searched for

IF The names are the same



Say “I’ve found it!”

ELSE IF the name is alphabetically less than the name in the box


Select the bottom half of the current section to be the new current section

ELSE

Select the top half of the current section to

be the new section to be considered

UNTIL you find the name OR there’s no boxes in the section


With solution 1, you might find the name on the first comparison, or the 1000
th

comparison. On
average, it will take 500 comparisons to find the name.


With so
lution 2, each comparison eliminates half of the boxes, and the greatest number of comparisons
necessary will be 10.


The speed or each algorithm is dependent upon the number of boxes to be searched.


Formally, the first algorithm’s speed directly proporti
onal to the number of boxes


it is said to have
linear performance. The second algorithm’s speed is proportional to the logarithm of the number of
boxes. What this means is that, if the number of boxes were doubled, the first algorithm would take, on
aver
age, twice as long. However, the second algorithm would require only one more comparison! In
fact, even with 1 million boxes, the second algorithm would only require a maximum of 20
comparisons, while the first would have a maximum of 1 million and an aver
age of ½ million!




Don’t worry if you find this exercise difficult to understand; the important point to remember is that
this exercise demonstrates how different algorithms can have very different levels of performance, and
therefore how important it is

to have a well
-
designed algorithm.


By the way, you should study the algorithms and note that there are certain details that are not properly
described; for example, in solution 2, what is the middle box if a section has an even number of boxes?
What is

the ‘top half’ of a section with only one box? These questions would have to be answered in
detail in an implementation of an algorithm as a computer program, but the beauty of pseudocode is
that we can be vague about such things while capturing the esse
nce of the algorithm. A good technique
is therefore to write our algorithm in pseudocode first, then refine it into a program on paper, test the
paper version of the program by dry
-
running it (see later), and only then type the program in and try to
get it

to work on a computer. This method can save a great deal of abortive work when compared to
sitting down a the computer at the beginning.



Review questions


Review Question
1

What are the characteristics of high
-
level lan
guages, as opposed to assembler language?


Answer to Review Question
1

Assembler instructions have a 1
-
1 relationship with the machine code instructions of the computer,
whereas high
-
level languages support more
abstract, problem
-
oriented instructions, each of which may
subsume several assembler language instructions, capturing a common pattern of usage of assembler
instructions.


Review Question
2

How does a computer run (execu
te) a high
-
level language program?



Answer to Review Question
2

It doesn’t! Computers can only execute machine code, and a program called a
compiler
is required to
translate the high
-
level language program into
machine code.


Review Question 3

How does Java differ from other high
-
level languages in this respect?



Answer to Review Question 3

Java programs are compiled to
byte
-
code
, which is effectively machine code for a virtual computer,
simulated by the Java Vi
rtual Machine (JVM) software. This means that the byte
-
code program can run
on any computer which has the JVM software, making Java programs extremely portable.


Review Question 4

What is an Integrated Development Environment (IDE)?



Answer to Review Ques
tion 4

It’s a piece of software which supports the editing, compiling and running of programs all within one
graphical user interface. In theory, this makes software development easier, but usually IDEs are quite
sophisticated, and novice programmers are l
ikely to spend most of their time getting to grips with the
IDE rather than writing programs! For this reason, we will be using the basic Java Development Kit
(JDK) which operates in a simple command line mode.


Review Question 5

What is meant by ‘levels o
f abstraction’?



Answer to Review Question 5

This refers to the different levels of detail at which a solution to a programming problem may be
viewed, from the initial specification of a solution on paper (high level of abstraction), through more
detailed

descriptions, to the final highly detailed and precise description of what the computer must do,
written in a programming language (low level of abstraction).


Review Question 6

What is object
-
orientation?



Answer to Review Question 6

Fundamentally, obje
ct orientation is a system for
modelling

in terms of the objects (entities) which
make up a system. It is based on the idea that software systems are best thought of as a set of
interacting parts,
objects
, that do the work of the system by sending messages

to each other. In object
-
oriented programming languages, the
class

construct is used to specify objects. Thus, for example, we
could define a class to describe the characteristics of a sheep, then create numerous sheep objects for a
program to simulate a

sheep
-
dog trials. Object orientation is one of the most important features of Java.
In this module, you will have the opportunity to create and use objects, but the definition of objects
using classes will be introduced in module CMT1501.



Multi
-
choice r
eview questions


1.

How many hours per week should you spend studying this module, outside class contact?


A:

5

B:

10

C:

15

D:

0


Answer: B


2.

What programming language does a computer understand?


A:

Java

B:

Byte code

C:

Machine code

D:

Assembler


Answer: C


3.

Which of these programming languages is most similar to Java?


A:

Pascal

B:

C++

C:

FORTRAN

D:

Cobol


Answer: B



4.

What software is recommended for creating Java programs in this module?


A:

Symantec Visual Café

B:

Jbuilder

C:

Java Development Kit

D:

Excel


Answer: C



5.

What is the command to compile a Java program called prog.java to bytecode?


A:

javac prog

B:

javac prog.java

C:

java prog

D:

java prog.java


Answer: B



6.

What is the command to run a compiled Java program called prog.class?


A:

javac prog

B:

javac prog.java

C:

java prog

D:

java prog.java


Answer: C



7.

Why are Java programs described as
portable
?


A:

They can fit onto a small, lightweight disk.

B:

They are easy to send over the web.

C:

They will run on any computer system which implements
the Java Virtual Machine

D:

They are object
-
oriented


Answer: C



8.

What is an applet?


A:

A small apple

B:

A program which may be embedded in a web page.

C:

A program for use only with databases

D:

Another name for a small application


Answer: B


9.

In the s
oftware development life
-
cycle, roughly what proportion of the lifecycle is spent in
maintenance of the software?


A:

10%

B:

30%

C:

50%

D:

70%


Answer: D


10.

What is a class?


A:

A means of giving structure to object
-
oriented programs

B:

A category of progra
mming language

C:

A social division

D:

A type of programming language


Answer: A