Pseudo Code

advertisement
Pseudo Code
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?
Start with a year value 0 and a balance of $10000
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
…
…
14
19799.32
15
20789.28
Report the final year value as the answer
Here are the kinds of pseudo code statements that we will use in this book:
• 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.
Report 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 fees. Give an algorithm to compute the
monthly charge for a given number of minutes.
Answer before read “How to 1.1”:
Total monthly charge = Fixed charge + Additional charge
Fixed charge = $29.95
If minute <= 300
Additional charge = 0
Else
Additional charge = (Tax+1) x (minute-300) x $0.45
Minute
0
1
2
…
300
301
302
…
Y
Report the total monthly charge as the answer
Total Monthly Charge ($)
29.95
29.95
29.95
29.95
29.95
29.95 + 1.125 x (301-300) x 0.45
29.95 + 1.125 x (302-300) x 0.45
…
29.95 + 1.125 x (y-300) x 0.45
Answer after read “How to 1.1”:
Step 1: determine the inputs and outputs
Input:
. Called Minute: how many minutes I have called in this month
Output:
. The Total Monthly Charge
Steps 2: break down the problem into smaller tasks
The Monthly Charge is Fixed Charge plus Additional Charge.
Fixed charge:
Fixed charge is a given number (in this case: $29.95)
For Additional Charge:
If Called Minute <= Specific number
Additional charge = 0
Else
Additional charge = (Tax+1) x (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 Monthly Charge is $29.95 + 1.125 x (302-300) x
$0.45 = $30.9625
Round the Result:
Round the result into 2 digits, decimal.
For example, if result is 2, round to 2.00; if result is $30.9625, round to $30.96; if result is
$30.9655, 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
Monthly 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.
• Use comments to help human readers understand your program.
• 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");
namePrinter("Allen");
capitalAllen();
print3Lines("Hans", "Martin", "Christian");
p18(10);
}
static void namePrinter(String sName)
{
//display my name in a box
System.out.println("+-----+");
System.out.println("|"+sName+"|");
System.out.println("+-----+");
}
static void capitalAllen()
{
System.out.println("
x
x
x
x x x
System.out.println("
x x
x
x
x
System.out.println(" x x x
x
x
x x x
System.out.println(" x
x x
x
x
System.out.println("x
x x x xxxxx x x x
}
x
x");
xx x");
x x x");
x x");
x
x");
static void print3Lines(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 p18(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 x x
x
x x x
x
x
xx x
x x x
x
x x
x
x
x
x x x xxxxx x x x
My friends are:
Hans
Martin
Christian
x
x
x
x
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
{
printMyName("Allen Guo");
printInputName();
greetImage();
}
static void printMyName(String str)
{
JOptionPane.showMessageDialog(null, "Hello, "+ str +"!");
System.exit(0);
}
static void printInputName()
{
String name = JOptionPane.showInputDialog("What's your name?");
//System.out.println("Good morning, "+name+"!");
JOptionPane.showMessageDialog(null,"Good morning, "+name+"!");
System.exit(0);
}
static void greetImage() throws Exception
{
URL imageLocation = new URL("http://ess.ch/blog/wpcontent/uploads/2011/08/BigJava-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?
printMyName("Allen Guo");
printInputName();
greetImage();
Answer:
System.exit(0);
Download