Introduction to Java. Main() Main method is where the program execution begins. There is only one...

48
Introduction to Java

Transcript of Introduction to Java. Main() Main method is where the program execution begins. There is only one...

Page 1: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

Introduction to Java

Page 2: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

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

Page 3: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

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

Page 4: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

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

Page 5: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

variables

• Variables are used to store and recall results of computations.

a=5;b=7;C=4;System.out.println(a+b+c);

Page 6: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

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

Page 7: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

variables

• Variables must be declared (defined)• Variables have types

– int (whole numbers)– double (decimal )

• int a;

• double b;

Page 8: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

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;

Page 9: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

Strings

• A sequence of 0 or more charactersString s = "java";

• First character is at position 0;• Characters are accessed via charAt– s.charAt(1) returns 'a' //character 'a' not string

"a"

Page 10: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

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: stmt1stmt2

condition is false: stmt2

Page 11: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

Control structures

if ( a > b)System.out.writeln(“a is larger than b”);

System.out.println(“done!!”);

a=8;b=3;

a > b is true output: a is larger than bdone!!

a=5;b=7;

a > b is false output: done!!

Page 12: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

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: stmt1stmt3

condition is false: stmt2stmt3

Page 13: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

Control structures

if ( a > b)System.out.writeln(“a is larger than b”);

elseSystem.out.writeln(“a is less than b”);

System.out.println(“done!!”);

a=8;b=3;

a > b is true output: a is larger than bdone!!

a=5;b=7;

a > b is false output: a is less than bdone!!

Page 14: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

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”);

}

Page 15: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

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”);

}

Page 16: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

Control structures

• More than one statement in the then-part and else-part?

If (cond) {…

}else {

…}

Page 17: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

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;

Page 18: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

Control structures

level = 7;

amount = 23.97;

what are the types?

Complete the program

Page 19: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

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

Page 20: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

input

• JOptionPane class

• JOptionPane.showInputDialog(null, “Enter the amount”);

• Returns the value typed in the text box (as String)

Page 21: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

input

String s;

s=JOptionPane.showInputDialog(null, “Enter the amount”);

double amount;

amount=Double.parseDouble(s);

Page 22: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

For Loops

• Loops are used to repeat a part of the program.

for (int i=0; i < 10; i=i+1)System.out.print(i);

Displays: 1 2 3 4 5 6 7 8 9

Page 23: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

For Loops

for (initialization; condition; step)body

for ( int i=0; i < 10; i=i+1 )

System.out.println(i);

Page 24: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

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

Page 25: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

For Loops

for (int i=0; i < 3; i=i+1)System.out.println(i);

System.out.println(“done”);

1: allocate an integer variable, i, and assign 0 to it2: 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

Page 26: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

For Loops

for (int i=0; i < 3; i=i+1)System.out.println(i);

System.out.println(“done”);

5: execute i=i+1; i becomes 22: 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!”

Page 27: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

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

Page 28: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

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!!

Page 29: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

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?

Page 30: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

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?

Page 31: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

while loop

while (condition)body

1. 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

Page 32: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

• Write a while loop that displays 1, 3, 5, … 99 int value=1;

while (value <= 99) {System.out.println(value); value = value +2

}

while loop

Page 33: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

• Write a while loop that displays 99, 97, 95, … 1

int value=99;

while (value >= 1) {System.out.println(value); value = value - 2;

}

while loop

Page 34: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

• 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; }

while loop

Page 35: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

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

Page 36: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

String s = "abbcdbz"; char x = 'f'; char a = 'd';

int i = 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();

Page 37: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

Break

• Break statement exits the loop

while (condition1) {…if (condition2)

break; // continue execution from right // after the end of the loop (stmt1)

…}stmt1

Page 38: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

break• Write all prime numbers between 10 and 1000

boolean prime; int i, 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); } }

Page 39: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

Write a method:

int getGCD(int m, int n)

that returns the largest integer that divides both m and n

Page 40: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

Public static int getGCD(int m, int n) { boolean found = 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;

}

Page 41: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

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"); }

Page 42: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

• 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));

Page 43: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

• Write a method:

int countVowels (String s)

that for a given string s, returns number of vowels in s

Page 44: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

• Write a method:

void multiplication (int n)

that for a given parameter n displays a nxn multiplication table

Page 45: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

• Write a method:

void triangle1 (int n)

that for a given parameter n displays:

11 21 2 31 2 3 41 2 3 4 51 2 3 4 5 6

Page 46: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

• Write a method:

void triangle2 (int n)

that for a given parameter n displays:

12 13 2 14 3 2 15 4 3 2 16 5 4 3 2 1

Page 47: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

• Write a method:

void triangle3 (int n)

that for a given parameter n displays:

12 43 6 94 8 12 165 10 15 20 256 12 18 24 30 36

Page 48: Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.

• 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’