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

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Repetition Statements Recitation – 02/20/2009 CS 180 Department of Computer Science, Purdue University.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
The for-statement. Different loop-statements in Java Java provides 3 types of loop-statements: 1. The for-statement 2. The while-statement 3. The do-while-statement.
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
For Loops 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while Which loop to use? task with a specific.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Chapter 4: Control Structures II
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourceshttp://
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Lecture 10. Review (Char) Character is one of primitive data types of variable (such as integer and double) –Character variable contains one character.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Programming Principles Operators and Expressions.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Fundamentals 2 1. Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Repetition Statements
CompSci 230 S Programming Techniques
CSC111 Quick Revision.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
2.5 Another Java Application: Adding Integers
Chapter 6 More Conditionals and Loops
Multiple variables can be created in one declaration
Primitive Data, Variables, Loops (Maybe)
Repetition-Counter control Loop
Repetition-Sentinel,Flag Loop/Do_While
TK1114 Computer Programming
Building Java Programs
Fundamentals 2.
Module 4 Loops.
Building Java Programs
Code Refresher Test #1 Topics:
Chapter 2 Programming Basics.
CIS 110: Introduction to Computer Programming
Repetition Statements
Building Java Programs
Just Enough Java 17-May-19.
CSS161: Fundamentals of Computing
More on iterations using
Presentation transcript:

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

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

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 b done!! 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:stmt1 stmt3 condition is false:stmt2 stmt3

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 > b is true output: a is larger than b done!! a=5; b=7; a > b is false output: a is less than b done!!

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:

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

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

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:

Write a method: void triangle2 (int n) that for a given parameter n displays:

Write a method: void triangle3 (int n) that for a given parameter n displays:

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’