Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Intro to Java

No description
by

Francesca Narea

on 14 February 2015

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Intro to Java

Intro to Java
1. start lowercase (myName)

2. no spaces

3. cannot start with a number

4. no punctuation

5. "reserved words" -- cannot use class or public as a variable name
Variables
Data Types in Java
Integer
a whole number with no decimal, can be negative
0

1
2
3
-1
-2
-3

int a;
?
a = -1;
int b = 2;
Decimal
0
1.1
1.5
2.0
-1.1
-1.5
-2.0
- double (in java)
double x = -2.0;
what is the value of y?
What does the user see?
name of your program
a way of organizing code
(method)
Press "compile" - like saving a Word Document
Then "run" - program knows to execute the code
Variable Comparisons
Boolean
another data type like int and double
*is either
true
or
false
boolean isTrue = true;

boolean heads = false;
Comparison in Math
*greater than (>)

*less than (<)

*greater than or equal to (>=)

*less than or equal to (<=)
equals (==)

does not equal (!=)

or (||)

and (&&)
5>3 = ?
5<3 = ?
5>=5 = ?
5<=2 = ?
Additional Java Comparisons
5==2
5!=5
If/Else Statements
If Statement w/ Numbers
Number Operations
Addition/Subtraction
*works the same way with doubles
Multiplication
*works the same way
with
doubles
Division
Ints and Doubles
double y = 1.0;
int x = y; //CAN NEVER STORE
DOUBLE IN AN INT

int a = 1;
double b = a; //allowed to store ints into doubles
Why!??!?
*
For integers, Java will
cut off the decimal portion
(NOT rounding)

*For doubles, the actual value is calculated
fun fact: holds up to 754 floating pt (1*10^754) places
Modulus (Remainder) %

1/10 ?
2/10 ?
20/10 ?
105/10 ?
0/10 ?
What is the remainder when you divide:
Java will calculate the remainder when you are dividing ints

(number you divide) % (divisor)
Remainder of 5/2 = ? = 5%2
if you are hungry, then you will eat 5 slices of pizza

if you are less than 13 years old, then you are not a teenager
If/Else w/ Numbers
Using Boolean
(true or false)
Boolean Syntax
Loops
how do you make 1-10 print out to the user?
what if you wanted to count to 100? 1,000? 10,000?
While Loops
While a number (x) is less than 10, add one to x
what is printed out to the user?
what is wrong?
Order Matters
For Loops
While vs. For Loops
for (variable declaration; interval; x change){
}

modify the for loop to count 10's up to 100
Challenge Questions
Arrays
Instagram
*organized containers to hold
information
Excel
Twitter
Declaring Arrays
int [ ] num = new int[5];
data type

name of array
(variable name)
number of spaces
Array Visual
int [ ] num = new int[5];
0
1
4
2
3
length=5
Filling Arrays
int [ ] num = new int[5];
num[0] = 1;
0
1
4
2
3
length=5
1
Fill Arrays During Declaration
int [ ] num = {4, 10, 3, 5, 9};
0
1
4
2
3
length=5
4
10
3
5
9
num[5]=2;
Referencing Arrays
4.2
3.99
3.14
5.0
9.0
double x = myArr[0];
double y = myArr[4];
int z= myArr.getLength;
myArr[1] = 4.0;
myArr
Fill an array with #'s 1- 10:
1. declare array
2. for loop (or while loop)
3. set values in array

*careful, array reference starts at 0 and you cannot reference an index greater than one less its length
what are the values of the array indices?
2-D Arrays
Array Visual
int [ ][ ] num = new int[5][3];
0
1
4
2
3
length=5
0
1
2
length=3
Strings
Declaring Strings
String str = "hello";
h e l l o
0 1 2 3 4
length = 5
Comparing Strings
string.equals(string)
* will return a

either
true
or
false
Length

indexOf(String str)

*Returns index of first time there is a certain letter or word present


int x = str.length();
System.out.println(x);

What is printed out?

h e l l o
0 1 2 3 4
String str = "hello";
Substring
h e l l o
0 1 2 3 4
String str = "hello";

substring(int beginIndex)

substring(int beginIndex, int endIndex)

str.substring(1);
str.substring(5);
str.substring(0,4);
str.substring(2,3);
CompareTo
compareTo(String anotherString)
*negative if first string comes before (lexicographically) the other string

*positive if first string comes after the other string

*zero if the strings are equal
compareTo returns 0
HelloWorld
Nested For Loops
Other Data Types
*less important
byte
short
long
float
char a ='a';
hungry

true
false
5 slices of pizza
dont eat
isTeenager
true
false
Yes, is a teenager.
Not a teenager.
Infinite Loops
*in infinite loops, there is not end

*the value will not reach any of the restrictions you have given
what shape does this print out?
Skeleton for If/Else
if(condition){}

else if(condition){}
else if(condition) {}
...
else{}
Examples
*x will always be greater than 0
*i will never equal 0
Break
*use break; to get out of a loop
how could you use break to fix this infinite loop?
when x equals 10, the loop will break
Steps of For Loops
1.Initialization

2. Test condition

3.mathmatical operation (increment/decrement, ect.)
a=b;
double myVar;
myVar = 10.0;
Classes/Methods
name of your program
a way of organizing code
(method)
HelloWorld
Method Headers
public static void main (String[] args)
*public vs. private
*http://www.cs.cornell.edu/courses/cs1130/2011sp/module2/module2part1/methodHeaders.html

http://www.javaworld.com/article/2076204/core-java/understanding-constructors.html
public - other classes can interact/talk with it
static - non-moving
void - does not return anything
main - name of method
String[] args - array named args of Strings is the parameter
HelloWorld Explained
method headers are an outline for what a method is going to do and exactly how it is going to be used
method header
Method Header How-To
setter (aka procedure)
- does not give anything back other than displaying text to the user, for example could be used to order an array from smallest to largest
Method Types
getter
- WILL return a value back, could for example go through an array and find the largest value
constructor
- creates something that can be used in the classes (could for example create a square, but other SETTER methods would be used to change the color and location of the square)
Changing Variable Values
changing values:
int x = 1;
int y = 1;
x = y;
how to swap values:
Swapping
Variable Declaration
*you can reassign the values for variables
Weird Java Syntax
*curly braces { } help enclose parts of your code
*semicolon ; is used at the end of almost every line of code
*quotes " " used to signify a word
*double slash // to signify beginning of comment in code
think of an integer as a small glass and a double as a big glass

you can't fit a big glass into a small one but you can fit a small glass into a bigger one
5==2 || 5==5
5==2 && 5==5
true/false?
While Loop Skeleton
int x;
while (condition){
increment/decrement x
do other stuff
}
Filling 2D Arrays
int[][] array = { {4,5,6},{2,1,3},{7,8,9} };
Easy Way
int[ ][ ] array = { { } ... { } }
4 5 6
2 1 3
7 8 9
User Inputs
creating an "object"*
*using the scanner class, something that is built in for the language java, you can take inputs from the user
asking the user for a number
and storing it into a variable
What does this code do?
ascii-code.com
IndexOf
Full transcript