APCS-AB: Java

1 / 21

# APCS-AB: Java - PowerPoint PPT Presentation

APCS-AB: Java. Control Structures October 17, 2005. If Statements (Review). if ( << conditional >> ) { << body >> } else if ( << conditional >> ) { << body >> } else { << body >> } The << conditional >> can be any true or false conditional A simple boolean like (true)

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## APCS-AB: Java

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### APCS-AB: Java

Control Structures

October 17, 2005

If Statements (Review)

if ( << conditional >> ) {

<< body >>

}

else if ( << conditional >> ) {

<< body >>

}

else {

<< body >>

}

• The << conditional >> 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))

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
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
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 ( << conditional >> ) {

<< body >>

}

Example

boolean keepLooping = true;

while (keepLooping){

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”);

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 ( <starting value>; <conditional>; <update statement>)

{

<< body >>

}

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
Class Exercise
• How would we write a method that would print the numbers between 1 and 100, but only in increments of 10?

### APCS-AB: Java

Control Structures

October 20, 2005

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
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)

### APCS-AB: Java

Java API & Strings

October 21, 2005

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
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/
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
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”;

X

“Jane”

String name

“Jane Dow”

name = name + “ Dow”;

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’;

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.applet java.util
• java.awt java.math
• java.io java.net
• java.lang javax.swing
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.*;

String Project/Schedule
• Codebreaker due Thursday