Week 2 - Monday What did we talk about last time? Software - - PowerPoint PPT Presentation

week 2 monday what did we talk about last time software
SMART_READER_LITE
LIVE PREVIEW

Week 2 - Monday What did we talk about last time? Software - - PowerPoint PPT Presentation

Week 2 - Monday What did we talk about last time? Software development Data representation Binary numbers What if you want to write a Java program that can Edit music files Stream video Organize your photo album


slide-1
SLIDE 1

Week 2 - Monday

slide-2
SLIDE 2

 What did we talk about last time?  Software development  Data representation

  • Binary numbers
slide-3
SLIDE 3
slide-4
SLIDE 4
slide-5
SLIDE 5
slide-6
SLIDE 6

 What if you want to write a Java program that can…

  • Edit music files
  • Stream video
  • Organize your photo album

 Each of these tasks manipulates a lot of data  Audio, video, and images are complicated kinds of data  Java must build these kinds of data out of much simpler kinds

  • f data
slide-7
SLIDE 7

 We are going to focus on five basic types of data in Java  These are:

  • int

For whole numbers

  • double

For rational numbers

  • boolean

For true or false values

  • char

For single characters

  • String

For words

 String is a little different from the rest, but we'll get into

that later

slide-8
SLIDE 8
slide-9
SLIDE 9

 As I just said, the int type is used to store integers (positive

and negative whole numbers and zero)

 Examples:

  • 54
  • -893992

 Inside the computer, an int takes up 4 bytes of space, which

is 32 bits (1s and 0s)

slide-10
SLIDE 10

 With 32 bits, an int can hold integers from about -2 billion up

to 2 billion

 Positive numbers are represented like we've seen in binary  Negative numbers need an extra trick that we aren't going to

go into

 The actual maximum value is: 2147483647

slide-11
SLIDE 11

 Let's say you add 100 to the maximum int value 2147483647  You do not get 2147483747  Instead, it becomes a very negative number: -2147483549  This phenomenon is called overflow  The opposite thing happens if you have a very negative

number and you subtract a number that makes it too negative

 This phenomenon is called underflow

slide-12
SLIDE 12

 Each type has a number of literals associated with it  A literal is a concrete value within a given type  Literals for the int type are numbers exactly like what you

would expect:

  • 115
  • -9837461
  • 2
slide-13
SLIDE 13

 It is also possible to create variables for each data type  Think of a variable as a "box" that you can put values into  The name of a variable is an identifier  We can declare a variable of type int with identifier i using

the following line of code: int i;

slide-14
SLIDE 14

 This line of code creates a box named i that is designed only

to hold ints

 For one thing, it's 32 bits in size

int i; i

slide-15
SLIDE 15

 Java variables are not like variables in math which have a fixed

(but unknown) value

 Instead, a Java variable can be changed by a line of code  We use the assignment operator (=) to set the value of a

variable as follows: i = 5;

slide-16
SLIDE 16

 This line of code stores 5 into i  Think of the = operator as an arrow pointing left  Let's see this in Eclipse

i i = 5;

5 5

slide-17
SLIDE 17
slide-18
SLIDE 18

 You will use the int type very often  Sometimes, however, you need to represent numbers with a

fractional part

 The double type is well suited to this purpose  Declaration of a double variable is just like an int variable:

double x;

slide-19
SLIDE 19

 This line of code creates a box named x that is designed only

to hold doubles

 It has a different size from an int

double x; x

slide-20
SLIDE 20

 This line of code stores 3.14159 into x  Remember that the = operator is like an arrow pointing left  Let's see this in Eclipse

x = 3.14159; x

3.14159 3.14159

slide-21
SLIDE 21
slide-22
SLIDE 22

 Numbers are great  But, sometimes you only need to keep track of whether or not

something is true or false

 This is what the boolean type is for  You will understand it better when we cover conditionals in a

couple of weeks

 Declaration of a boolean variable is like so:

boolean value;

slide-23
SLIDE 23

 This line of code creates a box named value that is designed

  • nly to hold booleans

 It cannot be used to store numbers

boolean value; value

slide-24
SLIDE 24

 This line of code stores false into value  Remember that the = operator is like an arrow pointing left

value = false; value

false

false

slide-25
SLIDE 25
slide-26
SLIDE 26

 Sometimes you need to deal with characters  This is what the char type is for  The char type only allows you to store a single character like

'$' or 'q'

 Declaration of a char variable is like so:

char c;

slide-27
SLIDE 27

 This line of code creates a box named c that is designed only

to hold chars

 It is used to store characters from most of the different scripts

in the world char c; c

slide-28
SLIDE 28

 This line of code stores the letter 'a' into into c  We must use the single quotes so that Java knows we are

talking about the character 'a' and not a variable named a c = 'a';

'a'

c

'a'

slide-29
SLIDE 29
slide-30
SLIDE 30

 The String type is different from the other types in several

ways

 The important thing for you to focus on now is that it can hold

a large number of chars, not just a single value

 A String literal is what we used in the Hello, World program

String word;

slide-31
SLIDE 31

 This line of code creates a box named word that is designed

  • nly to hold Strings

 It is used to store text of any length from most of the different

scripts in the world String word; word

slide-32
SLIDE 32

 This line of code stores the String "Mad flavor" into

word

 We must use the double quotes so that Java knows we are

talking about the text "Mad flavor" word = "Mad flavor"; word

"Mad flavor" "Mad flavor"

slide-33
SLIDE 33
slide-34
SLIDE 34

Type Kind of values Sample Literals int Integers

  • 5

900031

double Floating-point Numbers

3.14

  • 0.6

6.02e23

boolean Boolean values

true false

char Single characters

'A' 'Z' '&'

String Sequences of characters

"If you dis Dr. Dre" "10 Sesquipedalians"

slide-35
SLIDE 35
slide-36
SLIDE 36

 To output stuff, we just use System.out.println()  What about input?  Input is a little trickier  We need to create a new object of type Scanner

System.out.println("Flip mode is the squad!"); System.out.println(35);

slide-37
SLIDE 37

There are three parts to using Scanner for input

1.

Include the appropriate import statement so that your program knows what a Scanner object is

  • 2. Create a specific Scanner object with a name you choose

3.

Use the object you create to read in data

slide-38
SLIDE 38

 Lots of people have written all kinds of useful Java code  By importing that code, we can use it to help solve our

problems

 To import code, you type import and then the name of the

package or class

 To import Scanner, type the following at the top of your

program (before the class!)

import java.util.Scanner;

slide-39
SLIDE 39

 Once you have imported the Scanner class, you have to create a

Scanner object

 To do so, declare a reference of type Scanner, and use the new

keyword to create a new Scanner with System.in as a parameter like so:

 You can call it whatever you want, I chose to call it in  Doesn't make any sense? For now, that's okay.

Scanner in = new Scanner(System.in);

slide-40
SLIDE 40

 Now that you've got a Scanner object, you can use it to read

some data

 It has a method that will read in the next piece of data that user

types in, but you have to know if that data is going to be an int, a double, or a String

 Let's say the user is going to input her age (an int) and you want

to store it in an int variable called years

 We'll use the nextInt() method to do so:

int years; years = in.nextInt();

slide-41
SLIDE 41

 Scanner has a lot of methods (ways to accomplish some tasks)  For now, we're only interested in three  These allow us to read the next int, the next double, and

the next String, respectively:

Scanner in = new Scanner(System.in); int number = in.nextInt(); double radius = in.nextDouble(); String word = in.next();

slide-42
SLIDE 42

import java.util.Scanner; public class Age { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("What is your age?"); int years; years = in.nextInt(); years = years * 2; System.out.print("Your age doubled is "); System.out.println(years); } }

slide-43
SLIDE 43
slide-44
SLIDE 44

 In Java, each data type has a set of basic operations you are

allowed to perform

 It's not possible to define new operations or change how the

  • perations behave

 Some programming languages allow this, but not Java

slide-45
SLIDE 45

 Basic operations you can use on numerical values:

  • +

Add

  • -

Subtract

  • *

Multiply

  • /

Divide (some tricky things here)

  • %

Modulus (we'll cover this more later)

 There are ways to raise numbers to powers or find square

roots, but they are not built-in operators

slide-46
SLIDE 46

 You can use these operators on literals (actual values),

variables, or both

int a; int b; a = 5 + 6; // a contains 11 b = a * 3; // b contains 33 double c = 2.5; c = c – a; // c contains -8.5

slide-47
SLIDE 47
slide-48
SLIDE 48

 Lab 1 is tomorrow

  • (For students with last names in the first half of the class roster)

 We'll go deeper into math operations in the next lecture

slide-49
SLIDE 49

 Keep reading Chapter 3 of the textbook  Start working on Project 1

  • You've got most of the tools you need to finish it