Logout

Home Topic 4 Last Next

4.1.3

Explain the role of sub-procedures in solving a problem.

 

Teaching Note:

Constructing procedures that can then be referred to by their identifier.

LINK Abstraction, connecting computational thinking and program design, introduction to programming.


 

Sample Question:

sdfsdfsf

JSR Notes:


Sub-procedures In General

In general sub-procedures offer:

We will delve more into the benefits of decomposition and abstraction later on.


Java Methods

The teaching note refers to Java methods and the way we can access Java methods is by calling their name, i.e. their identifier.

Methods are, indeed,"named chunks of code". And so from that point on, when we want to run those lines of code we can identify them, i.e. call them, using their names, i.e. their identifiers. See the programming example below.

 

Non-Programming Example

Taking the classic "getting ready for school" example, we could take all of the major steps and make them into their own sub-procedures, i.e. methods. As follows:

 

Programming Example

15 public class ProjectForTopic4ProgrammingExamples {
16     static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
17     static int numHLCourses = 0;
18     static int numSLCourses = 0;
19     static double avgSLScore = 0.0;
20     static double avgHLScore = 0.0;
21     
22     public static void main(String[] args) throws Exception {
23         //HERE IS WHERE WE CAN SEE THE BREKING OF THE PROGRAM INTO SUB-PROCEDURES
24         initialUserInput();
25         doSL();
26         doHL();
27         finalCalcuationsAndResults();   
28     }  
29     
30     public static void initialUserInput(){
31         System.out.println("How many HL courses are you taking?");
32         numHLCourses = Integer.parseInt(br.readLine());
33         System.out.println("How many SL courses are you taking?");
34         numSLCourses = Integer.parseInt(br.readLine());
35     }
36     
37     public static void doSL() throws Exception{
38         double totalSLScore = 0.0;
39         for(int i = 0; i < numSLCourses; i++){
40             System.out.println("What is the score of your next HL course?");
41             double nextScore = Double.parseDouble(br.readLine());
42             totalSLScore += nextScore;
43         }
44         avgSLScore = totalSLScore/numSLCourses;
45     }
46     public static void doHL() throws Exception{
47         double totalHLScore = 0.0;
48         for(int i = 0; i < numHLCourses; i++){
49             System.out.println("What is the score of your next HL course?");
50             double nextScore = Double.parseDouble(br.readLine());
51             totalHLScore += nextScore;
52         }
53         avgHLScore = totalHLScore/numHLCourses;
54     }
55     
56     public static void finalCalcuationsAndResults(){
57         if(avgHLScore > avgSLScore){
58             System.out.println("Which means you scored on average better for HL.");
59         }
60         else if(avgSLScore > avgHLScore){
61             System.out.println("Which means you scored on average better for SL.");
62         }
63         else{
64             System.out.println("You scored equally well on HL and SL courses.");
65         }
66         System.out.println("Your average HL score is " + avgHLScore);
67         System.out.println("And your average SL score is " + avgSLScore);
68         
69     }
70 }
71