Method exercises Without IF. Setup Create one new project to hold all of these. In that project, create an empty class called WorkIt to hold all of your.

Slides:



Advertisements
Similar presentations
Overloading Having more than one method with the same name is known as overloading. Overloading is legal in Java as long as each version takes different.
Advertisements

Introduction to C Programming
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.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
Introduction to C++ September 12, Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules.
Structure of a C program
Method exercises. Setup Create one new project to hold all of these. In that project, create an empty class called WorkIt to hold all of your methods.
Primitive Data Types byte, short, int, long float, double char boolean Are all primitive data types. Primitive data types always start with a small letter.
1 Introduction to Coding. 2 Example Codes A lot of example codes are given with Arduino IDE A code can often be based on a previous example rather than.
11 Chapter 5 METHODS. 22 INTRODUCTION TO METHODS A method is a named block of statements that performs a specific task. Other languages use the terms.
Introduction to Methods
Functions A function is a snippet of code that performs a specific task or tasks. You use a multitude of functions daily when you do such things as store.
Chapter 6: Functions.
Chapter 4 Procedural Abstraction and Functions That Return a Value.
Chapter 3 Getting Started with C++
C++ for Engineers and Scientists Third Edition
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
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.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
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.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.
Catie Welsh February 23,  Lab 4 due on Friday  Lab 5 will be assigned on Friday 2.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-1 Why Write Methods? Methods are commonly used to break a problem down.
Methods CSCI 1301 What is a method? A method is a collection of statements that performs a specific task. Pre-Defined methods: available in Java library.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Programming Fundamentals Enumerations and Functions.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
2.1 First C Program. First Program Open visual studio, click new file Save as “programName.c” – Program must start with letter and have no spaces – Must.
2.5 Another Java Application: Adding Integers
Chapter 7 Top-Down Development
Chapter 3: Using Methods, Classes, and Objects
Object Oriented Systems Lecture 03 Method
Chapter Topics Chapter 5 discusses the following main topics:
Writing Methods.
Programming Vocabulary.
Starting Out with Java: From Control Structures through Objects
Stack Memory 2 (also called Call Stack)
Introduction to Java part 2
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Anatomy of a Java Program
Starting Out with Java: From Control Structures through Objects
Method exercises Without IF
Functions Imran Rashid CTO at ManiWeber Technologies.
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Methods/Functions.
Corresponds with Chapter 5
Presentation transcript:

Method exercises Without IF

Setup Create one new project to hold all of these. In that project, create an empty class called WorkIt to hold all of your methods.

First problem 1. ProfitOnOneSale

ProfitOnOneSale Step 1: Write the comment for your method Open the class Locate the curly brace that closed the last method. If it is the first, it should go right after the open curly brace for the class. Write the comments: public class WorkIt { /** * profitOnOneSale : Calculate profit on one sale * given the cost and price double price - the price that comes in; * double cost – the cost that comes in double profit – price minus cost */ }

ProfitOnOneSale – Step 2 Step 2 – write the method header Under the bracket, or after the end of another method, write “public static” Write the type of your result (ex: int). If it returns nothing, write “void”. Write the name of your method Between parentheses, write the type of your input followed by the name of the variable your method will use. If you have more than one, separate by commas. Every variable needs a type before it. Type an open and close curly brace.

ProfitOnOneSale – Step 2 Result public class WorkIt { /** * profitOnOneSale : Calculate profit on one sale * given the cost and price double price - the price that comes in; * double cost – the cost that comes in double profit – price minus cost */ public static double profitOnOneSale (double price, double cost) { }

ProfitOnOneSale – Step 3 Step 3 – Send back a dummy result so it will compile Type return Type a value that would be valid for the return type. Type a semicolon Compile to see no errors. Ex: return 3;

Step 4 – write the test case Right click on the workit class and select “create test case” Right click on the test case and select “create test method” Right click on the workit class and select “profitOnOneSale” method. Name the test profitOnOneSaleTest1 and Enter some inputs and then enter the expected result. Repeat for about 3 test cases.

Step 4 result public class WorkIt { /** * profitOnOneSale : Calculate profit on one sale * given the cost and price * profitOnOneSale(6,2.5)== 3.5 * profitOnOneSale(2,3) == -1.0 * profitOnOneSale(6.5,0) == 6.5

Step 5 – code inside the method Knowing you have a box in memory for every input variable already, code inside the method’s curly braces so that the proper return value will be sent back. This is very specific to every type of problem. Compile

Step 5 result public class WorkIt { /** * ProfitOnOneSale : Calculate profit on one sale * given the cost and price double price - the price that comes in; * double cost – the cost that comes in double profit – price minus cost */ public static double profitOnOneSale (double price, double cost) { return price - cost; }

Step 6 – test it Compile and run your method. Run it for each of your test cases (right click on the test case and choose Test All Click on view / test results to see which tests passed