Method exercises Without IF

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
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.
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
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.
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.
Writing Methods. Create the method Methods, like functions, do something They contain the code that performs the job Methods have two parts.
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.
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: 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.
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.
Java Syntax and Style JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin,
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.
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.
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.
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/16: Intro to Java, Languages, and Environments Types of programming languages –machine languages –assembly languages –high-level languages Java environment.
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.
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
Information and Computer Sciences University of Hawaii, Manoa
4. Java language basics: Function
Classes and Objects.
Lesson #6 Modular Programming and Functions.
COMP 170 – Introduction to Object Oriented Programming
Lesson #6 Modular Programming and Functions.
2.5 Another Java Application: Adding Integers
Introduction to C Language
Chapter 3: Using Methods, Classes, and Objects
by Tony Gaddis and Godfrey Muganda
Object Oriented Systems Lecture 03 Method
Method Mark and Lyubo.
Lesson #6 Modular Programming and Functions.
Chapter Topics Chapter 5 discusses the following main topics:
Static Variables ICS 111: Introduction to Computer Science I
Writing Methods.
Programming Vocabulary.
פרטים נוספים בסילבוס של הקורס
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.
Workshop for Programming And Systems Management Teachers
Documentation and Style
Anatomy of a Java Program
Starting Out with Java: From Control Structures through Objects
Lesson #6 Modular Programming and Functions.
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.
Unit 3: Variables in Java
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.
UNIT 1 First programs.
Corresponds with Chapter 5
How to Run a Java Program
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 * @param double price - the price that comes in; * double cost – the cost that comes in * @return 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 * @param double price - the price that comes in; * double cost – the cost that comes in * @return 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 Step 4 – write the test case in the comments Right below the comments about the method, add comments to list the test cases you will try and what the result should be.

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 * @param double price - the price that comes in; * double cost – the cost that comes in * @return 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 listed in the comments (Step 4) and compare to your expected results.