Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week71 APCS-AB: Java Control Structures October 17, 2005.

Similar presentations


Presentation on theme: "Week71 APCS-AB: Java Control Structures October 17, 2005."— Presentation transcript:

1 week71 APCS-AB: Java Control Structures October 17, 2005

2 week72 If Statements (Review) if ( > ) { > } else if ( > ) { > } else { > } The > can be any true or false conditional  A simple boolean like (true)  A check for equality like (x == 5)  A greater than or equal to like (x >= 1)  A combination of the above with && (and), || (or), or another conditional (( x==5 && y == 2) || (z > 42))

3 week73 If/Else Remember, the brackets are technically optional  BUT only if you want to execute ONE statement after the if or else statement if (amount == 0) System.out.println(“okay”); else System.out.println(“nonzero amount”); ____________________________________________ if(amount == 0) amount = 500; System.out.println(“amount equal to 0”); else amount = 200; System.out.println(“amount was not equal to 0”); In this bottom example, both print statements will print, regardless of the value of amount

4 week74 Looping The if/else code structure lets us change the flow of the program, depending on certain conditions Looping always us to easily repeat an action, until a condition has been met  What situations can you imagine in which this would be really helpful? There are two kinds of loops in Java  While they are technically interchangeable, each is syntactically geared to a specific kind of situation

5 week75 While loop While loops logically follow the pattern of “while something is true then perform the following set of actions”  This is useful in a situation in which you don’t know how many times you need to do something, but you know what the end result needs to be The syntax is simple: while ( > ) { > }

6 week76 Example boolean keepLooping = true; while (keepLooping){ printMenu(); int choice = getUserInput(); if(choice == 0){ // 0 is the “exit” choice keepLooping = false; } else{ System.out.println(“Good choice”); // do other stuff } System.out.println(“Thanks for playing”);

7 week77 For Loops We use for loops when we want to do a set of statements a predetermined number of times The syntax for a for loop is: for ( ; ; ) { > } for (int x = 0; x < 10; x++) { System.out.println(“x is: “ + x); } The conditional is the same as it is in a while loop The update statement is optional, but usually is used to increment or change the looping variable

8 week78 Class Exercise How would we write a method that would print the numbers between 1 and 100, but only in increments of 10?

9 week79 APCS-AB: Java Control Structures October 20, 2005

10 week710 Switch Statement The someValue needs to be an int or a char  If no case value is matched, then the optional default case is executed -- but it’s a good idea to always have the default case even if you don’t expect to use it int someValue; // someValue gets a value switch ( someValue ) { case 0: //do something break; case 1: //do something break; default: //do something } char someValue; // someValue gets a character switch ( someValue ) { case ‘A’: //do something break; case ‘B’: //do something break; default: //do something }

11 week711 Schedule Today: Work on finishing loop lab in class Homework: Mini-project first, loop lab if there is time Friday - quiz postponed; String Manipulation lecture Monday: Work Day Tuesday: Programming Quiz (One problem to solve, replaces Friday Quiz) Wednesday: Work Day/ Review Thursday: Cumulative Java Quiz (Written)

12 week712 APCS-AB: Java Java API & Strings October 21, 2005

13 week713 Checkpoint Loop Lab  How many of the tasks have you completed? Graphics Mini-Project  Due today, extensions (one free late, or 10% each day late) count weekend days, so get it to me over the weekend if you can

14 week714 Java API API = application programming interface In Java, it is the list of all the classes available, with details about the constructors, methods, and usually a description of how to use the class I had you download the full API to your computers at home, there is also a scaled down version that only has the methods and classes that are used for the APCS test  That is available online at: http://www.cs.duke.edu/csed/ap/subset/doc/

15 week715 Why this is Cool There is so much code in Java that is already written for you - you just have to  Know that it is out there  Figure out how to use it The API gives a standard way to look at classes and methods so that any Java programmer can understand how to use a class without having to see the code

16 week716 String Class (APCS subset)

17 week717 Strings are immutable Once a string is created, it cannot change So string methods always return new strings -- that way you can just change the pointer String name = “Jane”; String name “Jane” “Jane Dow” X name = name + “ Dow”;

18 week718 Other String Methods (Java API) In addition to what the AP people think you need to know, there are some other cool String methods  boolean equalsIgnoreCase(String str)  String replace (char oldChar, char newChar)  boolean endsWith (String suffix)  boolean startsWith (String prefix)  String toUpperCase()  String toLowerCase()  String concat(String str)  String trim() //takes off white space from front & back Note: to make a char: char ch = ‘A’;

19 week719 Java Packages All Java classes are grouped into libraries (or packages)  String is part of the java.lang package, which is pre-loaded when you are programming in Java  We’ve already seen one other library, the java.util library, where Scanner is Some of the other standard Java Libraries:  java.appletjava.util  java.awtjava.math  java.iojava.net  java.lang javax.swing

20 week720 Using Packages Everything in java.lang is available for use  So it’s as if somebody already did: import java.lang.*; To use other packages, we need to import either the specific class or the entire package (just like we did for Scanner class)  To import a class we use the whole package name: import java.util.Scanner; import java.io.File;  To import an entire library we use the asterisk: import java.util.*; import java.io.*;

21 week721 String Project/Schedule Codebreaker due Thursday


Download ppt "Week71 APCS-AB: Java Control Structures October 17, 2005."

Similar presentations


Ads by Google