java review slides hello world program
play

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) {


  1. Java Review Slides

  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

  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

  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

  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

  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

  7. Primitive Types CS 160, Fall Semester 2015 7

  8. Sample Expressions CS 160, Fall Semester 2015 8

  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

  10. if Statement with else • An if statement may have an optional else clause that will only be executed when the condition is false Give an example • Example: of when BOTH statements will if ( wages <= 57600.0 ) execute? tax = 0.124 * wages; NONE! NONE! One or the other One or the other else must execute must execute tax = 0.124 * 57600.0; Give an example of when NEITHER statement will execute? CS 160, Fall Semester 2015 10

  11. boolean Data Type • boolean • A primitive data type that can be set to: – true Notice there are no – false quotation marks around true and • Example: false ! boolean correct = true; CS 160, Fall Semester 2015 11

  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

  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

  14. Java Logical and Arithmetic Operator Precedence Rules 1. ! - (unary) 2. * / % 3. + - 4. < <= > >= 5. == != 6. ^ & | 7. && 8. || CS 160, Fall Semester 2015 14

  15. Control Loops 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); CS 160, Fall Semester 2015 15

  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

  17. Program example: find divisors public class foo { 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

  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

  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

  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

  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

  22. Arrays • An array is a set of variables of the same type accessed by their index int[] day = new int[4]; 31 28 31 30 day[0] day[2] day[1] day[3] CS 160, Fall Semester 2015 22

  23. Creating and Accessing Arrays • Figure 7.1 A common way to visualize an array • Note class ArrayOfTemperatures CS 160, Fall Semester 2015 23

  24. Java's Representation of Multidimensional Arrays • 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 CS 160, Fall Semester 2015 24

Recommend


More recommend