1 / 48

Introduction to Java

Introduction to Java. Main(). Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi there!”); System.out.println (5+6);. Operators. Relational operators Result is either true or false Equal == Less than <

jbarth
Download Presentation

Introduction to Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Java

  2. Main() • Main method is where the program execution begins. • There is only one main • Displaying the results: System.out.println (“Hi there!”); System.out.println(5+6);

  3. Operators • Relational operators • Result is either true or false • Equal == • Less than < • Less than or equal <= • Larger than > • Larger than or equal >= • 7 >= 8 is true • 6 == 9 is false

  4. Operators • Arithmetic operators • Add + • Subtract - • Multiply * • Divide / • Remainder % • System.out.println(5 + 9); • System.out.println (8 / 2); • System.out.println(8 % 3); • System.out.println(8 / 3);

  5. variables • Variables are used to store and recall results of computations. a=5; b=7; C=4; System.out.println(a+b+c);

  6. variables • Variables are used to store and recall results of computations. a=5; b=7; c=4; System.out.println(a+b+c); c=c+a; System.out.println(a+b+c);

  7. variables • Variables must be declared (defined) • Variables have types • int (whole numbers) • double (decimal ) • int a; • double b;

  8. variables • Variable declaration maps the variable name to a unique memory location • The value of the mapped memory location can be: • changed (assignment) a=5; • looked up System.out.println(a); b=a*0.10;

  9. Strings • A sequence of 0 or more characters String s = "java"; • First character is at position 0; • Characters are accessed via charAt • s.charAt(1) returns 'a' //character 'a' not string "a"

  10. Control structures if (condition) // if condition is true, then stmt1 is stmt1 // executed. If condition is not true, then stmt2 // stmt1 not executed condition is true: stmt1 stmt2 condition is false: stmt2

  11. Control structures if ( a > b) System.out.writeln(“a is larger than b”); System.out.println(“done!!”); a=8; b=3; a=5; b=7; • output: a is larger than b • done!! • output: done!! a > b is true a > b is false

  12. Control structures if (condition) // if condition is true, then stmt1 is stmt1 // executed and stmt2 is not executed. else // If condition is not true (is false), then stmt2 // stmt1 is not executed and stmt2 is executed stmt3 condition is true: stmt1 stmt3 condition is false:stmt2 stmt3

  13. Control structures if ( a > b) System.out.writeln(“a is larger than b”); else System.out.writeln(“a is less than b”); System.out.println(“done!!”); a=8; b=3; a=5; b=7; • output: a is larger than b • done!! • output: a is less than b done!! a > b is true a > b is false

  14. Control structures • More than one statement in the then-part? If ( a > b ) { System.out.println(a); System.out.println(b); System.out.println(“a is larger than b”); }

  15. Control structures • More than one statement in the else-part? If ( a > b ) System.out.println(a-b); else { System.out.println(a); System.out.println(b); System.out.println(“a is less than b”); }

  16. Control structures • More than one statement in the then-part and else-part? If (cond) { … } else { … }

  17. Control structures • Exercise: • Level of service indicates how happy we where with the service we received in a restaurant ( 1 – 10) • Bill is the amount of our bill; • Tip is 10% if the service level was less than 5; • Tip is 15% if the service level is 5 or higher;

  18. Control structures level = 7; amount = 23.97; what are the types? Complete the program

  19. Control structures int level = 7; double amount = 23.97; double topRate, total; if (level < 5) tipRate=0.1; else tipRate=0.15; total = amount + amount *tipRate; System.out.println(total);

  20. input • JOptionPane class • JOptionPane.showInputDialog(null, “Enter the amount”); • Returns the value typed in the text box (as String)

  21. input String s; s=JOptionPane.showInputDialog(null, “Enter the amount”); double amount; amount=Double.parseDouble(s);

  22. For Loops • Loops are used to repeat a part of the program. for (inti=0; i < 10; i=i+1) System.out.print(i); Displays: 1 2 3 4 5 6 7 8 9

  23. For Loops for (initialization; condition; step) body for ( inti=0; i < 10; i=i+1 ) System.out.println(i);

  24. For Loops for (initialization; condition; step) body • initialization is executed only once when the loop starts • Condition is evaluated. • If condition is true: • body is executed • Step is executed after completion of body • Condition is evaluated as in above • If condition is false • body is not executed – execution is continued right after the body

  25. For Loops for (inti=0; i < 3; i=i+1) System.out.println(i); System.out.println(“done”); 1: allocate an integer variable, i, and assign 0 to it 2: evaluate the condition i < 3; i < 3 is true; execute loop body output: 0 3: execute i=i+1; I becomes 1 4: evaluate the condition i< 3; i < 3 is true; execute loop body output: 1

  26. For Loops for (inti=0; i < 3; i=i+1) System.out.println(i); System.out.println(“done”); 5: execute i=i+1; ibecomes 2 2: evaluate the condition i < 3; i < 3 is true; execute loop body output: 2 3: execute i=i+1; I becomes 3 4: evaluate the condition i < 3; i < 3 is false; continue execution right after the loop body output: “done!”

  27. loops • Write a for loop that displays 1, 3, 5, … 99 for (int i = 1; i < 100; i = i + 2) System.out.println(i); • Write a for loop that displays 99, 97, 95, … 1 for (int i = 99; i > 0; i = i - 2) System.out.println(i);

  28. loops • Read a sequence of integers until zero is entered and display the sum of all the numbers that were read • Read and keep a running total until a zero is read!!

  29. Scanner in = new Scanner (System.in); int sum=0; System.out.println("Enter the next value "); for (int next= in.nextInt(); next > 0; next = in.nextInt()) { sum = sum+next; System.out.println("Enter the next value "); } Who understands this?

  30. Scanner in = new Scanner (System.in); int sum=0; int next; System.out.println("Enter the next value"); next = in.nextInt(); while (next > 0) { sum = sum + next; System.out.println("Enter the next value"); next = in.nextInt(); } System.out.println("Sum = " + sum); Who understands this?

  31. while loop while (condition) body • Condition is evaluated. • If condition is true: • body is executed, and then step 1 is repeated. • If condition is false • body is not executed – execution is continued right after the body

  32. while loop • Write a while loop that displays 1, 3, 5, … 99 int value=1; while (value <= 99) { System.out.println(value); value = value +2 }

  33. while loop • Write a while loop that displays 99, 97, 95, … 1 int value=99; while (value >= 1) { System.out.println(value); value = value - 2; }

  34. while loop • Write a while loop that displays 1, 3, 5, … 99, except the numbers that are divisible by 5 int value = 1; while (value <= 99) { if (value % 5 != 0) System.out.println(value); value = value + 1; }

  35. loops • Let s be a String • Write a program that • 1: • displays number of occurrences of letter a in s that appear before letter x in s • in case s does not contain letter x, the program must display 0. • 2: • Displays s in reverse order

  36. String s = "abbcdbz"; char x = 'f'; char a = 'd'; inti = 0; int count = 0; while (i < s.length()) { if (s.charAt(i) == a) { count = count + 1; } if (s.charAt(i) == x) break; // exit the loop i = i + 1; } if (i == s.length()) { // came out of the loop because we didn't see x count = 0; } System.out.println(count); for (i=s.length()-1; i>=0; i=i-1) System.out.print(s.charAt(i)); System.out.println();

  37. Break • Break statement exits the loop while (condition1) { … if (condition2) break; // continue execution from right // after the end of the loop (stmt1) … } stmt1

  38. break • Write all prime numbers between 10 and 1000 booleanprime; inti, j; for (i=10; i<1000; i=i+1) { //pick all numbers between 10 and1000 prime=true; for (j=2; j<i; j=j+1) if (i%j == 0) { prime=false; break; // i is not a prime number exit the loop } if (prime) System.out.println(j); } }

  39. Write a method: intgetGCD(intm, int n) that returns the largest integer that divides both m and n

  40. Public static intgetGCD(intm, int n) { booleanfound = false; if (m > n) { gcd = n; } else { gcd = m; } while (!found) { if (m % gcd == 0 && n % gcd == 0) { found = true; } else { gcd = gcd - 1; } } return gcd; }

  41. Write a boolean method that given an integer m returns true if m is a prime number; Test: Scanner inp = new Scanner(System.in); int x = inp.nextInt(); if (isPrime(x)) { System.out.println(x + " is a prime number"); } else { System.out.println(x + " is not a prime number"); }

  42. Write an integer method that given an integer, m, returns the smallest prime number that is larger than n Test: Scanner inp = new Scanner(System.in); int n = inp.nextInt(); System.out.println(nextPrime(n));

  43. Write a method: intcountVowels(String s) that for a given string s, returns number of vowels in s

  44. Write a method: void multiplication (int n) that for a given parameter n displays a nxn multiplication table

  45. Write a method: void triangle1 (int n) that for a given parameter n displays: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6

  46. Write a method: void triangle2 (int n) that for a given parameter n displays: 1 2 1 3 2 1 4 3 2 1 5 4 3 2 1 6 5 4 3 2 1

  47. Write a method: void triangle3 (int n) that for a given parameter n displays: 1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 6 12 18 24 30 36

  48. Write a method: char mostOftenIn(String s) that for a given string s, returns the character that occurs most often in s. For example for s = “abcaabbcb” mostOftenIn(s) must return ‘b’

More Related