 |
|

|
 |

08/25/09 - A Java Program to Draw a House and Writing to the Console (screen)
|
 |
 |
 |
  | A Java Program to Draw a House
|
 |
 |
 |
 |
  | When you run a Java program, it starts in the main method
|
 |
 |
 |
 |
  | Fill out description, author, version comments
|
 |
 |
 |
 |
  | All the steps we did manually last time, we can now do in code
|
 |
 |
 |
 |
  | Printing a message on the screen
|
 |
 |
 |
 |
  | How to print a message to the screen using a text editor and a terminal.
|
 |
 |
 |
 |
  | Note that Java source-code files must end (have an extension) of .java.
|
 |
 |
 |
 |
  | Note that the name of the source-code file (before the .java) must match the name of the class in the file.
|
 |
 |
 |
 |
  | How to print a message to the screen using the BlueJ IDE (integrated development environment).
|
 |
 |
 |
 |
  | BlueJ takes care of naming the file the same as the class.
|
 |
 |
 |
 |
  | BlueJ represents the class files with rectangles and shows interconnections between multiple classes.
|
 |
 |
 |
 |
  | BlueJ lets us see the methods inside a class.
|
 |
 |
 |
 |
  | BlueJ allows us to examine instance variables inside a class.
|
 |
 |
 |
 |
  | BlueJ gives us a debugger so that we can walk through our code line by line to find bugs.
|
 |
 |
 |
 |
  | BlueJ provides a link to online the Java documentation.
|
 |
 |
 |
 |
  | BlueJ makes it easy to create .jar files.
|
 |
 |
 |
 |
  | String literals: "Usually a series of characters enclosed in double quotes."
|
 |
 |
 |
 |
  | System.out.println is the method for displaying messages on the screen.
|
 |
 |
 |
 |
  | Each statement is terminated with a semicolon (;).
|
 |
 |
 |
 |
  | Special characters (escape sequences for newline, tab, quotes, ...)
|
 |
 |
 |
 |
  | Escape sequences are preceded by a backslash \ but count as a single character.
|
 |
 |
 |
 |
  | "\n" produces a new line (carriage return and line feed)
|
 |
 |
 |
 |
  | "\"" produces a double quote (")
|
 |
 |
 |
 |
  | "\\" produces a single backslash (\)
|
 |
 |
 |
 |
  | Naming classes, methods and variables
|
 |
 |
 |
 |
  | Some words are reserved (keywords) by Java and cannot be used to name classes, methods or variables.
|
 |
 |
 |
 |
  | Otherwise, names may begin with a letter or underscore (_) or dollar sign ($).
|
 |
 |
 |
 |
  | The convention is that class names should begin with an uppercase letter and that variable and method names should begin with a lowercase letter. Names formed by combining words should have the first letter of each word in uppercase.
|
 |
 |
 |
 |
  | myVariable, MyClass, myMethod, aLongNameForMyMethod, ...
|
 |
 |
 |
 |
  | Comments - notes to myself (and other readers of my code)
|
 |
 |
 |
 |
  | A single-line comment starts with a double forward-slash //.
|
 |
 |
 |
 |
  | Everything to the right of the // is ignored by the compiler
|
 |
 |
 |
 |
  | A multi-line comment starts out with a forward-slash-asterisk /* and ends with an asterisk-forward-slash */.
|
 |
 |
 |
 |
  | Everything between the /* and the */ is ignored by the compiler and the comment may extend for as many lines as you like.
|
 |
 |
 |
 |
  | 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(" *"); } }
|
 |
 |
 |
 |
  | Read pp. 1-25 of the text.
|
 |
 |
 |
 |
  | Write a complete Java program that prints the following output:
Hello, Chuck! My name is: (fill in your first and last name here). My email address is: (fill in your email address here).
and copy and paste your output as a comment at the bottom of your source file and then copy and paste the code from your source (including the sample output pasted in as a comment) into an email and send it to me (iverson@smccd.edu).
|
 |
 |
 |
 |
  | Write a complete Java program that prints the following output:
////////////////////// || Victory is mine! || \\\\\\\\\\\\\\\\\\\\\\
|
 |
 |
 |
 |
  | Write a complete Java program that prints the following output:
A well-formed Java program has a main method with { and } braces.
A System.out.println statement has ( and ) and usually a String that starts and ends with a " character. (But we type \" instead!)
|
 |
 |
 |
 |
  | Write a complete Java program that prints the following output:
What is the difference between a ' and a "? Or between a " and a \"?
One is what we see when we're typing our program. The other is what appears on the "console."
|
 |
 |
|


 |
 |
 |