/**********
*
* Lego notes - Tues Nov 12
*
* This week:
* Please read
* chapter 8 , more control structures
* -- skip this one -- chapter 9 , more on int, float, double
* chapter 10 , more practice with classes and methods
*
* For Friday:
*
* Go back over the programs that you've done before in earlier
* assigned exercises, or my examples for (a) and (b).
* (a) Re-write one of the "for" loops as a "do ... while ()" loop.
* (b) Re-write that same loop as a "while (){ ... }" loop.
* (c) Write a program that asks for a word, and repeats it back somehow.
* If the word is wrong, it should print an error message.
* Implement this with a try-catch structure, in which you throw
* your own error like I do in the example below. You get to pick
* what "wrong" means in your program.
*
* For next Tuesday:
*
* Pick one of your Lego robot earlier programming projects,
* one that has more than one file. Convert part of into a package,
* and add the documentation that I describe below. Consider
* breaking it into several classes if that looks appropriate,
* following the conventions described in the book - now that you're
* becoming a better Java progammer...
*
* Depending on how far we get today, I hope to give time on Friday
* for you to work on these in class.
*
*
* The code below has a compile-time bug in it! (Can you find it?)
* The code below also has a run-time bug in it! (Can you find that, too?)
*
************/
class LegoNotesNov12 {
public static void main(String[] args){
// ------------------------------------------------------
// First, here's what a "try", "catch" structure looks like.
try {
int i=0;
int j=2;
int k=j/i;
System.out.println("" + i + "/" + j + "=" + k + ".");
}
catch (Exception e){
// The Exception "e" is a Java object.
// Can you find it in the Java API?
// Can you find other methods besides getMessage() ?
System.out.println( "Oops! "+ e.getMessage() );
}
// ------------------------------------------------------
// Second, here's how you can throw your own exception.
// (Again - look this up in the Java API)
Exception oops = new Exception("*Something* bad happened.");
try {
for (int i=0; i<10; i++){
System.out.println( "i = "+i );
if (i==5){
throw(oops);
}
}
}
catch (Exception e){
System.out.prinln( "Oops! "+ e.getMessage() );
}
// ------------------------------------------------------
// Third, here's the "do...while() structure.
int i=0;
System.out.println("-- do/while -- take 1");
do {
System.out.println(" i = "+i);
i++;
} while (i<10);
// ------------------------------------------------------
// The same with "break" and "continue" keywords.
i=0;
System.out.println("-- do/while -- take 2");
do {
if (i==3) {
continue;
}
if (i==8) {
break;
}
System.out.println(" i = "+i);
i++;
} while (i<10);
// ------------------------------------------------------
// Next ... the "while(){...}" loop. The same only different.
// What's the difference ?
i=0;
while (i<10){
System.out.println(" i = "+i);
i++;
}
// ------------------------------------------------------
// And there's a "switch" statment which you can read about.
// A pretty straightforward way to choose between alternatives.
// ##################################################
// ##################################################
// chapter 10 -- object oriented design
//
// * methods typically have Verb names. (printThePage)
// * classes (objects typically have Noun names. (JimsCar)
//
// Classes can be
// (1) like main(), with no intent to "create" one, or
// (2) like LightSensor, where we do create and use in other files.
//
// Methods often do things like this:
// * construct an object, or convert to another kind of object
// * implement some task
// * access the object data, i.e. getColor() or setValue(x)
// --------
// "this" keyword - refering to the object itself.
// See examples on pg 404 and following.
// -------
// packages in java - a collection of classes
//
// (1) Put the line "package name1.name2.name3;" in each package file
// (2) Move them all into a name1/name2/name3/ directory
// (3) Make sure that files that look for them start above name1/
// by either being in the right place, or by setting CLASSPATH.
// (4) Other java stuff gets it with "import name1.name2.name3.*;"
// Write an example package in class.
// ------
// testing!
//
// Best practice is to write a "class testIt" main() method
// to verify that the pieces of your package behave the
// way you expect. This is *seperate* from the other things
// that actually use it for real.
// ------
// Documentation!
// Your package should have 3 kinds of documentation:
//
// (1) A readme.txt file that explains what the files are
// and gives an overview of what they do.
//
// (2) Some API specs that tell how other programs should use it,
// typically at the top of the files (preferable in javadoc form,
// but any form will do in a pinch.)
//
// (3) Comments within the code explaining how it works.
// Those are called "implementation" notes.
} // main()
} // class LegoNotesNov12
syntax highlighted by Code2HTML, v. 0.9.1