java foundations: unit 2 - university of...
TRANSCRIPT
Java Foundations: Unit 2
Interactive Development Environments(IDEs)
IDEs
IDEs: Create a New Project
IDEs: Name Your Project
You might want to delete the package
name for now.
IDEs: Edit Your Source Code
By default: • Code auto-saves • Code auto-compiles • Code auto-formats • Some code auto-generates
IDEs: Errors Are Highlighted
Hover over a highlighted error to see the error message.
IDEs: Managing Your Classpath
Add the folder your stored your class 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!
IDEs: Refactoring
IDEs: Controlling Source Formatting
menu: tools/options
Primitives and Objects
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
Primitives: Integer Constants
Constant Type
42L long
42 int
‘a’ char
true or false boolean
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)
Primitives: Escape Character
Constant Character
\b backspace
\t tab
\n newline
\f form feed
\r return
\” Double quote
\’ Single quote
\\ backslash
Primitives: Floating Point Types
Type Precision, Digits Exponent
double 15.95 -1022 to 1023
float 7.22 -126 to 127
Primitives: Floating Point Literals
Constant Type
42.0 double
42.0f float
4.555e2 double
5.444e2f float
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 );
}
}
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.
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();
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
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
}
}
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.
Arithmetic Operators Operator Operation
+ (binary) Addition
+ (unary) Positive indicator
- (binary) Subtraction
- (unary) Negative indicator
* Multiplication
/ Division
% Modulo
++ Increment
-- Decrement
The Modulo Operator
x % y The remainder of x divided by y
Expression Value
50 % 8 2
50 % 10 0
3 % 10 3
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;
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 = ?
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
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
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.
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 = ?