Download presentation
Presentation is loading. Please wait.
Published byErick McKinney Modified over 9 years ago
1
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input
2
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
3
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.
4
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);
5
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.
6
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
7
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
8
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); }
9
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
10
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
11
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
12
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); }
13
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...
14
Passing values to methods class 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
15
Output of hat program granby$ javac coldHead.java granby$ java coldHead Give me a hat… I’ve got a Baseball cap granby$
16
Example: using void return value class 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
17
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.
18
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
19
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...
20
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).
21
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.
22
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();
23
Coming up in Session 8... Using the Core Class Libraries...
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.