Revision - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Revision

Description:

Anyone still facing trouble running javac compiler in their personal computers/notebooks? ... Would cover increment/decrement operators and for/while loops ( and ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 20
Provided by: labu62
Category:

less

Transcript and Presenter's Notes

Title: Revision


1
Revision
Any questions regarding homework-2 or
homework-3? Anyone still facing trouble running
javac compiler in their personal
computers/notebooks?
2
For loops - Revisited
  • Make a Hello World program which prints Hello
    World! 5 times on the console.
  • Do not repeat System.out.println function 5
    times.
  • Instead, use for/while loops.

3
The Calculator problem - Revisited
  • Would cover increment/decrement operators and
    for/while loops ( and --)
  • Develop an integer calculator which accepts only
    two operands both operands inserted would be
    greater than zero.

4
The Rules
  • You have to implement the ADD function.
  • Binary operators, and , are not allowed
  • You can use unary operator. Usage of lt, gt, gt,
    lt, , and ! operators is allowed

5
The Rules (contd)
  • Sample Output
  • C\gt java Calc
  • Calculator
  • 1- Add
  • 2- Subtract
  • Your choice 1 User input
  • Enter integer 1 3 User input
  • Enter integer 2 2 User input
  • The answer is 5

6
One Possible Solution
  • Think of the two operands as buckets containing
    cells.
  • Assume var1 has 3 cells while var2 contains 2
    cells

Var2
Var1
7
One Possible Solution
  • Think of the two operands as buckets containing
    cells.
  • Assume var1 has 3 cells while var2 contains 2
    cells

Var2
Var1
8
One Possible Solution
  • Think of the two operands as buckets containing
    cells.
  • Assume var1 has 3 cells while var2 contains 2
    cells

Var2
Var1
9
One Possible Solution (contd)
  • Algorithm
  • Run a loop var2 times
  • And in each iteration increment the value of
    var1 by 1

10
One Possible Solution (contd)
  • Pseudocode
  • for i 1 to var2
  • increment var1 by one
  • Then finally display the value of var1(final
    answer)

11
One Possible Solution (contd)
  • Possible transformation to Java code
  • for (int i 1 i lt var2 i )
  • var1 var1 1 // or var1

12
Final Code
  • Public static void main (String args)
  • int var1 3
  • int var2 2
  • for ( int i 1 i lt var2 i)
  • var1
  • System.out.println( The final answer is
    var1 )

13
Related Exercises
  • Subtract (change only one line)
  • Multiply (nested for loops)
  • Divide (nested for loops)

14
Todays Exercise
  • Based on your current understanding of the for
    loops, implement the factorial function
  • Factorial of a positive integer is the product of
    all the positive integers less than or equal to it

15
Factorial (contd)
  • Example
  • Factorial( 5 ) 5 4 3 2 1 120
  • Factorial( 4 ) 4 3 2 1 24
  • Factorial( 1 ) 1
  • Important!
  • Factorial( 0 ) 1
  • Factorial( -ve numbers)?.... invalid input

16
Your Plan
  • Dont start coding immediately
  • Make your algorithm, and work out your plan on a
    piece of paper first
  • Spend 5 minutes thinking over this and come up
    with a pseudocode

17
Pseudocode
  • integer input integer from console
  • integer output 1 (default value)
  • for iterator, i input down to 1
  • output output i
  • Finally, print output (final answer)

18
Pseudocode
  • integer input integer from console
  • integer output 1 (default value)
  • if input 0, then output 1
  • else if input lt 0, then print Invalid Input
  • else
  • for iterator, i input down to 1
  • output output i
  • Finally, print output (final answer)

19
Final Code
  • import java.util.Scanner
  • public class Factorial
  • public static void main( String args )
  • int input, output 1
  • Scanner keyboard new Scanner( System.in )
  • input keyboard.nextInt()
  • if ( input 0 ) System.out.println( 1 )
  • else if ( input lt 0 ) System.out.println(Invalid
    input)
  • else
  • for ( int i input i gt 0 i--)
  • output output i
  • System.out.println( Answer output )
Write a Comment
User Comments (0)
About PowerShow.com