138x Filetype PDF File size 0.28 MB Source: www.doc.ic.ac.uk
Lecture 1 : Introduction to Textbooks
Programming in Java No textbook is required.
For programming beginners:
Lecturer : Susan Eisenbach Java Software Solutions: Foundations of Program
Design, John Lewis and William Loftus,
This is the 1st lecture on Java Publisher: Addison Wesley, 2002.
programming.This course is primarily For experienced programmers:
about writing imperative programs using – Learning the Java™ Language at
the Kenya system. http://java.sun.com/docs/books/tutorial/
Next term you will learn to write object – Thinking in Java, Bruce Eckel, Prentice Hall
oriented Java programs.
Susan Eisenbach 1 2
Functional versus
Software is required imperative languages
Functional languages are ideal for expressing the
http://www.doc.ic.ac.uk/kenya/ functional (the problem to be solved) component of any
download Java onto your home machine problem however...
at least 50% of all programs deal with input/output
follow the instructions to install it rather than a problem and functional languages aren’t
then follow the instructions to install very good at input/output.
Think of the programs you use now:
either Kenya or KenyaEclipse editor
mail
language translator (Haskell or Java)
web browser
Functional programming should have taught you to
appreciate concise elegant programs.
3 4
A statement written in Java print() and println()
println("Write this in Haskell!"); Text can be printed on the screen using
Commented version print() or println().
/* Susan Eisenbach Using println(" ") puts a carriage
* 12 November 2007 every statement is return on the end of the line.
* a bit of bravado terminated with a ; print( "7*3" );
*/ println( "=" ); println( 7 * 3 );
This code prints:
String exclaim = "Write this in Haskell!"; 7*3=
println(exclaim); 21
5 6
Concatenating output with + Comments
String drink = "slammers"; There are two ways of commenting code.
print("I like "); println(drink); // comments are terminated by the end of line
This code prints: I like slammers // Susan Eisenbach
println("I like Tequila " + drink); // 12 November 2007
This code prints: I like Tequila slammers // a bit of bravado
/* comments in Java are also terminated by */
println /* Susan Eisenbach
("6/9 = " + 6/9 + " or " + 6.0/9.0); * 12 November 2007
This code prints: * a bit of bravado good to make several
6/9 = 0 or 0.6666666666666666 */
lines of comments stand
out in your program
7 8
A function written in Haskell A function written in Haskell
bigger :: Int -> Int -> Int bigger :: Int -> Int -> Int
-- post: returns the larger of two numbers -- post: returns the larger of two numbers
bigger a b|a>b = a argument types bigger a b|a>b = a
|otherwise = b result type |otherwise = b
arguments
Same method written in Java Same method written in Java
int bigger(int a, int b){ int bigger(int a, int b){
//post: returns the larger of the 2 values //post: returns the larger of the 2 values
if (a > b) {return a;} if (a > b) {return a;}
else {return b;} else {return b;}
} }
9 10
Returning from a method and
A function written in Haskell conditionals
bigger :: Int -> Int -> Int int bigger(int a, int b){
-- post: returns the larger of two numbers //post: returns the larger of the 2 values
bigger a b|a>b = a predicate (test) must be in brackets()
|otherwise = b then and else if (a > b) {return a;} results have
branches are to be returned
Same method written in Java surrounded by{ } else {return b;} using the keyword
int bigger(int a, int b){ } return
//post: returns the larger of the 2 values
if (a > b) {return a;} conditionals - using the keywords if and optionally else
else {return b;} method bodies are
} surrounded by { }
11 12
A Java program must contain a
A function written in Haskell main method
biggest :: Int -> Int -> Int -> Int It is the main method that starts the execution
-- post: returns the largest of 3 numbers of a program off.
biggest a b c = bigger a (bigger b c) It doesn’t return anything. The return type of a
method that does not return anything is void.
Same function written in Java The first statement can be made into a program
as follows:
int biggest(int a, int b, int c){ void main(){
//post: returns the largest of the 3 values println("Write this in Haskell!");
return bigger(a, bigger(b,c)); }
} By custom the main method is the first method in
13 the program. 14
/*Susan Eisenbach
*12 November 2007
*chooses the largest of 3 numbers Variable declarations
*/
void main(){ Variables are names of storage locations. Variables can be
print("Type in your 3 numbers -> "); declared of the following types:
println(biggest(readInt(),readInt(),readInt())); int double boolean char String
} They must be declared before they are used.
int bigger(int a, int b){ int j;
//post: returns the larger of the 2 values double cost;
if (a > b) {return a;} String firstname; String surname;
else {return b;} Variables can be initialised in declarations
} int total = 0;
int biggest(int a, int b, int c){ char answer = 'y';
//post: returns the largest of the 3 values double start = 0;
return bigger(a, bigger(b,c)); double sum = 0.0;
} boolean finish = false;
15 16
no reviews yet
Please Login to review.