Java Foundations: Unit 2 - University of...

32
Java Foundations: Unit 2 Interactive Development Environments(IDEs)

Transcript of Java Foundations: Unit 2 - University of...

Page 1: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

Java Foundations: Unit 2

Interactive Development Environments(IDEs)

Page 2: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

IDEs

Page 3: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

IDEs: Create a New Project

Page 4: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

IDEs: Name Your Project

You might want to delete the package

name for now.

Page 5: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

IDEs: Edit Your Source Code

By default: • Code auto-saves • Code auto-compiles • Code auto-formats • Some code auto-generates

Page 6: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

IDEs: Errors Are Highlighted

Hover over a highlighted error to see the error message.

Page 7: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

IDEs: Managing Your Classpath

Add the folder your stored your class Java files to your classpath.

Page 8: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

IDEs: Code-completion, Documentation

Page 9: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

IDEs: Run Options

• Execute the main project. • Execute the current file.

You can also run in debug mode; more about this later.

Learn your shortcuts!

Page 10: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

IDEs: Refactoring

Page 11: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

IDEs: Controlling Source Formatting

menu: tools/options

Page 12: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

Primitives and Objects

Page 13: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

Primitives: Integer Types

Type Min Value Max Value

long -2e63 2e63 – 1

int About -2.1 billion About 2.1 billion

short -32,768 32,767

char 0 65,535

byte -128 127

boolean true or false

Page 14: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

Primitives: Integer Constants

Constant Type

42L long

42 int

‘a’ char

true or false boolean

Page 15: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

Primitives: Integer Literals

Constant Type

42L long

42 int

‘a’ char

true or false boolean

0123 int (octal)

0x12F int (hexadecimal)

0b1101 int (binary)

‘u1234’ char (hex/unicode)

Page 16: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

Primitives: Escape Character

Constant Character

\b backspace

\t tab

\n newline

\f form feed

\r return

\” Double quote

\’ Single quote

\\ backslash

Page 17: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

Primitives: Floating Point Types

Type Precision, Digits Exponent

double 15.95 -1022 to 1023

float 7.22 -126 to 127

Page 18: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

Primitives: Floating Point Literals

Constant Type

42.0 double

42.0f float

4.555e2 double

5.444e2f float

Page 19: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

Primitives: Example public class Prims

{

public static void main( String[] args )

{

int inx = 97;

double unit = .63;

double result = inx * unit;

System.out.println( result );

}

}

Page 20: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

Objects: Definition • An object stores data and executes actions. • An object has a name and a type.

What a Turtle knows: • What color am I? • What is my position? • What direction am I facing?

What a Turtle can do: • Turn and paint a line. • Turn and move. • Paint a circle. • Fill a box. • Fill a circle. • Etc.; see Turtle and Vic Class Documentation on the class website.

Page 21: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

Objects: Declaration and Initialization • An object must be declared:

Turtle pokey;

• An object must be created:

pokey = new Turtle();

• An object can be declared and created at the same time:

Turtle pokey = new Turtle();

Page 22: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

Objects: Methods • A method is a function that is used to interact with an

object. • A method is invoked using the executor (a period). • A method has 0 or more arguments that control its

execution. Turtle pokey = new Turtle();

pokey.fillBox( 64, 128 );

executor method

arguments

Page 23: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

Objects: Example public class Test

{

public static void main(String[] args)

{

Turtle todd = new Turtle();

todd.move( 180, 128 ); // Move to left

todd.move( 180, 0 ); // Restore orient.

todd.paint( 90, 64 ); // Right side

todd.paint( 90, 64 ); // Top side

todd.paint( 90, 64 ); // Left side

todd.paint( 90, 64 ); // Bottom side

}

}

Page 24: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

Exercises 1. Write a program that uses one Turtle to draw an octagon 64

pixels on a side (note: there are 45 degrees in the external angle of an octagon).

2. Write a program that creates two Turtles; use one to draw a red circle on the left of the screen (use swingAround), use the other to fill a blue box (use switchTo to change colors).

3. Write a program that uses a Turtle to draw a house 200 pixels wide and 150 pixels tall, with one door and two windows.

Page 25: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

Arithmetic Operators Operator Operation

+ (binary) Addition

+ (unary) Positive indicator

- (binary) Subtraction

- (unary) Negative indicator

* Multiplication

/ Division

% Modulo

++ Increment

-- Decrement

Page 26: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

The Modulo Operator

x % y The remainder of x divided by y

Expression Value

50 % 8 2

50 % 10 0

3 % 10 3

Page 27: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

The Increment/Decrement Operators inx++ Post-increment ++inx Pre-increment jnx-- Post-decrement --jnx Pre-decrement

Expression Value inx jnx

inx ++ * jnx 50 6 10

++inx * jnx 60 6 10

jnx-- * inx 50 5 9

--jnx * inx 45 5 9

Given: int inx = 5;

int jnx = 10;

Page 28: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

Exercise Complete the following calculations:

int inx = 200;

int jnx = 7;

int knx = inx % jnx;

int inx = 200;

int jnx = 7;

int knx = jnx % inx;

int inx = 6;

int jnx = 7

int knx = ++jnx + inx--;

knx = ?

knx = ?

inx = ?

jnx = ?

knx = ?

Page 29: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

Bit Operators

Operator Operation

& Bit-wise and

| Bit-wise or

^ Bit-wise exclusive or

<< left-shift

>> Arithmetic right-shift (signed division by 2)

>>> Logical right-shift (unsigned division by 2)

~ Bit-wise complement

Page 30: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

Truth Tables

& 0 1

0 0 0

1 0 1

| 0 1

0 0 1

1 1 1

^ 0 1

0 0 1

1 1 0

Page 31: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

Bit Operators: Examples 11001011

& 01100110

01000010

11001011

| 01100110

11101111

11001011

^ 01100110

10101101

~ 01100110

10011001

11001011

<<2

00101100

11001011

>>2

11110010

Arithmetic right shift; high-order bit replicates.

11001011

>>>2

00110010

Logical right shift; high-order bit does not replicate.

Page 32: Java Foundations: Unit 2 - University of Washingtonfaculty.washington.edu/jstraub/JavaFoundations/Unit2.pdfclass Java files to your classpath. IDEs: Code-completion, Documentation

Exercise Complete the following calculations:

byte inx = 12;

byte jnx = 2;

byte knx = inx >> jnx;

knx = ?

byte inx = 12;

byte jnx = 2;

byte knx = inx << jnx;

knx = ?

byte inx = 12;

byte jnx = 3;

byte knx = inx & jnx;

knx = ?

byte inx = 12;

byte jnx = 5;

byte knx = inx | jnx;

knx = ?