Title: Java Programs COMP 102
1Java ProgramsCOMP 102 3 2008 T1
2Menu
- OOS.
- OOP Object Oriented Programming
- The Java Language
- The anatomy of a Java program
- Reading
- L L Sections 1.4 and 2.1
- L L For next week rest of chapter 2 (except
2.8). - Announcements
- Class rep
- Session for Engineering Students Tuesday 5-7
- includes pizza
3Signs of Computer Distress
Sore
stiffness soreness discomfort weakness tightness h
eavy
tingling numbness aching throbbing burning hot
Stop moving
No oxygen to tissue
Immobility
No blood flow
Staying still makes muscle fatigue worse. Get
moving!
4True or False?
- False
- OOS/muscle fatigue is a crippling injury
- It will affect the rest of your life
- Once you have it, you will never get better
- True
- When people rested, pain got worse
- Immobilising sore areas makes pain worse
- Physical exercise works!
Muscle fatigue/OOS is not an injury it is a
lack of blood supply to muscles Get up and get
moving!
5What is a Program
- A program is a specification for the behaviour of
a computer - What the computer should do when
- the program is started
- a particular action is requested
- the user types something
- the user clicks with the mouse
- a message arrives over the network
- some input from a camera/switch/sensor arrives.
-
- Responses may be simple or very complex.
- A program consists of
- descriptions of responses to events/requests,
- written as instructions,
- in a language the computer can understand.
6Machine Language
- What the computer can understand
- Different for each computer
- Very detailed, low-level control of the computer
- Horrible to read
copy the contents of memory location 243 into
register 1.
201 243 051 531 211 1243
add the contents of memory location 531 to the
contents of register 1.
copy the contents of register 1 to memory
location 531.
7Better Programming Languages
- High-level programming languages
- designed for people to use
- designed to be translated into machine language
- (or to an intermediate language)
- compiled translated all at once
- interpreted translated one instruction at a time
- Must be
- Precise no ambiguity about what to do
- Expressive must be able to specify whatever you
want done. - Readable People must be able to read the
instructions. - Translatable able to be translated into machine
language - Concise not long-winded
Perl php python javascript ML C Eiffel Prolog Ha
skell Java C
FORTRAN LISP Algol COBOL Basic C Pascal Simula Sma
lltalk Modula Ada
8Programming Languages
- Different languages support different paradigms
- imperative,
- object-oriented,
- functional,
- logic programming, ...
- Object Oriented programming languages
- Organise program around Classes or types of
Objects - Each class of Objects can perform a particular
set of actions? specify instructions how to
perform each action - Many instructions consist of asking some object
to performone of its actions
9Java
- A high-level Object-Oriented programming language
- Designed by Sun Microsystems, early-mid 1990's.
- Widely used in teaching and industry.
- Related to C, but simpler. Similar to C.
- Good for interactive applications.
- Extensive libraries of predefined classesto
support, UIs, graphics, databases, web
applications, ... - Very portable between kinds of computers.
10MazeMouse a simple language
- Writing a program to control a Mouse in a Maze
- The mouse should get out of the maze
- No matter what shape the maze is!!
- The program must cope with the general case!
- What should the mouse do when
- theres a space ahead
- theres only a space to the left
- theres only a space to the right
- theres space on both sides
- its in a dead-end
- Language
- Sequence of Forward, Left, and/or Righteg
FLFR
11Constructing Programs
- Specify Design Code Test
- EditCompileRun cycle
InitialDesign
Edit typing in the Java code
Specification
Compile Translating to executable instructions
Run Testing the program to see that it works
12Designing a Program step 1
- Specification
- Write a program that will let the user do two
things - write out formula for converting from fahrenheit
to centigrade. - compute and write out the temperature in
centigrade given a temperature in fahrenheit - ? Two different actions in response to two
different requests. - Involves
- printing information out
- computing new values out of old values
13Designing a program step 2
- What are the kinds of objects?
- What are the actions of the objects?
- How should the computer do the actions?
14Designing a Program Step 3
- / Program for converting between temperature
scales / - public class TemperatureConverter
-
- / Print out the conversion formula /
- public void printFormula()
- System.out.println("centigrade (fahrenheit -
32) 5/9") -
-
- / Convert from fahrenheit to centigrade /
- public void fahrenToCent(int fahren)
- int cent
- cent (fahren - 32) 5 / 9
- System.out.println(fahren F -gt " cent
C") -
-
15Elements of the program
- Comments vs Code
- Keywords vs Identifiers vs Strings vs numbers vs
operators and punctuation - Keywords words with special meaning in the Java
Languageeg public, class, if, while
mostly to do with the structure of the program - Identifiers other words, used to refer to
things in the program. mostly made up by the
programmer,some are predefined. - Strings bits of text that the program will
manipulate.always surrounded by and - numbers
- operators and punctuation - /
, ( ) all have precise meanings and
rules for use
16Elements of the program
- Program Structure
- Class
- Top level components of program
- Describes a class of objects
- Specifies the set of actions this kind of object
can perform - (Can also specify information the objects can
hold) - Note name, and conventions for naming.
- Methods
- Main elements of a class
- Each method describes one action that the objects
of this class can perform - (compare to MazeMouse your program had 5
methods)