 |
|

|
 |

08/27/09 - Procedural Decomposition, Data Types and Variables, and Arithmetic Expressions
|
 |
 |
 |
  | Sequential execution of programs
|
 |
 |
 |
 |
  | Program execution begins with the main method.
|
 |
 |
 |
 |
  | The statements in the main are executed in the order they are listed from top to bottom.
|
 |
 |
 |
 |
  | Generally, a different order of execution will produce a different result.
|
 |
 |
 |
 |
  | For example, when you get dressed in the morning, you usually put your socks on before your shoes. If you change the order and put your shoes on first, you'll stretch your socks and wear them out faster.
|
 |
 |
 |
 |
  | If you take a shower and then get dressed, you'll have a different outcome than if you switched the order of those two actions.
|
 |
 |
 |
 |
  | Can you write the source code for the program which produces the following output?
|
 |
 |
 |
 |
  | ***** ***** * * * * * ***** ***** * * * * * ***** ***** * * * ***** ***** * * * * *
|
 |
 |
 |
 |
  | /** * Display a sequence of asterisks. * * @author Chuck Iverson * @version 6/22/09 */ public class Stars { public static void main(String [] args) { System.out.println("*****"); System.out.println("*****"); System.out.println(" * *"); System.out.println(" *"); System.out.println(" * *"); System.out.println("*****"); System.out.println("*****"); System.out.println(" * *"); System.out.println(" *"); System.out.println(" * *"); System.out.println("*****"); System.out.println("*****"); System.out.println(" *"); System.out.println(" *"); System.out.println(" *"); System.out.println("*****"); System.out.println("*****"); System.out.println(" * *"); System.out.println(" *"); System.out.println(" * *"); } }
|
 |
 |
 |
 |
  | Most programs have portions of code which are repeated at different places in the program.
|
 |
 |
 |
 |
  | Generally, repeated code is a bad idea. Whenever a change needs to made to the repeated code, the programmer has to remember to change the code everywhere it's repeated, and it's easy to miss at least one place. This introduces hard-to-find bugs in your program.
|
 |
 |
 |
 |
  | When you have repeated code, it's a good idea to isolate that code in a separate method and call it whenever you need it. When the code needs to be modified, you only need to change it in one place.
|
 |
 |
 |
 |
  | This process of breaking repeated code into isolated methods is referred to as procedural decomposition.
|
 |
 |
 |
 |
  | ModifiedStars.java source code to remove redundancy.
|
 |
 |
 |
 |
  | /** * Display a sequence of asterisks. * * @author Chuck Iverson * @version 6/22/09 */ public class Stars2 { public static void main(String [] args) { fiveStars(); twoOneTwo(); fiveStars(); twoOneTwo(); fiveStars(); threeOnes(); fiveStars(); twoOneTwo(); } public static void fiveStars() { System.out.println("*****"); System.out.println("*****"); } public static void twoOneTwo() { System.out.println(" * *"); System.out.println(" *"); System.out.println(" * *"); } public static void threeOnes() { System.out.println(" *"); System.out.println(" *"); System.out.println(" *"); } }
|
 |
 |
 |
 |
  | More Programming Exercises
|
 |
 |
 |
 |
  | Write a complete Java program that prints the following output. Use at least one static method besides main to help you.
////////////////////// || Victory is mine! || \\\\\\\\\\\\\\\\\\\\\\ || Victory is mine! || \\\\\\\\\\\\\\\\\\\\\\ || Victory is mine! || \\\\\\\\\\\\\\\\\\\\\\ || Victory is mine! || \\\\\\\\\\\\\\\\\\\\\\ || Victory is mine! || \\\\\\\\\\\\\\\\\\\\\\
|
 |
 |
 |
 |
  | Write a program that prints the following line of output 1000 times:
All work and no play makes Jack a dull boy.
Do not write a program whose source code is 1000 lines long; use methods to shorten the program (no loops or recursion for now). What is the shortest program you can write that will produce the 1000 lines of output using only the material from this chapter.
|
 |
 |
 |
 |
  | Primitive Data Types and Variables
|
 |
 |
 |
 |
  | Numbers, characters and booleans
|
 |
 |
 |
 |
  | There are two basic types of numbers in Java
|
 |
 |
 |
 |
  | Integers are positive or negative whole numbers (including 0).
|
 |
 |
 |
 |
  | They have various limited ranges and take up different amounts of memory.
|
 |
 |
 |
 |
  | byte: takes one byte (8-bits) and ranges from -128 to 127
|
 |
 |
 |
 |
  | short: takes two bytes (16-bits) and ranges from -32768 to 32767
|
 |
 |
 |
 |
  | int: takes four bytes (32-bits) and ranges from -2147483648 to 2147483647
|
 |
 |
 |
 |
  | long: takes eight bytes (64-bits) and ranges from -9223372036854775808 to 9223372036854775807
|
 |
 |
 |
 |
  | Floating-point numbers are used when you need numbers with fractions or a larger size than longs.
|
 |
 |
 |
 |
  | Floating-point numbers generally are not exact. This is because some numbers (in any number system) can not be represented in a finite number of digits. For example, 1/3 = 0.3333333... (forever) in the decimal number system. So, if you don't write an infinite number of 3's, you don't get exactly 1/3, just something close.
|
 |
 |
 |
 |
  | A float takes up four bytes and has a range from -3.4x1038 to 3.4x1038. A float has a precision of about 7 digits, which means if you write 1.0f/3.0f (where the f's make the numbers floats), you will get about seven 3's before you start getting other digits (e.g., 0.33333334215...).
|
 |
 |
 |
 |
  | A double (meaning double-precision float) takes up eight bytes and has a range from -1.7x10308 to 1.7x10308. A double has a precision of about 15 digits, which means if you write 1.0/3.0, you will get about fifteen 3's before you start getting other digits (e.g., 0.3333333333333330756...).
|
 |
 |
 |
 |
  | A char is used to store single characters (alphabetic, digits, punctuation, control codes, etc.) in two bytes, and supports Unicode. This means you can store up to 65536 different characters in a char, supporting languages such as Chinese and Japanese which have many more characters than English.
|
 |
 |
 |
 |
  | A boolean data type is used to store true or false results.
|
 |
 |
 |
 |
  | A literal is the actual value of a specific data type. For example, 'A', 'B', '3', and '!' are all literal characters; 234, -5, and 6789 are literal integers; 3.14 and 2.71 are literal doubles; true and false are literal booleans; and "Hello, world!" and "Catch ya later" are literal strings.
|
 |
 |
 |
 |
  | Arithmetic expressions are combinations of numbers with arithmetic operators. Arithmetic expressions evaluate to a numerical value.
|
 |
 |
 |
 |
  | 3 + 4 evaluates to 7. 17 - 3 evaluates to 14. 5 * 8 evaluates to 40. 3*(5+2) evaluates to 21.
|
 |
 |
 |
 |
  | Java has the standard arithmetic operators +, -, *, /. It also has %, the remainder or modulus operator. E.g., 7 % 5 evaluates to 2 because when 7 is divided by 5 the quotient is 1 and there is a remainder of 2. The usual order of operations applies: multiplication and division come first (in order from left to right), then addition and subtraction (in order from left to right).
|
 |
 |
 |
 |
  | Note that an integer divided by an integer results in an integer (any fraction or remainder is discarded).
|
 |
 |
 |
 |
  | A number without a decimal point is assumed to be an int, unless it's too big, and then it's a long.
|
 |
 |
 |
 |
  | A number with a decimal point is assumed to be a double, unless it's followed by a f or F, and then it's a float.
|
 |
 |
 |
 |
  | When an int is added to a double, the int is promoted to a double before the addition, and the result is a double.
|
 |
 |
 |
 |
  | An int can be assigned to a double with no loss in precision.
|
 |
 |
 |
 |
  | However, assigning a double to an int could result in a loss of precision, so a cast is required.
|
 |
 |
 |
 |
  | Variables are declared (memory is allocated for them and a name is associated to them) by typing the name of the data type, then a space, then the name of the variable.
|
 |
 |
 |
 |
  | Variables may also be initialized by assigning them values at the time they are declared.
|
 |
 |
 |
 |
  | Values are assigned to variables by putting the variable on the left side of an = sign and the value to be assigned on the right side.
|
 |
 |
 |
 |
  | An int can be assigned to a double with no loss in precision.
|
 |
 |
 |
 |
  | However, assigning a double to an int could result in a loss of precision, so a cast is required.
|
 |
 |
 |
 |
  | int myInt = (int)3.52; // only the integer part, the 3, is assigned to myInt
|
 |
 |
 |
 |
  | Imagine you are writing a personal fitness program that stores the user's age, gender, height (in feet or meters), and weight (to the nearest pound or kilogram). Declare variables with the appropriate names and types to hold this information.
|
 |
 |
 |
 |
  | Suppose you have an int variable called number. What Java expression produces the last digit of the number (the one's place). What Java expression produces the second-to-last digit of the number (the ten's digit)? What expression produces the third-to-last digit of the number (the hundred's digit)?
|
 |
 |
 |
 |
  | Read pp. 26-77 of the text.
|
 |
 |
 |
 |
  | Write a program that produces as output the lyrics of the song "There Was an Old Lady." Use methods for each verse and the refrain, and isolate repeated lines into methods. Here are the song's complete lyrics:
There was an old lady who swallowed a fly. I don't know why she swallowed the fly, Perhaps she'll die.
There was an old lady who swallowed a spider, That wriggled and jiggled and tickled inside her. She swallowed the spider to catch the fly, I don't know why she swallowed the fly, Perhaps she'll die.
There was an old lady who swallowed a bird, How absurd to swallow a bird. She swallowed the bird to catch the spider, She swallowed the spider to catch the fly, I don't know why she swallowed the fly, Perhaps she'll die.
There was an old lady who swallowed a cat, Imagine that, to swallow a cat. She swallowed the cat to catch the bird, She swallowed the bird to catch the spider, She swallowed the spider to catch the fly, I don't know why she swallowed the fly, Perhaps she'll die.
There was an old lady who swallowed a dog, What a hog, to swallow a dog. She swallowed the dog to catch the cat, She swallowed the cat to catch the bird, She swallowed the bird to catch the spider, She swallowed the spider to catch the fly, I don't know why she swallowed the fly, Perhaps she'll die.
There was an old lady who swallowed a horse, She's dead, of course.
|
 |
 |
|


 |
 |
 |