Z OLD Tutorial XX - james-bern/CS136 GitHub Wiki

Get a copy of the Codebase

  • Clone (or download a zip of) this repo.
    • Option A: Open a terminal and run git clone https://github.com/james-bern/CS136.git
    • Option B: Go to this repo's home, click <> Code v and select Download Zip. Save the zipped file to your computer and extract (unzip) it. On Windows, you will need to press, e.g., Extract All and then Extract.

Note: You should now have an (unzipped) folder named CS136 filled with starter code.

  • Move the folder somewhere that's easy to find, e.g., your Desktop.

  • Rename this folder hw00.

    • Option A: Right click the folder in a File Explorer / Finder and select Rename.
    • Option B: Use the terminal.
      • Windows: rename CS136 hw00
      • Mac: mv CS136 hw00

Experiment

  • Make your computer print Goodbye, World. instead.
  • Change class Main to class HelloWorld. How do you build and run now?
  • Put all your code on one line. Does it still work?
  • Try adding the line System.out.println("|-|" + "3" + "|_" + "|_" + "0" + " " + "world.");. Does it work?
  • Try adding the line int i = 4 / 0;. What happens?

Practice opening Terminals and File Explorers / Finders

Open a Terminal from the Desktop

  • Windows: press ⊞ Win; type cmd; press Enter
  • Mac: type ⌘ + Space; type terminal; press Enter

Open a File Explorer / Finder from a Terminal

  • Windows: start .
  • Mac: open .

Open a Terminal from a File Explorer / Finder

  • Windows: Click top ribbon (which says, e.g., This PC > Desktop > ...); type cmd; press Enter
    • Note: If this doesn't work, you may need to click the top ribbon, press Backspace and try again.
  • Mac: Right click a folder; select New Terminal at Folder

Show Hidden Files

  • Mac: (in Finder) ⌘ + Shift + .
  • Windows: press ⊞ Win; type Show hidden files; check all boxes and click Apply

Run DrJava -- Alternate Mac Instructions

* **Mac:**
    * Configure your Terminal to not pop up weird windows.
        * Open a Terminal.
        * `Terminal` -> `Preferences` -> `Profiles` -> `Shell` -> `When the shell exits`; Select `Close if the shell exited cleanly`
        * Close the Terminal.
    * Download [cs136.command](https://drive.google.com/file/d/19A7H5JyzOlsc4ypg5MScmCxAXbUaWjF8/view?usp=drive_link) to your Desktop.
    * Create a file called `runDrJava` that you can use to run DrJava.
        * Open a Terminal.
        * Run the following commands (you can copy and paste them all in at once.)
            ```
            cd ~/Desktop
            echo 'cd "`dirname "$0"`" && java -jar drjava.jar &' > runDrJava
            chmod +x runDrJava
            ```
        * Close the Terminal.
    * Double-click `runDrJava`

Functions

What is a function?

  • A function is a chunk of code you can "call" from somewhere else.
    • When you call the function, you can supply "arguments" to customize its behavior.
    • Here is the anatomy of a function.
      RETURN_TYPE functionName(ARG0_TYPE argumentZero, ARG1_TYPE argumentOne ... ) {
          // function body
      }
      
    • void is a special return type that means a function doesn't return anything
      • void-returning functions do NOT need to have any return statements (though they may)

When should you write a function?

  • When you have written at least two pieces of code that do basically the same thing, you may choose to "pull them out into a function."
    • Sometimes this is a good idea. Sometimes it is not.

Classes / Structs

What is a class?

  • A class is a collection of data that if useful to "pass around together."
    • This data takes the form of member variables (variables that are "members" of that class) aka fields
    • In languages like Java, a class can have member functions (functions that are "members" of that class) aka methods
    • Here is the anatomy of a class.
      class ClassName {
          
      }
      

When should you write a class?

  • When you have some data that is getting annoying to pass around together, you may choose to "pull it out into a class."

static

  • A static member variable or member function, can be used WITHOUT an instance of a class.