Graohics CSC 171 FALL 2001 LECTURE 16. History: COBOL 1960 - Conference on Data System Languages (CODASYL) - led by Joe Wegstein of NBS developed the.

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

1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Input review If review Common Errors in Selection Statement Logical Operators switch Statements Generating Random Numbers practice.
Methods. Read two numbers, price and paid (via JOptiondialog) Calculate change; s = JOptionPane.showInputDialog("Enter price”); Double price = Double.parseDouble(s);
Lecture 15: I/O and Parsing
Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.
MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
Formal Language, chapter 4, slide 1Copyright © 2007 by Adam Webber Chapter Four: DFA Applications.
Air Force Institute of Technology Electrical and Computer Engineering
Question from the last class What happens if we cast “too large” float/double to an int? int has range float a=1e10f; int b=(int)
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Objects contains data and methods Class – type of object Create class first Then object or instance String – defined class String s; // creates instance.
CS110 Programming Language I
Chapter 10 Review. Write a method that returns true is s1 and s2 end with the same character; otherwise return false. Sample Answer: public boolean lastChar(String.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
Files from Ch4. File Input and Output  Reentering data all the time could get tedious for the user.  The data can be saved to a file. Files can be input.
Streaming and I/0 Chapter 14 D&D. Menu Reading/writing text file streams Reading/writing string streams Command line arguments.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
 We can use a combination of the File and FileOutputStream to write a series of bytes to a file.
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.
COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
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.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
Using java’s Scanner class To read from input and from a file. (horstmann ch04 and ch 17)
Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
Georgia Institute of Technology Speed part 3 Barb Ericson Georgia Institute of Technology May 2006.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
1 CSC 110AA Introduction to Computer Science for Majors - Spring 2003 Class 5 Chapter 2 Type Casting, Characters, and Arithmetic Operators.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Question 1a) What is printed by the following Java program? int s; int r; int i; int [] x = {4, 8, 2, -9, 6}; s = 1; r = 0; i = x.length - 1; while (i.
CHAPTER 15 STREAMS. CHAPTER GOALS To be able to read and write files To become familiar with the concepts of text and binary files To be able to read.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering products.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
Java FilesOops - Mistake Java lingoSyntax
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
CSC111 Quick Revision.
Chapter 1.2 Introduction to C++ Programming
2.5 Another Java Application: Adding Integers
Multiple variables can be created in one declaration
Programming Language Concepts (CIS 635)
Computer Programming Methodology File Input
Introduction to Programming in Java
Chapter 7: Strings and Characters
null, true, and false are also reserved.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Recap Week 2 and 3.
Question 1a) What is printed by the following Java program? int s;
Topic 25 - more array algorithms
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Presentation transcript:

Graohics CSC 171 FALL 2001 LECTURE 16

History: COBOL Conference on Data System Languages (CODASYL) - led by Joe Wegstein of NBS developed the first standardized business computer programming language, COBOL (Common Business Oriented Language). For the next 20 years there were more programs written in COBOL than any other single language. That same year the second of the mathematical languages, ALGOL 60 was developed, not widely implemented ALGOL became the conceptual basis of many programming languages.

Coordinate System Origin in upper left X increases to right Y increases down (0,0) Increasing x Increasing y

Coordinate System So: g.drawLine(30,40,50,70); (0,0) Increasing x Increasing y (30,40) (50,70)

Basic Graphics Operations Translation – Change the position Scale – Change the size Rotation – Change the angle

Translation /*Moving an object is addition */ int x1,y1,x2,y2; x1=30; y1=40; x2=60; y2=80; g.drawLine(x1,y1,x2,y2); //20 to the left, 10 down x1+=20; x2+=20; y1+= 10; y2+=10; g.drawLine(x1,y1,x2,y2); (0,0) Increasing x Increasing y (30,40) (60,80) (50,50) (80,90)

Scale /*Sizing an object multiplication */ int x1,y1,x2,y2; x1=30; y1=40; x2=60; y2=80; g.drawLine(x1,y1,x2,y2); //double the size x1*=2; x2*=2; y1*= 2; y2*=2; g.drawLine(x1,y1,x2,y2); // NOT “in place” (0,0) Increasing x Increasing y (30,40) (60,80) (120,160)

Question Given “scale” and “translation” as above: How would you do “translation in place”? (ie : keep center of line in place, alter size) (30,40) (60,80) (15,20) (75,100)

Scale in place int x1,y1,x2,y2; x1=30; y1=40; x2=60; y2=80; g.drawLine(x1,y1,x2,y2); //center point int averageX = (x1+x2)/2; Int averageY = (y1+y2)/2; //put origin x1-=averageX; x2-=averageX; y1-=averageY; y2-=averageY; //scale as before x1*=2; x2*=2; y1*= 2; y2*=2; //center is still at center //so, return center to same //put origin x1+=averageX; x2+=averageX; x1+=averageY; y2+=averageY; g.drawLine(x1,y1,x2,y2);

Scale in place int x1,y1,x2,y2; x1=30; y1=40; x2=60; y2=80; g.drawLine(x1,y1,x2,y2); //center point int averageX = (x1+x2)/2; //45 Int averageY = (y1+y2)/2; //60 //put origin x1-=averageX; // = = -15 x2-=averageX; // = = 15 y1-=averageY; // = = -20 y2-=averageY; // = = 20 //scale as before x1*=2; // -15 * 2 = = -30 x2*=2; // 15 * 2 = = 30 y1*= 2; // -20 * 2 = = -40 y2*=2; // 20 * 2 = = 40 //center is still at center //so, return center to same //put origin x1+=averageX; // = =15 x2+=averageX; // = = 75 x1+=averageY; // = =20 y2+=averageY; // = =100 g.drawLine(x1,y1,x2,y2);

Rotation So, we can translate Scale, in place What about rotation – Rotation involves “angles” – Angles involve math In computer science math is used for application as well as analysis

A little trigonometry Recall: On the unit circle, Give the angle  x=cos(  ) y=sin(  ) (x,y) x=cos(  ) y=sin(  ) (1,0)(-1,0) (0,1) (0,-1)  y x

Any point in the plane x=r*cos(  ) y=r*sin(  ) Rotation about the origin is going from one point on the circle to another (adding an angle) (x,y) x=r*cos(  ) y=r*sin(  )  y x (0,r) (0,-r) (r,0)(-r,0)

Angle Addition Going from (x1,y1) add  to  is like finding the sum Remember: cos(  +  ) == cos  *cos  -sin  *sin  sin(  +  ) == sin  *cos  +cos  *sin  (x1,y1) (x2,y2)  

Angle Addition x1=r*cos(  ) y1=r*sin(  ) x2 = r*cos(  +  ) == r* (cos  *cos  -sin  *sin   x1*cos  -y1*sin  y2= r* sin(  +  ) == r*(sin  *cos  +cos  *sin   x1*sin  y1*cos  (x1,y1) (x2,y2)  

Rotation about the origin x2  x1*cos  -y1*sin  y2  x1*sin  +y1*cos  (x1,y1) (x2,y2)  

Rotation as a Matrix operation x2  x1*cos  -y1*sin  y2  x1*sin  +y1*cos 

Rotation by 90 degrees int x1,y1,x2,y2; x1=30; y1=40; x2=60; y2=80; g.drawLine(x1,y1,x2,y2); int x1p,y1p,x2p,y2p //sin(90) == 1 //cos(90) == 0 //what happens?? (0,0) (30,40) (60,80)

Rotation by 90 degrees x1p =x1*0-y1*1 // -40 y1p = y1*0+x1*1// 30 x2p = x2*0-y2*1 //-80 y2p = y2*0+x2*1 //60 (0,0) (30,40) (60,80) (-40,30) (-80,60)

Rotation about the Origin Not “In Place” How do we do “in place”?

Rotation about the Origin Not “In Place” How do we do “in place”? The same way as scale “in place” – Translate center of object to origin – Rotate around origin – Translate back

FILE IO FileReader class – read() method gets a character FileWriter class – write() method writes a character

The Caesar Cipher

Program Crypt.java import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Crypt { public static void main(String[] args) { boolean decrypt = false; int key = DEFAULT_KEY; FileReader infile = null; FileWriter outfile = null; if (args.length 4) usage(); // gather command line arguments and open files

try { for(int i = 0; i < args.length; i++) { if (args[i].substring(0, 1).equals("-")) // it is a command line option { String option = args[i].substring(1, 2); if (option.equals("d")) decrypt = true; else if (option.equals("k")) { key = Integer.parseInt (args[i].substring(2)); if (key = NLETTERS) usage(); } else { if (infile == null) infile = new FileReader(args[i]); else if (outfile == null) outfile = new FileWriter(args[i]);

} catch(IOException e) { System.out.println("Error opening file"); System.exit(0); } if (infile == null || outfile == null) usage(); // encrypt or decrypt the input if (decrypt) key = NLETTERS - key; try { encryptFile(infile, outfile, key); infile.close(); outfile.close(); }

catch(IOException e) { System.out.println("Error processing file"); System.exit(0); } /** Prints a message describing proper usage and exits. */ public static void usage() { System.out.println ("Usage: java Crypt [-d] [-kn] infile outfile"); System.exit(1); }

/** Encrypts a character with the Caesar cipher. Only upper- and lowercase letters are c the character to k the encryption the encrypted character */ public static char encrypt(char c, int k) { if ('a’ <= c && c <= 'z') return (char)('a’ + (c - 'a’ + k) % NLETTERS); if ('A’ <= c && c <= 'Z') return (char)('A’ + (c - 'A’ + k) % NLETTERS); return c; }

/** Encrypts all characters in a in the plaintext out the file to store the encrypted k the encryption key */ public static void encryptFile(FileReader in, FileWriter out, int k) throws IOException { while (true) { int next = in.read(); if (next == -1)return; // end of file char c = (char)next; out.write(encrypt(c, k)); } public static final int DEFAULT_KEY = 3; public static final int NLETTERS = 'z’ - 'a’ + 1; }

Whole class

Passing class

All exams & projects in 73/134

All exam & projects in 73/134