SOURCE CODE FILES FOR INTRO EXAMPLES ============================================================================== /** * Project Greeter1 * Class Main * * The traditional Hello World program that * displays the message "Hello, World." * No objects are created because the main * method is static. * * @Steve Jost * @6/9/02 */ public class Main { /** * The main method, which is the entry point * for the application. */ public static void main(String args[]) { System.out.println("Hello, World."); } } ============================================================================== /** * Project Greeter2 * Class Main * * The application inputs a name using an * input dialog, then prints a greeting for * that person. * * @Steve Jost * @8/25/02 */ import javax.swing.JOptionPane; public class Main { /** * The main method, which is the entry point * for the application. */ public static void main(String args[]) { String name; name = JOptionPane.showInputDialog("Enter your name."); System.out.println("Hello, " + name + "."); } } // Input in input dialog: Steve // Output: Hello, Steve. ============================================================================== /** * Project VendingMachine * Project Main * * Test the VendingMachine class methods. */ public class Main { public static void main(String[] args) { VendingMachine vm; // Create new VendingMachine object with // 3 candy bars. vm = new VendingMachine(3); // Print status of VendingMachine object. vm.printStatus(); // Deposit 3 quarters (correct amount) and // select candy. Then print status of vending // machine. vm.depositQuarter(); vm.depositQuarter(); vm.depositQuarter(); vm.selectCandy(); vm.printStatus(); // Deposit 5 quarters (too many) and select // candy. Then print the status. vm.depositQuarter(); vm.depositQuarter(); vm.depositQuarter(); vm.depositQuarter(); vm.depositQuarter(); vm.selectCandy(); vm.printStatus(); // Deposit 2 quarters (too few) and select // candy. Then print the status. vm.depositQuarter(); vm.depositQuarter(); vm.selectCandy(); vm.printStatus(); // Deposit 3 quarters (correct amount) and // select candy. Then print the status. vm.depositQuarter(); vm.depositQuarter(); vm.depositQuarter(); vm.selectCandy(); vm.printStatus(); // Deposit 3 quarters (correct amount) and // select candy. Then print the status. vm.depositQuarter(); vm.depositQuarter(); vm.depositQuarter(); vm.selectCandy(); vm.printStatus(); } } // Output in Terminal Window: Number of candy bars = 3 Number of quarters = 0 Candy bar dispensed. Number of candy bars = 2 Number of quarters = 0 Candy bar dispensed. Number of candy bars = 1 Number of quarters = 0 Could not dispense candy bar Number of candy bars = 1 Number of quarters = 2 Candy bar dispensed. Number of candy bars = 0 Number of quarters = 0 Could not dispense candy bar Number of candy bars = 0 Number of quarters = 3 ------------------------------------------------------------------------------ /** * Project VendingMachine * Class VendingMachine * * The VendingMachine is a variation of the class * Vending1 in Chapter 2 of the textbook. * * Assumptions: * (1) The vending machine only dispenses candy bars. * (2) Candy costs $0.75 (3 quarters). * (3) You use the depositQuarter method to enter quarters. * (4) After entering 3 quarters, use the selectCandy * method to get the candy bar. * (5) If you enter too many quarters, you do not get * change back. * (6) If the vending machine is out of candy bars, * you do not get your money back. */ public class VendingMachine { // Instance Variables: // The number of candy bars currently in machine. public int numberCandyBars; // The number of quarters deposited for current // candy bar purchase. public int numberQuarters; // Default NoArg Constructor public VendingMachine() { numberCandyBars = 0; numberQuarters = 0; } public VendingMachine(int n) { numberCandyBars = n; numberQuarters = 0; } public void depositQuarter() { numberQuarters++; } public void selectCandy() { if(numberQuarters >= 3 && numberCandyBars > 0) { System.out.println("Candy bar dispensed."); numberQuarters = 0; numberCandyBars--; } else { System.out.println( "Could not dispense candy bar"); } } public void printStatus() { System.out.println( "Number of candy bars = " + numberCandyBars); System.out.println( "Number of quarters = " + numberQuarters); System.out.println(); } } ============================================================================== /** * Project CoinFlipper * Class Main * * Simulate random flips of a coin and record * the results. * * @Steve Jost * @6/10/02 */ public class Main { public static void main(String[] args) { // Create the Coin object. Coin x = new Coin(); // Alternate between printing current face // and flipping the coin to get a new face. System.out.println("Flip #1: " + x); x.flip(); System.out.println("Flip #2: " + x); x.flip(); System.out.println("Flip #3: " + x); x.flip(); System.out.println("Flip #4: " + x); x.flip(); System.out.println("Flip #5: " + x); } } ------------------------------------------------------------------------------ /** * Project CoinFlipper * Class Coin * * The Coin class keeps track of the face that * is showing ("Head" or "Tail"). It also provides * a flip method for randomly changing the face. * * @Steve Jost * @6/9/02 */ public class Coin { // Keeps track of the face that is showing. private String face; /** * Constructor for objects of class Coin */ public Coin() { // Randomly set the initial value of face. flip(); } /** * The flip method randomly sets the currently * showing face using the library method random. */ public void flip() { if (Math.random() < 0.5) face = "Tail"; else face = "Head"; } /** * Return the currently showing face when * the object is printed. */ public String toString() { return face; } } ============================================================================== /** * Project TempConverter * Class Main * * @author (your name) * @version (a version number or a date) */ public class Main { public static void main(String[] args) { TempConverter x = new TempConverter(); } } ------------------------------------------------------------------------------ /** * Project TempConverter * Class TempConverter * * @Steve Jost * @6/10/02 */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TempConverter extends JFrame { // Declare instance variables. private JPanel panel; private JTextField cel, fahr; private JLabel celLabel, fahrLabel; private JButton button; /** * Constructor of TempConverter class. */ public TempConverter() { // Create control objects. panel = new JPanel(); cel = new JTextField(8); fahr = new JTextField(8); celLabel = new JLabel("Celsius"); fahrLabel = new JLabel("Fahrenheit"); button = new JButton("Convert"); // Add controls to panel. panel.add(cel); panel.add(celLabel); panel.add(fahr); panel.add(fahrLabel); panel.add(button); // Set panel as the content pane. setContentPane(panel); // Add an ActionListener object to the // button. MyActionListener al = new MyActionListener(); button.addActionListener(al); // Set size of the application window. setSize(120, 180); // Make the window visible. show(); } // Inner class that implements the ActionListener // object. Any ActionListener object must implement // an actionPerformed method. private class MyActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { int c, f; c = Integer.parseInt(cel.getText()); f = 9 * c / 5 + 32; fahr.setText(String.valueOf(f)); } } } ==============================================================================