Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Introduction to Programming
Week 8 Arrays Part 2 String & Pointer
Intro Programming By Trevor Decker Team 1743 By Trevor Decker Team 1743.
1 Lecture Today’s topic Arrays Reading for this Lecture: –Chaper 11.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
Scanner & Stepwise Refinement Pepper With credit to Dr. Siegfried.
Introduction to Methods
Multi-Dimensional Arrays Rectangular & Jagged Plus: More 1D traversal.
Unit 2: Java Introduction to Programming 2.1 Initial Example.
Java Unit 9: Arrays Declaring and Processing Arrays.
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
Laboratory Study October, The very first example, traditional "Hello World!" program: public class first { public static void main (String[ ]
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
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.
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
Procedural programming in Java Methods, parameters and return values.
Mixing integer and floating point numbers in an arithmetic operation.
Java Arrays and Methods MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
Indentation & Readability. What does this program do? public class Hello { public static void main ( String[] args ) { //display initial message System.out.println(
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
Catie Welsh February 23,  Lab 4 due on Friday  Lab 5 will be assigned on Friday 2.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
Creating and Using Class Methods. Definition Class Object.
Simple algorithms on an array - compute sum and min.
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
Java Methods 11/10/2015. Learning Objectives  Be able to read a program that uses methods.  Be able to write a write a program that uses methods.
Special Methods in Java. Mathematical methods The Math library is extensive, has many methods that you can call to aid you in your programming. Math.pow(double.
int [] scores = new int [10];
Classes - Intermediate
Methods.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
JAVA METHODS (FUNCTIONS). Why are they called methods? Java is a strictly object-oriented programming language Methods are functions inside of objects.
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
Last Revision. Question1 Novice Java programmers often write code similar to, class C { public int x;... }... C[] a = new C[10]; for(int i = 0; i < a.length;
Object Oriented Programming Lecture 2: BallWorld.
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
Slides by Evan Gallagher
Slides by Evan Gallagher
Department of Computer Science
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Building Java Programs
Building Java Programs
Writing Methods.
An Introduction to Java – Part I, language basics
Building Java Programs
Java Lesson 36 Mr. Kalmes.
Building Java Programs
CS 180 Assignment 6 Arrays.
Methods and Data Passing
Scope of variables class scopeofvars {
Building Java Programs
Names of variables, functions, classes
Building Java Programs
Loops CGS3416 Spring 2019 Lecture 7.
Loops and Iteration CS 21a: Introduction to Computing I
PROGRAMMING ASSIGNMENT I
Presentation transcript:

Methods

Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right here public static void main(String[ ] args) { }

Characteristics of a method reserved word staticreturn typeparameters static int methodname(int a, int b) { // body } All methods must have these things.

Methods do not have to have parameters/return types static void method1() static int method1(int a) static double method1() static void method1(int a, int b, String c)

All return types must have the word return at the end of the method. static int method1(int a, int b) { int sum = a + b; return sum; } This value must match return type. In this case, an integer.

In our code, a method that returns a value must be set equal to something. static int returnSum(int a, int b) { int sum = a + b; return sum;parameters must match } void main() { int value = returnSum(10,10); }

Here’s a method that gives line feeds static void skip (int n) { for(int k = 0; k < n; k++) System.out.println(); } void main() { skip(5); }

Here’s a method multiplies numbers together and returns results. static int method(int a, int b) { int c = a * b; return c; } void main() { int d = method(10,10); }

Here’s a method that takes an array and initializes all values to random numbers. static void method(int[ ] a) { for(int j = 0; j < a.length; j++) System.out.print(a[j] + “ “); } void main() { int[ ] array1 = new int[10]; method(array1); }

Example 1 static void skip() { for(int k = 0; k < 5; k++) System.out.println(); } void main() { skip(5); }

Example 2 static int skip(int n) { for(int k = 0; k < n; k++) System.out.println(); } void main() { int x = skip(5); }

Example 3 static void skip(int n) { for(int k = 0; k < n; k++) System.out.println(); return 0; } void main() { skip(5); }

Exercise 1 Write a method called PrintWorld. PrintWorld will take one integer as a parameter and print “Hello World” that many times.

Exercise 2 Create a method called ReturnSum that returns the sum of any two integers. ReturnSum will take two integers as parameters, add them together, and then return the value. Display the value of this sum in void main.

Exercise 3 Create a method called Factors. Factors takes three integer parameters. The first two integers are to be multiplied together repeatedly. The third integers indicates how many times the multiplication will occur. Display your results.

Exercise 4 Create a method called AddArray. AddArray has no return value and does not have any parameters. AddArray will create an array of size 10 and assign random integer values to its elements between Now create a method called SumArray. SumArray will return the sum of all the elements of the array that was passed to it. SumArray takes an integer array as its parameter. Run AddArray and then have AddArray call SumArray and pass the array as a parameter.

Exercise 5 Declare 3 methods. Method1 multiplies two numbers together and returns the value. Method2 adds two numbers together and returns the value. Method3 divides the first number by the second number. In void main, ask me to enter two numbers and then ask me what I want to do with those numbers. Call the appropriate method for my selection. This should program should keep running until I choose exit.