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

Slides:



Advertisements
Similar presentations
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Advertisements

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Lecture 2: Object Oriented Programming I
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 6: User-Defined Functions I
CMT Programming Software Applications
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Java Classes Using Java Classes Introduction to UML.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 13: An Introduction to C++
The Java Programming Language
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.
Lecture 2: Classes and Objects, using Scanner and String.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 11: Handling Errors; File Input/Output.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
School of Computer Science & Information Technology G6DICP - Lecture 11 Classes & Objects.
Working With Objects Tonga Institute of Higher Education.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Week 10 - Friday.  What did we talk about last time?  References and primitive types  Started review.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
OOP Basics Classes & Methods (c) IDMS/SQL News
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 9: Arrays; Revision Session.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Methods. Methods are groups of statements placed together under a single name. All Java applications have a class which includes a main method class MyClass.
3 Introduction to Classes and Objects.
Examples of Classes & Objects
Chapter No. : 1 Introduction to Java.
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Starting Out with Java: From Control Structures through Objects
IDENTIFIERS CSC 111.
Chapter 3 Introduction to Classes, Objects Methods and Strings
An Introduction to Java – Part I, language basics
Interfaces, Classes & Objects
Session 2: Introduction to Object Oriented Programming
Java for Beginners University Greenwich Computing At School DASCO
ITM 352 Functions.
Corresponds with Chapter 5
Presentation transcript:

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

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.

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);

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.

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

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

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); }

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

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

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

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); }

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...

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

Output of hat program granby$ javac coldHead.java granby$ java coldHead Give me a hat… I’ve got a Baseball cap granby$

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

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.

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

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...

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).

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.

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();

Coming up in Session 8... Using the Core Class Libraries...