.emacs example.
~/.emacs. Enjoy coding,
debugging, and even reading email from emacs.
Integer2Int.java
using your favorite editor as below:
public class Integer2Int {
public static void main(String[] args) {
Integer i = new Integer(1);
System.out.println(i.intValue());
}
}
/usr/csshare/bin/javac. So, add the path
/usr/csshare/bin to your PATH environment
variable if you haven't done so.
Integer2Int.java, run:
$ javac Integer2Int.java
$ java Integer2Int
gcj to compile the Java into a native executable.
$ gcj --main=Integer2Int Integer2Int.java -o Integer2Int
$ Integer2Int
ConsoleIO.java
import java.io.Console;
class ConsoleIO {
public static void main(String[] args) {
Console c = System.console();
if (c == null) {
System.err.println("No console!");
System.exit(-1);
}
String login = c.readLine("Login: ");
char[] passwd = c.readPassword("Password: ");
c.format("Login: [%s]\n", login);
//System.out.println("Login: " + login);
c.format("Password: ");
for (int i = 0; i < passwd.length; i++) {
c.format("%c", passwd[i]);
}
c.format("\n");
}
}
FileIO.java
import java.io.*;
class FileIO {
public static void main(String[] args) {
try {
BufferedReader fp = new BufferedReader(new FileReader(args[0]));
for (String line = fp.readLine();
line != null;
line = fp.readLine()) {
System.out.println(line);
}
fp.close();
} catch ( Exception e ) {
System.out.println(e);
}
}
}
ReadInt.java
import java.io.*;
import java.util.*;
class ReadInt {
public static void main(String args[]) {
try {
BufferedReader fp = new BufferedReader(new FileReader(args[0]));
for (String line = fp.readLine();
line != null;
line = fp.readLine()) {
StringTokenizer st = new StringTokenizer(line, " ");
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
System.out.format("(%d, %d)\n", x, y);
}
fp.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
Echo.java
class Echo {
public static void main (String[] args) {
for (int i = 0; i < args.length; i++)
System.out.println(args[i]);
}
}
Arrays.java
class Arrays {
public static void main(String[] args) {
// http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html
int[] factorial = { 1, 1, 2, 6, 24, 120, 720, 5040 };
char ac[] = { 'n', 'o', 't', ' ',
'a', ' ',
'S', 't', 'r', 'i', 'n', 'g' };
String[] aas = { "array", "of", "String", };
Exception ae[] = new Exception[3];
Object aao[][] = new Exception[2][3];
for (int i = 0; i < ac.length; i++) {
System.out.print(ac[i]);
}
System.out.println();
}
}
HelloWorldSwing.java
// Modified from http://java.sun.com/docs/books/tutorial/uiswing/examples/start/HelloWorldSwingProject/src/start//HelloWorldSwing.java
import javax.swing.*;
public class HelloWorldSwing {
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add the ubiquitous "Hello World" label.
JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Menu.java
// Modified from: http://zerioh.tripod.com/ressources/menu.html
import javax.swing.*;
import java.awt.event.*;
public class Menu extends JFrame {
public Menu() {
super("Menu Demo for CS181");
// Close the window properly
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Add menu and menu items
JMenu file = new JMenu("File");
file.setMnemonic('F');
JMenuItem newItem = new JMenuItem("New");
newItem.setMnemonic('N');
file.add(newItem);
JMenuItem openItem = new JMenuItem("Open");
openItem.setMnemonic('O');
file.add(openItem);
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.setMnemonic('x');
file.add(exitItem);
// Add an action listener to exit item
exitItem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Exit is pressed");
}
}
);
// Add a menu bar
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
bar.add(file);
// Show the frame
setLocation(50, 50);
setSize(300, 200);
setVisible(true);
}
public static void main(String[] args) {
// Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
new Menu();
}
}