Logout

Scope Explanation Program

ScopeMain.java
/Users/adelaide/Public/Netbeans - All JSR Projects/Scope Project/src/scopepackage/ScopeMain.java
 1 /*
 2  * To change this template, choose Tools | Templates
 3  * and open the template in the editor.
 4  */
 5 
 6 package scopepackage;
 7 
 8 /**
 9  *
10  * @author John Rayworth
11  */
12 public class ScopeMain {
13 
14     static double f = 8.9;
15     public static void main(String[] args) {
16         System.out.println("Our Program Showing Scope Issues");
17         System.out.println("---------------------------------");
18         System.out.println("");
19 
20         scopeMethodThatWorks();
21         double g = 7.3; // g declared within the main method.
22 
23         scopeMethodWithProblem();
24 
25         double a = 44.4;
26         double b = 66.7;
27 
28         methodShowingSopeAndPrameters(a, b);//a and b are arguments sent to the method.
29         
30         methodWithMultipleBlocks(12, 2);
31 
32 
33 
34     }// g is garbage collected.
35     // a and b are also garbage collected.
36     
37     
38     private static void scopeMethodThatWorks(){
39         double d = 3.2;
40         double e = 4.3;
41         System.out.println("d + e + f = " +  (d + e + f)); //d and e and f are all in scope.
42 
43     }
44 
45     private static void scopeMethodWithProblem(){
46         double h = 34.2;
47         double i = 32.2;
48         System.out.println("h + i + g = " + (h + i + g)); // h and i are in scope, but g is not in scope.
49         //SO THIS WOULD BE AN ERROR.
50     }
51 
52     private static void methodShowingSopeAndPrameters(double aIn, double bIn){//parameters received; the are new variables.
53         double c = 234.2;
54         double d = 4545;
55         System.out.println("a + b + c + d = " + (aIn + bIn + c + d));
56         System.out.println("a + b + c + d = " + (aIn + bIn + c + d + f));
57 
58     }// aIn, bIn, c, and d are all garbage collected.
59     //The way we managed to work with values declared as variables in the main method was by passing
60     //a copy of those values by way of arguments which were received and copied as parameters with
61     //local scope.
62     
63     
64     public static void methodWithMultipleBlocks(int a, int b){
65         
66         
67         if(a < 10){
68             String sentence1 = "stringWithinFirstBlock";
69             for(int i = 0; i < a; i++){
70                 System.out.println("If the first number taken in ia a reasonably small value,"
71                         + "we will print this line and " +  sentence1 + "that many (" + a + ")times.");
72             }
73             
74         }else if(b < 3){
75             for(int i = 0; i < b; i++){
76                 System.out.println("If the second number taken in ia less than 3,"
77                         + "we will print this line and " +  sentence1 + "that many (" + b + ")times.");
78                 //We would say in this case that the variable sentence1 "is out of scope".
79                 //This is becasue sentence1 was not declared in one blocks in which we are trying to use it.
80                 //SO THIS WOULD BE AN ERROR
81             }
82 
83         }else{
84             System.out.println("Both numbers taken in were bigger than this method can handle.");
85             
86         }
87        
88     }
89     
90 
91 
92 }// f is garbage collected
93 
94