Georgia Institute of Technology Speed part 3 Barb Ericson Georgia Institute of Technology May 2006.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

CSCI 160 Midterm Review Rasanjalee DM.
Begin Java Pepper. Objectives What is a program? Learn the basics of your programming tool: BlueJ Write a first Java program and see it run Make some.
Final Review.
Recursion1 Barb Ericson Georgia Institute of Technology March 2010.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
James Tam Simple file handling in Java Simple File Input And Output Types of Java files Simple file output in Java Simple file input in Java.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
1 Text File I/O Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l.
1 Streams Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l Writing.
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
Georgia Institute of Technology Making Text for the Web part 4 Barb Ericson Georgia Institute of Technology March 2006.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Basics Programming Concepts. Basics A computer program is a set of instructions to tell a computer what to do Machine language = circuit level language.
Georgia Institute of Technology Creating and Modifying Text part 4 Barb Ericson Georgia Institute of Technology Oct 2005.
Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer.
CreatingClasses-part11 Creating Classes part 1 Barb Ericson Georgia Institute of Technology Dec 2009.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Input & Output In Java. Input & Output It is very complicated for a computer to show how information is processed. Although a computer is very good at.
Based on OOP with Java, by David J. Barnes Input-Output1 The java.io Package 4 Text files Reader and Writer classes 4 Byte stream files InputStream, FileInputStream,
Georgia Institute of Technology Movies part 2 Barb Ericson Georgia Institute of Technology April 2006.
1 Recitation 8. 2 Outline Goals of this recitation: 1.Learn about loading files 2.Learn about command line arguments 3.Review of Exceptions.
BUILD ON THE POLYGLOT COMPILER FRAMEWORK MIHAL BRUMBULLI 7th Workshop “SEERE” Montenegro-Risan 9-14 September 2007 SimJ Programming Language.
File IO Basics By Dan Fleck Coming up: Data Streams.
CPSC1301 Computer Science 1 Chapter 12 Creating and Modifying Text part 4.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Georgia Institute of Technology Movies Barb Ericson Georgia Institute of Technology April 2006.
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
Georgia Institute of Technology Creating Classes part 4 Barb Ericson Georgia Institute of Technology May 2006.
CreatingClasses-SlideShow-part41 Creating Classes part 4 Barb Ericson Georgia Institute of Technology Dec 2009.
Georgia Institute of Technology Movies part 4 Barb Ericson Georgia Institute of Technology April 2006.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Georgia Institute of Technology Speed part 4 Barb Ericson Georgia Institute of Technology May 2006.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Georgia Institute of Technology Making Text for the Web part 2 Barb Ericson Georgia Institute of Technology March 2006.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Data-Driven Programs Eric Roberts CS 106A February 26, 2016.
Georgia Institute of Technology Creating and Modifying Text Barb Ericson Georgia Institute of Technology June 2005.
For Friday Finish reading chapter 9 WebCT quiz 17.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Georgia Institute of Technology Making Text for the Web part 3 Barb Ericson Georgia Institute of Technology March 2006.
OO Design and Programming II I/O: Reading and Writing
Lecture 5 Text File I/O; Parsing.
Creating and Modifying Text part 2
Going from C++ to Java Jayden Navarro.
Unit 6 Working with files. Unit 6 Working with files.
Barb Ericson Georgia Institute of Technology April 2006
Workshop for Programming And Systems Management Teachers
JAVA Constructors.
Manipulating Pictures, Arrays, and Loops
Workshop for Programming And Systems Management Teachers
Making Text for the Web part 6
More on Creating Classes
Developing Java Applications with NetBeans
Creating and Modifying Text part 3
Developing Java Applications with NetBeans
Exceptions.
More on Creating Classes part 3
Barb Ericson Georgia Institute of Technology May 2006
Exceptions.
Presentation transcript:

Georgia Institute of Technology Speed part 3 Barb Ericson Georgia Institute of Technology May 2006

Georgia Institute of Technology Learning Goals Computing Concepts –Write a compiler for a simple graphical language –Extend the compiler –Understand why compiled code is faster than interpreted code

Georgia Institute of Technology Writing a Compiler To write a graphics compiler we need to –Write the code for a java class that draws the picture for the given graphics commands We need to start the class definition including starting the main method – the prologue Then we need to open the input file and output a line per command in the main Then we need to finish the main method and class definition – the epilogue

Georgia Institute of Technology Creating a Graphics Compiler import java.io.*; /** * Class that reads in a file of graphics instructions, and * then generates a NEW Java Program that * does the same thing as the instructions. Default picture size is 640x480. * * Format of the file is a bunch of lines of the form: * Command X Y * Commands are "line" with parameters of end X,Y and * "circle" with a parameter of diameter. * * For example: * line * circle * * Which draws a line from (10,10) to (50,70) and a circle at (10,20) * with a diameter of 30. * Barb Ericson Mark Guzdial */

Georgia Institute of Technology Start of Class and writePrologue public class GraphicsCompiler { /** Method to write out the prologue for the new program: * All the imports, the class definition, main, etc. file BufferedWriter to write the prologue to **/ public void writePrologue(BufferedWriter file){ try { // Write out the prologue lines file.write("import java.awt.*;"); file.newLine(); file.write("public class GeneratedDrawing{"); file.newLine(); file.write(" public static void main(String args[]){"); file.newLine(); file.write(" Picture frame = new Picture(640,480);"); file.newLine(); file.write(" Graphics g = frame.getGraphics();"); file.newLine(); file.write(" g.setColor(Color.black);"); file.newLine();} catch (Exception ex) { System.out.println("Error during write of prologue"); }

Georgia Institute of Technology writeEpilogue Method /** Method to write out the epilogue for the new program: * Show the picture. Close the main and the class. file BufferedWriter to write the epilogue to **/ public void writeEpilogue(BufferedWriter file){ try { // Write out the epilogue lines file.write(" frame.show();"); file.newLine(); file.write(" } // end main()"); file.newLine(); file.write("} // end class"); file.newLine();} catch (Exception ex) { System.out.println("Error during write of epilogue"); }

Georgia Institute of Technology Start of CompileCommands Method /** * Method to compile the commands in the given file */ public void compileCommands(String fileName) { String line = null; String [] params = null; int x1, y1, x2, y2, diameter; // try the following try { // read from the file BufferedReader reader = new BufferedReader(new FileReader(fileName)); BufferedWriter writer = new BufferedWriter(new FileWriter( FileChooser.getMediaPath("GeneratedDrawing.java"))); writePrologue(writer);

Georgia Institute of Technology Loop and handle line command // loop till end of file while ((line = reader.readLine()) != null) { // what command is this? if (line.startsWith("line")) { // Get the parameters for drawing the line params = line.split(" "); // params[0] should be "line" x1 = Integer.parseInt(params[1]); y1 = Integer.parseInt(params[2]); x2 = Integer.parseInt(params[3]); y2 = Integer.parseInt(params[4]); // Now, write the line that will LATER // draw the line writer.write(" g.drawLine("+x1+","+y1+","+x2+","+y2+");"); writer.newLine(); }

Georgia Institute of Technology Handle circle command else if (line.startsWith("circle")) { // Get the parameters for drawing the circle params = line.split(" "); // params[0] should be "circle" x1 = Integer.parseInt(params[1]); y1 = Integer.parseInt(params[2]); diameter = Integer.parseInt(params[3]); // Now, draw the circle in writer.write(" g.drawOval("+x1+","+y1+","+diameter+","+ diameter+");"); writer.newLine(); }

Georgia Institute of Technology Finish compileCommand Method else { System.out.println("Uh-oh! Invalid command! "+line); return;} } writeEpilogue(writer); writer.close(); } catch (FileNotFoundException ex) { System.out.println("Couldn't find file " + fileName); fileName = FileChooser.pickAFile(); compileCommands(fileName); } catch (Exception ex) { System.out.println("Error during read or write"); ex.printStackTrace(); }

Georgia Institute of Technology Main for Testing public static void main(String[] args) { GraphicsCompiler compiler = new GraphicsCompiler(); String fileName = FileChooser.getMediaPath( "graphics-commands.txt"); compiler.compileCommands(fileName); } } // end class

Georgia Institute of Technology How it Works Open the same file for input as the interpreter –But don't create a blank picture to write to –Instead write to GeneratedDrawing.java Write out a class definition And a main that creates the blank picture, gets the graphics context on that picture, and does the drawing. Compile GeneratedDrawing and execute it to see the picture

Georgia Institute of Technology Exercise Add the code to handle triangles to the GraphicsCompiler class –triangle x1 y1 x2 y2 x3 y3 It will draw a triangle with points at (x1,y1), (x2,y2), and (x3, y3)

Georgia Institute of Technology Compilers Compilers still have to interpret the language they are compiling –But, they only do this one time When you compile –Then you can execute the compiled code many times Applications like Word or Photoshop are written in C or C++ and then compiled to machine language programs –The generated machine language program is faster than an interpreted one

Georgia Institute of Technology Summary Compilers read a language and output the required code in another language –Like from our graphical language to Java commands –Or from C to machine language You compile code once to turn it into the desired language –You can run the compiled code many times Running compiled code is faster than running code that must be interpreted