Introduction to Methods in java

Slides:



Advertisements
Similar presentations
Programming Methodology (1). Implementing Methods main.
Advertisements

Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
CS110 Programming Language I
Computer Programming Lab 8.
1 Repetition structures Overview while statement for statement do while statement.
03 Data types1June Data types CE : Fundamental Programming Techniques.
Reading Information From the User Making your Programs Interactive.
11 Methods1June Methods CE : Fundamental Programming Techniques.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
LAB 10.
1 Scanner objects. 2 Interactive programs We have written programs that print console output. It is also possible to read input from the console.  The.
Computer Programming Lab(5).
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
1 Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.1.
Input & Output In Java. Input & Output It is very complicated for a computer to show how information is processed. Although a computer is very good at.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
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.
Interactive Programs with Scanner. 2 Input and System.in interactive program: Reads input from the console. –While the program runs, it asks the user.
Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦
CSC1401 Strings (text). Learning Goals Working with Strings as a data type (a class) Input and output of Strings String operations.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
Building Java Programs Program Logic and Indefinite Loops.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
TOPIC 8 MORE ON WHILE LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
Building Java Programs Chapter 4 Lecture 4-1: Scanner ; cumulative algorithms reading: 3.3 – 3.4, 4.2.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Variable scope. Variable Scope Variables do not live forever. Failing to take that into account leads to problems. Let's look at an example. Let's write.
Building Java Programs
Chapter 2 Clarifications
CSC111 Quick Revision.
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
Chapter 6 More Conditionals and Loops
Maha AlSaif Maryam AlQattan
Lecture Note Set 1 Thursday 12-May-05
Computer Programming Methodology Input and While Loop
TK1114 Computer Programming
SELECTION STATEMENTS (1)
Topic 11 Scanner object, conditional execution
Something about Java Introduction to Problem Solving and Programming 1.
While Statement.
Conditional Loops.
Introduction to Classes and Methods
Subprograms Intro & Procedures.
More on Classes and Objects
Building Java Programs
CSE Module 1 A Programming Primer
Building Java Programs
Java Coding 4 (part2) David Davenport Computer Eng. Dept.,
CSC1401 Input and Output (with Files)
Building Java Programs
Random Numbers while loop
Building Java Programs
CSE Module 1 A Programming Primer
Building Java Programs
More on iterations using
Computer Science Club 1st November 2019.
Validation We have covered one way to get user input, using Scanner to get it from the console We will soon cover at least one additional way to get.
Optional Topic: User Input with Scanner
Presentation transcript:

Introduction to Methods in java Unit 3 - Methods Introduction to Methods in java

What is a method? A method is a block of code that can be re-used (called) over and over whenever we want. The benefit is that you can reuse methods in different programs. This is especially useful for making our programs smaller and modular

Sample – Reusable Code // Create a Scanner to retrieve input. Scanner sc = new Scanner (System.in); // Request input from the user. System.out.print(“Please enter an integer: “); int num = sc.nextInt();

Creating a Method Methods require three things: 1) A unique name (similar to a variable) 2) A return type (if applicable – void if none) 3) A body of code (possibly with a return type) SYNTAX: public static <return type> <method name> () { <body of code> }

Example: Retrieving a number public static int getNumber () { // Create a Scanner to retrieve input. Scanner sc = new Scanner (System.in); // Request input from the user. System.out.print(“Please enter an integer: “); int num = sc.nextInt(); // Return the int value to the calling. return num; }

Using a Method To use a method, simply write its name within your main() code. public static void main (String[] args) { getNumber(); }

Using a Method To use a method, simply write its name within your main() code. public static void main (String[] args) { getNumber(); } PROBLEM: The main method isn’t doing anything with the integer that getNumber() returns.

Using a Method To use a method, simply write its name within your main() code. public static void main (String[] args) { // Solution: Save its returned value in a variable! int userNumber = getNumber(); }

Void Methods A method with a return type of void doesn’t return anything. It simply does stuff. public static void greetUser () { // Say hello and ask for their name. Scanner sc = new Scanner(System.in); System.out.print(“Hey there, what’s your name?”); String name = sc.next(); System.out.println(“Nice to meet you, “ + name); // No return statement needed. }