Strings and Graphics Jerry Cain CS 106J April 12, 2017.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Chapter 2—Programming by Example The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Programming by Example C H A P T E R 2.
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
PHY-102 SAPIntroductory GraphicsSlide 1 Introductory Graphics In this section we will learn how about how to draw graphics on the screen in Java:  Drawing.
Variables and Operators
Chapter 2 Programming by Example. A Holistic Perspective Three sections separated by blank lines. Program comment File: FileName.java
Classes and Objects Java programs are written as collections of classes, which serve as templates for individual objects. Each object is an instance of.
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Applets and Graphics.
Four simple expressions in meta. Data objects Pieces of data in a computer are called objects Today, we’ll talk about four kinds of objects Numbers Pictures.
Inheritance in C++ Eric Roberts CS 106B March 1, 2013.
©Brooks/Cole, 2003 Chapter 2 Data Representation.
Chapter 2 Data Representation. Define data types. Visualize how data are stored inside a computer. Understand the differences between text, numbers, images,
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Foundations of Computer Science Computing …it is all about Data Representation, Storage, Processing, and Communication of Data 10/4/20151CS 112 – Foundations.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
1 Introduction to Graphics b The last one or two sections of each chapter of the textbook focus on graphical issues b Most computer programs have graphical.
Objective You will be able to define the basic concepts of object-oriented programming with emphasis on objects and classes by taking notes, seeing examples,
Simple Java Eric Roberts CS 106A January 11, 2016.
Digital Media Lecture 5: Vector Graphics Georgia Gwinnett College School of Science and Technology Dr. Jim Rowan.
Simple Java Eric Roberts CS 106A January 11, 2010.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Five-Minute Review 1.What steps do we distinguish in solving a problem by computer? 2.What are essential properties of an algorithm? 3.What is a definition.
REEM ALMOTIRI Information Technology Department Majmaah University.
1 Sections 5.1 – 5.2 Digital Image Processing Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Main characteristics of Vector graphics  Vector graphics provide an elegant way of constructing digital images (diagrams, technical illustration and.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Multidimensional Arrays
Chapter 02 Data and Expressions.
Jerry Cain and Eric Roberts
Five-Minute Review What steps do we distinguish in solving a problem by computer? What are essential properties of an algorithm? What is a definition of.
Eric Roberts and Jerry Cain
Chapter 6 JavaScript: Introduction to Scripting
Jerry Cain and Eric Roberts
Adapted from slides by Marty Stepp and Stuart Reges
Categories of Biological Classification
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Chapter 3 Syntax, Errors, and Debugging
Review.
IMAGES.
Building Java Programs
reading: Art & Science of Java,
CS 106A, Lecture 12 More Graphics
Expressions in JavaScript
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
An Introduction to Classification
slides courtesy of Eric Roberts
Chapter 2—Programming by Example
CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random
SSEA Computer Science: Track A
Graphics.
Chapter 2 Data Representation.
Interactive Graphics Jerry Cain and Ryan Eberhardt CS 106AJ
slides courtesy of Eric Roberts
Multidimensional Arrays
slides courtesy of Eric Roberts
Chapter 2—Programming by Example
Presentation transcript:

Strings and Graphics Jerry Cain CS 106J April 12, 2017

Nonnumeric Data The arithmetic expressions in the early sections of Chapter 2 enable you to perform numeric computation. Much of the excitement of modern computing, however, lies in the ability of computers to work with other types of data, such as characters, images, sounds, and video. As you will learn in Chapter 6, all of these data types are represented inside the machine as sequences of binary digits, or bits. When you are getting started with programming, it is more important to think about data in a more abstract way in which you focus on the conceptual values rather than the underlying representation.

Data Types The notion that data values come in many different forms gives rise to the notion of a data type, which defines the common characteristics of data values that have a particular form or purpose. In computer science, each data type is defined by a domain, which is the set of values that belong to that type, and a set of operations, which shows how the values in that domain can be manipulated. For example, the JavaScript type for numbers has a domain that consists of numeric values like 1.414213 or 42. The set of operations includes addition, subtraction, multiplication, division, remainder, and a few more that you haven’t learned yet.

The String Type One of the most important data types in any programming language is the string type. The domain of the string type is all sequences of characters. In JavaScript, you create a string simply by including that sequence of characters inside quotation marks, as in "Jerry". The set of operations that can be applied to strings is large, but you don’t need to know the entire set. In fact, for the first five chapters in the text, the only string operation you need to know is concatenation, as described on the next slide. You will learn about other operations in Chapter 6. All values—including numbers, strings, graphical objects, and values of many other types—can be assigned to variables, passed as arguments to functions, and returned as results.

Concatenation One of the most useful operations available for strings is concatenation, which consists of combining two strings end to end with no intervening characters. Concatenation is built into JavaScript using the + operator. For example, the expression "ABC" + "DEF" returns the string "ABCDEF". If you use + with numeric operands, it signifies addition. If at least one of its operands is a string, JavaScript interprets + as concatenation. It automatically converts the other operand to a string and concatenates the two strings, so that "Catch" + -22 "Catch-22"

The Graphics Model SJS uses the same graphics model that we have used for the last decade, which is based on the metaphor of a collage. A collage is similar to a child’s felt board that serves as a backdrop for colored shapes that stick to the felt surface. As an example, the following diagram illustrates the process of adding a blue rectangle and a red oval to a felt board: Note that newer objects can obscure those added earlier. This layering arrangement is called the stacking order.

The BlueRectangle Program import "graphics"; function BlueRectangle() { var gw = GWindow(500, 200); var rect = GRect(150, 50, 200, 100); rect.setColor("Blue"); rect.setFilled(true); gw.add(rect); } rect import "graphics"; function BlueRectangle() { var gw = GWindow(500, 200); var rect = GRect(150, 50, 200, 100); rect.setColor("Blue"); rect.setFilled(true); gw.add(rect); } rect BlueRectangle

The JavaScript Coordinate System BlueRectangle (0, 0) (150, 50) 200 pixels 100 pixels pixels Positions and distances on the screen are measured in terms of pixels, which are the small dots that cover the screen. Unlike traditional mathematics, Java defines the origin of the coordinate system to be in the upper left corner. Values for the y coordinate increase as you move downward.

Systems of Classification In the mid-18th century, the Scandinavian botanist Carl Linnaeus revolutionized the study of biology by developing a new system for classifying plants and animals in a way that revealed their structural relationships and paved the way for Darwin’s theory of evolution a century later. Linnaeus’s contribution was to recognize that organisms fit into a hierarchy in which the placement of individual species reflects their anatomical similarities. Carl Linnaeus (1707–1778)

Biological Class Hierarchy Living Things Living Things Animals Arthropoda Insecta Hymenoptera Formicidae Iridomyrmex purpureus Plants Fungi Animals Kingdom Annelida Brachiopoda Mollusca Chordata Arthropoda Phylum Crustacea Arachnida Insecta Order Class Every red ant is also an animal, an arthropod, and an insect, as well as the other superclasses in the chain. Classification of the red ant Iridomyrmex purpureus Family Genus Species

Drawn after you, you pattern of all those. Instances vs. Patterns Drawn after you, you pattern of all those. —William Shakespeare, Sonnet 98 In thinking about any classification scheme—biological or otherwise—it is important to draw a distinction between a class and specific instances of that class. In the most recent example, the designation Iridomyrmex purpureus is not itself an ant, but rather a class of ant. There can be (and usually are) many ants, each of which is an individual of that class. Each of these fire ants is an instance of a particular class of ants. Each instance is of the species purpureus, the genus Iridomyrmex, the family Formicidae (which makes it an ant), and so on. Thus, each ant is not only an ant, but also an insect, an arthropod, and an animal.

The GObject Hierarchy The classes that represent graphical objects form a hierarchy, part of which looks like this: GObject GRect GOval GLine The GObject class represents the collection of all graphical objects. The three subclasses shown in this diagram correspond to particular types of objects: rectangles, ovals, and lines. Any GRect, GOval, or GLine is also a GObject.

Creating a GWindow Object The first step in writing a graphical program is to create a window using the following function declaration, where width and height indicate the size of the window: var gw = GWindow(width, height); gw.add(object) Adds an object to the window. gw.remove(object) Removes the object from the window. gw.add(object, x, y) Adds an object to the window after first moving it to (x, y). gw.getWidth() Returns the width of the graphics window in pixels. gw.getHeight() Returns the height of the graphics window in pixels. The following functions apply to a GWindow object:

Operations on the GObject Class The following operations apply to all GObjects: object.getX() Returns the x coordinate of this object. object.getY() Returns the y coordinate of this object. object.getWidth() Returns the width of this object. object.getHeight() Returns the height of this object. object.setColor(color) Sets the color of the object to the specified color. All coordinates and distances are measured in pixels. Each color is a string, such as "Red" or "White". The names of the standard colors are defined in Figure 2-7 on page 61.

Drawing Geometrical Objects Functions to create geometrical objects: GRect( x, y, width, height) Creates a rectangle whose upper left corner is at (x, y) of the specified size. GOval( x, y, width, height) Creates an oval that fits inside the rectangle with the same dimensions. GLine( x0, y0, x1, y1) Creates a line extending from (x0, y0) to (x1, y1). Methods shared by the GRect and GOval classes: object.setFilled( fill) If fill is true, fills in the interior of the object; if false, shows only the outline. object.setFillColor( color) Sets the color used to fill the interior, which can be different from the border.

The GRectPlusGOval Program function GRectPlusGOval() { var gw = GWindow(500, 200); var rect = GRect(150, 50, 200, 100); rect.setFilled(true); rect.setColor("Blue"); gw.add(rect); var oval = GOval(150, 50, 200, 100); oval.setFilled(true); oval.setColor("Red"); gw.add(oval); } oval rect function GRectPlusGOval() { var gw = GWindow(500, 200); var rect = GRect(150, 50, 200, 100); rect.setFilled(true); rect.setColor("Blue"); gw.add(rect); var oval = GOval(150, 50, 200, 100); oval.setFilled(true); oval.setColor("Red"); gw.add(oval); } rect oval GRectPlusGOval

The DrawDiagonals Program import "graphics"; /* Constants */ const GWINDOW_WIDTH = 500; const GWINDOW_HEIGHT = 200; function DrawDiagonals() { var gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT); gw.add(GLine(0, 0, GWINDOW_WIDTH, GWINDOW_HEIGHT)); gw.add(GLine(0, GWINDOW_HEIGHT, GWINDOW_WIDTH, 0)); } import "graphics"; /* Constants */ const GWINDOW_WIDTH = 500; const GWINDOW_HEIGHT = 200; function DrawDiagonals() { var gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT); gw.add(GLine(0, 0, GWINDOW_WIDTH, GWINDOW_HEIGHT)); gw.add(GLine(0, GWINDOW_HEIGHT, GWINDOW_WIDTH, 0)); } DrawDiagonals

The End