// // Ruth Anderson // In Class Example about Variables // 10/02/06 // // This program is just meant to demonstrate a few things about // variables. I would recommend downloading it and compiling // and running it yourself. Try changing parts of the program // and see how the values of the variables change. // Try out what you have learned about expressions as well. // // I put an excessive number of comments throughout this file // to explain what is happening. You should have a header comment // like this one at the top of your programs, but in general // do not need to write comments to the level I have done here // elsewhere in the program. // // Comments are ignored by the compiler, but are useful to humans // reading your program. They appear in GREEN in Dr. Java. // public class Lect3 { public static void main(String[] args) { // Declare two integer variables int max; int min; // Store the integer value 5 in the variable named max max = 5; // I commented out the line of code below but you can uncomment it // by deleting the // to see how it changes what is printed out. // max = 7 + 3 * 10; // Print out what is currently stored in the variable max System.out.println(max); // Add 6 to what is currently stored in max // // Notice how evaluating the expression on the right requires looking // up the value that is currently stored in max. // Notice that the value of the expression on the right is calculated // BEFORE it is stored back into max. // max = max + 6; // Notice this actually CHANGES what is stored in max System.out.println(max); // REPLACE what is currently stored in max with the value 12 max = 12; System.out.println(max); // Print out the value of an expression using the value store in max // Notice that this does NOT change what is stored in max System.out.println(max + 2 * 5); // What is stored in the variable max now? System.out.println(max); // What do you expect these to print out? System.out.println(max / 7); System.out.println(max % 7); } }