Lecture01 Intro to Prog Pples
Lecture01 Intro to Prog Pples
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
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
27/03/2018 / Slide 4
The programming process
• The purpose of writing a program is to solve a
problem – always use these 6 steps
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
27/03/2018 / Slide 9
Test Plan
• Visualise the program running on a computer
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
• Cooking algorithm?
ConvertDollars
prompt for ausDollars
usDollars ausDollars * 0.75
display usDollars
EndConvertDollars
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
27/03/2018 / Slide 16
5. Write the program(s)
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
27/03/2018 / Slide 19
Logic errors
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)
class body
27/03/2018 / Slide 24
A class contains methods
An application always has a main method
27/03/2018 / Slide 25
A method contains statements
A statement is a single command
ending with a semicolon “;”
• 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