Pseudo Code

Λογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 4 χρόνια και 8 μήνες)

77 εμφανίσεις

P
seudo

C
ode

Question
: you put \$10000 into a bank account that earns 5 % interest per year. How many years
does it take for the account balance to be double the original?

year

Balance

0

10000

Repeat the
following steps with the balance is less than 20000

Add 1 to the year value.

Multiply the balance value by 1.05 (a 5 percent increase)

year

balance

0

10000

1

10500

19799⸳2

20789⸲8

H敲攠慲a⁴Ue⁫楮T猠o映
p獥sTo⁣oTe

s瑡tem敮瑳e瑨tt

w攠睩汬⁵s攠楮⁴U楳⁢ o欺

• Use statements such as the following to describe how a value is set or changed:

total cost = purchase price + operating cost

or

Multiply the balance value by 1.05.

or

Remove the
first and last character from the word.

• Describe decisions and repetitions as follows:

If total cost 1 < total cost 2

While the balance is less than \$20,000

For each picture in the sequence

Use indentation to indicate which statements should be selected

or repeated.

For each car

operating cost = 10 x annual fuel cost

total cost = purchase price + operating cost

Here, the indentation indicates that both statements should be executed for

each car.

• Indicate results with statements such as

Choose car1.

Rep
ort the final year value as the answer.

The exact wording is not important. What is important is that the pseudo

code

describes a sequence
of steps that is

• Unambiguous

• Executable

• Terminating

A sequence of steps that is unambiguous, executable, and terminating is called an
algorithm
.

Question:
Suppose your cell phone carrier charges you \$29.95 for up to 300 minutes of calls, and
\$0.45 for each additional minute, plus 12.5 percent taxes and fee
s. Give an algorithm to compute the
monthly charge for a given number of minutes.

How to 1.1”
:

Total monthly charge = Fixed charge + Additional charge

Fixed charge = \$29.95

If minute <=

300

Else

itional charge = (Tax+1) x
(
minute
-
300)

x
\$0.45

Minute

Total
Monthly Charge (\$)

0

29.95

1

29.95

2

29.95

29⸹5

300

29⸹5

301

29⸹5 ⬠1.125⁸ 301
-
300)⁸‰⸴5

302

29⸹5 ⬠1.125⁸ 302
-
300)⁸‰⸴5

Y

29.95 + 1.125 x (y
-
300) x 0.45

Report the
total monthly charge as the answer

Step 1: determine the inputs and outputs

Input:

.
Called
Minute
:

how many minutes I have called in this month

Output:

. The
T
otal
M
onthly
C
harge

Steps 2: break down the problem into

The Monthly Charge is Fixed Charge plus Additional Charge.

Fixed charge
:

Fixed charge

is a

given number

(
in this case
: \$29.95)

:

If
Called M
inute <=

Specific number

Else

(Called Minute
-
Specific number) x Unit Price

(in this case, Tax = 0.125, Specific number = 300, Unit Price = \$0.45)

For example, if you called 10 minutes this month, the Monthly Charge is \$29.95 + 0 = \$29.95;
if you called 302 minutes this month, the Mont
hly Charge is \$29.95 + 1.125 x (302
-
300) x
\$0.45 = \$30.9625

Round

the

Result:

R
ound

the result
into

2 digits,
decimal.

For e
xample, if result is 2, round

to 2.00; if result is \$30.9625, round to \$30.96; if result is
\$30.9
655, round to \$30.97.

Step 3:
Describe each subtask in pseudo code

Monthly Charge = Fixed Charge + Additional Charge

Here’s the algorithm for the Monthly Charge:

If Called Minute <= Specific number

Monthly Charge =
Fixed Charge

Else

M
onthly Charge =
Fixed Charge

+

(Tax+1) x (Called
Minute
-
Specific number) x Unit Price

Return
Round(
Monthly Charge
)

Steps 4: Test my pseudo code

Use below sample values:

Specific number = 300

Fixed Charge

= 29.95

Tax = 0.125

Called Minute = 302

Unit Price = 0.45

Here’s the calculation:

Monthly Charge =
Fixed Charge + Additional Charge = 29.95 + (0.125+1) x (302

300) x 0.45

= \$30.9625

Round(Monthly Charge) = \$30.96

So I need pay \$30.96 this month.

Summary of learning Objectives

Describe the process of translating high
-
level languages to machine
code.

• Generally, machine code depends on the CPU type. However, the instruction set of

the Java virtual
machine (JVM) can be executed on many CPUs.

• Because machine instructions are encoded as numbers, it is difficult to write programs

in machine
code.

• High
-
level languages allow you to describe tasks at a higher conceptual level than

machine code.

• A compiler translates programs written in a high
-
level language into machine code.

Describe the building blocks of a simple program and the structure of a

method call.

• Classes are the fundamental building blocks of Java programs.

• Every Java application contains a class with a
main

method. When the application

starts, the
instructions in the main method are executed.

• Each class contains definitions of
methods. Each method contains a sequence of

instructions.

• A method is called by specifying an object, the method name, and the method

parameters.

• A string is a sequence of characters
enclosed in quotation marks.

Classify program errors as syntax and logic errors.

• A syntax error is a violation of the rules of the programming language. The compiler

detects syntax
errors.

• A logic error causes a program to take an action that the
programmer did not

intend. You must test
your programs to find logic errors.

Classes, Objects, and Methods Introduced in this Chapter

java.io.PrintStream

print

println

java.lang.System

out

HelloSwiss.java

package

package01;

public

class

HelloSwiss {

public

static

void

main(String[] args)

{

//Hello world

System.
out
.println(
"hello swiss"
);

System.
out
.println(
"3+4"
);

System.
out
.println(3+4);

System.
out
.println(3+
"4"
);

n
amePrinter
(
"Allen"
);

c
apitalAllen
();

p
rint3Lines
(
"Hans"
,
"Martin"
,
"Christian"
);

p
18
(10);

}

static

void

n
amePrinter(String sName)

{

//display my name in a box

System.
out
.println(
"+
-----
+"
);

System.
out
.println(
"|"
+sName+
"|"
);

System.
out
.println(
"+
-----
+"
);

}

static

void

c
apitalAllen()

{

System.
out
.println(
" x x x x x x x x"
);

System.
out
.println(
" x x x x x xx x"
);

System.
out
.println(
" x x x x x x x x x x x"
);

System.
out
.println(
" x x x x x x x"
);

System.
out
.println(
"x x
x x xxxxx x x x x x"
);

}

static

void

p
rint3Lines(String s1, String s2, String s3)

{

//display my name in a box

System.
out
.println(
"My friends are:
\
n"
+s1+
"
\
n"
+s2+
"
\
n"
+s3+
"
\
n"
);

}

static

void

p
18(
int

i)

{

int

a =1;

int

sum = 0;

while

(a<
=i)

{

sum = sum + a;

a++;

}

System.
out
.println(sum);

}

}

---------

hello swiss

3+4

7

34

+
-----
+

|Allen|

+
-----
+

x x x x x x x x

x x x x x xx x

x x x x x x x x x x x

x x x x x x x

x x x x xxxxx x x x x x

My friends are:

Hans

Martin

Christian

55

DialogViewer.java

package

package01;

import

javax.swing.JOptionPane;

import

java.net.URL;

import

javax.swing.ImageIcon;

public

class

DialogViewer {

public

static

void

main(String[] args)
throws

Exception

{

p
rintMyName("Allen Guo");

p
rintInputName();

g
reetImage
();

}

static

void

p
rintMyName(String str)

{

JOptionPane.
showMessageDialog
(
null
,
"Hello, "
+ str +
"!"
);

System.
exit
(0);

}

static

void

p
rintInputName()

{

String name = JOptionPane.
showInputDialog
(
);

//System.out.println("Good morning, "+name+"!");

JOptionPane.
showMessageDialog
(
null
,
"Good morning, "
+name+
"!"
);

System.
exit
(0);

}

static

void

g
reetImage()
throws

Exception

{

URL imageLocation =
new

URL(
"http://ess.ch/blog/wp
-
-
242x300.jpg"
);

JOptionPane.
showMessageDialog
(
null
,
"
\
rHello, I am tiger!"
,
"Tiger"
, JOptionPane.
PLAIN_MESSAGE
,
new

ImageIcon(imageLocation));

System.
exit
(0);

}

}

-------------------------

Question: why stopped after completing the first method?

p
rintMyName("Allen Guo");

p
rintInputName();

g
reetImage
();