Unformatted text preview:

Just Enough Java Jan 14 2019 Variables A variable is a box that holds data Every variable has a name Examples name age address isMarried Variables start with a lowercase letter In multiword variables each new word is capitalized Every variable has a type of value that it can hold For example name might be a variable that holds a String sequence of characters age might be a variable that holds an integer value isMarried might be a variable that holds a boolean true or false value 2 Some Java data types In Java the four most important primitive simple types are int variables hold integer values double variables hold floating point numbers that is numbers containing a decimal point boolean variables hold a true or false value char variables hold single characters Another important type is the String A String is an Object not a primitive type A String is composed of zero or more chars 3 Declaring variables Every variable that you use in a program must be declared in a declaration The declaration specifies the type of the variable The declaration may give the variable an initial value Examples int age int count 0 double distance 37 95 boolean isReadOnly true String greeting Welcome to CIT 591 String outputLine 4 Assignment statements Values can be assigned to variables by assignment statements The syntax is variable expression The expression must be of the same type as the variable The expression may be a simple value or it may involve computation Examples name Dave count count 1 area 4 0 3 0 3 1416 radius radius isReadOnly false When a variable is assigned a value the old value is discarded and totally forgotten 5 Comments A comment is a note to any human reading the program comments are ignored by Java A comment starts with and goes to the end of the line A comment may be put after a statement on the same line to say something about that one statement A comment may be put on a line by itself to say something about the following statements Example Swap the values of x and y temp x save old value of x in temp x y replace old value of x with y y temp this many comments is just silly 6 Methods A method is a named group of declarations and statements void tellWhatYearItIs int year 2005 System out println Hello in year We call or invoke a method by naming it in a statement tellWhatYearItIs This should print out Hello in 2005 7 Organization of a class A class may contain data declarations and methods and constructors which are like methods but not statements A method may contain temporary data declarations and statements A common error class Example int variable simple declaration is OK int anotherVariable 5 declaration with initialization is OK variable 5 statement This is a syntax error void someMethod int yetAnotherVariable declaration is OK yetAnotherVariable 5 statement inside method is OK 8 Arithmetic expressions Arithmetic expressions may contain An operation involving two ints results in an int to indicate addition to indicate subtraction to indicate multiplication to indicate division to indicate remainder of a division integers only parentheses to indicate the order in which to do things When dividing one int by another the fractional part of the result is thrown away 14 5 gives 2 Any operation involving a double results in a double 3 7 1 gives 4 7 9 Boolean expressions Arithmetic comparisons result in a boolean value of true or false There are six comparison operators less than less than or equals greater than greater than or equals equals not equals There are three boolean operators and true only if both operands are true or true if either operand is true not reverses the truth value of its one operand Example x 0 x 99 x is greater than zero and is not greater than 99 10 Spaces You should put a single space around every binary operator including comparisons and Example perimeter 2 width height Do not put spaces just inside parentheses perimeter 2 width height bad These are style rules not Java rules If you break these rules your program will still compile OK but you will lose points if we notice 11 String concatenation You can concatenate join together Strings with the operator In fact you can concatenate any value with a String and that value will automatically be turned into a String Example fullName firstName lastName Example System out println There are count apples Be careful because also still means addition int x 3 int y 5 System out println x y x y The above prints 8 35 Addition is done left to right use parentheses to change the order 12 if statements An if statement lets you choose whether or not to execute one statement based on a boolean condition Syntax if boolean condition statement Example if x 100 x x 1 adds 1 to x but only if x is less than 100 C programmers take note The condition must be boolean An if statement may have an optional else part to be executed if the boolean condition is false Syntax if boolean condition statement else statement Example if x 0 x limit y x limit else System out println x is out of range x 13 Compound statements Multiple statements can be grouped into a single statement by surrounding them with braces Example if score 100 score 100 System out println score has been adjusted Unlike other statements there is no semicolon after a compound statement Braces can also be used around a single statement or no statements at all to form an empty statement It is good style to always use braces in the if part and else part of an if statement even if the surround only a single statement Indentation and spacing should be as shown in the above example 14 while loops A while loop will execute the enclosed statement as long as a boolean condition remains true Syntax while boolean condition statement Example n 1 while n 5 System out println n squared is n n n n 1 Result 1 squared is 1 2 squared is 4 3 squared is 9 4 squared is 16 C programmers take note The condition must be boolean Danger If the condition never becomes false the loop never exits and the program never stops 15 Indentation and spacing if x 10 x x 1 else x x 1 OR if x 10 x x 1 else x x 1 while x 0 System out println x x x 1 Recommended indentation is 4 spaces and must be consistent throughout the program Notice that there is not a space after println Most important style rule If you are modifying or adding to an existing program keep to the original style whatever it may be 16 Method calls A method call is a request to an object to do something or to compute a value A method call may


View Full Document

Penn CIT 591 - Just Enough Java

Documents in this Course
Stacks

Stacks

11 pages

Arrays

Arrays

30 pages

Arrays

Arrays

29 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Vectors

Vectors

14 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Static

Static

12 pages

Eclipse

Eclipse

23 pages

JAVA

JAVA

24 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

JUnit

JUnit

23 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Buttons

Buttons

20 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
Loading Unlocking...
Login

Join to view Just Enough Java and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Just Enough Java and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?