program development
play

Program Development The creation of software involves four basic - PDF document

Program Development The creation of software involves four basic activities: Java Program Statements establishing the requirements creating a design implementing the code Selim Aksoy testing the implementation Bilkent


  1. Program Development � The creation of software involves four basic activities: Java Program Statements � establishing the requirements � creating a design � implementing the code Selim Aksoy � testing the implementation Bilkent University � The development process is much more Department of Computer Engineering involved than this, but these are the four basic development activities saksoy@cs.bilkent.edu.tr Fall 2004 CS 111 2 Program Development Conditional Statements � Software requirements specify the tasks a program � A conditional statement lets us choose must accomplish (what to do, not how to do it) which statement will be executed next � A software design specifies how a program will � Therefore they are sometimes called accomplish its requirements selection statements � In object-oriented development, the design establishes the classes, objects, methods, and data that are required � Conditional statements give us the � Implementation is the process of translating a design into source code power to make basic decisions � Almost all important decisions are made during requirements � Java's conditional statements are and design stages � A program should be executed multiple times with � the if statement various input in an attempt to find errors � the if-else statement � Debugging is the process of discovering the causes of � the switch statement problems and fixing them Fall 2004 CS 111 3 Fall 2004 CS 111 4 The if Statement Boolean Expressions � A condition often uses one of Java's equality � The if statement has the following operators or relational operators , which all syntax: return boolean results: The condition m us t be a bool e an e xpr e s s i on. == equal to if i s a Java I t m us t e val uat e t o e i t he r t r ue or f al s e . != not equal to r e s e r ve d wor d < less than > greater than if ( condition ) <= less than or equal to statement ; >= greater than or equal to � Note the difference between the equality operator ( == ) and the assignment operator ( = ) I f t he condition i s t r ue , t he statement i s e xe c ut e d. I f i t i s f al s e , t he statement i s s ki ppe d. Fall 2004 CS 111 5 Fall 2004 CS 111 6

  2. The if-else Statement Example import cs1.Keyboard; � An else clause can be added to an if public class Wages { statement to make an if-else statement //Reads the number of hours worked and calculates wages. public static void main (String[] args) if ( condition ) { final double RATE = 8.25; // regular pay rate final int STANDARD = 40; // standard hours in a work week statement1 ; double pay = 0.0; else System.out.print ("Enter the number of hours worked: "); statement2 ; int hours = Keyboard.readInt(); System.out.println (); � If the condition is true, statement1 is // Pay overtime at "time and a half" executed; if the condition is false, if (hours > STANDARD) pay = STANDARD * RATE + (hours-STANDARD) * (RATE * 1.5); statement2 is executed else pay = hours * RATE; System.out.println ("Gross earnings: " + pay); � One or the other will be executed, but not } } both Fall 2004 CS 111 7 Fall 2004 CS 111 8 Block Statements Example import cs1.Keyboard; � Several statements can be grouped import java.util.Random; public class Guessing together into a block statement { //Plays a simple guessing game with the user. � A block is delimited by braces : { … } public static void main (String[] args) { final int MAX = 10; int answer, guess; � A block statement can be used Random generator = new Random(); wherever a statement is called for by answer = generator.nextInt(MAX) + 1; System.out.print ("I'm thinking of a number between 1 and " the Java syntax + MAX + ". Guess what it is: "); guess = Keyboard.readInt(); � For example, in an if-else statement, if (guess == answer) System.out.println ("You got it! Good guessing!"); the if portion, or the else portion, or else { System.out.println ("That is not correct, sorry."); both, could be block statements System.out.println ("The number was " + answer); } } } Fall 2004 CS 111 9 Fall 2004 CS 111 10 Logical Operators Logical Operators � Boolean expressions can use the following � Conditions can use logical operators to form logical operators : complex expressions ! Logical NOT if (total < MAX+5 && !found) && Logical AND System.out.println ("Processing…"); || Logical OR � Logical operators have precedence � They all take boolean operands and produce relationships among themselves and with boolean results other operators � Logical NOT is a unary operator (it operates on one operand) � all logical operators have lower precedence than � Logical AND and logical OR are binary the relational or arithmetic operators operators (each operates on two operands) � logical NOT has higher precedence than logical AND and logical OR Fall 2004 CS 111 11 Fall 2004 CS 111 12

  3. Short Circuited Operators Comparing Strings � Remember that a character string in Java is � The processing of logical AND and an object logical OR is “short-circuited” � We cannot use the relational operators to compare strings � If the left operand is sufficient to � The equals method can be called with determine the result, the right operand strings to determine if two strings contain is not evaluated exactly the same characters in the same order if (count != 0 && total/count > MAX) � The String class also contains a method System.out.println ("Testing…"); called compareTo to determine if one string � This type of processing must be used comes before another (based on the Unicode carefully character set) Fall 2004 CS 111 13 Fall 2004 CS 111 14 Lexicographic Ordering Comparing Float Values � Because comparing characters and strings is based � We also have to be careful when comparing two floating point values ( float or double ) on a character set, it is called a lexicographic ordering for equality � This is not strictly alphabetical when uppercase and � You should rarely use the equality operator lowercase characters are mixed ( == ) when comparing two floats � For example, the string "Great" comes before the � In many situations, you might consider two string "fantastic" because all of the uppercase floating point numbers to be "close enough" letters come before all of the lowercase letters in even if they aren't exactly equal Unicode � Therefore, to determine the equality of two � Also, short strings come before longer strings with floats, you may want to use the following the same prefix (lexicographically) technique: � Therefore "book" comes before "bookcase" if (Math.abs(f1 - f2) < 0.00001) System.out.println ("Essentially equal."); Fall 2004 CS 111 15 Fall 2004 CS 111 16 More Operators Increment and Decrement � The increment and decrement operators are � To round out our knowledge of Java arithmetic and operate on one operand operators, let's examine a few more � The increment operator ( ++ ) adds one to its � In particular, we will examine operand � the increment and decrement operators � The decrement operator ( -- ) subtracts one � the assignment operators from its operand � The statement count++; is functionally equivalent to count = count + 1; Fall 2004 CS 111 17 Fall 2004 CS 111 18

  4. Increment and Decrement Increment and Decrement � The increment and decrement � When used in a larger expression, the prefix operators can be applied in prefix form and postfix forms have different effects (before the operand) or postfix form � In both cases the variable is incremented (after the operand) (decremented) � When used alone in a statement, the � But the value used in the larger expression prefix and postfix forms are functionally depends on the form used: equivalent. That is, Expr e s s i on O pe r at i on Val ue Us e d i n Expr e s s i on count++; count++ add 1 ol d val ue ++count add 1 ne w val ue is equivalent to count-- s ubt r ac t 1 ol d val ue ++count; --count s ubt r ac t 1 ne w val ue Fall 2004 CS 111 19 Fall 2004 CS 111 20 Assignment Operators Assignment Operators � The behavior of some assignment � There are many assignment operators, operators depends on the types of the including the following: operands O pe r at or Exam pl e Equi val e nt To � If the operands to the += operator are += x += y x = x + y strings, the assignment operator -= x -= y x = x - y performs string concatenation *= x *= y x = x * y � The behavior of an assignment operator /= x /= y x = x / y ( += ) is always consistent with the %= x %= y x = x % y behavior of the "regular" operator ( + ) Fall 2004 CS 111 21 Fall 2004 CS 111 22 Repetition Statements The while Statement � Repetition statements allow us to execute a � The while statement has the following statement multiple times syntax: � Often they are referred to as loops while ( condition ) � Like conditional statements, they are while i s a statement ; controlled by boolean expressions r e s e r ve d wor d � Java has three kinds of repetition statements: � the while loop I f t he condition i s t r ue , t he statement i s e xe c ut e d. The n t he condition i s e val uat e d agai n. � the do loop � the for loop � The programmer should choose the right kind The statement i s e xe c ut e d r e pe at e dl y unt i l t he condition be c om e s f al s e . of loop for the situation Fall 2004 CS 111 23 Fall 2004 CS 111 24

Recommend


More recommend