FromScratch

Writing a program
"from scratch"

Part 1:
Creating a project,
writing to the console

CSSE 120
Fundamentals of Software Development I

Rose-Hulman Institute of Technology
Computer Science & Software Engineering

Winter term, 2003-2004

Learning objectives:

After completing this project, you should:
Be able to: Explain the following concepts:
  • What it means to write a program "from scratch"
  • When (and why) software engineers create software "from scratch" and when (and why) they build upon or reuse existing software
  • Writing text and other forms of data:
    • To the console
    • To a popup window
    • To the frame
  • Reading text and other forms of data:
    • From the console
    • From a popup window
    • From the frame
  • What a constructor is
  • The special role of the main method
  • import statements
  • Basic Console application
  • Basic Swing application
Be able to: Do the following in our IDE (JCreator Pro and Tortoise CVS):
  • Create a new Basic Swing Application
  • Create a new Basic Console Application
  • Write text and other forms of data:
    • To the console
    • To a popup window
    • To the frame
  • Read a String from a popup window
  • Parse a String into an int
Items in red are learning objectives for this part of the project.

Instructions:

Individual work

  • Do this project by yourself, to make sure you master the mechanics that it presents.
    • As usual, get help as needed from your instructor and the student assistants.

FromScratch report

  • When you see a question prefaced by
        Question: blah blah blah ...
    put your answer in your FromScratchReport.txt report (more on this shortly).

Time limit

  • Most students complete this exercise in 30 to 60 minutes after seeing their instructor demonstrate the ideas.
  • If you find yourself spending much more time than that on this exercise, please see your instructor.

What you should do:

What you should learn:

Resources:

  1. Create a new project that is a Basic Swing Application whose project name is FromScratch (spelled exactly like that).

  2. Download this FromScratchReport.txt file, placing it in the docs subfolder of the FromScratch project that you just created.

  3. Open your FromScratchReport.txt file.

  • How to create a new Basic Swing Application

  • How to open a project's report

To create a new Basic Swing Application:

  1. Start JCreator
  2. Select File ~ Close Workspace if a workspace is open
  3. Select Project ~ New Project
  4. On the Projects tab of the resulting dialog box:
    • Select Basic Swing Application
    • Type a Project name of your choosing
    • Change the Location of the project if you wish

To open a project's report: Double-click on the FromScratchReport.txt file in the project's docs subfolder.

  • For projects that we create, we will put the report file in the docs subfolder.
  • For projects that you create, you will put the report file in the docs subfolder.

  1. Skim this document. Then return to this point.

  2. Question: About how many minutes do you think it will take you to complete Part 1 of FromScratch?

  3. Question: What time is it now? (Later we will ask you how long you spent on Part 1 of FromScratch.)

  • Begin (by practice) to develop the ability to estimate the time required for developing software
For most software that you develop in this course, you will:
  1. Estimate how long you think it will take you to develop that software
  2. Record how long it actually took you to develop that software
  3. Compare your estimate to the actual time
By using this process, you will (over time) acquire the ability to estimate accurately the time required to develop software per its specifications

  1. The FromScratch.java file has the following structure:
    • import statements
    • FromScratch class that extends JFrame and contains:
      • A constructor
      • A method called main
    Examine the FromScratch.java file and identify each of the above parts.

  2. Examine each of the statements in the FromScratch.java file, in the following order, making sure that you understand what each statement does:
    1. First examine the statements in the main method
    2. Then examine the statements in the FromScratch constructor
    3. Finally, examine import statements at the beginning of the file

  • The basic structure of a Basic Swing Application

  • The ability to read the statements of a Basic Swing Application

  1. Compile and execute (run) your project. Note that it writes (i.e., prints) a message to the console.
    • You shouldn't see any error messages. If you do, please ask an assistant for help.

  2. Add a statement at the end of the main method that writes a message of your own choosing to the console. Compile and run.

  • How to write a String to the console

To write a String to the console, use a statement like:

    System.out.println("Hello World");
but replacing the String literal "Hello World" by a String of your own choosing.

  • This uses the System class that is a part of the built-in Java.lang library

  • The System class has an out object that can be used for writing to the console

    • The out object knows how to println (that is, write a String on a line)

    • The out object also knows how to print (that is, write a String without generating a new line)

  1. Question: The following statement, which appears in main, declares a variable.
    FromScratch mainFrame = new FromScratch();
    
    1. What is the name of the variable declared by the above statement?
    2. What is the type of that variable?
    3. What value is that variable given?

  2. Add a statement inside the FromScratch constructor (NOT in the main method) that writes another message of your own choosing to the console. Compile and run.

  3. Question: At this point, your program writes three messages to the console. Does the message that you just put inside the FromScratch constructor appear first, second, or third in the output? Why?

  4. Comment out the 5 statements that refer to the mainFrame variable, as follows:
    /*
    FromScratch mainFrame = new FromScratch();
    
    mainFrame.setSize(600, 400);
    mainFrame.setTitle("FromScratch");
    mainFrame.setVisible(true);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    */
    
    Compile and run.

  5. Question: Now, does the message that you put inside the FromScratch constructor get printed? Why or why not?

  • How to declare a variable

  • The type / name pattern

  • How to assign a variable a value

  • What code the new operator invokes (i.e., causes to execute)

  1. Uncomment-out the 5 statements that refer to mainFrame. Compile and run.

  2. Question: Without those five statements, the program stops running when it reaches the end of main. But with those five statements, when the program reaches the end of main, does the program stop running? Why or why not?

  3. Continue to practice writing to the console, by putting a multi-line message of your own choosing at the end of main.
    • As always, compile and run to test your code after making any change.

  • Creating persistent objects by using new

  • How to write a multi-line message

To write a multi-line message, either:

  • Use several System.out.println statements (one per desired output line). For example:
        System.out.println("Line 1");
        System.out.println("Another line");
    

  • Embed newline characters '\n' in the String to be printed (at each desired line break). For example:
        System.out.println("Line 1\nAnother line");
    

  1. Create a new CVS module for your FromScratch project, calling the module FromScratch, spelled just like that.

  2. Checkin your FromScratch project, tagging it
        Stage1
    
    spelled just like that.

  3. Close your FromScratch project in the proper fashion.

  • How to create a new CVS module

  • How to checkin a project (update, add contents, commit, tag)

  • How to close a project

How to create a new module by using Tortoise CVS

How to checkin a project (update, add contents, commit and tag) by using Tortoise CVS

To close a project, leaving a clean slate:

  • Window ~ Close All
  • File ~ Save Workspace
  • File ~ Close Workspace

Summary

  1. Question: What JCreator menu, and which item on that menu, do you use to create a new project?

  2. Question: What statement do you use to write a String to the console?

  3. Question: What code would the expression new Blah() cause to execute?

  4. Question: How much time did you spend on Part 1 of FromScratch?

    Compare your answer to how much time you had estimated that you would spend.

    • You receive full credit no matter how far off your estimate is!

  • How to create a new Basic Swing Application

  • How to write a String to the console

  • What code the new operator invokes (i.e., causes to execute)

  • Begin (by practice) to develop the ability to estimate the time required for developing software
For most software that you develop in this course, you will:
  1. Estimate how long you think it will take you to develop that software
  2. Record how long it actually took you to develop that software
  3. Compare your estimate to the actual time
By using this process, you will (over time) acquire the ability to estimate accurately the time required to develop software per its specifications