Writing Methods.

Slides:



Advertisements
Similar presentations
Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
Advertisements

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.
Begin Java Pepper. Objectives What is a program? Learn the basics of your programming tool: BlueJ Write a first Java program and see it run Make some.
IT151: Introduction to Programming
Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
© A+ Computer Science - public class CompSci { } All Java programs start with a class.
JAVA BASICS SYNTAX, ERRORS, AND DEBUGGING. OBJECTIVES FOR THIS UNIT Upon completion of this unit, you should be able to: Explain the Java virtual machine.
 2005 Pearson Education, Inc. All rights reserved Introduction.
Your First Java Program: HelloWorld.java
© A+ Computer Science - public class CompSci { } All Java programs start with a class.
Chapter 2: Your First Program! Hello World: Let’s Program  All programs must have the extension.java  Our first program will be named: HelloWorld.java.
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Java Intro. A First Java Program //The Hello, World! program in Java public class Hello { public static void main(String[] args) { System.out.println("Hello,
01 Introduction1June Introduction CE : Fundamental Programming Techniques.
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.
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA.
Begin Java having used Alice Pepper - Some slides from Alice in Action with Java.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
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,
Writing Methods. Create the method Methods, like functions, do something They contain the code that performs the job Methods have two parts.
1. 2 Chapter 1 Introduction to Computers, Programs, and Java.
Unit 2: Java Introduction to Programming 2.1 Initial Example.
“Introduction to Programming With Java”
Hello AP Computer Science!. What are some of the things that you have used computers for?
Basics Programming Concepts. Basics A computer program is a set of instructions to tell a computer what to do Machine language = circuit level language.
CompSci 42.1Intro to Java Anatomy of a Class & Terminology Running and Modifying a Program.
Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
1 Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.1.
The Java Programming Language
Board Activity Find your seat on the seating chart Login – Remember your login is your first initial your last name and the last three numbers of your.
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!
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.
3 Major Steps for Creating/Running a Java Program You write the source code for the program and save it as a.java file. You compile the.java program using.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Comments in Java. When you create a New Project in NetBeans, you'll notice that some text is greyed out, with lots of slashes and asterisks:
Output in Java Hello World!. Structure of a Java Program  All Java files in ICS3U1 have the following structure: class HelloWorld { }  Notice the open.
Working with arrays (we will use an array of double as example)
JAVA Practical Creating our first program 2. Source code file 3. Class file 4. Understanding the different parts of our program 5. Escape characters.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
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.
Creating a Java Application and Applet
Java FilesOops - Mistake Java lingoSyntax
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 1 Introduction to Computers, Programs,
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
© A+ Computer Science - public class CompSci { } All Java programs start with a class.
Computer Programming A simple example /* HelloWorld: A simple C program */ #include int main (void) { printf (“Hello world!\n”); return.
1 CSE1340 Class 4. 2 Objectives Write a simple computer program in Java Use simple Output statements Understand the different types and uses of comments.
Methods.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
11 ITI 1120 Lab # 2 Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot.
CSC 110 – Intro to Computing - Programming
Objective Write simple computer program in C++ Use simple Output statements Become familiar with fundamental data types.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
Introduction to programming in java
Computer Programming Your First Java Program: HelloWorld.java.
John Woodward A Simple Program – Hello world
CSC201: Computer Programming
GC101 Introduction to computer and program
Chapter 3 GC 101 Java Fundamentals.
Chapter 2, Part I Introduction to C Programming
Intro to Java.
How to Run a Java Program
Java Intro.
Introduction to Java Brief history of Java Sample Java Program
Developing Java Applications with NetBeans
Developing Java Applications with NetBeans
Local variables and how to recognize them
Presentation transcript:

Writing Methods

Create the method A Method is a tool to do something It contains the code that performs the job Methods have two parts

Create the method The first part is called the Header modifier return name public void message( ) { open & closing braces }

Methods have two parts The second part is called the Body The body contains the lines of code that instruct the computer what to do You can have as many lines of code in the body as you want

Methods have two parts This Body has two lines of code It tells the computer to print to the screen the text that is inside the set of double quotes after skipping a blank line public void message( ) { System.out.println(); System.out.println(“java is not kawfy”); }

Create a Class To use the method we need to create a simple class The class is like a tool bag It can hold one or many tools (methods) The class also has a Header and a Body

Create a Class The first part is called the Header modifier structure name public class MyMethods { open & closing braces }

Create a Class The second part is called the Body The body contains one or more methods public class MyMethods { public void message() System.out.println(); System.out.println(“java is not kawfy”); }

Create an Application An Application is an executable program It uses the methods of different classes to execute the program Some methods are from the Java Class Library Other methods are from the Classes that We wrote

Create an Application We will create an object (instance) of the class We will call (use) a method of the class The Application class also has a Header and a Body The Application has one method named main

Create an Application Syntax: public class RunMyMethods { public static void main(String [] args) MyMethods mm = new MyMethods(); mm.message(); //class object calls method }

Compile and Run Compile … correct any syntax errors Run … check for correct output If necessary make any corrections Then recompile and rerun.