Csc238

Only available on StudyMode
  • Download(s) : 66
  • Published : July 2, 2012
Open Document
Text Preview
OBJECT ORIENTED PROGRAMMING CSC238

Lecture 3 – Common Algorithms

Lecture 3

Objectives
Common Algorithms
Total T l Average Counting Minimum value and maximum value
Ranged value Unlimited l U li i d value

1

Mohd Hanapi Abdul Latif

Total / Sum group of values
The algorithm in this application may be used to solve sol e problem such as s ch To calculate the total of payment for N employees; To sum the amount that a customer has to pay; To calculate the total of salary that a person receives in a year; and To l l t the total T calculate th t t l payment of ticket f a show. t f ti k t for h

Mohd Hanapi Abdul Latif

Total / Sum group of values
Steps Example – display the sum of 10 prices double price, sumPrice; p , ; sumPrice=0.0; 1. Introduce a variable to store the sum of values and initial it to zero. 2. In a repetition structure, sum the a number.

for (int bil = 0; bil < 10; bil++) { //input and sum value of price price=inp.nextDouble(); sumPrice= sumPrice + price; } System.out.println(“The sum prices is RM: ”+sumPrice); of 10

3. Display or use the summed value after the loop

2

Mohd Hanapi Abdul Latif

Common mistakes
Variable to sum numbers is not initialized Variable to sum numbers is initialized in the loop. p for (int cnt=0; cnt < 10; cnt++) { sumNum = 0.0; System.out.print ("Enter a number: "); no = inp.nextDouble(); sumNum += no; } Incorrect arithmatic expression to sum numbers. for (int cnt=0; cnt < 10; cnt++) { : no = inp.nextDouble(); sumNum = no; : }

Mohd Hanapi Abdul Latif

Counting of occurrence
Counting the occurrence of item such as
the number of odd values, h b f dd l the numbers of all values that the user keys in, the number of male gender, the number of students that have cgpa between 3.00 until 4.00

3

Mohd Hanapi Abdul Latif

Steps

Example – count the number of prices that is higher than RM50.00 g

1) Introduce a variable of type double price; int and initial it to zero. int cntHigh50=0; 2) In a repetition structure, check for (int bil = 0; bil < 10; bil++) { //input value of price for the appropriate condition. price=inp.nextDouble(); if (price > 50.00) cntHigh50 ++; If it met increase the value of met, } variable by 1 3) Display or use the value of System.out.println (“The number of prices that is higher than variable after the loop RM50.00 is “ + cntHigh50);

Mohd Hanapi Abdul Latif

Common mistakes
Confuse between to sum and to count Variable to count is not initialized Variable to count is initialized in the loop. for (int cnt=0; cnt < 10; cnt++) { countHigh = 0; : } Condition is not check for (int cnt 0; cnt < 10; cnt++) cnt=0; { : no = inp.nextDouble(); countHigh++; : }

4

Mohd Hanapi Abdul Latif

Exercise
Write an application to calculate the sum and average a erage of all even n mbers from 20 n mbers that e en numbers numbers the user keys in.

Mohd Hanapi Abdul Latif

Average value
Steps 1) Sum the numbers Example – calculate the average of 10 marks double mark, sumMark=0.0; System.out.println (“Enter 10 marks”); for (int bil = 0; bil < 10; bil++) { mark=inp.nextDouble(); sumMark+= mark; } 1) Calculate the average avg=sumMark/10;

1) Display or use the System.out.println (“The average value is :“ + avg); average value after the loop

5

Mohd Hanapi Abdul Latif

Common mistakes
Expression to calculate average is placed inside the loop. loop for(int ind=0; ind0.0)&&cgpa high) high=cgpa; }//if } //for System.out.println(“The highest value is “ + high); :

Find the minimum value of 10 GCPAs
// assign the maximum value low = 4.0; System.out.println(“Enter 10 CGPAs”); for (int bil = 0; bil < 10; bil++) { cgpa=inp.nextDouble(); if((cgpa>0.0)&&cgpa high) high=cgpa; } //for

7

Mohd Hanapi Abdul Latif

Exercise
Write an application to find the highest value of N values (between -100 to 100) al es 100

Maximum and minimum value – ranged values
// assign the minimum and maximum value high = 0.0; low...
tracking img