1.00 Tutorial 2 Control Structures and Classes Outline • • • • Take Attendance Uploading Problem Sets Control Structures Simple Classes 1 Uploading… • When using SecureFX, set the protocol to “FTP over SSH2” and not just regular “FTP”, otherwise you will be getting warning emails about insecure connection. • Only upload .java files and not the entire folder or any .class files or any .java~ files. Control Structures • List the control structures you have learn in lecture. 2 Control Structures (cont.) • Lists of control structures: – – – – if … else … for loop while loop do … while loop Control Structure Examples if (a>5) if (a<10) b = 10; else b = 0; else b = -1; When a is 15, what is b? How about when a is 6, what is b? And when a is 0, what is b? 3 A slight change b = 7; if (a>5) { if (a<10) b = 10; } else b = 0; If a=15, what is b? If a=0, what is b? One more change Now, remove the braces… b = 7; if (a>5) if (a<10) b = 10; else b = 0; If a=15, what is b? If a=0, what is b? 4 Answer • Unless braces are used to explicity denote which if clause is associated with an else clause, the else clause is associated with the nearest if clause that precedes it. Readability of Code • if (a>5) if (a<10) b = 10; else b = 0; else b = -1; • b = 7; if (a>5) { if (a<10) b = 10; } else b = 0; (aren’t these easier to read? On Forte, you can do right click on your code and choose reformat code) 5 Loops • How would you write a for loop that calculate the sum of even numbers from 2 to 20? • Can you use a while loop to do the calculation instead? • The three different kinds of loops are interchangeable, but sometimes one is better than the other. Do you know the differences? Logical Operators • Do you know what the follow operators mean? – ==, &&, ||, != • What is the value of this expression: (assuming c = 5, a = 2, b = 10) c!=0 || a >= 10 && b < c*c/a • What will happen when you compile the following code: (assume x is –5) if (x=0) System.out.println(“x is 0!”); else System.out.println(“x is not zero. It is “ + x); 6 Classes • What do classes contain? – Data (what kind of data can they have?) – Methods • Lets write a Rectangle class. – What data fields do you think it should have? – What methods might it have? 7