Tutorial 2 (Jan 21): Basic Programming Exercises
Department Announcement: Extended Tutorial for Unix and Computer Science Skills
Topics: UNIX tutorials, Class examples, student questions during tutorial
The department is offering the following tutorial sessions (1pm - MS 239):
- Intro Unix Command Line: Jan 22 & 25
- Advanced Unix Command Line (SSH!): Jan 20 & 27
- Debugging: Jan 29
- Version Control: Feb 1
- Regular Expressions: Feb 3
- Bash Scripting: Feb 5
There is an additional tutorial presented by Jennifer Lee about the CPSC Library on Thurs, Jan 21 at 2pm - MS 239
For more details on topics covered, refer to this webpage (!!! the dates at this link are not updated, so refer to the dates I posted above !!!).
Download Handout
Download Code for Example 1
Download Code for Example 2
Example 1: Output, math
Page 38 of your Savitch textbook, FirstProgram.java
- Enter code, compile, and run program
- Change the program to print out other messages.
- Change the program so it does different calculations.
- Change the program so that it prints out the name of the operating system. Check the Java documentation for the System class!
Example 2: Basic Input and Output
- Look up the Scanner class in the Java API documentation
- Write a short program in main() of some class that prompts the user for some input, gets that input, and then echoes the input onto the screen.
- use standard I/O (System.out.println() and Scanner class -> Chapter 2 of Savitch text)
import java.util.Scanner;
Scanner scan = new Scanner(System.in);
scan.nextLine(); //returns a String
scan.nextInt(); //returns an Int
Questions from students:
What is the "script" for Assignment 1?
We want to see the compilation and sample run of your code. Similar to...
javac Grades.java
java Grades
CPSC
0
50
100
-1
Copy EVERYTHING from when you compile your program to when it ends, including all the output from your program. Save it in a text file called script.txt and include it in your submission on D2L.
How can I open a single .JAVA file and run it in Eclipse?
Unfortunately, you must have a project to run your code. When you create a new program, make sure you are creating a new project, before you add code to it (by creating new classes). Your Eclipse will open the default workspace you setup the first time you ran the program. It will automatically load all the projects in there. If you switched workspaces, you will not see the projects anymore. You can either switch back to the original workspace (File > switch workspace) or you can create a new project and manually import the code:
- Create a project
- Right-click the src folder under your new project (on the left side, under Package Explorer)
- Select "Import..." and pick General > File System in the popup
- Click Next, and select the .JAVA file you want to add
- NOTE: If you see an "x" over the file you just added, it probably means your filename doesn't match the class name you specified in your code. Remember, it has to be the same!
If I'm using SSH to remote into the school server, how can I run programs?
If you run what's called a "console application", which is what most of our examples have been so far, it will be okay. If you run an applet like the Welcome program from the first tutorial, it will not work. SSH does not support graphics. You should actually install Java on your own computer and run the program locally. Then you can just come into the labs to test it once.
If I'm using SSH to remote into the school server, how can I upload files?
You usually cannot upload files directly through SSH. What you need is called SCP, or "secure copy". Linux has a tool called SCP, for Windows you can follow the instructions in Tutorial 1 to install WinSCP. WinSCP lets you drag-and-drop files from your computer to the server.
If I'm not from this tutorial, can I still attend it?
I personally do not mind, as long as there are enough computers or you bring your own. Please note that your own assigned TA will be marking your assignments, so their expectations may be slightly different.