Presentation is loading. Please wait.

Presentation is loading. Please wait.

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);

Similar presentations


Presentation on theme: "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);"— Presentation transcript:

1 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); A method is a short way of calling a bunch of other statements

2 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

3 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"

4 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

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

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

7 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

8 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)

9 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

10 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)

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

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

13 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)

14 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 * 3.1416; // 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

15 Things to note about ComputeArea 3.1416 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.

16 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

17 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”;

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


Download ppt "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);"

Similar presentations


Ads by Google