EXAMPLES FOR DAY 1 (Day1.txt file) // Example HelloWorld // Class Greeting // Print the message "Hello, World." when // the static method main executes. public class Greeting { public static void main(String[] args) { System.out.println("Hello, world."); } } ' ******************* // Project JOptionPane // Class Greeting2 // Ask user for name, // then greet that person. import javax.swing.JOptionPane; public class Greeting2 { public static void main(String[] args) { // Declare name. String name; // Ask for name in input box. name = JOptionPane.showInputDialog("Enter your name."); // Show message box with greeting. JOptionPane.showMessageDialog(null, "Hello, " + name + "."); } } ' ******************* // Project Coin // Class CoinTest // Call the Coin constructor and // Flip the coin three times. public class CoinTest { public static void main(String[] args) { Coin coin; int i; coin = new Coin(); System.out.println("Initial value = " + coin); // First flip coin.flip(); System.out.println("Flip 1 = " + coin); // Second flip coin.flip(); System.out.println("Flip 2 = " + coin); // Third flip. coin.flip(); System.out.println("Flip 3 = " + coin); // New line. System.out.println(); } } ' ------------------- // Project Coin // Class Coin // A coin that can be flipped randomly. public class Coin { // private instance variable. private boolean side; // Constructor for Coin. public Coin() { // Flip the coin randomly to // for initial value. flip(); } // Flip the coin randomly. public void flip() { side = (Math.random() >= 0.5); } // Convert a string representation of // the side of the coin to be printed. public String toString() { if (side) return "head"; else return "tail"; } } ' ******************* // Project Applet // Class Bullseye // Display a bullseye target. import java.applet.Applet; import java.awt.*; public class Bullseye extends Applet { private final int MAX_WIDTH = 300; private final int NUM_RINGS = 5; private final int RING_WIDTH = 25; // Paint a bullseye target. public void paint(Graphics page) { int x = 0, y = 0, diameter; setBackground(Color.cyan); diameter = MAX_WIDTH; page.setColor(Color.white); for(int count = 0; count < NUM_RINGS; count++) { // Alternate colors. if(page.getColor() == Color.black) page.setColor(Color.white); else page.setColor(Color.black); // Draw circle. page.fillOval(x, y, diameter, diameter); diameter -= (2 * RING_WIDTH); x += RING_WIDTH; y += RING_WIDTH; } // Draw the red bullseye in the center. page.setColor(Color.red); page.fillOval(x, y, diameter, diameter); } } ' ******************* // Project Controls // Class TestControls public class TestControls { public static void main(String[] args) { Controls frame = new Controls(); } } ' ------------------- // Project Controls // Class Controls // Create an application that displays common // user interface controls. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Controls extends JFrame { // Global instance variables that must be visible // in the inner class ButtonListener. private JLabel label; private JTextField textfield; private JCheckBox checkbox; private JRadioButton radio1, radio2; private JButton button; private ButtonGroup radiogroup; // constuctor public Controls() { // Set frame caption. super("Some User Interface Controls"); // Set the size of the frame to // width = 300 pixels, height = 150. setSize(300, 150); // Set the container and layout manager. Container c = getContentPane(); c.setLayout(new FlowLayout()); // Create controls objects. label = new JLabel("TextField"); textfield = new JTextField(5); radio1 = new JRadioButton("Radio Button 1", true); radio2 = new JRadioButton("Radio Button 2", false); checkbox = new JCheckBox("Check Box", false); button = new JButton("Show Values"); // Create radiogroup object and add radio buttons. radiogroup = new ButtonGroup(); radiogroup.add(radio1); radiogroup.add(radio2); // Add controls to the container. c.add(label); c.add(textfield); c.add(checkbox); c.add(radio1); c.add(radio2); c.add(button); // Create a listener for the button. ButtonListener listener = new ButtonListener(); // Let the listener listen to the button. button.addActionListener(listener); // Show the frame. show(); } // Inner class definition of the listener for the button. private class ButtonListener implements ActionListener { public void actionPerformed (ActionEvent e) { JOptionPane.showMessageDialog(null, "TextField Value = " + textfield.getText() + "\nCheckBox Value = " + checkbox.isSelected() + "\nRadio1 Value = " + radio1.isSelected() + "\nRadio2 Value = " + radio2.isSelected() ); } } } ' ******************* // Project Variables // Class Variables // Demonstrate the builtin datatypes in Java. public class Variables { public static void main(String[] args) { byte w; // byte is a 1 byte integer. short x; // short is a 2 byte integer. int y; // int is a 4 byte integer. long z; // long is an 8 byte integer. w = 127; x = 20429; y = 103733249; z = 1000000000000L; // <-- One trillion. Trailing L // for long integer constant. System.out.println(w + " " + x + " " + y + " " + z); System.out.println(); float s; // float is a 4 byte floating point. double t; // double is an 8 byte floating point. s = 3.546e19F; // Trailing F for float constant. t = 5.42e98; // Default decimal constant is double. System.out.println(s + " " + t); System.out.println(); char letter; // char contains a 2 byte unicode character. // representing an ascii code. letter = 'A'; System.out.println(letter + " " + (int) letter); System.out.println(); String str; // objects in the String class // represent strings of characters. str = "apple"; System.out.println(str); System.out.println(); boolean p, q; // boolean can have values // false or true. p = true; q = false; System.out.println(p + " " + q); System.out.println(); } } ' ******************* // Project Operators // Class Operators public class Operators { public static void main(String[] args) { int x = 7, y = 3, a, b, c, d; boolean f, g, h, k, m, n, p; // Arithmetic operators. a = x + y; b = x - y; c = x * y; d = x / y; System.out.println(a + " " + b + " " + c + " " + d); // Arithmetic assignment operators. a += y; b -= y; c *= y; d /= y; System.out.println(a + " " + b + " " + c + " " + d); // Assignment operator = associates right to left. a = b = c = d = 17; System.out.println(a + " " + b + " " + c + " " + d); // Bitwise operators. x = 5; y = 13; a = x | y; b = x & y; c = x ^ y; d = ~x; System.out.println(a + " " + b + " " + c + " " + d); // Bitwise assignment operators are available also. x = 5; y = 13; x |= y; System.out.println(a + " " + b); // Relational operators. x = 7; y = 3; f = (x < y); g = (x <= 7); h = (x > y); k = (x >= y); m = (x != 3); n = (x == 3); p = !f; System.out.println(f + " " + g + " " + h + " " + k + " " + m + " " + n + " " + p); System.out.println(); // Logical And operator. f = false && false; g = false && true; h = true && false; k = true && true; System.out.println(f + " " + g + " " + h + " " + k); System.out.println(); // Logical Or operator. f = false || false; g = false || true; h = true || false; k = true || true; System.out.println(f + " " + g + " " + h + " " + k); System.out.println(); // Logical Not operator. f = !false; g = !true; System.out.println(f + " " + g); System.out.println(); // Tertiary conditional operator. In this case z will // contain the minimum of x and y. a = 5; b = 9; c = a < b ? a : b; System.out.println(a + " " + b + " " + c); } }