Color Codes:
Black = Instructions / Notes / Comments / Feedback
Green = Quizzes /Formative or Summative Assessments
Red = Student Tasks / Required Action
Blue = Connect Past Knowledge / Buildng New Knowledge
Orange = Motivation
Lesson Overview:
In this lesson, you will be emerged into the world of Java programming where you will create, compile, and execute a simple Java program. As you will experience, the Java system is a collection of applications that enable you to create many types of different programs. Once you have mastered the collection of applications, you will be able to successfully create: websites, games, chat clients, and several of our exciting applications that can be run both on computers and mobile phones. It truly is a sensational time to be a programmer!
DoDEA Standards:
PT-PSD1d: The student will demonstrate the effective use of tools for software development in order to:
PT-PSD1d.1: apply tools for developing software applications; and
PT-PSD1d.2: apply language-specific programming tools/techniques
Big Ideas:
The lesson is designed to uphold a long held tradition where people learning a new programming language usually create a program called "Hello World" as their first program. This program displays the words "Hello World" on a computer screen. So, since you are embarking on learning a new programming language, it is fitting that you too honor the tradition by being introduced to your first Java program.
Notwithstanding, this lesson will also provide insight to the process used to create, compile, and execute Java programs. You will gain a basic understanding how to use the BlueJ editor together with the rules governing syntax, spacing, indenting, keywords, arguments, and the like to create the program.
Next you will compile the Java program, formatting your program in a manner that your computer understands, somewhat like a translator. Thereafter, you will actually run your program on your computer, hopefully getting the desired result, (e.g., seeing "Hello World" on your computer screen).
Objectives:
At the completion of the lesson the student should:
have created a basic Java program which will print “Hello World”.
be aware of style concepts like indenting code at beginnings of structures and commenting.
be able to print statements with the program code.
Note to Students (Feedback)
I am here for you. My job is to be responsive to you as you learn to program in Java. Therefore, if you are having difficulties understanding this lesson, its assignments, and or just have questions. Please PM me. That is why I am here. No question is ever to small.
Also, I will continuously be providing you feedback on the work you have submitted. Please do not take anything personal as it is just constructive feedback, hopefully to help guide you through this course.
Again, I am here to help you! So, please feel free to 'BUG' me all you want. (Get it BUG me!)
Warmest Regards,
Coach Rivera
Interesting Factoid (Gaining Student's Attention)
You may be asking yourself why spend the time learning a programming language like Java. Well, for one, those who become proficient computer programmers will always be in demand, especially in this rather weak economy where unemployment continues to plague many Americans who lack technology skills like Java programming. Secondly, those who master Java programming will play a major role in transforming the world that we live in. These are truly exciting times on the horizon.
Read the following article and write me a quick response and submit it to me via Blackboard. I want to know what your thoughts are on the following article:
High Demand: The Occupational Outlook for Developers
Connecting the Dots (Prior Learning)
In our previous lesson, we installed BlueJ and the Java SDK, preparing ourselves to begin programming in Java. It is imperative that we have correctly installed the IDE BlueJ together with the appropriate Java SE Development Kit for your respective computer operating system.
So, let's review the process by viewing the following YouTube video. After viewing the video, check to make sure you have correctly installed BlueJ and the appropriate JDK.
Install BlueJ and JDK for Windows 7
Test your configuration by clicking on the BlueJ icon on your Desktop, opening the BlueJ program. Does it work? You should see the following screen:
Quick Quiz 1(Formative Assessment)
How did you know which Java SDK you need to install on your computer?
(HINT: Control Panel -> Device Manager)
What does SDK stand for?
What does IDE mean? Name a few of them.
Turn in your answers electronically, using Blackboard.
The Hello World Java Program (Guided Practice)
1. Log onto the computer and open BlueJ by double-clicking on BlueJ icon on your Desktop.
2. Once BlueJ has started, click on "Project", then choose "New Project".
3. In the "File name" box, type "Java Projects", then click the "Create" button. You then should see the following:
4. Double-click on the paper icon and close it for this activity.
(We will discuss how to document your programs later on.)
5. Click on the button "New Class", entering HelloWorld in the "Class Name" dialogue box and click "OK".
6. You should see the following project window, representing the class HelloWorld.
7. Double-click on it and you will see that BlueJ has written some Java code for you. For now, highlight all the code and delete it by clicking the "Cut" button.
8. You should now see the following project screen with no Java code.
9. Enter the following Java code for HelloWorld exactly.
class HelloWorld
{
public static void main( String[] args )
{
System.out.println( "Hello, world." );
}
}
You should see the following Java project screen:
10. Click the "Compile" button, and wait for it to display either an error message or that the text "Class compiled - no syntax errors" at the bottom of the HelloWorld window. If there are any errors, fix them and recompile.
11. Once your class has compiled correctly, right-click on the HelloWorld class rectangle and choose "void main(args)". By doing so, it sends a message to the class to run the method called main. When the "BlueJ: Method Call" window pops up, just click "OK" to send no arguments to the method.
12. Once you see the screen below, click "OK".
13. If all went successfully, the BlueJ Terminal Window should pop up and you should see the text "Hello world!" inside it. Congratulations, you have earned your right of passage into the world of Java programmers! Totally awesome!
Quick Quiz 2 (Formative Assessment)
What do "/*" and "*/" do?
What is a class?
What is a method?
Turn in your answers electronically, using Blackboard.
Part A - Creating a SketchPad (Independent Practice)
1. Using the BlueJ and the Hello World program you created, change the first appearance of the word "println" to "print". What happens?
2. Close the "Terminal Window" and the "Hello World" Java code window. Exit BlueJ by clicking on the "Project" menu, then clicking "Quit."
3. Create, compile, and execute the following Java program:
class FunTime
{
public static void main( String[] args )
{
SketchPad doodleBoard; // Declare SketchPad object named doodleBoard
doodleBoard = new SketchPad(); // Create the SketchPad object
doodleBoard.setVisible(true); // Make doodleBoard visible
doodleBoard.toFront(); // Make doodleBoard appear in front
}
}
If you are successful, you should see a SketchPad window in front of you. Try it out. When you are done, close the window to end the program.
Quick Quiz 3 (Formative Assessment)
What does "//" do? How is it different from "/*" and "*/"?
What is a SketchPad object?
Explain the Declare function and give an example.
Turn in your answers electronically, using Blackboard.
Part B - Creating an Applet (Independent Practice)
Note: An applet is a program that runs within a Web page. Good news! BlueJ creates the Web page for us, allowing the applet that you create to work. In this case, it will display a simple drawing.
1. Create a Project. Choose Project --> New Project.... For File name: type in DrawingApplet (as one word, no spaces) and click Create. (If you like, you can first close the earlier project by choosing Project --> Close, but this isn't necessary).
2. Create a class. Click the New Class... button, type in Drawing for the class name, select Applet, and click Ok. You should see the familiar orange box with blue stripes appear.
3. Edit the new class. Either double-click on the orange box, or right-click on it and choose Open Editor. Once again you will get some "starter code," but it's a lot more complicated than for an application. This time, just select all the text and hit the Delete key on your keyboard--we'll type in everything from scratch.
4. Here's the code. Copy and paste it into BlueJ. But if you decide to type it in, be very careful of capitalization.
import java.awt.*; import javax.swing.*; public class Drawing extends JApplet { public void paint(Graphics g) { g.setColor(Color.BLACK); g.drawRect(10, 10, 80, 80); // x, y, width, height g.setColor(Color.BLUE); g.fillRect(110, 10, 80, 80); g.setColor(Color.DARK_GRAY); g.drawOval(210, 10, 80, 80); g.setColor(Color.CYAN); g.fillOval(310, 10, 80, 80); g.setColor(Color.LIGHT_GRAY); // startAngle, degrees g.drawArc(10, 110, 80, 80, 0, 45); g.setColor(Color.MAGENTA); g.fillArc(110, 110, 80, 80, 45, 90); g.setColor(Color.ORANGE); // x, y, width, height, arcWidth, arcHeight g.drawRoundRect(210, 110, 80, 80, 40, 40); g.setColor(Color.PINK); g.fillRoundRect(310, 110, 80, 80, 20, 60); g.setColor(Color.RED); // startX, startY, endX, endY g.drawLine(10, 210, 90, 290); g.drawString("ABC123", 110, 210 + 50); // Now for something more complicated g.setColor(Color.BLACK); g.drawPolygon(new int[] { 250, 290, 210 }, new int[] { 210, 290, 290 }, 3); g.setColor(Color.YELLOW); g.fillPolygon(new int[] { 320, 350, 380, 310, 390 }, new int[] { 290, 210, 290, 240, 240 }, 5); } }
5. Compile the code. Click either the Compile button in the editor window or the Compile button in the main BlueJ window. Fix any errors.
6. Execute the applet. Right-click on the orange box and choose Run Applet. In the dialog box that appears, make sure Run Applet in appletviewer is checked, enter 300 for Height and 400 for Width, and click Ok. If all goes well, you should get a variety of figures, in a 3 x 4 grid.
7. Review the following explanation of the commands that were used in the drawing applet used in Part A.
g.setColor(color)
Use the specified color for subsequent drawing commands.
g.drawRect(x, y, width, height)
Draw the outline of a rectangle starting at x, y, with the given width and height.
g.fillRect(x, y, width, height)
Draw a solid rectangle starting at x, y, with the given width and height.
g.drawOval(x, y, width, height)
Draw the outline of a circle included in the rectangle starting at x, y, with the given width and height.
g.fillOval(x, y, width, height)
Draw a solid circle included in the rectangle starting at x, y, with the given width and height.
g.drawArc(x, y, width, height, startAngle, degrees)
Draw an arc of a circle whose center is the center of the circle included in the rectangle starting at x, y, with the given width and height. The arc starts at startAngle (0 is the 3 o'clock position) and continues counterclockwise for the specified number of degrees.
g.fillArc(x, y, width, height, startAngle, degrees)
Draw a sector (wedge) of a circle whose center is the center of the circle included in the rectangle starting at x, y, with the given width and height. The sector starts at startAngle (0 is the 3 o'clock position) and continues counterclockwise for the specified number of degrees.
g.drawRoundRect(x, y, width, height, arcWidth, arcHeight)
Draw the outline of a rectangle with rounded corners starting at x, y, with the given width and height. The amount of "roundedness" is given by arcWidth and arcHeight.
g.fillRoundRect(x, y, width, height, arcWidth, arcHeight)
Draw a solid rectangle with rounded corners starting at x, y, with the given width and height. The amount of "roundedness" is given by arcWidth and arcHeight.
g.drawLine(x1, y1, x2, y2)
Draw a line from the point (x1, y1) to the point (x2, y2).
g.drawString("Some quoted string", x, y)
Draws the characters inside quotes starting at the position (x, y). Unlike the other methods, the point (x, y) is the bottom left corner of the first letter.
Draws the outline of a polygon. The sequence of points is given by the n corresponding values in xArray and yArray. For example, the command
g.drawPolygon(new int[] { 250, 290, 210 },
new int[] { 210, 290, 290 }, 3);
will draw a triangle using the 3 points (250, 210), (290, 290), and (210, 290).
Draws a solid polygon. The sequence of points is given by the n corresponding values in xArray and yArray.When the edges of the polygon cross, some areas may be considered to be "outside" the polygon, hence not colored in.
g.drawPolygon(xArray, yArray, n)
g.fillPolygon(xArray, yArray, n)
Part C - Creating Your Own Applet or Program (Differentiated Product / Summative Assessment)
For Part C, you will either create your own applet based on the Java code previously provided Part, modifiying it as you wish.
Or you will create your own Java program that reflects your interest.
If you are going to create your own applet based on the Java code previously provided, follow the instructions for Option 1.
If you are going to create your Java program, follow the instructions for Option 2.
Option 1
1. Create a third project named MyDrawingProject, and create an applet class called MyDrawing. Use the following code to get started:
import java.awt.*; import javax.swing.*; public class MyDrawing extends JApplet { public void paint(Graphics g) { // Replace this line with your code } }
2. Analysis the applet code in Part A, then try creating a drawing of your own.
Draw something simply like a person, a house, a car, and the like. By doing so, it will help if you understand the x-y coordinate system as described briefly below.
Remember, you don't have to understand everything. You should be able to do this assignment by imitating the applet in created in Part B, copying lines and changing numbers as necessary.
Option 2
1. Search the internet to find simply Java programs that fall in line with your interest. Once you have found something that you would like to tinker with, modify and adopt it as you see fit.
2. Use BlueJ to create a program called MyProgram, compile it, and execute the program.
Assignments (Formative Assessment)
1. Work through Parts A and B. You don't need to turn anything in.
2. Turn in only Part C, MyDrawing or MyProgram.
For this assignment, you only need to turn in the one file, which should be named MyDrawing.java or MyProgram.java
Turn in your program electronically, using Blackboard
Skill Building
X-Y Coorindate System
The variable g is the "Graphics" that you are drawing on, and is a part of every drawing command.
The named colors you can use are RED, ORANGE, YELLOW, GREEN, CYAN, BLUE, PINK, MAGENTA, BLACK, DARK_GRAY, GRAY, LIGHT_GRAY, and WHITE. (You can create other colors, but these are the only ones that have names.)
Java uses an X-Y coordinate system, where the units are pixels (picture elements, usually about 72 pixels per inch). X=0, Y=0 is the top left corner of the applet window. If the applet window is 400 pixels wide and 300 pixels high (as in the previous applet), then the bottom right corner is X=400, y=300.
Most of the drawing commands take place within a rectangle defined by (x, y, width, height), where x is the number of pixels in from the left edge and y is the number of pixels down from the top edge. Values outside the applet window are legal, but are not drawn on the screen.
For a more complete discussion of the x-y coordinate, take your browser to:
http://java.sun.com/j2se/1.5.0/docs/api/, click on java.awt in the upper left pane, then click on Graphics in the lower left pane. The large pane on the right will show detailed explanations of these and other commands.)
Lesson Summary
In this lesson, you were able to demonstrate the effective use of tools for software development so as to apply language-specific programming tools/techniques to develop simple programs.
You became familiar with an IDE called BlueJ where you created a basic Java program which will print “Hello World”. Additionally, you became be aware of style concepts like indenting code at beginning of structures and commenting. Lastly, you were able to print statements with the program code.
Lesson Formative Assessment
Write a two page, double-spaced paper on the pros and cons of using BlueJ while contrasting BlueJ with other popular IDEs. Please cite your references.
Turn in this paper in electrically through Blackboard.
Additional Resources: