Java Review Slides Hello World Program // HelloWorld // Author: - - PowerPoint PPT Presentation

java review slides hello world program
SMART_READER_LITE
LIVE PREVIEW

Java Review Slides Hello World Program // HelloWorld // Author: - - PowerPoint PPT Presentation

Java Review Slides Hello World Program // HelloWorld // Author: Chris Wilcox // Date: 1/1/2015 // Class: CS160 // Email: wilcox@cs.colostate.edu import java.lang.*; public class HelloWorld { public static void main(String[] args) {


slide-1
SLIDE 1

Java Review Slides

slide-2
SLIDE 2

Hello World Program

// HelloWorld // Author: Chris Wilcox // Date: 1/1/2015 // Class: CS160 // Email: wilcox@cs.colostate.edu import java.lang.*; public class HelloWorld { public static void main(String[] args) { System.out.println( "Hello World!" ); } }

CS 160, Fall Semester 2015 2

slide-3
SLIDE 3

Literals

  • Literals are values that are directly recognized

by Java:

– numbers

237, 10, 9, 1.5, 5.8, 99.999

– characters

‘a’, ‘Z’, ‘0’, ‘$’

– strings

“hello”, “there”

CS 160, Fall Semester 2015 3

slide-4
SLIDE 4

Java Identifiers

  • An identifier is a name, such as the name of a

variable.

  • Identifiers may contain only

– Letters – Digits (0 through 9) – The underscore character (_) – And the dollar sign symbol ($) which has a special meaning

  • The first character cannot be a digit.

CS 160, Fall Semester 2015 4

slide-5
SLIDE 5

Java Types

n In Java, there are two kinds of data types:

n Primitive data types

n Hold a single, indivisible piece of data n Pre-defined by the language n Examples: int, char, double, boolean

n Classes

n Hold complex combinations of data n Programs may define new classes n Examples: String, System

CS 160, Fall Semester 2015 5

slide-6
SLIDE 6

Primitive Types

  • Integer types: byte, short, int, and long

– int is most common

  • Floating-point types: float and double

– double is more common

  • Character type: char
  • Boolean type: boolean

CS 160, Fall Semester 2015 6

slide-7
SLIDE 7

Primitive Types

CS 160, Fall Semester 2015 7

slide-8
SLIDE 8

Sample Expressions

CS 160, Fall Semester 2015 8

slide-9
SLIDE 9

Assignment Compatibilities

  • A value of each following type can be

assigned to a variable of type to the right:

byte à short à int à long à float à double

– but not to a variable of any type to the left.

  • You can assign a value of type char to a

variable of type int.

  • … except through type casting

CS 160, Fall Semester 2015 9

slide-10
SLIDE 10

if Statement with else

  • An if statement may have an optional else

clause that will only be executed when the condition is false

  • Example:

if ( wages <= 57600.0 ) tax = 0.124 * wages; else tax = 0.124 * 57600.0;

Give an example

  • f when BOTH

statements will execute? Give an example

  • f when NEITHER

statement will execute?

NONE! One or the other must execute NONE! One or the other must execute

CS 160, Fall Semester 2015 10

slide-11
SLIDE 11

boolean Data Type

  • boolean
  • A primitive data type that can be set to:

– true – false

  • Example:

boolean correct = true;

Notice there are no quotation marks around trueand false!

CS 160, Fall Semester 2015 11

slide-12
SLIDE 12

Numeric Relational Operators

Math Java description < < Less than > > Greater than <= Less than or equal to >= Greater than or equal to = == Equal to != Not equal to

≤ ≥ ≠

CS 160, Fall Semester 2015 12

slide-13
SLIDE 13

boolean Operators

  • Logical “and” (conjunction)

– Java symbol && – Math symbol ∧ – true only when both expressions are true (MIN_WAGE <= wages) && (wages <= MAX_WAGE)

  • Logical inclusive “or” (disjunction)

– Java symbol || – Math symbol ∨ – true when either or both expressions are true (wages < MIN_WAGE ) || (wages > MAX_WAGE )

CS 160, Fall Semester 2015 13

slide-14
SLIDE 14

Java Logical and Arithmetic Operator Precedence Rules

1. !

  • (unary)

2. * / % 3. +

  • 4.

< <= > >= 5. == != 6. ^ & | 7. && 8. ||

CS 160, Fall Semester 2015 14

slide-15
SLIDE 15

Control Loops

CS 160, Fall Semester 2015 15

for loops for (int i=0; i<=9; i++... for (int i=100; i>0; i-=5)... for (char c=‘a’; c<=‘z’; c++)... while loops while (d < 1.2345)... do/while loops do {...} while (!done);

slide-16
SLIDE 16

Temperature Conversion Program

System.out.println(“\tDEGREES C\tDEGREES F”); for (int cent = 50; cent <= 100; cent++) { double fahr = (9.0 / 5.0) * cent + 32.0; System.out.print(“\t” + cent); System.out.println(“\t” + fahr); }

CS 160, Fall Semester 2015 16

slide-17
SLIDE 17

Program example: find divisors

public classfoo { public static void main(String[] args) { int number = Integer.parseInt(args[0]); int divisor = 2; while (divisor < number ) { if ((number % divisor) == 0) { System.out.print(divisor + " "); } divisor = divisor + 1; } } }

CS 160, Fall Semester 2015 17

slide-18
SLIDE 18

Method Declarations

  • public double sin(double angle)
  • public double cos(double angle)
  • public char charAt(int index)
  • public int indexOf(char c)
  • public int minimum(int i, int j)
  • public String toLower(String s)
  • public int[] getArray()

CS 160, Fall Semester 2015 18

slide-19
SLIDE 19

Method Example

public static int[] truncate(double dArray[]) { int iArray[] = new int[dArray.length]; for (int i = 0; i < dArray.length; i++) iArray[i] = (int) dArray[i]; return iArray[]; } double doubles[] = {1.1, 2.2, 3.3, 4.4}; int integers[] = truncate(doubles);

CS 160, Fall Semester 2015 19

slide-20
SLIDE 20

public and private

  • public

– Can access the class, method, or data by name outside defining class

  • private

– Can access the class, method, or data by name only inside defining class

  • Classes generally specified as public
  • Instance variables usually are private
  • Methods can be public or private

CS 160, Fall Semester 2015 20

slide-21
SLIDE 21

Caution: Pass by value

  • What do you expect this to print?

public class PassByValue { public static void main(String[] args) { int number = 100; increment(number); System.out.println(“Number: “ + number); } public static void increment(int n) { n++; } }

  • The value of the argument is copied, so no change to number!

CS 160, Fall Semester 2015 21

slide-22
SLIDE 22

Arrays

  • An array is a set of variables of the same type

accessed by their index

31 28 31 30 int[] day = new int[4]; day[0] day[1] day[2] day[3]

CS 160, Fall Semester 2015 22

slide-23
SLIDE 23
  • Figure 7.1 A common way to visualize an

array

  • Note class ArrayOfTemperatures

Creating and Accessing Arrays

CS 160, Fall Semester 2015 23

slide-24
SLIDE 24
  • Multidimensional array represented as several one-

dimensional arrays

  • Given

int [][] table = new int [10][6];

  • Array table is actually 1 dimensional of type int[]

– It is an array of arrays

  • Important when sequencing through

multidimensional array

Java's Representation of Multidimensional Arrays

CS 160, Fall Semester 2015 24