CIS 790             JAVA LESSONS          PROF. AUCIELLO


    1. PROGRAMMING EXERCISE ON CODING AND COMPILING (100)
    2. JAVA QUIZ (to 100)
    3. PROGRAMMING EXERCISE (hwloop4.java)    (100)


MAKE SURE YOU HAVE COMPLETED 'JAVA SURVIVAL TRAINING' LESSONS.
THE JAVA COMPILER, THE WINDOWS EDITORS, AND THE COMMAND PROMPT
ENVIRONMENT NEED TO BE MASTERED IN THE  JST LESSONS.

Quiz on compiling.  Understanding compile errors.
Debugging.  Structure of Java Applets.
Organize the java statemens

(100 points)

// hwloop.jav follows:
// SOURCE CODE BEGINS:
// ============================================================
// step 1.  define all library links.


// step 2.  define a class named 'hwloop' that extends and
//          inherits from java.applet.Applet class.


// step 2.5  open bracket (brackets the class just defined.)

//step 3. define all global variables.
//  define textfields, fonts, name field as string.

 
// step 4.  create an init() method
//          define a method called 'init'

// step 4.a.0
// open bracket for init method.

// Step 4a. Prompt for name.
//          define a new label.
//          add that label to the program.
//          define a text field.
//          add that text field to the program.

// step 4b.
//          test for no (null) name 
//          if no name, then use "Harvey" as a name.

// step 4c.
//          end init method


// step 5.  define a paint method.
//          "paint" writes to the screen.
//          paint() method (displays output)

// step 5.a.0  open bracket for 'paint'

// step 5a.  get an an image. Insert yours if possible.
      Image gifname270 = getImage(getDocumentBase(),"MONICA.GIF");  

// 5b.   set 36 point font.

// 5c.   set background to yellow.

// 5d.   set text to blue.

// 5e.   draw the image defined above at hor 400 and ver 65.
      g.drawImage(gifname270, 400, 65, this);

// 5f.   display the string "Hello, World" at  hor 5, and ver 145.


// 5g.   take the name field from the input above,
//       concatenate (ask about this) it with "from" and "!"
//       and draw it at   hor 5, ver 180.


// 5h.   set font to 24 point.

// 5i.   given.
      g.drawString ("In blue on yellow at 36 points!", 5, 245);

//  bracket ending paint method


//  Step 6. start a method that "listens" when the "enter button"
//          is clicked.
//          It passes (2) arguments -- event and arg. given.
    public boolean action(Event event, Object arg)

//  6.1 open bracket.


//  6.2  command to "get text" and call it "name"

//  6.3  repaint the screen.

//  6.4  end the boolean function with a "return" true.

//  6.5  end event-listening method.

//  6.6  end of applet.


//   PLEASE TRY TO EXTEND THIS PROGRAM,  ADD MUSIC,
//   CHANGE FONTS, COLOR, GRAPHICS.
//   POST THIS APPLET TO YOUR HOME PAGE.
//   SHOW ME WHAT YOU CAN DO.
//  

//   ALSO, WE ARE TRYING TO OPERATE IN A 'LEARNING
//   COMMUNITY' MODE,  A 'TEAM CONCEPT' MODE.
//   EACH OF US HAS UNIQUE SKILLS THAT ARE 
//   STRENGTHENED WHEN SHARED.

=========================

// SOURCE CODE BEGINS:
// ============================================================


      setBackground(Color.yellow);
      g.setColor(Color.blue);
      g.drawImage(gifname270, 400, 65, this);
      g.drawString ("Hello World", 5, 145);
      g.drawString ("from " + name + "! ",5,180);


import java.applet.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.Color;           // color
import java.awt.event.*;
import java.awt.Font;            // font
import java.awt.Graphics;        // graphics library
import java.awt.image.*;         // req for images.
import java.lang.Math;
import java.util.Date;
import java.util.Random;
import java.util.*;

public class hwloop extends java.applet.Applet    // declares class.
{ 

  TextField textField1;          // define textfield1
  Font ftr24 = new Font("TimesRoman", Font.ITALIC, 24);

 if (name == null)
            name = "Harvey";

  Font fss36 = new Font("Sans Serif", Font.BOLD, 36);   // define Font
  String name;
 
  public void init()
   {   
       Label namelabel1 = new Label(" Enter Your Name ",Label.RIGHT);
       add(namelabel1);  
       textField1 = new TextField(15);
       add(textField1);                    // add textfield to structure     
       
                  
        name     = textField1.getText();    // get name
        repaint();

      
   } // end init method

  public void paint(Graphics g)
   {  
      Image gifname270 = getImage(getDocumentBase(),"MONICA.GIF");  

      g.drawString ("In blue on yellow at 36 points!", 5, 245);
   } // end of paint method

    public boolean action(Event event, Object arg)
      {


      g.setFont(fss36);   // setFont, part of the graphics 'library',
                          // now 'linked' with the program.
    
      g.setFont(ftr24);
        return true;
      }  // end event-listenig method.
} // end of applet.


=======================================

Spend 30 minutes on this, if you cant do this,
take this quiz instead:

(100 points)
Use book for answers:

Does a Boolean function (method) always return "true" or "false"?
Explain.


Is a 24 point font larger than a 36?


How do you make a comment in Java?


What is a library?  How do you make a link to a library?


What is a "class"?


What is an "object"?


Is the idea of 'extending a class' connected with
inheritance from the "mother of all classes"?


Is a global variable one that is effective throughout the applet?


If a variable is defined in a method, is it local or global?


Is Case important?  Is Java Case-Sensitive  y or No?

What does  "String name" do?


What does 'void' mean?
 
   
Does the statement 
       Label namelabel1 = new Label(" Enter Your Name ",Label.RIGHT);
define 'namelabel1' at the same time it is being assigned?


Is the "=="  or  the "=" correct for making comparisons?

Does 'name = Harvey'   assign a value to a instance variable?


What does the 'paint' method do?

Following is a two-step instruction --
      Image gifname270 = getImage(getDocumentBase(),"MONICA.GIF");  
Explain it.


Besides yellow and blue, what other names can be used for colors?
Name at least 2.


What does the following statement do?
      g.drawImage(gifname270, 400, 65, this);


Explain this statement:
      g.drawString ("Hello World", 5, 145);


How does 'catenation' work in this statement?
      g.drawString ("from " + name + "! ",5,180);


What does 'event processing' mean?


Is there a method that 'listens' for the keyboard to be struck?


When the enter key is pressed, what happens?                 
        name     = textField1.getText();    // get name


Why is it necessary to repaint the screen?


What do the words -- 'event listening' mean?


In what directory is the java compiler?


What is the name of the java compiler?


Is this correct syntax to compile?

     javac  a:\hwloop.jav           


If the applet is named   UPPERCASE,
will this statement work?

     javac a:\upppercase.java    


If   hwloop compiles successfully,
     name the object that was created.



How is this object executed?


Does Netscape function to execute a  java applet?


Will a browser only read an HTML file?


How do you put a class object in an HTML file?

============


(100 points)


// CIS 790                  QUIZ                PROF. AUCIELLO

// type this program in.
// add comments to every line.


   import java.awt.Graphics;      // req. for app & prog
   import java.awt.*;

public class hwloop4 extends java.applet.Applet // req for app
  { public void paint(Graphics g)   // req for app
     { int red = 20; int x=1; int xxx = 0;
       Font f10 = new Font("Sans Serif", Font.BOLD, 10);   // define Font
       Font f15 = new Font("Monospaced", Font.BOLD, 15);   // define Font
       g.setFont(f10);   // setFont is part of the graphics 'library'
                         // setFont is now 'linked' with the program.
       setBackground(Color.yellow);
       g.setColor(Color.red);
         while (x<11)
           {
            g.setFont(f10);
            g.drawString("This is helping me to understand loops --"+ x + " iteration.",10,red*10);
            repaint();
            red += 1;
            x   += 1;
            while (xxx < 50000)
              {xxx += 1;}
            xxx = 0;
           }  // end while loop.
     }  // end paint method.
 } // end class.



//       Displays 

//       This is helping me to understand loops -- 1 iteration.
//       This is helping me to understand loops -- 2 iteration.
//
//       This is helping me to understand loops -- 10 iteration.