Object-Oriented Programming Lab 1 Exercises Introduction Today's ...

28 downloads 831 Views 62KB Size Report
Object-Oriented ProgrammingLab 1 Exercises. Inf1 :: 2009/10. 1 / 24. Introduction . Who's who. Scheduled vs. drop-in labs. Ewan Klein (School of Informatics).
Introduction

.

. ..

.

.

.

Object-Oriented Programming Lab 1 Exercises

Who’s who Ewan Klein

Scheduled vs. drop-in labs

School of Informatics

Inf1 :: 2009/10

Ewan Klein (School of Informatics)

Object-Oriented ProgrammingLab 1 Exercises

Inf1 :: 2009/10

1 / 24

Today’s Exercises

... ... ... ...

Object-Oriented ProgrammingLab 1 Exercises

Inf1 :: 2009/10

2 / 24

Already discussed in this week’s lecture.

1

HelloWorld

2

PersonalGreeting

3

Adder

4

Ewan Klein (School of Informatics)

HelloWorld

Variants:

... ... ... 1

2 3

Divider

Ewan Klein (School of Informatics)

Object-Oriented ProgrammingLab 1 Exercises

Inf1 :: 2009/10

3 / 24

modify the string that is being printed out experiment with print vs. println make some deliberate errors to see what happens (e.g., miss out a closing }), mis-spell the class name, use single quotes for the string)

Ewan Klein (School of Informatics)

Object-Oriented ProgrammingLab 1 Exercises

Inf1 :: 2009/10

4 / 24

HelloWorld

PersonalGreeting

Already discussed in this week’s lecture. Variants:

3

New stuff — uses variables and functions

modify the string that is being printed out experiment with print vs. println make some deliberate errors to see what happens (e.g., miss out a closing }), mis-spell the class name, use single quotes for the string)

Ewan Klein (School of Informatics)

Object-Oriented ProgrammingLab 1 Exercises

Inf1 :: 2009/10

Let’s look at variables

5 / 24

Declaring Variables

Ewan Klein (School of Informatics)

Object-Oriented ProgrammingLab 1 Exercises

Inf1 :: 2009/10

6 / 24

Putting it Together

.

Declarations plus Assignment

.

..

But Java wants to know the type of the value!

String name;

Declaring a variable: type var;

name = ”Bob”;

. Variable Declarations ..

.

String name;

int number; number = 33;

. ..

.

.

int number;

Important: first declare the variable (reserve some space in memory); then assign a value of the appropriate type.

. .. . Short Version ..

.

.

String name = ”Bob”; int number = 33;

. ..

Ewan Klein (School of Informatics)

Object-Oriented ProgrammingLab 1 Exercises

Inf1 :: 2009/10

.

1

2

7 / 24

.

Ewan Klein (School of Informatics)

Object-Oriented ProgrammingLab 1 Exercises

Inf1 :: 2009/10

.

... ... ...

8 / 24

Hello World with added variables

Using return value from a function

.

Putting the message in a variable

.

..

public class HelloWorld {

Suppose we have a function today() that returns a string giving today’s date. We can assign this return value to a variable:

public static void main ( String [] args ) {

.

String msg = ”Hello World!”;

Assigning a return value . ..

.

Object-Oriented ProgrammingLab 1 Exercises

Inf1 :: 2009/10

9 / 24

Finally

Adder and Divider should be fairly self-explanatory

Questions? The lab 1 exercises: http://www.inf.ed.ac.uk/teaching/courses/inf1/oop/Labs/build/ html/lab1.html

Or follow link from: http://www.inf.ed.ac.uk/teaching/courses/inf1/oop/

Ewan Klein (School of Informatics)

Object-Oriented ProgrammingLab 1 Exercises

Inf1 :: 2009/10

.

.

. ..

.

String date = today();

}

Ewan Klein (School of Informatics)

.

..

System.out.println( msg ); }

11 / 24

Ewan Klein (School of Informatics)

Object-Oriented ProgrammingLab 1 Exercises

Inf1 :: 2009/10

10 / 24