Data Representation and Applets

Similar documents
Data Representation and Applets

Data Representation and Applets

Applets and the Graphics class

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Data Representation and Applets

Data Representation Classes, and the Java API

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:


CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 9, Name: KEY

Learning objectives: Objects and Primitive Data. Introduction to Objects. A Predefined Object. The print versus the println Methods

Graphical User Interfaces

Graphical User Interfaces

Graphical User Interfaces

Designing Classes Part 2

Chapter 2 Exercise Solutions

Appendix F: Java Graphics

Appendix F: Java Graphics

CS1004: Intro to CS in Java, Spring 2005

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 26, Name: Key

2: Basics of Java Programming

Preparation: Get to know the Java coordinate system. 3. How is the background color of the window specified? Copy the exact code:

Objects and Primitive Data

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Lecture 3: Variables and assignment

Chapter. Let's explore some other fundamental programming concepts

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU. Week 21/02/ /02/2007 Lecture Notes: ASCII

ECE 122 Engineering Problem Solving with Java

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

CSC 1051 Data Structures and Algorithms I

Graphics Applets. By Mr. Dave Clausen

CMPT 125: Lecture 3 Data and Expressions

Graphics Applets. By Mr. Dave Clausen

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Introduction to Computer Science I

Discussion. Why do we use Base 10?

What did we talk about last time? Examples switch statements

Declaration and Memory

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

@Override public void start(stage primarystage) throws Exception { Group root = new Group(); Scene scene = new Scene(root);

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 2, Name:

CSC 1051 Data Structures and Algorithms I

Using sticks to count was a great idea for its time. And using symbols instead of real sticks was much better.

CSC System Development with Java Introduction to Java Applets Budditha Hettige

CSC 1051 Data Structures and Algorithms I

A variable is a name for a location in memory A variable must be declared

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 6, Name:

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

The type of all data used in a C (or C++) program must be specified

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1

CSC Algorithms and Data Structures I. Midterm Examination February 25, Name:

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

Basics of Java Programming variables, assignment, and input

[301] Bits and Memory. Tyler Caraza-Harter

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Lab 3: Work with data (IV)

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Introduction to Algorithms and Data Structures

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Intermediate Programming & Design (C++) Notation

More Programming Constructs -- Introduction

The type of all data used in a C++ program must be specified

Chapter. We've been using predefined classes. Now we will learn to write our own classes to define objects

Program Fundamentals

Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections

1.1 Bits and Bit Patterns. Boolean Operations. Figure 2.1 CPU and main memory connected via a bus. CS11102 Introduction to Computer Science

4 Programming Fundamentals. Introduction to Programming 1 1

CSC 1051 Algorithms and Data Structures I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

REPRESENTING INFORMATION:

CS 112 Introduction to Programming

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

CISC 1600 Lecture 3.1 Introduction to Processing

Worksheet - Storing Data

CS-201 Introduction to Programming with Java

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

TWO-DIMENSIONAL FIGURES

Data Conversion & Scanner Class

CMSC 1513 Lecture 1.2

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

COMP 110 Introduction to Programming. What did we discuss?

12/22/11. } Rolling a Six-Sided Die. } Fig 6.7: Rolling a Six-Sided Die 6,000,000 Times

public static void main(string[] args) { GTest mywindow = new GTest(); // Title This program creates the following window and makes it visible:

Basics of Java Programming

Introduction to Arrays

INTRODUCTION TO COMPUTERS

Selection Statements and operators

System Unit Components Chapter2

Algorithms and Conditionals

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings

Chapter 6 Primitive types

Chapter. Focus of the Course. Object-Oriented Software Development. program design, implementation, and testing

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

CHAPTER 3 Expressions, Functions, Output

Types and Expressions. Chapter 3

Lecture Set 4: More About Methods and More About Operators

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Transcription:

Data Representation and Applets CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: http://www.csc.villanova.edu/~map/1051/ Data Representation Computers store all information digitally, using binary codes: numbers text images audio video program instructions 9278 9279 9280 9281 9282 9283 9284 9285 9286 01110100 Review Some slides in this presentation are adapted from the slides accompanying Java Software Solutions by Lewis & Loftus Why Binary Numbers? A byte is a group of eight bits Simplest way to represent digital information: Electronic circuits: high/low voltage Magnetic devices (eg hard drive): positive/negative Optical devices (eg DVD): light reflected/not reflected due to microscopic grooves 01110100 a number? a letter? the red component of a pixel? a program instruction? A binary digit is called a bit - binary digit Computing devices store & use binary codes to represent data of all kinds Dr. Papalaskari 1

Binary codes 1 bit 0 1 2 bits 00 01 10 11 Each additional bit doubles the number of possible codes 3 bits 000 001 010 011 100 101 110 111 4 bits 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Binary Codes How many codes? How many codes of N bits? 1 bit? 2 bits? 3 bits? 4 bits? 5 bits? How many bits are needed to represent 64 items? How many bits are needed to represent 80 items? How many bits are needed to represent each of the 50 states (so that each state corresponds to a unique code)? Storage requirements examples If a code requires 5 bits, a document consisting of 4000 such codes will require a total of: 5 x 4000 = 20,000 bits how many bytes is that? If a code requires 8 bits (i.e., a byte), a document consisting of 2000 such codes will require 2000 bytes. how many bits is that? If a code requires 32 bits, a program that needs to store 2000 such codes will require bits or bytes. Storage Capacity Every memory device has a storage capacity, indicating the number of bytes it can hold Unit Symbol Number of Bytes kilobyte KB 2 10 = 1024 megabyte MB 2 20 (over one million) gigabyte GB 2 30 (over one billion) terabyte TB 2 40 (over one trillion) petabyte PB 2 50 (a whole bunch) Dr. Papalaskari 2

What s a picture? Representing Images programs represent pictures as grids of picture elements or pixels Bitmap 1 bit Grayscale 8 bits RGB Color 3 colors: red, green, blue 8 bits/color 24 bits Stephanos with his eraser collection Example: Representing Pixels red=116 green=86 blue=142 Additive/Subtractive Color y = 8 Color ( 01110100, 01010110, 10001110 ) We choose 3 primary colors that can be combined to produce almost all visible colors x = 11 Additive primaries - combining light Red Green Blue Subtractive primaries - combining ink, thus subtracting light Cyan Yellow Magenta fff Dr. Papalaskari 3

Bitmap image Grayscale Image A 2000 by 1000 bitmap image contains Bitmap 1 bit/pixel A 2000 by 1000 grayscale image contains Grayscale 8 bits/pixel pixels. pixels. Thus, we need bits to Thus, we need bytes to represent this image, or bytes to represent this image, or bits RGB Image A 2000 by 1000 RGB image contains pixels. RGB color 24 bits/pixel 3 bytes/pixel Graphics and images in Java Images are represented as objects in Java. Color components and positions for pixels can be represented as integers (but also in other ways). Thus, we need bytes to represent this image, or bits Java libraries allow flexibility, providing many alternative ways of representing and processing images. We will be using the Graphics class of the awt package and Japplet from the swing Dr. Papalaskari 4

Applets A Java applet is a program that is intended to be transported over the Web and executed using a web browser An applet doesn't have a main method The type of program we have seen so far is a Java application - a stand-alone program with a main method Example: Einstein.java // Einstein.java Author: Lewis/Loftus // // Demonstrates a basic applet. import javax.swing.japplet; import java.awt.*; public class Einstein extends JApplet // Draws a quotation by Albert Einstein among some shapes. public void paint (Graphics page) page.drawrect (50, 50, 40, 40); // square page.drawrect (60, 80, 225, 30); // rectangle page.drawoval (75, 65, 20, 20); // circle page.drawline (35, 60, 100, 120); // line page.drawstring ("Out of clutter, find simplicity.", 110, 70); page.drawstring ("-- Albert Einstein", 130, 100); // Einstein.java Author: Lewis/Loftus // // Demonstrates a basic applet. import javax.swing.japplet; import java.awt.*; public class Einstein extends JApplet // Draws a quotation by Albert Einstein among some shapes. public void paint (Graphics page) page.drawrect (50, 50, 40, 40); // square page.drawrect (60, 80, 225, 30); // rectangle page.drawoval (75, 65, 20, 20); // circle page.drawline (35, 60, 100, 120); // line page.drawstring ("Out of clutter, find simplicity.", 110, 70); page.drawstring ("-- Albert Einstein", 130, 100); Drawing a Line 20 45 Y 10 150 Start x y page.drawline (10, 20, 150, 45); or End x y page.drawline (150, 45, 10, 20); X Dr. Papalaskari 5

Drawing a Rectangle Drawing an Oval 50 X 175 X 20 20 40 80 100 bounding rectangle Y Start x y Width Height Y 50 page.drawrect (50, 20, 100, 40); page.drawoval (175, 20, 50, 80); Drawing an Arc An arc is defined by an oval, a start angle, and an arc angle: Filled vs unfilled shapes Instead of using drawrect(), drawoval() etc, we can use fillrect(), filloval() etc We can set the color using setcolor() See Snowman.java See also Snowman applet on a webpage Dr. Papalaskari 6

// Snowman.java Author: Lewis/Loftus // // Demonstrates basic drawing methods and the use of color. import javax.swing.japplet; import java.awt.*; public class Snowman extends JApplet // Draws a snowman. public void paint (Graphics page) final int MID = 150; final int TOP = 50; continued page.setcolor (Color.white); page.filloval (MID-20, TOP, 40, 40); // head page.filloval (MID-35, TOP+35, 70, 50); // upper torso page.filloval (MID-50, TOP+80, 100, 60); // lower torso page.setcolor (Color.black); page.filloval (MID-10, TOP+10, 5, 5); // left eye page.filloval (MID+5, TOP+10, 5, 5); // right eye setbackground (Color.cyan); page.drawarc (MID-10, TOP+20, 20, 10, 190, 160); // smile page.setcolor (Color.blue); page.fillrect (0, 175, 300, 50); // ground page.drawline (MID-25, TOP+60, MID-50, TOP+40); // left arm page.drawline (MID+25, TOP+60, MID+55, TOP+60); // right arm continued page.setcolor (Color.yellow); page.filloval (-40, -40, 80, 80); // sun page.drawline (MID-20, TOP+5, MID+20, TOP+5); // brim of hat page.fillrect (MID-15, TOP-20, 30, 25); // top of hat The Java Color Class A color in a Java program is represented as an object created from the Color class The Color class also contains several predefined colors, eg: Object RGB Value Color.black 0, 0, 0 Color.blue 0, 0, 255 Color.cyan 0, 255, 255 Color.orange 255, 200, 0 Color.white 255, 255, 255 compiler Color.yellow 255, 255, 0 machine-dependent Using a color: page.setcolor(color.blue); low-level language 00101011 00000000 Creating a new color: 00010001 Machine 01010000 00101000 Color salmon = new Color(255, 140, 128); code page.setcolor(salmon); Translation of programs into machine code High-level language source code 01001010 00000000 00010001 01010000 00101000 01001010 Dr. Papalaskari 7

Java Translation is different High-level language Java source code Java compiler Bytecode interpreter Java bytecode machine-independent intermediate language Bytecode compiler machine-dependent low-level language 00101011 00000000 Machine 00010001 01010000 code 00101000 01001010 00000000 00010001 01010000 00101000 CSC 1051 M.A. 01001010 Papalaskari, Villanova University The HTML applet Tag An applet is embedded into an HTML file using a tag that references the bytecode file of the applet The bytecode version of the program is transported across the web and executed by a Java interpreter that is part of the browser. <html> <head> <title>the Einstein Applet</title> </head> <body> <applet code="einstein.class" width=350 height=175> </applet> </body> </html> Numeric Primitive Data The difference between the numeric primitive types is their size and the values they can store: Type Storage Min Value Max Value Characters in Java Characters, including spaces, digits, and punctuation are represented by numeric codes H i, H e a t h e r. byte short int long float double 8 bits 16 bits 32 bits 64 bits 32 bits 64 bits -128-32,768-2,147,483,648 < -9 x 10 18 127 32,767 2,147,483,647 > 9 x 10 18 +/- 3.4 x 10 38 with 7 significant digits +/- 1.7 x 10 308 with 15 significant digits 72 105 44 32 72 101 97 116 104 101 114 46 The ASCII (American Standard Code for Information Interchange) character set uses eight bits per character, allowing for 256 unique characters 01110100 ASCII 00000000 01110100 UNICODE The Unicode character set extends ASCII to sixteen bits per character, allowing for 65,536 unique characters. Dr. Papalaskari 8

Characters in Java A char variable stores a single character Character literals are delimited by single quotes: 'a' 'X' '7' '$' ',' '\n' Note the difference between a primitive character variable, which holds only one character, and a String object, which can hold multiple characters char grade = 'A'; char terminator = ';', separator = ' ', newline = '\n'; Automatic type conversion Values of different types can be combined in an assignment or an expression Example: int dollars = 5; double money = dollars + 2.50; System.out.println(dollars + " dollars"); char letter = 't'; char next = letter++; // 'u' These are all examples of widening conversions, i.e, smaller data type è larger data type Converting from one type to another Widening conversions small data type è larger one eg: int è double 32 bits è 64 bits Narrowing conversions large data type è smaller one eg: double è int 64 bits è 32 bits narrowing conversions can lose information! narrowing conversions cannot happen automatically (for example, through assignment) Casting Casting forces a change of type, even if information is lost Can be used for both widening and narrowing conversion To cast, put the type in parentheses in front of the value to be converted: int total = 5; double result = (double) total / 2; int answer = (int) result + 4; double angle = 0; // 0 radians int x = (int)(math.cos(angle) * 300); (cast has higher precedence than arithmetic operators) Dr. Papalaskari 9

Data Conversion How to use cast? Widening Conversions Narrowing Conversions Forcing floating point division between int expressions int qp = 35; int credits = 10; double gpa = (double) qp / credits; int qp = 35; int credits = 10; gpa should be 3.5? double gpa = (double) (qp / credits); How to use cast? Scaling a double and converting to int double gpa = 3.2; int gpapercent = (int) (gpa / 4) * 100; double gpa = 3.2; gpapercent should be 80 int gpapercent = (int) ((gpa / 4) * 100);? char ó int Conversion A char variable is stored as its unicode representation char ó int conversion: convert between char and its unicode. e.g.: t ó 116 increment and decrement of char variables takes you up and down in alphabetical order (codes are in numeric sequence) char letter = 't'; int lettercode = letter; // 116 (code for 't ) char next = letter++; // 'u' int nextcode = next; // 117 (code for 'u ) char nextalt = (char) nextcode; // 'u // narrowing conversion - requires cast char whatsthis = (char) ('A' + 3); //??? int num = (int) (letter 'p'); //??? Dr. Papalaskari 10