Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)

Similar presentations


Presentation on theme: "Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)"— Presentation transcript:

1 Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)

2 Java Data Types byte 1 byteIntegers in the range -128 to +127 short 2 bytesIntegers in the range of -32,768 to +32,767 int 4 bytesIntegers in the range of -2,147,483,648 to +2,147,483,647 long 8 bytesIntegers in the range of -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 float 4 bytesFloating-point numbers in the range of ±3.410E-38 to ±3.410E38, with 7 digits of accuracy double 8 bytesFloating-point numbers in the range of ±1.710E-308 to ±1.710E308, with 15 digits of accuracy use int for your integers use double for your floating-point numbers

3 Variables As in mathematics, a variable is a symbolic name used to represent numeric (or other) values In Java, variables must be declared and must have a specific data type: int x; float y; double average; long q; specify the data type specify the variable name

4 Assigning Values to Variables Assign values to variables using the = operator: int x; float y; double average; long q; x = 47; y = -12.375F; average = 42.7; q = 875L; specify the variable name specify the value

5 Arithmetic Expressions Rules to writing arithmetic expressions : Two binary operators may not be next to one another Parentheses form groupings and all expressions within parentheses are evaluated first int result = 14 + * 87; will not compile! int result = (7 + 10) * (11 – 1); evaluate parentheses first!

6 Arithmetic Expressions More rules to writing arithmetic expressions : Parentheses may be nested, meaning sets of parentheses may be enclosed by other parentheses Parentheses cannot be used to indicate multiplication int result = (2 * (4 – 8)) / 5; int result = (7 + 10)(11 – 1); evaluate inner parentheses first! will not compile!

7 Calculating Volume How do we calculate the volume of a cylinder? Given radius r and height h How do we translate this equation to Java? r h Volume V =  r 2 h

8 Calculating Volume r h How do we calculate the volume of a cone? Given radius r and height h How do we translate this equation to Java? Volume V =  r 2 h 1 3

9 Using Mathematical Methods What is a Java method ? A Java method is precompiled code that solves a problem (e.g. calculates the square root) using an algorithm A Java method accepts inputs A Java method produces one output Think of a Java method as a mathematical function

10 Calculating Square Roots How do you calculate a square-root? I don’t know! Let’s use Java’s Math class to do it: public class SquareRoots { public static void main( String[] args ) { System.out.print( "Square root of 81.0: " ); System.out.println( Math.sqrt( 81.0 ) ); System.out.print( "Square root of 90.27: " ); System.out.println( Math.sqrt( 90.27 ) ); } Square root of 81.0: 9.0 Square root of 90.27: 9.501052 output sqrt() method expects a double as input

11 Calculating Square Roots When using a method, answer these questions: What are the expected inputs? What are the data types of each input? What is the expected output? What is the data type of the output? Math.sqrt() inputs double x double Math.sqrt(x) output

12 Operator Precedence Operator Precedence specifies the order of evaluation: Evaluate expressions within parentheses first (starting with innermost set of parentheses) Evaluate methods (e.g. Math.sqrt(), Math.pow() ) Perform all negations (e.g. result = x * -y ) Perform multiplication, division, and modulus operations From left to right Perform addition and subtraction operations From left to right

13 Operator Precedence double r = 1; r = (19 – (-sqrt(4.0 + 5.0) / -r)) * (18 + 7 % 4 * 3 - 1); r = (19 – (-sqrt(9.0) / -r)) * (18 + 7 % 4 * 3 – 1); r = (19 – (-3.0 / -r)) * (18 + 7 % 4 * 3 – 1); r = (19 – (-3.0 / -1)) * (18 + 7 % 4 * 3 – 1); r = (19 – 3.0) * (18 + 7 % 4 * 3 – 1); r = (16.0) * (18 + 3 * 3 – 1); r = (16.0) * (18 + 9 – 1); r = (16.0) * (26) = 416.0

14 Accumulating a Sum Accumulating a sum has the following form: Also note a shortcut form: int sum = 0; int value = 12; sum = sum + value; value = 18; sum = sum + value; sum += value; then assign result to left-hand side variable evaluate the right-hand side first

15 Counting by One Counting by one has the following form: And this shortcut form: And these shortcut forms: sum = sum + 1; sum += 1; sum++; ++sum;

16 Common Programming Errors What are examples of programming pitfalls? Don’t rush to writing code until you have sketched out an algorithm or solution Create backups of your code using flash drives Make sure every beginning has an end: public static int void main(String[] args) { System.out.println("Quotes come in twos"); System.out.println("Answer: " + (9 + 5)); } parentheses braces quotes

17 Common Programming Errors More examples of programming pitfalls: Watch for misspelled Java keywords Java is a case-sensitive language, so be careful of uppercase and lowercase letters Don’t forget to end statements with a semicolon ( ; ) Watch for incompatible data types when you assign values to variables: int x; x = 928.502;

18 Common Programming Errors Even more examples of programming pitfalls: Watch out for integer division: Don’t use a variable unless it has been declared and assigned a value: q = 928.502; int x; System.out.println(x); int result; result = 1 / 2;


Download ppt "Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)"

Similar presentations


Ads by Google