COM158 Algorithmic Programming 1 Semester 1 - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

COM158 Algorithmic Programming 1 Semester 1

Description:

Tony Jenkins and Graham Hardman, How to Program Using Java, Palgrave Macmillan, ... is Bruce Eckel's Thinking in Java eBook which can be downloaded from: http://www. ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 32
Provided by: H15
Category:

less

Transcript and Presenter's Notes

Title: COM158 Algorithmic Programming 1 Semester 1


1
COM158 Algorithmic Programming 1Semester 1
  • Lecture 1 Introduction
  • Week beginning 20th September 2004
  • Lecturer Dr. H. Sayers
  • Room MG236
  • Tel 75553 (internal)
  • 028 71 375553 (external)
  • E-mail hm.sayers_at_ulster.ac.uk
  • Web http//www.infm.ulst.ac.uk/heather

2
Reading Materials
  • The core text for this module is
  • Tony Jenkins and Graham Hardman, How to Program
    Using Java, Palgrave Macmillan, 2004
    ISBN1-4039-1223-8
  • You should purchase this book immediately as you
    will be required to read chapters from it each
    week.
  • There are many other textbooks on Java which can
    be used to supplement the core text. A list of
    reading materials is available on the Module
    Handout.
  • A useful Internet resource is Bruce Eckels
    Thinking in Java eBook which can be downloaded
    from http//www.mindview.net/Books/TIJ/

3
Lecture Aims
  • To introduce computer programming and computer
    languages
  • To introduce the concept of an object-oriented
    programming language
  • To introduce you to the process of problem
    analysis and designing a solution
  • To introduce the BlueJ Integrated Development
    Environment (IDE)
  • To introduce WebCT

4
Learning Outcomes
  • You should be aware of the concepts of computer
    programming and computer languages
  • You should be aware of the concepts of object
    oriented computer programming
  • You should be able to take a simple problem,
    analyse it, and design a solution
  • You should be able to find your way around the
    BlueJ IDE and the WebCT environment

5
What is a computer?
  • An electronic device responding to patterns of
    electrical impulses
  • 1ON 0OFF
  • 2 main components
  • Hardware electronic devices
  • Software operating system, programs
  • Aim - to simulate human intelligence
  • How?
  • Computer programs

6
Data Meaning Information
  • Program
  • Data Processing
  • A computer is only as intelligent as the sequence
    of instructions it uses i.e. The Program, which
    is created and entered by YOU

Information
7
A Computer Program
  • An algorithm presented to a computer to enable it
    to carry out some task.
  • Written by a programmer in a programming language
  • The programming language has a vocabulary which
    the programmer uses to write a set of
    instructions which the computer can understand
    and execute
  • This set of instructions form the computer program

8
Programming Languages
  • There are 3 general types
  • Machine Language (Binary code) - the only
    language computers can understand
  • Assembly Language (Eg. LDA, ADD, STO)
  • Translator programs (Assemblers) were developed
    to convert to machine language
  • High Level Languages (Eg. COBOL, PASCAL, C, C,
    JAVA)
  • Instructions look almost like everyday English

9
Compilation
  • Since a computer can only understand two symbols
    (0 or 1 (binary)), a translation mechanism is
    necessary if a programmer is to be able to
    communicate with a computer effectively
  • A program is created in a text editor or an IDE
    such as BlueJ
  • A program exists as a file on your computer
    system. Java files have the extension .java (e.g.
    program.java)
  • At this stage, it is called the source code of
    the program

10
Compilation
  • The source code has to be translated into a form
    that the computer can execute it has to be
    compiled
  • Translator programs compilers. They carry out
    the process of compilation
  • The result of compilation is an executable file

Source Code
Executable
Compiler
11
Compilation
  • Carrying out the process of compilation when
    using an Integrated Development Environment such
    as BlueJ is usually a matter of clicking a button
  • If the compilation process is successful, another
    file will be produced with a .class extension
  • In the case of program.java, a file will be
    created called program.class this is known as
    bytecode and can be run (executed) on any
    platform using a java interpreter

12
Compilation errors
  • Programmers sometimes make mistakes!
  • If this is the case, the program will not compile
    to produce an executable file
  • The compiler will tell the programmer (as best it
    can) where or what the error is and the
    programmer must correct the error and compile
    again
  • Hints for understanding your compilers error
    report
  • If a long list of errors is reported, correct the
    first and recompile
  • If the line where the error is identified is
    correct, look at the lines directly before it

13
Running (executing) your program
  • Once compiled successfully (no errors), your
    program is ready to be executed
  • Running a program requires the Java Virtual
    Machine - another program which runs the
    compiled version of code (the bytecode) on the
    computer
  • The JVM can also access other program code from
    various libraries on the system, known as
    packages which contain basic Java functions (They
    have to be imported into your program)
  • Execution of Java code again depends on your
    computer system

Source Code
Bytecode
JVM
Compiler
14
Objects
  • Java is an object-oriented programming language
    and is based on the use of basic building blocks
    called objects
  • An object (in the context of computer
    programming) is something that is processed or
    manipulated by the program
  • Example a person there are many different
    people and each one represents an instance of the
    object type person
  • Every object has attributes features that
    characterise it
  • Each attribute has only one value for each object
    instance
  • Each attribute has a particular type e.g. a
    persons date of birth is a number (programming
    languages support a range of different types)
  • An objects attributes can themselves be objects
    (e.g. the date of birth attribute of a person
    object could be an object itself with the
    attributes of day, month and year)

15
Methods
  • An object may have procedures which use its
    attributes to produce particular results
    methods
  • There are two types of methods
  • Those which return a value, and
  • Those which do not
  • Take the Date example. Possible methods might be
  • Find the next date
  • Determine if the date is valid
  • Display the date in a particular format
  • Finding the next date will involve returning
    another date
  • Determining if a date is valid will involve
    returning either true or false ( a boolean value)
  • Displaying the date will not return any value but
    will display something on the screen

16
Methods
  • A method which returns some value must have a
    type known as its return type
  • A method which does not return a value is known
    as a void method
  • An object type is defined by ATTRIBUTES and
    METHODS
  • A programmer can now use our Date object type by
    writing programs which create instances of a Date
    and use them

17
Why use objects?
  • Reusability program code of one object type can
    be reused in other programs which also make use
    of the object type
  • Easy to build up libraries (packages) which can
    be used over and over again
  • Savings in time and money

18
Object-oriented programming
  • Step One - look at the problem and identify the
    types of the objects needed
  • Step Two - write the code for these object types
  • Step Three - write the code which use the
    instances of these object types

19
Step One Analysis and Design
  • THE MOST IMPORTANT STEP!
  • Before any program is written, it is vitally
    important that the problem is carefully analysed
    to produce a carefully designed solution
  • Analysis and design are usually structured in the
    form of a formal methodology or, more informally,
    in a step-by-step approach

20
A set of steps
  • Identify objects the basic building blocks for
    your program
  • Identify attributes and their types (this may
    lead to the identification of other objects)
  • Identify methods decide what you want to be
    able to do with the object
  • Design the methods each method is really a
    small program which carries out a task, and has
    to be designed separately
  • Design the program this is the final program
    which makes use of the object types

21
An example
  • There are many students taking the module
    COM158M1 from many different courses. A program
    is required to assist with the administration of
    the module. The module coordinator wants to be
    able to keep track of all the students progress.
    She needs to know each students name, gender,
    age and course on which he/she is enrolled. When
    a student completes an assignment (of which there
    are three), she needs to record the mark achieved
    and be able to calculate an overall average mark
    ad corresponding grade (Grade D is lt40, C is
    40-50, B is 51-65, A is 66).

22
Identify the objects
  • Nouns within the problem description include
  • Student
  • Module
  • Course
  • Module coordinator
  • Assignment
  • Mark
  • Grade
  • Which of these nouns are needed as objects?

23
Identify the objects
  • Refined list
  • Student
  • Course
  • Assignment
  • Why have the others been left out of the refined
    list?
  • We end up with 3 objects which we will call
    Student, Course and Assignment
  • The next step is to identify the attributes for
    each object

24
Identify the attributes and their types
  • This can be done from the problem description,
    but is more usually done in consultation with the
    person who requires the program (in this case,
    the module coordinator)
  • Student
  • Name (String) Age (Number) Gender (Character
    M or F)
  • Course (instance of a Course object)
  • Assignment1 Assignment2Assignment3 (instances
    of an Assignment object)
  • Grade
  • Course
  • Title (String)
  • Code (String)
  • Assignment
  • Number (Number)
  • Title (String)
  • Mark (Number)

25
Identify the methods
  • Focus on the problem description what is the
    main focus of the final program?
  • Possibilities for the Student object
  • Print a description of a students personal
    details
  • Calculate a students average mark
  • Calculate a students grade
  • Print a students average mark and grade
  • Possibilities for the Course object
  • Change the Course title
  • Change the Course code

26
Identify the methods
  • Possibilities for the Assignment object
  • Read in and store a mark for the assignment
  • Print the assignment mark
  • It is possible for an object type to have no
    methods only attributes
  • It may also be the case that the module
    coordinator wants to display a list of students
    on a particular course who are taking the module,
    and calculate the average for that group of
    students
  • Methods which can do this are not methods which
    belong to the object types they operate on
    collections of objects and not on individual
    objects they are carried out by the programs
    which use the objects and are implemented by the
    programmer

27
Design the methods
  • This is usually done in a form of pseudocode
    rather than in text (which can become quite
    complicated in a complex method), it is usually
    quite close to the programming language
  • There are various styles which can be used its
    should be clear and unambiguous
  • Example
  • IF the average mark is 70 or above
  • RETURN the value A

28
Design the program
  • Once the object types have been designed, the
    last design stage involves designing the program
    that will use them
  • Again, this involves pseudocode
  • Example
  • Read in a students personal details
  • Read in the assignment marks
  • Print the assignment marks
  • Calculate the average mark and grade
  • Print the average mark and grade
  • This is very high level pseudocode which needs to
    be refined into much more detail we will learn
    how to do this as we move on
  • Step Two and Three (writing the code) follow on
    from design
  • To be able to write the code, we first need to
    become familiar with the BlueJ IDE..

29
The BlueJ Integrated Development Environment
30
Steps for developing an application using the
BlueJ IDE
  • Create a project (Its useful to do this each
    week)
  • Set up a folder in your U directory called
    JavaPrograms and save all your projects in this
    folder (You should also make backup copies on
    floppy disk or CD)
  • Create a class within your project
  • Edit the class (type in your code) and save it
  • Compile (Compile button on the toolbar)
  • If there are errors, correct them. A message will
    appear in the area at the bottom of the window
    telling you where the errors are.
  • When compiled with no errors, close the editor
  • Execute the program
  • NOTE There are full instructions for setting up
    and using BlueJ on my website http//www.infm.uls
    t.ac.uk/heather/lecture03.htm

31
Exercises
  • Essential Reading
  • Chapters 1-5 of the core text (before next weeks
    lecture)
  • In this weeks practical
  • Print the BlueJ user guide available on the web
    and become familiar with the IDE
  • Login to WebCT and become familiar with the
    environment
Write a Comment
User Comments (0)
About PowerShow.com