Lab & Assignment 1. Lecture 3: ArrayList & Standard Java Graphics. Random Number Generator. Read Lab & Assignment Before Lab Wednesday!

Similar documents
+! Today. Lecture 3: ArrayList & Standard Java Graphics 1/26/14! n Reading. n Objectives. n Reminders. n Standard Java Graphics (on course webpage)

Lecture 3: Java Graphics & Events

Lecture 5: Java Graphics

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Java Programming Lecture 6

Computer Science 210: Data Structures. Intro to Java Graphics

CSC 161 SPRING 17 LAB 2-1 BORDERLAYOUT, GRIDLAYOUT, AND EVENT HANDLING

Lab. Lecture 26: Concurrency & Responsiveness. Assignment. Maze Program

Come & Join Us at VUSTUDENTS.net

IT101. Graphical User Interface

Assertions, pre/postconditions

To gain experience using GUI components and listeners.

Graphics and Painting

H212 Introduction to Software Systems Honors

Introduction to the JAVA UI classes Advanced HCI IAT351

First Name: AITI 2004: Exam 2 July 19, 2004

Sample Examination Paper Programming and Software Development

ming 3 Resize the Size to [700, 500]. Note that the background containers in the program code: reference the then under () { 153, 255, 0 ) );

First Name: AITI 2004: Exam 2 July 19, 2004

2IS45 Programming

Final Exam CS 251, Intermediate Programming December 13, 2017

Packages: Putting Classes Together

Give one example where you might wish to use a three dimensional array

Agenda. Programming Seminar. By: dr. Amal Khalifa. Coordinate systems Colors Fonts Drawing shapes Graphics2D API

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

G51PRG: Introduction to Programming Second semester Applets and graphics

CS 335 Lecture 02 Java Programming

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

Text User Interfaces. Keyboard IO plus

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

Sharing is the Key. Lecture 25: Parallelism. Canonical Example. Bad Interleavings. Common to have: CS 62 Fall 2016 Kim Bruce & Peter Mawhorter

1.00 Lecture 8. Using An Existing Class, cont.

CS 251 Intermediate Programming GUIs: Components and Layout

CS 106A, Lecture 25 Life After CS 106A, Part 1

CS506 Web Programming and Development Solved Subjective Questions With Reference For Final Term Lecture No 1

CS 251 Intermediate Programming GUIs: Event Listeners

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Graphical User Interfaces (GUIs)

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Peer Instruction 1. Elementary Programming

Programming Languages and Techniques (CIS120)

Review sheet for Final Exam (List of objectives for this course)

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

News and info. Array. Feedback. Lab 4 is due this week. It should be easy to change the size of the game grid.

Introduction This assignment will ask that you write a simple graphical user interface (GUI).

Example: Building a Java GUI

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

Computer Science 145

CS 211: Existing Classes in the Java Library

Example: Building a Java GUI

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Programming graphics

Graphic User Interfaces. - GUI concepts - Swing - AWT

AP CS Unit 11: Graphics and Events

CS 3331 Advanced Object-Oriented Programming. Final Exam

CS Fall Homework 5 p. 1. CS Homework 5

CS 215 Software Design Homework 3 Due: February 28, 11:30 PM

Name Section. CS 21a Introduction to Computing I 1 st Semester Final Exam

n Specifying what each method does q Specify it in a comment before method's header n Precondition q Caller obligation n Postcondition

Chapter 10 Classes Continued. Fundamentals of Java

SAMPLE EXAM Exam 2 Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 5 November 2009

CP122 CS I. Chapter 11: File I/O and Exceptions

AplusBug dude = new AplusBug(); A+ Computer Science -

Graphical User Interface (GUI)

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

Agenda. Container and Component

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

Object-oriented programming in Java (2)

Name: CSC143 Exam 1 1 CSC 143. Exam 1. Write also your name in the appropriate box of the scantron

CIS 120 Programming Languages and Techniques. Final Exam, May 3, 2011

Java Coordinate System

Program Graph. Lecture 25: Parallelism & Concurrency. Performance. What does it mean?

Graphical User Interfaces 2

Introduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014

CS 11 java track: lecture 3

L4,5: Java Overview II

1 Short Answer (10 Points Each)

CS11 Java. Fall Lecture 3

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

8. Polymorphism and Inheritance

CS 134 Programming Exercise 7:

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

CS 201 Advanced Object-Oriented Programming Lab 6 - Sudoku, Part 2 Due: March 10/11, 11:30 PM

OOP Assignment V. For example, the scrolling text (moving banner) problem without a thread looks like:

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Contents Chapter 1 Introduction to Programming and the Java Language

CS108, Stanford Handout #22. Thread 3 GUI

CS Exam 1 Review Suggestions

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Road Map. Introduction to Java Applets Review applets that ship with JDK Make our own simple applets

CS415 Human Computer Interaction

Implementing Graphical User Interfaces

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Transcription:

Lab & Assignment 1 Lecture 3: ArrayList & Standard Java Graphics CS 62 Fall 2015 Kim Bruce & Michael Bannister Strip with 12 squares & 5 silver dollars placed randomly on the board. Move silver dollars to fill 5 leftmost squares Coins move only to the left. No coin may pass another. No square may hold more than one coin. Last person to move wins. Complete description in text. Random Number Generator Read Lab & Assignment Before Lab Wednesday! Rest of today s lecture is info for lab and assignment! class Random in java.util package w/ method int nextint(int n) -- returns random k s.t. 0 k < n See bottom of pg 30 in text. Create Random object once, send nextint many times. See LottoHelper example.

Text Input Scanner class Constructor: myscanner = new Scanner(System.in) can use file instead of System.in new Scanner(new File( filename )) Read values: myscanner.nextint() -- returns an int myscanner.nextdouble() -- returns a double myscanner.nextline() -- returns String -- to end of line see documentation for more Back to assertions in Java Won t use Assert class from Bailey. Command to check assertions in standard Java Two forms assert boolexp assert boolexp: message Article on when to use assert: http://docs.oracle.com/javase/7/docs/technotes/guides/ language/assert.html Short summary -- never use for preconditions of public methods (OK for private) make explicit checks Use for postconditions & class invariants Turning on assert Turn on assertions when run program, by adding -ea (without quotes) as virtual machine argument in arguments tab in Eclipse when set up runtime configuration. If leave it off, then ignores assert statements. If on and assertion is false, then will raise an AssertionError exception and will print associated message Using Assert & Pre/ postconditions Preconditions of public methods must be enforced. But don t use assert! Why not? Preconditions of private methods should also be enforced Can use assert to check preconditions of private method Why? Use assert to check postconditions and other class invariants

Arrays Arrays & ArrayList Containers that hold objects C[ ] myarray = new C[10] // fixed length Different syntax from objects Public instance variable length Ugh! Because of limitations of Java virtual machine, cannot create array of type variable: E.g., new T[5] illegal if T is type variable new C[5] is legal if C is primitive, class, or interface name. ArrayList What happens if need more space in array than originally allocated? ArrayList is class that creates objects that dynamically expand as needed. Part of java.util package To get access write import java.util.arraylist or import java.util.* Lab: Squares rep by ArrayList of CoinSquares. Text uses Vector rather than ArrayList ArrayList more efficient if no concurrency ArrayList Specification Class ArrayList<E> Important methods: add, get, set, indexof, isempty, remove, size, contains, clear size, isempty, get, set take constant time add (to end) is amortized constant time See javadoc at http://download.oracle.com/javase/8/docs/api/ See PostIt example later!

Overview Java Graphics For details, see document on course web page associated with lecture Also see GUI cheat sheet in documentation and handouts section. Graphical User Interfaces (GUI) JFrame (window), JPanel (grouping) JButton, JTextField, JSlider, JChooser, Events: Generated by mouse actions, button clicks, etc Use MouseListener, MouseMotionListener, ActionListener, etc. to respond Graphics Drawing items on the screen - today s focus Graphics Create objects want to draw: Rectangle2D.Double, Line.Double, etc. Constructors take x,y coords and dimensions, but don t actually draw items. All drawing takes place in paint method using a graphics context Triggered implicitly by uncovering window or explicitly by calling repaint method. Adds repaint event to event queue eventually draws it Graphics context All drawing is done in paint method of component public void paint(graphics g) g is a Graphics context provided by system pen that does the drawing Programmer calls repaint(), not paint!! Need to import classes from java.awt.*, java.awt.geom.*, & javax.swing.* See MyGraphicsDemo

General graphic applications Geometric Objects Create an extension of component (either JPanel, JFrame, or JApplet) and implement paint method in the subclass. See main method of demo to get window to show At start of paint method cast g to Graphics2D to get access to new methods Call repaint() on component every time make a change. Causes OS to schedule call of paint in event queue Called automatically if window obscured and revealed Objects from classes Rectangle2D.Double, Line2D.Double, etc. from java.awt.geom There are also float versions Constructors take params x, y, width, height, but don t draw object myobj.setframe(x,y,width,height) can move object g2.draw(myobj) -- gives outline g2.fill(myobj) -- gives filled version g2.drawstring( a string,x,y) draws string MyGraphicsDemo PostItStdApplication Class extends JFrame, which creates window. Constructor calls super with title of window. Main method creates object, sets size, visibility, and enables go-away box in upper left Paint method creates and draws objects. More sophisticated JFrame contains two JPanels JFrame uses BorderLayout, so add controls to JPanel in SOUTH, drawing canvas in CENTER of the JFrame Ignore controls for now. See GUI cheat sheet for details DrawingCanvas extends JPanel -- contains paint method Note use of ArrayList to hold PostIts.