Table of Contents
Today's topics:
We will go through them in order and one at a time. Work on the first topic for a few minutes, in pairs. Then, share your impresions with the rest of the class. Repeat for the rest of the topics.
/* * A simple class, just to play with some basic Java statements */ public class Example { }
/* * A simple class, just to play with some basic Java statements */ public class Example { public static void main (String[] args) { } }
shortMsg
of type String, with an initial value of "Short Message".
/* * A simple class, just to play with some basic Java statements */ public class Example { public static void main (String[] args) { String shortMsg = "Short Message"; } }
longMsg
of the same type and an initial value of "Looooooooooong message".
/* * A simple class, just to play with some basic Java statements */ public class Example { public static void main (String[] args) { String shortMsg = "Short Message"; String longMsg = "Looooooooooong message"; } }
printShort
, with an initial value of true.
/* * A simple class, just to play with some basic Java statements */ public class Example { public static void main (String[] args) { String shortMsg = "Short Message"; String longMsg = "Looooooooooong message"; boolean printShort = true; } }
counter
, but don't define it:
/* * A simple class, just to play with some basic Java statements */ public class Example { public static void main (String[] args) { String shortMsg = "Short Message"; String longMsg = "Looooooooooong message"; boolean printShort = true; int counter; } }
shortMsg
to the standard output if printShort
is true:
/* * A simple class, just to play with some basic Java statements */ public class Example { public static void main (String[] args) { String shortMsg = "Short Message"; String longMsg = "Looooooooooong message"; boolean printShort = true; int counter; if (printShort) { System.out.println(shortMsg); } } }
printShort
, compile and run.
longMsg
instead, if printShort
is false:
/* * A simple class, just to play with some basic Java statements */ public class Example { public static void main (String[] args) { String shortMsg = "Short Message"; String longMsg = "Looooooooooong message"; boolean printShort = false; int counter; if (printShort) { System.out.println(shortMsg); } else { System.out.println(longMsg); } } }
printShort
to true again. Compile and run.
printShort
:
/* * A simple class, just to play with some basic Java statements */ public class Example { public static void main (String[] args) { String shortMsg = "Short Message"; String longMsg = "Looooooooooong message"; boolean printShort = false; int counter; String alwaysMsg = "This message is always printed"; if (printShort) { System.out.println(shortMsg); } else { System.out.println(longMsg); } System.out.println(alwaysMsg); } }
printShort
to false. Compile and run.
/* * A simple class, just to play with some basic Java statements */ public class Example { public static void main (String[] args) { final int NUM_QUESTIONS = 10; String shortMsg = "Short Message"; String longMsg = "Looooooooooong message"; boolean printShort = false; int counter; String alwaysMsg = "This message is always printed"; String questionMsg = "Are we there yet?"; if (printShort) { System.out.println(shortMsg); } else { System.out.println(longMsg); } System.out.println(alwaysMsg); for (int i = 0; i<NUM_QUESTIONS; i++) { System.out.println(questionMsg); } } }
/* * A simple class, just to play with some basic Java statements */ public class Example { public static void main (String[] args) { final int NUM_QUESTIONS = 5; String shortMsg = "Short Message"; String longMsg = "Looooooooooong message"; boolean printShort = false; int counter; String alwaysMsg = "This message is always printed"; String questionMsg = "Are we there yet?"; String answerMsg = "Nop"; if (printShort) { System.out.println(shortMsg); } else { System.out.println(longMsg); } System.out.println(alwaysMsg); for (int i = 0; i<NUM_QUESTIONS; i++) { System.out.println(questionMsg); System.out.println(answerMsg); } } }
counter
as a counter:
/* * A simple class, just to play with some basic Java statements */ public class Example { public static void main (String[] args) { final int NUM_QUESTIONS = 5; final int NUM_SELFMSG = 7; String shortMsg = "Short Message"; String longMsg = "Looooooooooong message"; boolean printShort = false; int counter; String alwaysMsg = "This message is always printed"; String questionMsg = "Are we there yet?"; String answerMsg = "Nop"; String selfMsg = "I'm going to pass this course."; if (printShort) { System.out.println(shortMsg); } else { System.out.println(longMsg); } System.out.println(alwaysMsg); for (int i = 0; i<NUM_QUESTIONS; i++) { System.out.println(questionMsg); System.out.println(answerMsg); } counter = 0; while (counter < NUM_SELFMSG) { System.out.println(selfMsg); counter++; } } }
Do the following self-evaluation tests: