Programming Language - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Programming Language

Description:

{ if ((col =(hight row-1))&&(col (hight-row))) System.out.print(star); else. System.out.print ... (int hight, char star){ int row=1, col=1; while (row = hight) ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 23
Provided by: Luba3
Category:

less

Transcript and Presenter's Notes

Title: Programming Language


1
Programming Language
Prepared by Amir and Luba Sapir
2
Lecture 5. Plan
  • Repetition (Loops)
  • While
  • Do while
  • For
  • Methods
  • Scope of variables
  • Passing of Parameters
  • Various Examples

3
While
  • While (expression)
  • statement

4
While
  • condition
  • true
  • false
  • B1
  • B2

5
  • import java.io.
  • class Factors
  • public static void main (String args) throws
  • IOException
  • BufferedReader stdinnew BufferedReader
  • (new InputStreamReader(System.in))
  • int num Integer.parseInt(stdin.readLine(
    ))
  • int count1
  • while (countlt(num/2))
  • if ((num count)0)
  • System.out.println(count)
  • countcount1
  • //while
  • // method main
  • //class Factors

6
Do While Statement
  • B1
  • true
  • condition
  • false
  • B2

7
  • import java.io.
  • class Factors
  • public static void main (String args) throws
  • IOException
  • BufferedReader stdinnew BufferedReader
  • (new InputStreamReader(System.in))
  • int num Integer.parseInt(stdin.readLine(
    ))
  • int count1
  • do
  • if ((num count)0)
  • System.out.println(count)
  • countcount1
  • while (countlt(num/2))
  • // method main
  • //class Factors

8
The For Statement
  • for ( initialization test update)
  • for (j1, sum0 jlt5 jj1)
  • sumsumj
  • System.out.println(sum)

9
  • import java.io.
  • class Factors
  • public static void main (String args) throws
  • IOException
  • BufferedReader stdinnew BufferedReader
  • (new InputStreamReader(System.in))
  • int num Integer.parseInt(stdin.readLine(
    ))
  • for (int count1 countltnum/2countcoun
    t1)
  • if ((num count)0)
  • System.out.println(count)
  • //end of for
  • // method main
  • //class Factors

10
  • class piramida
  • final static char star''
  • public static void main (String args)
  • int hight 4, row1, col
  • while (row lt hight)
  • col1
  • while (collt2hight-1)
  • if ((collt(hightrow-1))(colgt
    (hight- row)))
  • System.out.print(star)
  • else
  • System.out.print(" ")
  • colcol1
  • //inner while
  • System.out.println( )
  • rowrow1
  • //while
  • // method main

11
  • class piramida1
  • final static char star''
  • public static void main (String args)
  • int hight 4, row1, col1
  • while (row lt hight)
  • if(collt2hight-1)
  • if ((collt(hightrow-1))(colgt
    (hight-row)))
  • System.out.print(star)
  • else
  • System.out.print(" ")
  • colcol1 //if
  • else
  • col1
  • rowrow1
  • System.out.println( )// else
  • //while
  • // method main
  • // class piramida1

12
Methods
  • Method is a group of statements that are given a
    name.) Procedure or function)
  • Return type method-name (parameter-list)
  • statement list
  • int minimum (int a, int b)

13
  • class Test
  • public static void main (String args)
  • int x5, y6, min1, min2
  • min1minimum(x,y)
  • min2minimum(y,x)
  • System.out.prinln(min1 and min2)
  • // method main
  • public static int minimum (int a,int b)
  • if (agtb)
  • return b
  • else
  • return a
  • // minimum
  • //class Test

14
Passing parameters of primitive types
5
0
5
  • By value

0
min1
min2
6
2000
  • x

5
  • y

1000
6
5
  • b

500
  • a

300
5
6
  • a
  • b

3000
800
15
Piramida
  • Rectangle with
  • A number of rows4 and columns24-1
  • in a fixed row in col1..(4-row) or
    (4row)..(24-1)
  • a char should be a space

16
  • class piramida1
  • final static char star'' //global constant
  • public static void main (String args)
  • printPiramid( )
  • //method main
  • public static void printPiramid( )
  • int hight 4, row1, col1
  • while (row lt hight)
  • if(collt2hight-1)
  • if ((collt(hightrow-1))(colgt
    (hight-row)))
  • System.out.print(star)
  • else
  • System.out.print(" ")
  • colcol1 //if
  • else
  • col1
  • rowrow1
  • System.out.println( )// else

17
  • class piramida1
  • public static void main (String args)
  • int gova6 char star
  • printPiramid(gova,star)
  • //method main
  • public static void printPiramid(int hight, char
    star)
  • int row1, col1
  • while (row lt hight)
  • if(collt2hight-1)
  • if ((collt(hightrow-1))(colgt
    (hight-row)))
  • System.out.print(star)
  • else
  • System.out.print(" ")
  • colcol1 //if
  • else
  • col1
  • rowrow1
  • System.out.println( )// else

18
More complicated example
  • Sin(x)x-x3/3!x5/5!-x7/7!

19
  • import java.io.
  • class SinusCalk
  • public static void main (String args)
    throws IOException
  • BufferedReader stdin new BufferedReader
  • (new InputStreamReader(System.in))
  • System.out.println ("Enter degree")
  • int tempInteger.parseInt
    (stdin.readLine())
  • double atemp3.14/180
  • System.out.println ("Enter the tolerance")
  • double epsDouble.valueOf(stdin.readLine()).do
    ubleValue()
  • printNow(a, temp, eps)
  • //main

20
  • public static void printNow(double x, int temp2,
    double diff)
  • double MySinsinus(x,diff)
  • System.out.println ("MySin("temp2")
    "MySin)
  • System.out.println ("Ready "
    "sin("x")"Math.sin(x))
  • System.out.println ("The tolerance between
    the both
  • functions is "Math.abs(Math.sin(x)-MySin)
    )
  • // printNow

21
  • public static double sinus(double num, double
    tol)
  • double sum0, prev100
  • for(int i1,j0Math.abs(sum-prev)gttolii2,j
    )
  • prevsum
  • if ((j2)0)
  • sum sumhezka(num,i)/factorial(i)
  • else
  • sum sum-hezka(num,i)/factorial(i)
  • //for
  • return sum
  • // end sinus

22
  • public static double hezka(double num, int
    power)
  • double result1
  • for(int i1(iltpower)(powergt0)i)
  • result resultnum
  • return result
  • // end hezka
  • public static long factorial (int n)
  • long result1
  • for(int i1iltnii1)
  • result resulti
  • return result
  • // factorial
  • // class SinusCalc
Write a Comment
User Comments (0)
About PowerShow.com