[go: up one dir, main page]

0% found this document useful (0 votes)
3 views29 pages

Lecture01 Intro to Prog Pples

The document provides an introduction to programming principles, defining a program as a precise set of instructions for computers. It outlines the programming process in six steps, from understanding the problem to testing the solution, and emphasizes the importance of pseudocode in designing algorithms. Additionally, it discusses Java as a programming language, its editions, and the structure of Java programs, including classes and methods.

Uploaded by

maureen kakoma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views29 pages

Lecture01 Intro to Prog Pples

The document provides an introduction to programming principles, defining a program as a precise set of instructions for computers. It outlines the programming process in six steps, from understanding the problem to testing the solution, and emphasizes the importance of pseudocode in designing algorithms. Additionally, it discusses Java as a programming language, its editions, and the structure of Java programs, including classes and methods.

Uploaded by

maureen kakoma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Computer Programming

Introduction to Programming Principles

Study as if you were to live forever. Live as if you were to die tomorrow.
- Isodore de Seville
27/03/2018 / Slide 1
What is a Program
• A program is a precise set of instructions that tells the
computer what to do to perform a task
– a step-by-step procedure
– the steps must be precise and unambiguous
• Get a number from a location in its memory
• Add this to another number in another memory location
• Display this result on the screen

• Collectively, these instructions form an algorithm

27/03/2018 / Slide 2
Programming Languages
• A programming language is a special language used to
write computer programs
– it specifies the words and symbols that we can use to write a
program
• Hundreds of languages have been developed
– Each has been developed for a specific purpose
– Eg Fortran, COBOL, Pascal, C, C++, Ada

• Java one of the more recent additions


– Java was created by Sun Microsystems, Inc.
– It was introduced in 1995 and has become very popular
27/03/2018 / Slide 3
– It is an object-oriented language
Java editions
• There are different editions of Java:
– J2SE - Java2 Standard Edition.
– J2EE - Java2 Enterprise Edition.
– J2ME - Java2 Micro Edition

– We are using J2SE

27/03/2018 / Slide 4
The programming process
• The purpose of writing a program is to solve a
problem – always use these 6 steps

1. Understand the problem


2. Determine how we can test our solution
3. Design a solution
4. Test the design
5. Write the program(s)
6. Test the solution and fix any problems that exist
27/03/2018 / Slide 5
1. Understand the problem

• You will be given a Requirements Specification


– ie a problem to solve by writing programs in Java
• Ensure you completely understand what is required
• Do exactly what you are asked

• Resolve assumptions and ambiguities

27/03/2018 / Slide 6
Resolve ambiguities

Australian sign

27/03/2018 / Slide 7
Understand the problem
Requirements Specification
• You are to prompt the user to enter a number of Australian
dollars, and convert them to US dollars at the rate of 0.75.
• Display the resulting number of US dollars

• Any ambiguities?
Eg How should the prompt be worded?
– is the input only whole dollars?
– How many decimal places should the output show?
– etc
27/03/2018 / Slide 8
2. Determine how to test the solution
You must understand the problem in order to do this
– So this helps ensure you do understand what you are to do

• Visualise the program running on a computer


– Every program takes inputs and turns them into outputs

• Develop test data


– For a range of inputs – what are the expected outputs?

27/03/2018 / Slide 9
Test Plan
• Visualise the program running on a computer

Input prompt Input Expected output Actual output


Enter Australian $: 0 US$0.0

1 US$0.75

100 US$75.0

250 US$187.5

27/03/2018 / Slide 10
3. Design a solution
Again, before writing a single line of Java (or other) code

• Develop a set of precise steps (ie an algorithm) that


describe exactly:
– the tasks to be performed
– the order in which they are to be carried out

• For this we use pseudocode


– Enforces concentration on the problem not the syntax of the
language
27/03/2018 / Slide 11
Algorithms
• Knitting algorithm
Size 4 mm (8) needles
Working in St. St.
Pick up 70 (76, 82, 88, 102, 114, 118) Sts. around armhole edge
Work 10 (10, 10, 10, 12, 2, 0, 0) rows in St. St.
Next row K.2. K.2. Tog. T.B.L. K. to last 4 Sts. K.2 Tog. K.2.

• Cooking algorithm?

• The order of instructions is vital


27/03/2018 / Slide 12
Programming Algorithm
Pseudocode:

ConvertDollars
prompt for ausDollars
usDollars  ausDollars * 0.75
display usDollars
EndConvertDollars

NB  means assigning a new value to a variable


To write it in WORD use < followed by 2 hyphens --
27/03/2018 / Slide 13
Pseudocode
• This algorithm is written in a structured form of English
called pseudocode.
– It is not complete English sentences
– Nor is it the code of any programming language

• Pseudocode in an outline of a program that can easily be


converted into real program statements
• It is easy to read and write
• Like a bulleted “to do” list of statements
• Precise syntax is not so important
27/03/2018 / Slide 14
Pseudocode
• Name the algorithm
– The name should describe the process
– Often start with a verb
– Capitalise words and leave no spaces eg. ConvertDollars
• Indicate the end with the word EndConvertDollars
• Each line has a single processing step
– Data items need meaningful names
– start in lowercase with capitals for starting letters in subsequent
words ( no spaces) eg ausDollars

27/03/2018 / Slide 15
4. Test the design
• Detect logic errors in the pseudocode
– Saves a lot of time
– As opposed to finding them in the actual program code

• “Desk check” the algorithm


– Walk through the algorithm with the test cases
– You act as the computer
– Keep track of variables on a sheet of paper

27/03/2018 / Slide 16
5. Write the program(s)

Write the program code in the appropriate language (Java)

• Avoid the impulse to start here!!


• Only begin coding after the previous steps have been
completed

27/03/2018 / Slide 17
6. Test the solution
Syntax errors
• Syntax errors are mistakes that the programmer has made
that violate the rules of the language

Logic errors
• Logic errors are mistakes that the programmer made that
result in the program not meeting the Requirements
Specification

27/03/2018 / Slide 18
Syntax errors

Compile the program


• Syntax errors are detected by the compiler
• If syntax errors exist, an executable version of the program
is not created
• Fix any syntax errors until the code compiles

• The compiler creates another file that holds the translated


instructions for execution

27/03/2018 / Slide 19
Logic errors

Execute (run) the program


• How do you know if the program works properly?

• Logic errors are detected by your test data


• Run each test data case through the program
• Fix logic errors until test case actual output matches
expected output

27/03/2018 / Slide 20
After running test cases
Input prompt Input Expected output Actual output
Enter Australian $: 0 US$0.0 √
1 US$0.75 √
100 US$75.0 √
250 US$187.5 √

27/03/2018 / Slide 21
Program Development Process
Saves Java statements
Text editor Source code
(.java)

Produces Byte code


Java compiler (.class)

Java Results in Program


Virtual Execution
Machine
27/03/2018 / Slide 22
Development Environments
• An integrated development environment (IDE) is an
collection of software tools for developing and testing
programs
– A programmer can write a program, compile it, and execute it, all
without leaving the IDE
• There are many IDE’s which develop Java software:
– Borland JBuilder
– Microsoft Visual J++
– BlueJ
• Though the details of these environments differ, the basic
compilation and execution process is essentially the same
27/03/2018 / Slide 23
A java program is made up of one or
more classes
public class DemoProgram
{
class header

class body

27/03/2018 / Slide 24
A class contains methods
An application always has a main method

public class DemoProgram


{

public static void main (String[] args)


{
method header
method body
}
}

27/03/2018 / Slide 25
A method contains statements
A statement is a single command
ending with a semicolon “;”

public class QuoteWatson


{
public static void main (String[] args)
{
System.out.println (“A quote by Thomas Watson, Chairman IBM, 1943:”);
System.out.println (“I think there is a world market for maybe 5 computers.”);
}
}

A quote by Thomas Watson, Chairman IBM, 1943:


I think there is a world market for maybe 5 computers.
27/03/2018 / Slide 26
Displaying output to the screen

The println() and print() methods

• System.out.println( )
– Prints what is contained in the argument ie. between ( )
– println() always advances to the next line after displaying its
argument

• System.out.print( )
– Does not advance to the next line after displaying its argument

27/03/2018 / Slide 27
public class Countdown
{
public static void main (String[] args)
{
System.out.print ("Three... ");
System.out.print ("Two... ");
System.out.print ("One... ");
System.out.print ("Zero... ");
System.out.println ("Liftoff!"); // appears on first output line
System.out.println ("Houston, we have a problem.");
}
}

Three…Two…One…Zero…Liftoff!
Houston, we have a problem.
Lecture Outcomes
• Today we have covered:
– the requirements of the unit in terms of assessment and reference
material
– the importance of high quality software
– programming languages
– the six stages in problem solving
– the structure of a Java program
– Displaying output

• Questions?
27/03/2018 / Slide 29

You might also like