Methods: a first introduction Two ways to make tea: 1: boil water; put teabag into cup;... etc...... 2: tell your younger brother: makeTea(1 milk, 0 sugar);

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
How do we make our Welcome.java program do something? The java in our Welcome.java file won’t do anything by itself. We need to tell the computer to execute.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Using JOptionPanes for graphical communication with our programs. Pages Horstmann 139.
Lecturer: Fintan Costello Welcome to Hdip 001 Introduction to Programming.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
CMT Programming Software Applications
Constants Variables change, constants don't final = ; final double PI = ; … area = radius * radius * PI; see Liang, p. 32 for full code.
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 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
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.
Chapter 2 types, variables, methods Pages Horstmann.
Fundamental data types Horstmann Chapter 4. Constants Variables change, constants don't final = ; final double PI = ; … areaOfCircle = radius *
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Laboratory Study October, The very first example, traditional "Hello World!" program: public class first { public static void main (String[ ]
***** SWTJC STEM ***** Chapter 2-2 cg Identifiers - Naming Identifier, the name of a Java programming component. Identifier naming rules... Must.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
Chapter 2 Elementary Programming
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Chapter 2: Java Fundamentals
Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
9/29/99B-1 CSE / ENGR 142 Programming I Variables, Values, and Types © 1998 UW CSE.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
A Sample Program #include using namespace std; int main(void) { cout
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Copyright 2010 by Pearson Education APCS Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Elementary Programming
Building Java Programs
Console Output, Variables, Literals, and Introduction to Type
Introduction to Computer Science / Procedural – 67130
Variables and Primative Types
Primitive Data, Variables, Loops (Maybe)
Computer Programming Methodology Introduction to Java
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Variables ICS2O.
Building Java Programs
Java Tutotrial for [NLP-AI] 2
Building Java Programs Chapter 2
CS 200 Primitives and Expressions
elementary programming
CS2011 Introduction to Programming I Elementary Programming
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Building Java Programs
Building Java Programs
Primitive Types and Expressions
Building Java Programs Chapter 2
Building Java Programs
Presentation transcript:

Methods: a first introduction Two ways to make tea: 1: boil water; put teabag into cup;... etc : tell your younger brother: makeTea(1 milk, 0 sugar); A method is a short way of calling a bunch of other statements

Built-in classes and methods Printing things on the screen is actually quite complicated (we’d need to know how to draw the characters, where to put them, how to move to a new line, and so on.) Java has built-in System.out classes that other programmers have written to handle all these complications. These classes contain methods like println or print that we can call to execute the series of statements needed to print something on the screen. There are a lot of such built-in classes and methods in Java that do all sorts of useful things. We will learn many of them

More on methods System.out.println("Hi there"); Method: makeTea Carried out by: brother Arguments: 1 milk,0 sugar We need to know 1) the method we want to execute; 2) the class that can carry out that method; 3) the arguments the method takes. Method: println Carried out by: System.out Arguments: "Hi there"

Programs and classes Each class describes a thing that does certain actions. Large programs can contain multiple classes describing different things. public class Foo { public static void main(String[] args) { // your code in here } This would be placed in a file called Foo.java. A java file always has the exact same name as the class containing main. Smallest possible Java program: 1 class containing 1 (main) method & 0 statements

How do we develop programs? understand the problem design an algorithm (semi-formal) implement the algorithm (write Java) compile and test Debug (fix mistakes)

Edit... Compile... Debug cycle Write/edit Java code Compile and run Debug (find the mistakes)

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 learn about variables 1. Store the radius value 2. Compute area using formula: Area = radius x radius x Pi 3. Store the Area value

Variables Variables store data…input, output or inBetweeny stuff Variables are like shaped boxes…. Variables must be set up ("declared") before use Each variable is of a certain datatype ( int, float, double, String ) and each variable has a name (an identifier). Integers go in integer-shaped variables (labelled int ) Real numbers go in real-number shaped ones ( float, double) Strings (“hello”) can go in string-shaped variables (String)

Declaring variables……. ; int x; // a box called x that will hold an integer double radius; /* a box called radius that will hold a floating point no. */ char ch; // ch can hold a single character Int, double, float, char all begin with a lower case letter. String begins with a capital letter. String myName; // myName holds a string of characters

Identifiers Identifiers name things: variables, methods, classes….. Must start with a letter (or _ or $) Cannot contain an operator, e.g. +, - Cannot be a reserved word, e.g. public, static, double, int Can be as long as you like……… Should be descriptive Rules for identifiers (variable names, and names for methods, classes)

Identifiers…… Good or bad? area radius403 3rdRadius d+4 thisIsAVeryLongIdentifierIndeed i double ISTHISOK

Assigning to variables = ; x = 1; radius = 1.0; ch = 'a'; x = 1 + (2 * 3) + (3 * 4); = means put something into a variable.

Program: ComputeArea Public class ComputeArea { public static void main(String[] args) { double radius; double area; // step 1: store radius // step 2: compute and store area // step 3: display the area } (same as the Algorithm we saw earlier)

Program: ComputeArea Public class ComputeArea { public static void main(String[] args) { double radius; double area; // step 1: store radius radius = 1.23; // step 2: compute, store area area = radius * radius * ; // step 3: display the area System.out.print(“A circle of radius ”); System.out.print(radius); System.out.print(“ has an area of ”); System.out.println(area); } ‘Declare’ variables radius and area (boxes to hold radius and area values) Store radius value in the variable Compute area and store in area variable

Things to note about ComputeArea is the value of pi. We could have a variable called pi if we wanted, and put that value in it. What type? There is a sequence of print statements, ending with a println. This means everything will be printed on a single line. We can print strings (in quotes) and variables ( radius and area ). When we print a variable, its value comes out. We have to type the value of radius into the program. It’d be better to read it in: we’ll see how to do that later.

More assignment Datatype on LHS (Left Hand Side) must be compatible with datatype on RHS (Right Hand Side): int x; x = 3; // ok x = 3.1; // not ok, as we said x was of type int LHS: must be a variable 1 = x; // this is meaningless RHS: expression can include variables… x = x + 1; // ok if x already has some legal value

Shortcut: declare and initialize Initialization: giving a variable a value for the first time We can declare and initialize in the same line (good practice!) int x = 20; double d = 3.14; Variables must be declared before usage! myName = “Fintan”; String myName; // NOPE! String myName = “Fintan”;

Homework Read Liang Ch.2 pp. 25 – 32. In your labs this week you will be doing printing and some variable assignment.