Hello World


SaysHello.java

Deitel & Deitel
Before You Begin section (right before Ch 1)
Read 1.4, 1.5, 1.7, 1.8, 1.9, 1.10
Skim rest of Ch 1

Getting NetBeans.
NetBeans Project


Classwork


Part A - create a program

Open NetBeans and create a new project.   If this is the first time using NetBeans, Java with Maven is probably the default, change this to Java with Ant, and accept Java Application. In the process of creating the project:

Now your project should be created and the SaysHello file should open.

At the top of the SaysHello file, add a comment with all group member names (whenever you see an instruction like this while working independently, make up an imaginary second person's information to use).

In your main  (that is, between the curly braces of the main method)  add the following code:

        System.out.println("Hello World");
Run the program  using Run>Run File or Shift+F6.   You should see Hello World printed out in the output below. (You should not need to explicitly compile first, NetBeans recompiles constantly).

 Changing the name of the project. Right click on the project name in the tree at the left and use Rename change the name of the project using today's classwork number and your userid or group number (e.g. cwk00jsmith or cwk01g02, whatever).  Make sure to click the Rename project directory checkbox.

Changing the name of a file. Right click on the SaysHello file in the tree at th

e left and use Refactor>Rename to change the name of the file to HelloWorld.  Note that this changes the class name in the file as well -- the class name must match the name of the file.

Adding new classes, and adding example code to your project.  Right click on the package (just above the HelloWorld.java file) in the tree at the left and add a new Java class VariableExample. Go into this example code link:  VariableExample.java and copy all the text.  Replace the text of the new file you just created in the IDE. 

Note that there is now an error in the first line, because my package name is different from yours.  Netbeans puts a lightbulb in the margin of the code to show you where the problem is.  Click on the lightbulb and you should see an option to Change the package declaration; clicking on it should fix the problem (you can also fix this simply by typing the correct name in the package statement.

Both HelloWorld and VariableExample have main methods, so both can run.  To run the file you are looking at, make sure you use Run File, not just Run.  Run VariableExample, which gives examples of creating variables, doing operations, and printing.


Part B

To the package, add a new Java class named AboutMe

Copy and paste exactly the code from your HelloWorld into AboutMe, replacing what was automatically generated there.

The compiler marks a line with a red dot to indicate there is an error.   Note that the error assumes your class name is right and your file name is wrong, but in our case it is the other way around.  Fix the code so that it will work in the new file.

In AboutMe, replace the statement that prints "Hello World" with separate statements that print greetings using the names of each member of your group (in any assignment with directions like this, if you are working independently, make up information for one imaginary person and use that in addition to your own).

For one of these, use print instead of println, and use '\n' to put a linefeed at the end of the message.

For one of these use print to print the message, then call println with no argument to add a newline after it.


Part C - Short Answer:
Answer these questions in a Word .docx document. (Always turn in short answer questions in the same submission as your project by attaching your Word document in addition to your project zip file.  Do not hide your short answers inside your project zip unless told otherwise.)  You should be able to see the contents of your word document inside blackboard after you submit.

Always answer short answer questions in your own words, as if explaining to a peer.

  1. What advantages does Java get from being both compiled and interpreted?
  2. For end-users of Java programs, which would be better, to speed up the compiler, or to speed up the JVM (Java interpreter)? Why?
  3. [EC*] Is there any part of "public static void main(String[] args)" that you can change and have the program still run normally? (Spacing / new lines / indentation doesn't count)

[EC] (*EC means this part of the assignment is not required, it's a stretch goal if you're looking for something more interesting to try)

Java provides the printf method for formatted printing (covered in detail in the text).  Add the following code to your main

System.out.printf("piece %.0f of %.5f pi %.9f %n", 3.141592653589793238, 3.141592653589793238, 3.141592653589793238); 

and see what it prints.    Based on this, add a line of code to AboutMe that uses printf to print:

For the low, low price of $99.44! (+$4.00 shipping and handling)

where the code uses the numbers 99.4411444 and 4. .

(hint: look veeery carefully at what I wrote in the previous line)


Part D - Turning In

Go to where your project directory was saved by NetBeans.  Right-click the folder icon and click Send To> Compressed (zipped) folder.  A .zip file should be created. 

 In Blackboard, under Classwork, you will find an entry for this classwork (group links have a picture of people, assignment links have a picture of drawing tools).  Click it. (If you ever see a blue Continue button at the right, click it and you should then see the submission page.)

Attach your zip file and submit.  Looking at your submitted assignment, you should see a link to download your project zip file. 

Click the link for your project and download it to your computer, then unzip it. Drag the unzipped directory to the editing area of Netbeans. Your project should open (this will probably mean you now have two copies of your project open).

Using this process you can always go back and get a copy of the work you submitted.    It is your responsibility to check that you have turned in the correct project, correctly zipped, so I would strongly suggest downloading and checking your work every time you submit.