Writing methods. Calling Methods nameOfMethod(parameters) if method returns something, use that value Math.pow(2, 3) returns 8 System.out.println(Math.pow(2,

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

1 Classes and Objects in Java Parameter Passing, Delegation, Visibility Control, and Object Cleanup.
***** SWTJC STEM ***** Chapter 4-1 cg 42 Object Oriented Program Terms Up until now we have focused on application programs written in procedural oriented.
Lecture 1: Comments, Variables, Assignment. Definitions The formal (human-readable) instructions that we give to the computer is called source code The.
Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Your First Java Program: HelloWorld.java
©2004 Brooks/Cole Chapter 6 Methods. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Using Methods We've already seen examples of using methods.
Methods Liang, Chapter 4. What is a method? A method is a way of running an ‘encapsulated’ series of commands. System.out.println(“ Whazzup ”); JOptionPane.showMessageDialog(null,
Access to Names Namespaces, Scopes, Access privileges.
Java: How to Program Methods Summary Yingcai Xiao.
Writing methods and Java Statements. Java program import package; // comments and /* … */ and /** javadoc here */ public class Name { // instance variables.
Classes, methods, and conditional statements We’re past the basics. These are the roots.
A simple program: Area of a circle Problem statement: Given the radius of a circle, display its area Algorithm: 4. Display area To store values, we need.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
Block Scope By Greg Butler Purpose The purpose of this presentation is to familiarize the student with the concept of variable scope, as it relates to.
Introduction to Methods
Hello AP Computer Science!. What are some of the things that you have used computers for?
Multiple Choice Solutions True/False a c b e d   T F.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
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.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
The Java Programming Language
JAVA JAVA is an object-oriented programming (OOP) language introduced by Sun Microsystems in In the Java programming language: A program is made.
CSE 131 Computer Science 1 Module 1: (basics of Java)
Classes CS 21a: Introduction to Computing I First Semester,
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
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.
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.
Anatomy of a Java Program. AnotherQuote.java 1 /** A basic java program 2 * 3 Nancy Harris, James Madison University 4 V1 6/2010.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
Documentation and Style. Documentation and Comments  Programs should be self-documenting.  Use meaningful variable names.  Use indentation and white.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Classes and Methods. Classes Class Definition Data Fields –Variables to store data items –Differentiate multiple objects of a class –They are called.
CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
The Math class Java provides certain math functions for us. The Math class contains methods and constants that can be very useful. The Math class is like.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Building java programs, chapter 3 Parameters, Methods and Objects.
Side effects A side effect is anything that happens in a method other than computing and/or returning a value. Example: public class hello { public int.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Methods.
OOP Basics Classes & Methods (c) IDMS/SQL News
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Function PrototypetMyn1 Function Prototype We can declare a function before we use or define it by means of a function prototype. A function prototype.
JAVA METHODS (FUNCTIONS). Why are they called methods? Java is a strictly object-oriented programming language Methods are functions inside of objects.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
INTRODUCTION Java is a true OO language the underlying structure of all Java programs is classes. Everything must be encapsulated in a class that defines.
Chapter 2 Clarifications
Writing Methods.
Computing Adjusted Quiz Total Score
Documentation and Style
Chapter 2: Java Fundamentals cont’d
A Methodical Approach to Methods
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Methods/Functions.
Visibilities and Static-ness
Presentation transcript:

Writing methods

Calling Methods nameOfMethod(parameters) if method returns something, use that value Math.pow(2, 3) returns 8 System.out.println(Math.pow(2, 3)); // prints 8 2 and 3 are called parameters Can use variables also x = 2; y = 3; System.out.println(Math.pow(x, y)); // prints 8 System.out.println( ) is a method call

Program Design Putting all of our code in main will make main too large Want to be able to split code up functionally Use methods

Writing Methods public void myMethod( ) { } Can return values also (int, double, etc) Can give the method information also (in parameters) Make all methods public for now

Writing our own method: calcVolume public double calcVolume(double radius) { double volume; // or george. call it anything volume = 4.0 / 3.0 * Math.PI * Math.pow(radius, 3); // pow(x, y) is method call to calculate x y (radius 3 here) return volume; }

Class Details: Methods public double calcVolume(double radius ) { double volume; volume = 4.0 / 3.0 * Math.PI * Math.pow(radius, 3); return volume; } Java statements inside body, e.g., single = assignment return type double first line signature or header return statement Local variable and constant stuff inside curly braces is method body

return statement public double calcVolume( ) { // NO private in local variables; final for constants final double PI = ; double volume; volume = 4 / 3 * PI * radius * radius * radius; return volume; } type of method is return type to return a value, use ‘return value’; can be calculation

Calling methods that you wrote someMethod( ) { double r; // can call this anything r = 2.0; double v; // can call this anything v = calcVolume( r ); System.out.println(“Volume is “ + v ); } called radius in calcVolume. Doesn’t matter Called Volume in calcVolume. Doesn’t matter

Notes on methods main is a method Java statements must be within a method Methods can not be embedded: public void somemethod( ) { public void someothermethod( ) { } } To call a method, use ( )s, NO TYPES: answer = calcVolume(r) NOT answer = double calcVolume(double r); calling methods we write from main does not work as you might expect.

more on main public static void main(String[ ] args) { } main must always be public main must also be static. we’ll discuss this later in the semester. Our methods will not be static. static is why method calls in main do not work as we expect main always returns a void name of method one array parameter. We’ll figure this out later

How to make method calls work Because main is static, you cannot call methods from it. Instead, create a new class that you can call methods from

class DriverClass // NO Public { // put your method definitions here public void start() { // put all method calls here } } // end of the DriverClass public class HelloWorld { public static void main(String args[]) { DriverClass driver = new DriverClass(); driver.start(); } // end main } // end HelloWorld Question 6 }

Practice Write a method from the practice methods on the 150 review page: Given a test score return a letter which corresponds to the grade. E.g. 88 returns a B. Ignore +s and -s.

public class TestScoreConversion { public static void main(String args[]) { DriverClass driver = new DriverClass(); driver.start(); } class DriverClass { public void start( ) { char letter = letter_grade(88); System.out.println("An 88 is a " + letter); }

public char letter_grade(int score) { if (score >= 90) return 'A'; if (score >= 80) return 'B'; if (score >= 70) return 'C'; if (score >= 60) return 'D'; return 'F'; } } // DriverClass