CS 106 Introduction to Computer Science I 02 / 24 / 2010 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

Methods Java 5.1 A quick overview of methods
CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann.
Building Java Programs
1 MATH METHODS THAT RETURN VALUES. 2 JAVA'S MATH CLASS.
Return values.
CS 206 Introduction to Computer Science II 01 / 20 / 2009 Instructor: Michael Eckmann.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
CS 106 Introduction to Computer Science I 02 / 29 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 14 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 11 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 09 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 14 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 01 / 29 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 25 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 26 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 27 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 22 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 28 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 16 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 08 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 09 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 330 Programming Languages 09 / 30 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 09 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 19 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 08 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 18 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 01 / 23 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 23 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
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.
10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions.
CS 106 Introduction to Computer Science I 03 / 19 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
Math With Java The Math Class. First, A Quick Review of Math Operators in Java Primitive Data type in Java that represent numbers: Primitive Data type.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 11 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 29 / 2007 Instructor: Michael Eckmann.
The Math Class Methods Utilizing the Important Math Operations of Java!
CS 106 Introduction to Computer Science I 02 / 01 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 04 / 08 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 08 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 10 / 2007 Instructor: Michael Eckmann.
Presentation transcript:

CS 106 Introduction to Computer Science I 02 / 24 / 2010 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Spring 2010 Today’s Topics Comments and/or Questions? One last comment on multidimensional arrays A few comments about good programming style and programming conventions Math class methods Creating our own methods

Multi-dimensional arrays Not only can we have one dimensional, and two dimensional arrays but we can have n dimensional arrays, where n is any positive integer. Example when n=3: double temperatures[][][] = new double [12][31][24]; take a guess as to what might be stored in this array and what the indices mean

Multi-dimensional arrays Unfortunately the first index must go from 0 to 11, the second from 0 to 30 and the third from 0 to 23. What if we wanted the index to represent exactly the month (1 to 12), day (1 to 31) and hour (0 to 23)? Is there anything we could do to this line? double temperatures[][][] = new double [12][31][24];

Homework/Program Requirements 1) variables should start with a lowercase letter 2) class names should start with an uppercase letter 3) create meaningful variable names that relate to what the variable represents e.g. int height; instead of int a; 4) all programs should have proper indentation and whitespace for readability

Homework/Program Requirements 5) when I show you example output, try to make your program produce output as close to that format as possible 6) be careful of case in primitive types, even if you are just writing a sentence about them double is different than Double, also with String type - -- String is the type, not string

Math class Let's look at the Java API for the Math class. Specifically these methods: abs – guess what this does. cos, sin, tan ceil – returns smallest whole number >= parameter. pow – takes two parameters – raises first to second and returns the result. random – returns random # in the range: [0.0, 1.0)‏ sqrt Michael Eckmann - Skidmore College - CS Spring 2010

A few more methods in the Math class max ( x, y ) method that returns the larger of x and y min ( x, y )‏ method that returns the smaller of x and y There are versions of these methods that work for x and y being floats, doubles, ints and longs and return a result that is same type.

example calls to static methods in the Math class double w = 5.1, z = 10.56, a, b, c; a = Math.max ( w, z ); // what value would a have? a = Math. max ( z, w ); // what value would a have, now? b = Math. min ( z, w ); c = Math. sqrt ( z );

random( ) method in the Math class double rand_num; rand_num = Math.random ( ); // what value might rand_num have after this line of code? // is a possible value? // is 2 a possible value? // is a possible value?

random( ) method in the Math class random ( ) returns a double whose value is >= 0 and < 1, but sometimes we want a random integer How might we do that?

random( ) method in the Math class random ( ) returns a double whose value is >= 0 and < 1, but sometimes we want a random integer One way to do that is to first multiply the result by some integer to get a value that isn’t necessarily between 0 and 1. Then, cast this new value to an int by using the (int) cast operator.

random( ) method in the Math class // example: int some_random_int; double some_random_dbl; some_random_dbl = Math.random ( ) * 25; // this will result in a value >= 0 and < 25. some_random_int = (int) (Math.random ( ) * 25); // what is the range of values for some_random_int here?

random( ) method in the Math class int random_card_value; int random_card_suit; random_card_value = 1 + (int) (Math.random ( ) * 13); random_card_suit = (int) (Math.random ( ) * 4); Let’s put this code in a program and execute it.

random( ) method in the Math class What if I put the cast to int without using parentheses around the rest of the expression? e.g. random_card_suit = (int) Math.random ( ) * 4;

random( ) method in the Math class What if I put the cast to int without using parentheses around the rest of the expression? e.g. random_card_suit = (int) Math.random ( ) * 4; since the cast operator (int) has higher precedence than the multiplication operator *, it would be done first, which means what?

random( ) method in the Math class random_card_suit = (int) Math.random ( ) * 4; the Math.random() method call would return a double value and immediately this value would be cast to an int. Casting a double to an int causes the truncation of any decimal portion. Recall that the double that is returned by Math.random() is >= 0.0 and < 1.0 So, what's the possible values of (int) Math.random() ?

random( ) method in the Math class (int) Math.random( ) would always be zero.

what is a method A method is small piece of a program designed to achieve some specific task and usually returns some piece of information. A method is invoked by a method call. To call a method, you provide its name and the correct arguments that are necessary for the method to execute. Michael Eckmann - Skidmore College - CS Spring 2010

methods Here’s a good analogy of a worker and boss to describe methods and their callers. A boss (the caller) asks a worker (the method that is called) to perform a task (the code in the method) and report (return results) back when the task is done. Michael Eckmann - Skidmore College - CS Spring 2010

methods All methods are defined within some class. we have seen the main method defined in the class of every application so far. Michael Eckmann - Skidmore College - CS Spring 2010

other methods we’ve used We have used other methods available in the Java API, methods like parseInt and parseFloat and println are available to us to use in their respective classes Integer, Float and System.out The methods in the Math class are other examples. These methods were defined for us to do a specific task. We call them when we need them. e.g. in the case of Integer.parseInt --- it’s task is to convert a String into an int. It takes a String as an argument (parameter) and returns a value of type int. Michael Eckmann - Skidmore College - CS Spring 2010

other methods we’ve used e.g. in the case of Integer.parseInt --- it’s task is to convert a String into an int. It takes a String as an argument (parameter) and returns a value of type int. example of a call to this method: some_int = Integer.parseInt( some_str ); In this example, some_str is the argument that is being passed in to the parseInt method and some_int is the variable that will get set to the value returned by the method. Michael Eckmann - Skidmore College - CS Spring 2010

methods We can create our own methods that we can call to perform specific tasks. Let’s say we’re writing a program to handle employee’s salaries. We might need to compute a salary after a raise. This example method would need to have access to the current salary and the raise percentage. It would then calculate the salary after the raise and return this value. Michael Eckmann - Skidmore College - CS Spring 2010

example of a programmer-defined method we might name this method salary_after_raise we need to take in two values, one for the current salary and one for the raise percentage. What primitive type might these be? we also need to return the salary that is computed. What primitive type might this returned value be? Michael Eckmann - Skidmore College - CS Spring 2010

example of a programmer-defined method so, this method could look like: public static double salary_after_raise(double curr_sal, double raise_pct )‏ { double new_sal; new_sal = curr_sal * ( 1 + raise_pct / 100 ); return new_sal; } Michael Eckmann - Skidmore College - CS Spring 2010