The ecs100 library vs textbook code.
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 compared to using the standard Java libraries. It provides lots of different functions via
UI that would otherwise require lots of different classes and objects (when using 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 textbook. The library is designed to simplify the required code, compared to the textbook code. There are two kinds of changes:
- the textbook 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 textbook 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 textbook 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 / textbook
System.out.print("hello");
System.out.println("hello"+ name);
System.out.printf("%s is %d cm\n", name, height);
=============================================================
- 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 / textbook
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 / textbook
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 / textbook
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 / textbook
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();
=============================================================
- Key idea: Use UI.addButton instead of JButton, ActionListener, etc
Using the ecs100 library:
In the setupGUI method:
UI.addButton ("Go", this::doGo);
UI.addButton ("Quit", UI::quit);
The doGo method:
public void doGo () {
UI.println("going now!);
}
Standard Java / textbook
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!");
}
}
=============================================================
- Key idea: Use UI.setMouseListener and a doMouse method instead of ??MouseListener, mousePressed, mouseReleased, mouseClicked, ...
Using the ecs100 library:
In the constructor:
UI.addMouseListener (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 / textbook
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){}