The ecs100 library and the text book.

The programs you write in this course will use the ecs100 library (a collection of files of java code). This library is designed to make small programs easier to write than if you use the standard Java libraries. It provides a predefined object called UI that brings together lots of different functions that require lots of different classes and objects if you use only the standard Java library. It also simplifies many aspects of making a program that interacts with the user.

However, the textbook does not use the ecs100 library, which means that the course is not quite consistent with the text book. The library is designed to work in a very similar way to the text book code, but simpler. There are two kinds of changes:

  • the text book uses a variety of different classes where the course just uses the UI class: you can simply use UI in place of those classes.
  • the text book frequently uses code to set things up which is not necessary with the UI class: you can leave out some bits of code that the text book describes.

The following table lists all the important differences so that you will be able to read the textbook and make the appropriate translations.

=============================================================

Simple text output

  • Key idea: Use UI instead of System.out
Using the ecs100 library:
UI.print("hello");

UI.println("hello"+ name);

UI.printf("%s is %d cm\n", name, height);

Standard Java / text book
System.out.print("hello");

System.out.println("hello"+ name);

System.out.printf("%s is %d cm\n", name, height);

=============================================================

Prompted input

  • Key idea: Use UI instead of JOptionPane

Using the ecs100 library:
String name = UI.askString ("What is your name");

int age = UI.askInt ("How old are you");

boolean ans = UI.askBoolean ("Are you a student");

Standard Java / text book
String name = JOptionPane.showInputDialog( "What is your name");

int age = Integer.parseInt(JOptionPane.showInputDialog( "How old are you"));

boolean ans = Boolean.parseBoolean(JOptionPane.showInputDialog( "Are you a student"));

=============================================================

Message to user

  • Key idea: Use UI instead of JOptionPane
Using the ecs100 library:
UI.printMessage ("The file was broken");
Standard Java / text book
JOptionPane.showConfirmDialog(null, "The file was broken");

=============================================================

General text input

  • Key idea: Use UI instead of a Scanner and System.in

Using the ecs100 library:
String word = UI.next();

while ( UI.hasNextDouble()){

tot = tot + UI.nextDouble();

}

Standard Java / text book

Scanner scan = new Scanner(System.in);

String word = scan.next();

while ( scan.hasNextDouble()){

tot = tot + scan.nextDouble();

}

=============================================================

Graphical output

  • Key idea: Use UI vs setting up a new window and repaint methods.

Using the ecs100 library:
UI.drawRect(10, 20, 300, 100); UI.fillOval(x, y, size, size/2); UI.eraseRect (10, 20, wd, ht); UI.clearGraphics ();

Standard Java / text book
this is quite tricky to accomplish

JFrame frame = new JFrame(); frame.add(...) frame.setVisible(true);

??.drawRect(10, 20, 300, 100); ??.fillOval(x, y, size, size/2); ??.setColor(Color.white); ??.fillRect(10, 20, wd, ht); ??.repaint();

=============================================================

Button input.

  • Key idea: Use UI.addButton instead of JButton, ActionListener, etc

Using the ecs100 library:
In the constructor or setupGUI method:
UI.addButton ("Go", this::doGo);
UI.addButton ("Quit", UI::quit);

The doGo method:
public void doGo () {
    UI.println("going now!);
}

Standard Java / text book

JFrame frame = new JFrame();

frame.add(new ButtonPanel());

public class ButtonPanel extends JPanel {

JButton goButton = new JButton( "Go" );

goButton.addActionListener(new GoButtonListener());

this.add(goButton);


private class GoButtonListener implements ActionListener {

public void actionPerformed(ActionEvent e){

System.out.println("going now!");

}

}

=============================================================

Mouse input.

  • Key idea: Use UI.setMouseListener and a doMouse method instead of MouseListener, mousePressed, mouseReleased, mouseClicked, ...

Using the ecs100 library:
In the constructor or setupGUI method:
UI.setMouseListener (this::doMouse);

The doMouse method:
public void doMouse(String action, double x, double y) {
if (action.equals("pressed"))
UI.drawOval( x, y, 10, 10);
else if (action.equals("released"))
UI.drawRect( x, y, 10, 10);
}

Standard Java / text book

public class ..... implements MouseListener {
:
JFrame frame = new JFrame();
?? make canvas
??. addMouseListener (this);

public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
??.drawOval(x, y, 10, 10);
}

public void mouseReleased(MouseEvent e){
int x = e.getX();
int y = e.getY();
??.drawRect(x, y, 10, 10);
}

public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}