Getting started with X11 and Linux
During cs230 lab, we'll use the X11 application on the Macs to ssh to puma,
to work with Linux.
How to start x11
From this point on, it is as if you were working on a Linux machine.
Frequently used Linux Commands
- pwd
- ls, ls -l, ls -a, ls --help
- mkdir
- cd
- cp
- rm, rmdir
- man
- emacs filename &
Here's a useful list of Linux Commands.
Frequently used emacs commands
- File Reading/Writing Commands:
- C-x C-s (Save)
- C-x C-c (Save file and Quit Emacs)
- C-x C-w (Save As)
- C-x C-f (Find file)
- Cursor Movement Commands:
- C-f (forward)
- C-b (backward)
- C-n (down one line)
- C-p (up one line)
- C-e (end of line)
- C-a (start of line)
- C-v (forward one word)
- M-b (backward one word)
- Edit text Commands:
- C-d (delete one character)
- C-k (delete from the cursor to the end of the line)
- C-y (yank back what you just deleted with C-k)
- Other handy Commands:
- C-x u (undo)
- C-g (quit the current command)
- M-x goto-line (Moves cursor to specified line number in file)
Problem 0: A bit of Background Information and File Organization
Here's a picture of how to set up your account for the semester:
Items shown in RED: folders automatically created
Items shown in GREEN: folders for CS classes taken already
Items shown in BLUE: folders you will create today and in the future
YELLOW BLOBS: individual accounts
It's a good idea to have a labs directory in your
account. You can put all your labs from the semester in this folder.
Create a labs directory, and then create a subdirectory
named lab1, under your labs directory, and
make it your working directory, as shown below.
Here is how to create a new folder (we use the terms folder and
directory interchangeably here):
- Make sure you are in the place where you want the new folder to be created
(in this case, in your
cs230 folder). pwd helps
you
figure out where you are.
-
mkdir creates a new directory with the supplied name,
e.g. mkdir labs makes a lab folder in your current folder.
-
ls will list the contents of your folder, to make sure
that the folder you just created is indeed there.
-
cd labs changes your current directory to be the labs folder that you just created
-
cd .. changes your directory to be the parent of your current directory (e.g. it moves you up the tree one level)
With each new lab, create a new folder for that lab
and keep all the files inside that folder. You can set up a similar structure for problem sets.
Problem 1: Hello World!
In CS230, you'll download (copy) files using the cp command.
Each time we have a file or folder for you, you'll copy it from the
designated directory (no more Fetch). Let's download the first file that we'll work with today.
cd into your (newly created) lab1 folder.
Download a copy of Hello.java from the
download folder of the cs230 directory.
Here is the command to download Hello.java:
cp /home/cs230/download/Lab1/Hello.java Hello.java
Here is an alternate way to do the same thing:
cp /home/cs230/download/Lab1/Hello.java .
This diagram will help you
understand the command:
Alternatively, you can use Emacs to create your own Hello.java file from scratch. (Typing emacs Hello.java & at the linux prompt will start emacs and create an empty file with the name Hello.java).
The power of the ampersand 
Whenever you use the Emacs editor to create or edit a file, always add an ampersand (&) to the end of your command line, like this:
emacs Yummy.java &
The ampersand allows you to run the emacs window in the background. This allows you to still type commands in your linux shell (e.g. compile and run your java programs) and edit your files without having to quit out of emacs.
|
Edit, compile and run the Java program
- Add your name and the current date in the java comments
- Compile
Hello.java [ javac Hello.java ]
- Run
Hello.java [ java Hello ]
- Modify the existing program to include a couple of additional
println statements in the
main method (print whatever you like)
- Save, compile and run the modified program
- Repeat the above steps many times until you feel comfortable with the cycle
Problem 2: Introduce yourself to linux
The purpose of this task is to give you some practice using Emacs.
Copy the file Introduction.java into your Lab1 folder.
If you compile and run it, you'll see that it prints a template introduction. You need to edit the file with your personal information (you can make it up).
Then save, compile and run the program, and your personal introduction should print to the screen, perhaps something like this:
Prompt User for Information
Instead of using predefined strings to specify the user information, prompt the user for each item:
- Java documentation for the Scanner class.
- The
Scanner class, which is part of the standard Java class library, provides methods for reading input values. You will
need to import the java.util.Scanner class by adding the following statement at the beginning of your file:
import java.util.Scanner;
- In your program, you will create a
Scanner object with the following statement:
Scanner scan = new Scanner(System.in);
- After prompting the user for the information, use the
nextLine() method of the
Scanner class to read into your program a line of text from the standard input, like this:
System.out.print("Enter name:");
name = scan.nextLine();
- Re-compile and re-run your program using the user-provided input. Below is a sample run of the program
that allows interaction with the user:
Problem 3: Become a Fortune Teller!
You can download
Fortune.class from the Lab1 folder in the
cs230 download directory, and run this program like this:
java Fortune
It prints a random fortune to the screen from a pool of five
fortunes. Note that you can only run the program, and not see the
source code, because you have been provided with the
Fortune.class file only, and not the
Fortune.java source file. Write your own version of this
simple program (hint: you need the java.util.* library to
use the random number generator). Here's a link to the java documentation
for the Random class.
Practice submitting your work
Today, we'll practice electronic submission of your program (your work from today will not be graded).
Let's practice submitting your modified
Hello.java
program. Make sure you are still in your lab1 directory (you can verify this by typing pwd in Linux). Then, type:
submit cs230 lab1 Hello.java
The above command, will create a directory named lab1, under /cs230/drop/your_name,
and will copy your Hello.java from the current directory into it.
If you want all java files in your current directory to be submitted, then type:
submit cs230 lab1 *.java
Your instructors can access those files for grading purposes.
|