Download Introduction to Programming

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Introduction to
Programming
Java Fundamentals
(Variables, Arithmetic, etc.)
David Goldschmidt, Ph.D.
Computer Science
The College of Saint Rose
Java Data Types
byte
1 byte
Integers in the range
-128 to +127
short
2 bytes
Integers in the range of
-32,768 to +32,767
int
4 bytes
Integers in the range of
-2,147,483,648 to +2,147,483,647
use double for your
floating-point numbers
long
8 bytes
Integers in the range of
-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
float
4 bytes
Floating-point numbers in the range of
±3.410E-38 to ±3.410E38, with 7 digits of accuracy
double 8 bytes
Floating-point numbers in the range of
±1.710E-308 to ±1.710E308, with 15 digits of accuracy
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;
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;
Arithmetic Expressions

Rules to writing arithmetic expressions:

Two binary operators may not be next to one another
int result = 14 + * 87;

Parentheses form groupings and all expressions within
parentheses are evaluated first
int result = (7 + 10) * (11 – 1);
Arithmetic Expressions

More rules to writing arithmetic expressions:

Parentheses may be nested, meaning sets of parentheses
may be enclosed by other parentheses
int result = (2 * (4 – 8)) / 5;
evaluate inner parentheses first!

Parentheses cannot be used to indicate multiplication
int result = (7 + 10)(11 – 1);
Calculating Volume

How do we calculate the volume of a cylinder?

Volume V = r 2h

r
Given radius r and height h
h
How do we translate this equation to Java?
Calculating Volume

How do we calculate the volume of a cone?

Volume V = 13 r 2h

r
Given radius r and height h
h
How do we translate this equation to Java?
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
Calculating Square Roots

How do you calculate a square-root?

I don’t know! Let’s use Java’s Math class to do it:
sqrt() method expects
a double as input
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
output
Square root of 90.27: 9.501052
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?
inputs
double x
output
Math.sqrt()
double Math.sqrt(x)
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
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
Accumulating a Sum

Accumulating a sum has the following form:
int sum = 0;
int value = 12;
sum = sum + value;
evaluate the right-hand side first
then assign result to left-hand side variable
value = 18;
sum = sum + value;

Also note a shortcut form:
sum += value;
Counting by One

Counting by one has the following form:
sum = sum + 1;

And this shortcut form:
sum += 1;

And these shortcut forms:
sum++;
++sum;
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));
}
quotes
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;
Common Programming Errors

Even more examples of programming pitfalls:

Watch out for integer division:
int result;
result = 1 / 2;

Don’t use a variable unless it has been declared and
assigned a value:
q = 928.502;
int x;
System.out.println(x);