Introduction to Java. Main() Main method is where the program execution begins. There is only one...
Transcript of Introduction to Java. Main() Main method is where the program execution begins. There is only one...
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 <– Less than or equal <=– Larger than >– Larger than or equal >=• 7 >= 8 is true• 6 == 9 is false
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);
variables
• Variables are used to store and recall results of computations.
a=5;b=7;C=4;System.out.println(a+b+c);
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);
variables
• Variables must be declared (defined)• Variables have types
– int (whole numbers)– double (decimal )
• int a;
• double b;
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;
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"
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
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!!
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
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!!
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”);
}
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”);
}
Control structures
• More than one statement in the then-part and else-part?
If (cond) {…
}else {
…}
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;
Control structures
level = 7;
amount = 23.97;
what are the types?
Complete the program
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);
input
• JOptionPane class
• JOptionPane.showInputDialog(null, “Enter the amount”);
• Returns the value typed in the text box (as String)
input
String s;
s=JOptionPane.showInputDialog(null, “Enter the amount”);
double amount;
amount=Double.parseDouble(s);
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
For Loops
for (initialization; condition; step)body
for ( int i=0; i < 10; i=i+1 )
System.out.println(i);
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
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
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!”
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);
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!!
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?
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?
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
• 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
• 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
• 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
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
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();
Break
• Break statement exits the loop
while (condition1) {…if (condition2)
break; // continue execution from right // after the end of the loop (stmt1)
…}stmt1
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); } }
Write a method:
int getGCD(int m, int n)
that returns the largest integer that divides both m and n
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;
}
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"); }
• 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));
• Write a method:
int countVowels (String s)
that for a given string s, returns number of vowels in s
• Write a method:
void multiplication (int n)
that for a given parameter n displays a nxn multiplication table
• 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
• 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
• 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
• 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’