# CS61B-Lab 01

## Java, Emacs

Posted by Tianxiang Gao on June 22, 2015

## Part II: Compile-time errors

Running emacs & can run the emacs but cannot access emacs directly. In fact, the emacs is being suspended. In order to access emacs, need to type %emacs.

After that, C-x C-f and type the file whole name to open the file.

The easistest way to compile and run file.java file is typing javac file.java in command line, which complies the java file and create file.class bytecode files. The name of the bytecode file is based on the class in the java file instead of the name of java file.

In the command line, typing java file to really run the program.

The corrected Names.java is attached.

/* Names.java */

import java.io.*;

/** The Names class provides a single function, main, that will
*   perform various manipulations of the name John Fitzgerald Kennedy.
*   This is a modification of the program on page 43 of Arnow and Weiss.
*/

class Names {

/** Performs various string operations on the name John Fitzgerald Kennedy.
*  @param arg is not used.
*/
public static void main(String arg[]) {
String first = "John";
String middle = "Fitzgerald";
String last = "Kennedy";
String initials;
String firstInit, middleInit, lastInit;

firstInit = first.substring(0,1);
middleInit = middle.substring(0,1);
lastInit = last.substring(0,1);
initials = firstInit.concat(middleInit);
initials = initials.concat(lastInit);

System.out.println();
System.out.println(first + " " + middle + " " + last + " ");
System.out.println(initials);
System.out.println(last + ", " + first + " " + middle);
System.out.println(last + ", " + first + " " + middleInit +".");
System.out.println(first.toUpperCase() + " " + last.toUpperCase());

System.out.println(first + " equals john is " + first.equals("john"));
System.out.println(first + " equals john (ignoring case) is "
+ first.equalsIgnoreCase("john"));
System.out.println("The character at index 3 in " + middle + " is " +
middle.substring(3,4));
System.out.println("The index of \"gerald\" within " + middle + " is " +
middle.indexOf("gerald"));
System.out.println("The index of \"gerald\" within " + last + " is " +
last.indexOf("gerald"));

System.out.println();
}
}


## Part IV: Emacs help

Type C-h ? would helpful if not so familiar with the emacs and help facilities.

M-x sort-lines helps sort the documents by SURNAME alphabetically. C-x C-S and “yes” to save and exit Emacs. Emacs is powerful but not so friendly to beginner. In order to save time from digging the help facilities, I just choose Sublime Text 2 in this course.