23
Programming for Programming for Beginners Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input

Embed Size (px)

Citation preview

Page 1: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input

Programming for Programming for BeginnersBeginners

Martin Nelson

Elizabeth FitzGerald

Lecture 7: Methods & User Input

Page 2: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input

Revision of Session 6

Last time, we... Introduced object-oriented programming. Saw the difference between classes & objects Understood why classes and objects are important

in object-oriented programming Talked about classes as user-defined data types. Learned how to create our own Classes

Page 3: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input

Revision of Session 6

An object is a piece of self-contained code which represents a specific thing in the real world.

The source code responsible for defining an object is called a class.

We may have a class called “Date” and an object called “today”. “today” is an instance of class Date.

Classes have capital letters, primitive variables have lower case letters.

Classes should be self-contained and reusable. “Construction kit” approach to programming.

Page 4: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input

Revision of Session 6

Class definition can go at the top of the application source code, or in its own file.

Once the class is defined, we create an object by instantiating the class:

Date today = new Date(); We can access an object’s member variables using

the dot syntax:

today.weekday = “Thursday”;

today.year = 2010;

System.out.println(today.weekday);

Page 5: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input

Session 7 - aims & objectives

Adding functionality to our classes (methods) What methods are and why they are important Return types in methods Methods with arguments

Getting input from the user.

Page 6: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input

Methods of classes

Methods contain program code that carries out specific tasks Methods of an instantiated class may be called

(“invoked”) from within that class or from another class

Methods can be called repeatedly in the same main program

Each new method must be given a name

Values from other parts of the program may be passed to a method as arguments

Values may also be returned from the method

Page 7: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input

Example – The Pen Class

The class Pen represents any generic pen. Properties (data)

Colour Thickness

Functionality (methods)

Objects are instances of class Pen – representing specific pens, belonging to specific people: Liz’s biro Martin’s board marker Bob’s fountain pen

Draw circle Replace lid

Remove lid Draw line

Page 8: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input

Defining a method Method definitions require at least 3 pieces of

information: Method name

List of input arguments (which could be empty)

Return type (what sort of data is passed back to the calling code?)

class Pen

{

void writeSomething(String text)

{

System.out.println(text);

}

}

Page 9: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input

Defining a method Method definitions require at least 3 pieces of

information: Method name

List of input arguments (which could be empty)

Return type (what sort of data is passed back to the calling code?)

class Pen

{

void writeSomething(String text)

{

System.out.println(text);

}

}

Method name

Page 10: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input

Defining a method Method definitions require at least 3 pieces of

information: Method name

List of input arguments (which could be empty)

Return type (what sort of data is passed back to the calling code?)

class Pen

{

void writeSomething(String text)

{

System.out.println(text);

}

}

List of arguments

Method name

Page 11: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input

Defining a method Method definitions require at least 3 pieces of

information: Method name

List of input arguments (which could be empty)

Return type (what sort of data is passed back to the calling code?)

class Pen

{

void writeSomething(String text)

{

System.out.println(text);

}

}

List of arguments

Method name

Return type

Page 12: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input

Input Arguments

In the method definition, the list of input arguments must specify the names of the inputs and what type of data they are.

Can have as many input arguments as you like – just separate them by commas.

void printManyTimes(String text, int number)

{

for(int i=0; i<number; i++)

{

System.out.println(text);

}

}

Page 13: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input

Return Types

A method can return almost anything: void – the method doesn’t return anything. (This

doesn’t mean that the method isn’t useful!) int, double, float, boolean, String etc. – A method

can return any of the primitive date types. Date, Pen, Person etc. – We can ask the method to

return any of the classes which we have defined ourselves.

A method can only ever return one thing. What if we need to return more than one piece of

information? We’ll come back to this in Session 9...

Page 14: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input

Passing values to methodsclass passValue

{

static void blob (String hat)

{

System.out.println(“I’ve got a “+hat);

}

}

class coldHead

{

public static void main(String[] args)

{

System.out.println(“Give me a hat…”);

passValue.blob(“Baseball cap”);

}

}

Method called blob has an argument called hat

A value is assigned to the argument

Page 15: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input

Output of hat program

granby$ javac coldHead.java

granby$ java coldHead

Give me a hat…

I’ve got a Baseball cap

granby$

Page 16: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input

Example: using void return valueclass Date

{

int day;

String weekday;

String month;

int year;

void printDate()

{

System.out.print(weekday + ", " + day + " ");

System.out.println(month + ", " + year + ".");

}

}

The Date class contains a method called “printDate” with a void return value

Page 17: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input

Returning values from methods Methods will often return information to the calling

code

Return type must be specified in the method declaration

The information to return is identified using the “return” reserved word

The method may then be invoked in an assignment

For any return type other than void, the method must have a return statement.

Page 18: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input

Example: using String return value

class Date2

{

int day;

String weekday;

void printDate()

{

System.out.print(weekday + ", " + day + " ");

}

String getVersion()

{

return "Date [version 3.0]";

}

}

The getVersion method returns a value of type String

Page 19: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input

Static Variables & Methods

Variables & methods can be declared as static. This means that they are the same for every instance of a class.

If a static variable is updated for one instance of a class, its value will also change for every other instance of the same class.

Since static variables/methods are the same for every instance, it doesn’t matter which instance it is called for.

This is really useful for methods as we can actually call methods without instantiating the class at all!

We’ll see some examples of static methods shortly...

Page 20: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input

A Note on Access Control

We can control which bits of code are allowed to use our methods.

We can use reserved words public and private in the method definition to control this.

Public methods This is the default value; public methods can be used

anywhere.

Private methods Access is restricted to the class that contains the method.

Access control is more important when working on large projects. We will ignore this issue in this course, keeping everything as public (by default).

Page 21: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input

Using External Classes

Class definitions can be placed in the same file as the application class, or in their own file.

Many classes are distributed with the JDK in the Core Class Libraries. We’ll talk about how to use these next time...

Or, you can place your own classes in their own file...

As long as you save the file in the same place as your application class, you will be able to use the classes without doing anything new.

Saving classes in their own files makes them more portable and easier to deploy in many different applications.

Page 22: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input

The UserInput Class

Written specifically for the University of Nottingham’s Java programming courses.

Contains three methods for getting input from the user: static int readint()

static double readdouble()

static String readString()

Since the methods are static, you can use them without instantiating the UserInput class.

To read an int from the user, and assign to variable num:

int num;

num = UserInput.readint();

Page 23: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input

Coming up in Session 8...

Using the Core Class Libraries...