120 likes | 292 Views
CS 240 – Computer Programming I Lab. Kalpa Gunaratna – kalpa@knoesis.org http://knoesis.wright.edu/researchers/kalpa/. Contact. Contact by e-mail kalpa@knoesis.org Office hours 376 Joshi Mondays & Wednesday 3:00 pm – 4:00 pm. Get input from key board.
E N D
CS 240 – Computer Programming ILab KalpaGunaratna – kalpa@knoesis.org http://knoesis.wright.edu/researchers/kalpa/
Contact • Contact by e-mail • kalpa@knoesis.org • Office hours • 376 Joshi • Mondays & Wednesday 3:00 pm – 4:00 pm
Get input from key board • Scanner keyboard = new Scanner(System.in); • keyboard is the given name to the variable here. You can use any name you like. Object type name Create new object and assign to name
Useful Arithmetic & Relational operators • > greater than, < less than, == equals to, != not equal to, >= greater than or equal to, <= less than or equal to, etc. • Example: 5 > 6 will output false. Number 5 is not greater than 6. • Boolean operators • && - logical AND • || - logical OR • Example: x==5 && y<5, this expression outputs true if and only if value of x is 5 and value of y is less than 5.
If statements • Syntax • if (<boolean expression> ) { runs if < boolean expression> is true } else { runs if < boolean expression> is false }
If else if … if(< boolean expression>) { your code here } else if(< boolean expression>) { your code here } else { your code here }
Print statements • System.out.print() • Prints a line and stays in the current line • Sytem.out.println() • Prints a line and goes to a new line • System.out.printf(“<rule>”, <number>) • Ex: System.out.printf(“%.2f \n", 1234.123);
Pseudo Code • Not a Java code or any programming language • Human readable code similar to a programming language. • Uses to describe algorithms. • Example: • Algorithm – if input value is greater than 5, out put to the console a success message. Otherwise exit program. • Pseudo Code – • Get user input value x. if x is greater than 5 then out put to console “Success” else exit from program • You see that pseudo code has nothing to do with Java! • More examples : • http://www.unf.edu/~broggio/cop2221/2221pseu.htm
Flow Chart • Condition • Statement input true false
Example If then else
Flow chart Pseudo code sum = 0 count = 1 REPEAT IF count is even THEN sum = sum + count count = count + 1 UNTIL count > 20 DISPLAY sum
You can find useful information and helpful tips about flow charts at, • http://users.evtek.fi/~jaanah/IntroC/DBeech/3gl_flow.htm