building java programs, chapter 3 parameters, methods and objects

Download Building java programs, chapter 3 Parameters, Methods and Objects

If you can't read please download the document

Upload: amelia-gardner

Post on 06-Jan-2018

218 views

Category:

Documents


1 download

DESCRIPTION

At the end of this class, you will be able to Define method parameters Define formal parameters and actual parameters Identify formal and actual parameters in an existing method call Trace the values of parameters across method calls in an existing code Write a method which produces different output by calling it with different parameters

TRANSCRIPT

Building java programs, chapter 3 Parameters, Methods and Objects From the previous chapter, you know how to Declare variables of primary primitive types Write an expression that assigns a variable a value from an expression using operators Evaluate expressions using the rules of precedence Use the increment and decrement operators Output to the console string concatenation results Cast a variable from one type to another Trace control flow through a for-loop Write simple and nested for-loops At the end of this class, you will be able to Define method parameters Define formal parameters and actual parameters Identify formal and actual parameters in an existing method call Trace the values of parameters across method calls in an existing code Write a method which produces different output by calling it with different parameters So far, we have seen method calls public static void main (String args[]) { printSomething(); } private static void printSomething() { System.out.println("********"); } Parameters A parameter is a value that is used to communicate between methods. Actual parameter A specific value or expression in a method call Formal parameter A variable name that appears in the header of a method call Example code for parameters public static void main (String args[]) { int actualParameter = 10; callMethod(actualParameter); } private static void callMethod(int formalParameter) { System.out.println("value of parameter inside callMethod is + formalParameter); } Parameters are one way passing You can use parameters to send information to methods but methods cannot use parameters to send information back public static void main (String args[]) { int parameter = 10; callMethod(parameter); System.out.println("value of parameter inside main is " + parameter); } private static void callMethod(int parameter) { parameter++; System.out.println("value of parameter inside callMethod is " + parameter); } Methods can take multiple parameters Any *primitive data type can be passed in as a parameter to a method. public static void main (String args[]) { int parameter1 = 10; double parameter2 = 3.14; callMethod(parameter1, parameter2); System.out.println("value of parameters inside main is " + parameter1 + + parameter2); } private static void callMethod(int anotherNameForParameter1, double anotherNameForParameter2) { anotherNameForParameter1++; anotherNameForParameter2++ System.out.println("value of parameters inside callMethod is " + anotherNameForParameter1 + + anotherNameForParameter2); } Code along - On your computer Write a program to print a given number 5 times. Your main method should call another method called print5 which takes in the number to be printed as a parameter and print it 5 times in the same line separated by a space. Eg. For input 4, your program should print Write a program to print a given number any number of times. Now, your main method should call another method called printN which takes in the number to be printed along with the number of times you should print it. Eg. For input number 4 and count 9, your program should print Eg. For input number 4 and count 9, your program should print Homework Reading assignment BJP 3.1 PracticeIt Self Check 3.4, 3.5, 3.6, 3.7 PracticeIt Exercise 3.1, 3.2, 3.3