week 2 - fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · week 2 - friday what did we...

31
Week 2 - Wednesday

Upload: others

Post on 25-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a

Week 2 - Wednesday

Page 2: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a

What did we talk about last time? Types int boolean double char String

Basic input

Page 3: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a
Page 4: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a

Rules for variables: They must start with a lowercase letter, an uppercase letter, or an underscore After that, you can have as many letters, digits, and underscores as you want No spaces in variable names!

Regular variables should be in lowercase The camel case convention says that new words start with an uppercase letter:

▪ elegantBalloons▪ aReallyLongUselessVariableName

Constants should be in ALL CAPS Then, underscores are used to separate the words:

▪ GRAVITATIONAL_CONSTANT

Page 5: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a
Page 6: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a

There are three parts to using Scanner for input1. Include the appropriate import statement so that your program

knows what a Scanner object is2. Create a specific Scanner object with a name you choose3. Use the object you create to read in data

Page 7: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a

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();

Page 8: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a

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 = in.nextInt();years = years * 2;System.out.print("Your age doubled is ");System.out.println(years);

}}

Page 9: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a
Page 10: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a

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 operations behave

Some programming languages allow this, but not Java

Page 11: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a

Today, we are going to consider the basic operations for numerical types: int double

Page 12: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a
Page 13: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a

Use the + operator to add two ints together

int a;int b;a = 5 + 6; // a contains 11b = a + 3; // b contains 14

a + b; // not allowed, does nothing

a = a + 1; // a contains 12, and b?

Page 14: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a

Some expressions are used so often, Java gives us a short cut x = x + y; can be written x += y; x = x + 1; can be written x++; or ++x;

int x;

x = 6; // x contains 6x += 4; // x contains 10

x++; // x contains 11++x; // x contains 12

Page 15: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a

Exactly like + except performs subtraction

int a;int b;a = 5 - 6; // a contains -1b = 3 - a; // b contains 4

a -= 10; // shortcut for a = a – 10;

a--; // shortcut for a = a – 1;

Page 16: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a

The * operator performs multiplication

int a;int b;a = 5 * 6; // a contains 30b = a * 3; // b contains 90

a *= 2; // shortcut for a = a * 2;

Page 17: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a

The / operator performs integer division Not the same as regular division

The factional part is dropped, not rounded

int a;int b;a = 3; // a contains 3b = a / 2; // b contains 1

a /= 2; // shortcut for a = a / 2;

Page 18: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a

The % operator is the mod operator It finds the remainder after division

This operator is a good way to find out if a number is even or odd

int a;int b;a = 8; // a contains 8b = a % 5; // b contains 3

a %= 2; // shortcut for a = a % 2;

Page 19: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a
Page 20: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a

Exactly the same as + for int, except now you can have fractional parts

double a;double b;a = 3.14159; // a contains 3.14159b = a + 2.1; // b contains 5.24159

a += 1.6; // shortcut for a = a + 1.6;

a++; // shortcut for a = a + 1.0;

Page 21: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a

No surprises here They do subtraction and multiplicationdouble a;double b;a = 3.14159; // a contains 3.14159b = a - 2.1; // b contains 1.04159

a = b * 0.5; // a contains 0.520795

Page 22: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a

Unlike int, this division does have fractional parts

Can you explain this mystery?

double a;double b;a = 3; // a contains 3.0b = a / 2; // b contains 1.5

b = 3 / 2; // b contains 1.0

Page 23: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a

Yes, there is a % operator for double, but it is rarely used Don't worry about it for now

Page 24: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a

Given a temperature in Celsius, what is the equivalent in Fahrenheit?

TF = (9/5)TC + 32

Page 25: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a

How complex can expressions get?

What's the value of a? 18!

int a = 31;int b = 16;int c = 1;int d = 2;

a = b + c * d – a / b / d;

Page 26: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a

Order of operations holds like in math

You can use parentheses to clarify or change the precedence Now a is 16

int a = 31;int b = 16;int c = 1;int d = 2;

a = (((b + c) * d) – a / b) / d;

Page 27: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a

You cannot directly store a double value into an int variable

However, you can cast the double value to convert it into an int

Casting tells the compiler that you want the loss of precision to happen

You can always store an int into a double

int a = 2.6; // fails!

int a = (int)2.6;// succeeds! (a = 2)

Page 28: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a
Page 29: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a
Page 30: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a

Practice using mathematical operations Operations on boolean and char values

Page 31: Week 2 - Fridayfaculty.otterbein.edu/wittman1/comp1600/slides... · Week 2 - Friday What did we talk ... Include the appropriate import statement so that your program knows what a

Keep reading Chapter 3 of the textbook Get a start on Project 1 Lab 2 is tomorrow for second half of the class, alphabetically