CSCI111: Examples

CSCI111: Examples

Experiments

Some of these classes will compile and some will not. We need to see why they do and/or why they do not.
If they do not compile, what do we need to do?

Class variables and main

Instance variables and main

public class Test {
   public static double classVariable; 
   int instanceVariable = 9;

	
	public static void main(String [] args){
		System.out.println("Class Variable defaults to " + classVariable + " and is accessible from static method");
		System.out.println("Instance Variable " + instanceVariable + " is not accessible from main method since no instance");
		
	}
}

Instance variables after instantiation in main

public class Test {
   public static double classVariable; 
   int instanceVariable = 9;
	
	public static void main(String [] args){
              Test myExample = new Test();
              System.out.println("Instance Variable " + instanceVariable + " is still not accessible from main method since not accessed through instance");
              System.out.println("Instance Variable " + myExample.instanceVariable + " is accessible from instance");		
	}
}

Passing parameters to main from the command line

public class Test {
   public static double classVariable; 
   int instanceVariable = 9;
	
	public static void main(String [] args){
              if (args.length >0){
                 classVariable = Double.parseDouble(args[0]);
                 if(args[1]!= null)
                    myExample.instanceVariable = Integer.parseInt(args[1]);  // can you fix this problem?
              }
                
              Test myExample = new Test();
              System.out.println("Class Variable " + classVariable + " is accessible from main method");
              System.out.println("Instance Variable = " + myExample.instanceVariable + " is accessible from instance");	
	}
}

Passing an array to another method (in this case args from main method to aother method in the class)

Good object-oriented programming techniques have main only instantiate and start the class. One could even parse the args elsewhere if desired.

public class Test {
   public static double classVariable; 
   int instanceVariable = 9;
	
       public void parseArgs(String [] passedArgs){
              if (args.length > 0){                              // problem here - do you know what it is?
                 classVariable = Double.parseDouble(args[0]);   // problem here - do you know what it is?
                 if(passedArgs[1]!= null)
                    instanceVariable = Integer.parseInt(passedArgs[1]);  // notice do not need instance name anymore
              }
              System.out.println("Class Variable " + classVariable + " is accessible from any method");
              System.out.println("Instance Variable = " + instanceVariable + " is accessible from instance methods in the class");
       }

	public static void main(String [] args){               
              Test myExample = new Test();
              myExample.parseArgs(args);	
	}
}

More on arrays

Primitive data types are passed by value:

public class Test {
   int x = 42;
	
       public void start(){
              x = x + 1;
              System.out.println("x =" + x + " from inside the start method");
              changeMe(x);
       }

       public void changeMe(int newX){
              newX = newX + 1;
              System.out.println("x =" + x + " from inside the changeMe method, and newX =" + newX);
       }


	public static void main(String [] args){             
              Test myExample = new Test();
              myExample.start();	
	}
}

Arrays and all objects in Java are passed by reference. Let's make a whole new example:

public class Test {
   int [] numbers;

   public void start(){   // make an array
      numbers = new int[5];
      for (int i=0; i < numbers.length; i++){
         numbers[i] = i;
         System.out.println("numbers at index " + i + " is " + numbers[i]);
      }
      changeMe(numbers);
   }

   public static void main(String [] args){  // even if you do not have any arguments, you still have to define main this way              
      Test myExample = new Test();
      myExample.start();	
   }

   public void changeMe(int [] newNumbers){
      for(int i=0; i < newNumbers.length; i++){
        newNumbers[i] =   newNumbers[i]+2;
        System.out.println("newNumbers at "+ i+ " is "  + newNumbers[i] + " from inside the changeMe method, and numbers is also = " + numbers[i]);
      }
   }
}