CSC 1051 M.A. Papalaskari, Villanova University 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/ f13
53
Embed
CSC 1051 M.A. Papalaskari, Villanova University Data Representation and Applets CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
CSC 1051 M.A. Papalaskari, Villanova University
Data Representation and Applets
CSC 1051 – Data Structures and Algorithms IDr. Mary-Angela Papalaskari
Department of Computing Sciences
Villanova University
Course website:
http://www.csc.villanova.edu/~map/1051/f13
Some slides in this presentation are adapted from the slides accompanying Java Software Solutions by Lewis & Loftus
• Development of number systems– Abacus (2400 BC)– Number systems (Babylonian, Greek, Roman, Arabic
1000 BC - 800 AD)
• The notion of an algorithm– Euclid (300 BC) – al-Khwārizmī (780 AD)
• Creation of special purpose calculators– Stonehenge (1900-1600 BC)– Napier's bones (1600, a precursor of the slide rule)– Pascal's adder (1642)– Leibniz's calculator (1670s)– modern calculators
CSC 1051 M.A. Papalaskari, Villanova University
Mechanization of Arithmetic
+ Automatic Control of Computation
= Modern ComputerCSC 1051 M.A. Papalaskari, Villanova University
Computer ScienceCan be viewed as a culmination of humanity’s search for understanding of:• Problem solving• Mechanization• Computation• Representation & encoding• AbstractionJust like Physics and other sciences branched off from philosophy during the renaissance, so CS emerged in the 20th century from the work of philosophers and mathematicians (with the help of dedicated, visionary practitioners, experimental scientists and engineers).
CSC 1051 M.A. Papalaskari, Villanova University
Analog vs. Digital Data• Analog
– continuous, in direct proportion to the data represented– music on a record album - a needle rides on ridges in the grooves
that are directly proportional to the voltages sent to the speaker
• Digital– information is broken down into pieces, and each piece is
represented separately– sampling – record discrete values of the analog representation
CSC 1051 M.A. Papalaskari, Villanova University
Binary Numbers
• Number system consisting of 1’s & 0’s• Simplest way to represent digital
information• modern computers use binary numbers
internally
A binary digit is called a bit - binary digitA byte is a group of eight bits
CSC 1051 M.A. Papalaskari, Villanova University
Representing and processing bits
• Electronic circuits: high/low voltage
• Magnetic devices (eg hard drive): positive/negative
• Optical devices (eg DVD): light reflected/not reflected due to microscopic grooves
Each additional bit doubles the number of possible permutations
CSC 1051 M.A. Papalaskari, Villanova University
Bit Permutations
• How many permutations of N 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)?
1 bit ?
2 bits ?
3 bits ?
4 bits ?
5 bits ?
How manyitems can be
represented by
CSC 1051 M.A. Papalaskari, Villanova University
Storage requirements• If a code requires 5 bits, a document consisting of
1000 such codes will require 5000 bits total
• If a code requires 8 bits (i.e., a byte), a document consisting of 2000 such codes will require 16,000 bits total, or 2000 bytes.
Because the numbers get very large very quickly, we usually measure storage requirements in terms of kilobits, megabits, etc or kilobytes, megabytes, etc.
CSC 1051 M.A. Papalaskari, Villanova University
Storage Capacity
• Every memory device has a storage capacity, indicating the number of bytes it can hold
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
CSC 1051 M.A. Papalaskari, Villanova University
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).• 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
Next:
Java applets – learning to paint on a Graphics context
CSC 1051 M.A. Papalaskari, Villanova University
CSC 1051 M.A. Papalaskari, Villanova University
//********************************************************************// 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
X
Y
10
20
150
45
page.drawLine (10, 20, 150, 45);
page.drawLine (150, 45, 10, 20);
or
CSC 1051 M.A. Papalaskari, Villanova University
Startx y
Endx y
Drawing a Rectangle
X
Y
page.drawRect (50, 20, 100, 40);
50
20
100
40
CSC 1051 M.A. Papalaskari, Villanova University
Startx y
Width Height
Drawing an Oval
X
Y
page.drawOval (175, 20, 50, 80);
175
20
50
80
boundingrectangle
CSC 1051 M.A. Papalaskari, Villanova University
Drawing an Arc• An arc is defined by an oval, a start angle, and an
arc angle:
CSC 1051 M.A. Papalaskari, Villanova University
Filled vs unfilled shapes
• Instead of using drawRect(), drawOval() etc, we can use fillRect(), fillOval() etc
//********************************************************************// 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;