Review.

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

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.
Variables and Operators
Chapter 2 Programming by Example. A Holistic Perspective Three sections separated by blank lines. Program comment File: FileName.java
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
A Simple Applet. Applets and applications An applet is a Java program that runs on a web page –Applets can be run from: Internet Explorer Netscape Navigator.
Chapter 4 Statement Forms. Statement types 1.Simple statements expression; println(“The total is “ + total + “.”); (method call) 2. Compound statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
METHODS AND SCOPE CSCE More on Methods  This is chapter 6 in Small Java.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Graphical Structures Eric Roberts CS 106A January 29, 2010.
Flow of Control Part 1: Selection
Applets Applet is java program that can be embedded into HTML pages. Java applets runs on the java enabled web browsers such as mozilla and internet explorer.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Chapter 4—Statement Forms The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Statement Forms C H A P T E R 4 The statements.
Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for.
Expressions Eric Roberts CS 106A January 13, 2016.
Simple Java Eric Roberts CS 106A January 11, 2016.
Test Review. General Info. All tests will be comprehensive. You will be tested more on your understanding of code as opposed to your ability to write.
Control Statements Eric Roberts CS 106A January 15, 2010.
Chapter 4 Statement Forms. Statement types 1.Simple statements expression; println(“The total is “ + total + “.”); (method call) 2. Compound statements.
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.
6.1 Coordinates The screen of a computer is a grid of little squares called pixels. The color of each pixel can be set individually, and drawing on the.
CS 106A, Lecture 27 Final Exam Review 1
Chapter 02 Data and Expressions.
CS 106A, Lecture 4 Introduction to Java
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.
Chapter 3 Syntax, Errors, and Debugging
Building Java Programs
Primitive Data, Variables, Loops (Maybe)
CS106A, Stanford University
Java Programming: Guided Learning with Early Objects
Beyond Console Programs
Variables, Printing and if-statements
reading: Art & Science of Java,
CS106A, Stanford University
Computation as an Expressive Medium
CS 106A, Lecture 12 More Graphics
CS 106A, Lecture 7 Parameters and Return
CS 106A, Lecture 6 Control Flow and Parameters
CS106A, Stanford University
CS 106A, Lecture 22 More Classes
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Arrays, For loop While loop Do while loop
IDENTIFIERS CSC 111.
CS 106A, Lecture 5 Booleans, Control Flow and Scope
CS106A, Stanford University
Chapter 2—Programming by Example
Introduction to C++ Programming
SSEA Computer Science: Track A
Building Java Programs
Building Java Programs
CS106A, Stanford University
Graphics.
CS106A, Stanford University
Algorithms computer as the tool process – algorithm
Chapter 2 Programming Basics.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 2—Programming by Example
Just Enough Java 17-May-19.
Building Java Programs
Building Java Programs
Presentation transcript:

Review

Java

Three Properties of Variables name type total 42 (contains an int) Type name = value value

Types // integer values int num = 5; // real values double fraction = 0.2; // letters char letter = ‘c’; // true or false boolean isLove = true; * Why is it called a double?

Binary Operators + Addition * Multiplication – Subtraction / Division % Remainder Today is your day, tio

Remainder // an example of the % operator println(17 % 4); // reads a number from the user int num = readInt("?: "); // stores the ones digit int onesDigit = num % 10; // equal to 1 if num is odd, // 0 if num is even. int isOdd = num % 2;

Binary Operators + Addition * Multiplication – Subtraction / Division % Remainder

Challenge Carbon Dating Write a program that can turn a measurement of C14 into an estimate of age.

Example: Carbon Dating C14 = 1.2 dpm C14 = 13.6 dpm

Carbon Dating Equation Amount of C14 in your sample Amount of C14 in a living sample Half life of C14 Age of the sample ½ because of half life convention * Some of these values are constants ** Use the function: Math.log( num )

End Review

Today’s Goal Know how to use graphics variables Understand For / While / If in Java

Today’s Route You are here Simple Java For Loops Conditions Graphics Review The River of Java

Graphics Programs

GRect GRect is a variable type that stores a rectangle. As an example, the following run method displays a blue square public void run() { Grect rect = new GRect(200, 200); rect.setFilled(true); rect.setColor(Color.BLUE); add(rect, 50, 50); } LargestOval

Graphics Coordinates 0,0 40,20 120,40 getHeight(); 40,120 getWidth();

Learn By Example

Graphics Variable Types GObject GLabel GRect GOval GLine GRect myRect = new GRect(350, 270);

Primitives vs Classes Primitive Variable Types Class Variable Types int double char boolean GRect GOval GLine … Class variables: Have upper camel case types You can call methods on them Are constructed using new Are stored in a special way

Graphics Trace hello, world hello, world hello, world The following program illustrates sending a message to an object. Note that the label doesn’t appear until it is added to the canvas. public class HelloProgram extends GraphicsProgram { public void run() { GLabel label = new GLabel("hello, world"); label.setFont("SansSerif-36"); label.setColor(Color.RED); add(label, 100, 75); } label hello, world hello, world hello, world HelloProgram hello, world skip simulation Graphic courtesy of Eric Roberts

Label Location hello, world (0, 0) (100, 75) GLabel coordinates are baseline of first character (0, 0) HelloProgram hello, world (100, 75) Graphic courtesy of Eric Roberts

Operations on GRect object.setColor(color) Sets the color of the object to the specified color constant. The standard color names are defined in the java.awt package: Color.BLACK Color.DARK_GRAY Color.GRAY Color.LIGHT_GRAY Color.WHITE Color.RED Color.YELLOW Color.GREEN Color.CYAN Color.BLUE Color.MAGENTA Color.ORANGE Color.PINK

Operations on GRect object.setColor(color) object.setLocation(x, y) Sets the color of the object to the specified color constant. object.setLocation(x, y) Changes the location of the object to the point (x, y). object.move(dx, dy) Moves the object on the screen by adding dx and dy to its current coordinates. 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. object.getWidth() Returns the width of the rectangle. object.getHeight() Returns the height of the rectangle.

Operations on GOval object.setColor(color) object.setLocation(x, y) Sets the color of the object to the specified color constant. object.setLocation(x, y) Changes the location of the object to the point (x, y). object.move(dx, dy) Moves the object on the screen by adding dx and dy to its current coordinates. 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. object.getWidth() Returns the width of the rectangle. object.getHeight() Returns the height of the rectangle.

Operations on GLabel Methods specific to the GLabel class label.setFont( font) Sets the font used to display the label as specified by the font string. label.getAscent( ) Returns the height of the label above its baseline. label.getWidth( ) Returns the width of the label. The font is typically specified as a string in the form "family-style-size" family is the name of a font family style is either PLAIN, BOLD, ITALIC, or BOLDITALIC size is an integer indicating the point size Based on slides by Eric Roberts

Construction new GRect( width, height) new GOval(width, height) Creates a rectangle with dimensions width and height. new GOval(width, height) Creates an oval that fits inside the rectangle with the same dimensions. new GRect( x, y, width, height) Creates a rectangle whose upper left corner is at (x, y) of the specified size. new GOval( x, y, width, height) Creates an oval that fits inside the rectangle with the same dimensions. new GLabel( text) Creates a label with the given text. new GLine( x1, y1, x2, y2) Creates a line with the given start and end points. Based on slides by Eric Roberts

Graphics Methods add( object) add( object, x, y) getWidth() Adds an object to the canvas. Location is not specified. add( object, x, y) Adds an object to the canvas at a specific location. getWidth() Returns the width of the screen. getHeight( object, x, y) Returns the height of the screen. Based on slides by Eric Roberts

Another Example

Today’s Route You are here Simple Java For Loops Conditions Graphics Review The River of Java

Today’s Route You are here Simple Java For Loops Conditions Graphics Review The River of Java

While Loop in Karel while(frontIsClear()) { body } if(beepersPresent()) { body }

The condition should be a “boolean” which is either true or false While Loop Redux while(condition) { body } if(condition) { body } The condition should be a “boolean” which is either true or false

What Does This Do? * It calculates the age of a C14 sample // read the amount of C14 from the user double amount = readDouble("Amount of C14 in your sample: "); // use the half life formula to calculate the age double age = Math.log(amount/LIVING_C14) / Math.log(0.5) * HALF_LIFE; age = Math.round(age); println("Your sample is " + age + " years old."); * It calculates the age of a C14 sample

Before repeating the body, check if this statement evaluates to true What Does This Do? Before repeating the body, check if this statement evaluates to true while(true) { // read the amount of C14 from the user double amount = readDouble("Amount of C14 in your sample: "); // use the half life formula to calculate the age double age = Math.log(amount/LIVING_C14) / Math.log(0.5) * HALF_LIFE; age = Math.round(age); println("Your sample is " + age + " years old."); // add an extra line between queries println(""); } * It repeatedly calculates the age of a C14 sample

Boolean Comparisons Equals Test == Not equals != Less than < > Greater than <= Less than equals >= Greater than equals

Guess My Number

Guess My Number int secretNumber = getHeight() % 100; println("I am thinking of a number between 0 and 99..."); int guess = readInt("Enter a guess: "); // true if guess is not equal to secret number while(guess != secretNumber) { // true if guess is less than secret number if(guess < secretNumber) { println("Your guess is too low"); } else { println("Your guess is too high"); } println(""); // an empty line guess = readInt("Enter a new number: "); println("Congrats! The number was: " + secretNumber);

How would you println “Nick rocks socks” 100 times

For Loop Redux public void run() { for(int i = 0; i < 100; i++) { println(“Nick rocks socks!”); }

For Loop Redux for(int i = 0; i < 100; i++) { Enters the loop if this condition passes This line is run each time the code gets to the end of the ‘body’ This line is run once, just before the for loop starts for(int i = 0; i < 100; i++) { println(“Nick rocks socks!”); }

For Loop Redux for(int i = 0; i < 3; i++) { println(“Nick rocks socks!”); } For Loop Redux

For Loop Redux for(int i = 0; i < 3; i++) { println(“Nick rocks socks!”); } For Loop Redux

For Loop Redux i for(int i = 0; i < 3; i++) { for(int i = 0; i < 3; i++) { println(“Nick rocks socks!”); } For Loop Redux

For Loop Redux i for(int i = 0; i < 3; i++) { for(int i = 0; i < 3; i++) { println(“Nick rocks socks!”); } For Loop Redux

For Loop Redux i for(int i = 0; i < 3; i++) { for(int i = 0; i < 3; i++) { println(“Nick rocks socks!”); } For Loop Redux Nick rocks socks

For Loop Redux i 1 for(int i = 0; i < 3; i++) { println(“Nick rocks socks!”); } For Loop Redux Nick rocks socks

For Loop Redux i 1 for(int i = 0; i < 3; i++) { println(“Nick rocks socks!”); } For Loop Redux Nick rocks socks

For Loop Redux i 1 for(int i = 0; i < 3; i++) { println(“Nick rocks socks!”); } For Loop Redux Nick rocks socks Nick rocks socks

For Loop Redux i 2 for(int i = 0; i < 3; i++) { println(“Nick rocks socks!”); } For Loop Redux Nick rocks socks Nick rocks socks

For Loop Redux i 2 for(int i = 0; i < 3; i++) { println(“Nick rocks socks!”); } For Loop Redux Nick rocks socks Nick rocks socks

For Loop Redux i 2 for(int i = 0; i < 3; i++) { println(“Nick rocks socks!”); } For Loop Redux Nick rocks socks Nick rocks socks Nick rocks socks

For Loop Redux i 3 for(int i = 0; i < 3; i++) { println(“Nick rocks socks!”); } For Loop Redux Nick rocks socks Nick rocks socks Nick rocks socks

For Loop Redux i 3 for(int i = 0; i < 3; i++) { println(“Nick rocks socks!”); } For Loop Redux Nick rocks socks Nick rocks socks Nick rocks socks

For Loop Redux for(int i = 0; i < 3; i++) { println(“Nick rocks socks!”); } For Loop Redux Nick rocks socks Nick rocks socks Nick rocks socks

For Loop Redux for(int i = 0; i < 3; i++) { println(“Nick rocks socks!”); } For Loop Redux Nick rocks socks Nick rocks socks Nick rocks socks

You can use the for loop variable

How would you println the first 100 even numbers?

Printing Even Numbers

Printing Even Numbers for(int i = 0; i < NUM_NUMS; i++) { println(i * 2); }

Printing Even Numbers for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux

Printing Even Numbers for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux

Printing Even Numbers i for(int i = 0; i < 3; i++) { for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux

Printing Even Numbers i for(int i = 0; i < 3; i++) { for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux

Printing Even Numbers i for(int i = 0; i < 3; i++) { for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux

Printing Even Numbers i 1 for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux

Printing Even Numbers i 1 for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux

Printing Even Numbers i 1 for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux 2

Printing Even Numbers i 2 for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux 2

Printing Even Numbers i 2 for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux 2

Printing Even Numbers i 2 for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux 2 4

Printing Even Numbers i 3 for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux 2 4

Printing Even Numbers i 3 for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux 2 4

Printing Even Numbers i 3 for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux 2 4

Printing Even Numbers for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux 2 4

Today’s Route You are here Simple Java For Loops Conditions Graphics Review The River of Java

Today’s Route You are here Simple Java For Loops Conditions Graphics Review The River of Java

Today’s Goal Know how to use graphics variables Understand For / While / If in Java

String Art