10/4: the for loop & the switch structure Primitive data types –why we mention them Return to counter-controlled repetition.

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

Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
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.
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Session 5 JavaScript/JScript: Control Structures II Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
1 Outline Chapter 4 Introduction Control Structures if Single-Selection Statement if else Selection Statement while Repetition Statement Assignment Operators.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Chapter 5 - Control Structures - Part 2 Outline 5.1Introduction 5.2Essentials of Counter-Controlled Repetition 5.3The for Repetition Structure 5.4Examples.
Introduction to Computers and Programming Lecture 7:
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Control Structures: continued.
Control Structures in C++ while, do/while, for switch, break, continue.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
1 Drawing Shapes and Text With Applets. 2 Drawing in the paint method import java.awt.*;// access the Graphics object import javax.swing.*;// access to.
Computer Science [3] Java Programming II - Laboratory Course Lab 6: Introduction to Java Applets Sample Applets from the Java Simple Java Applet: Drawing.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled Repetition.
Agenda For Feb Finish up Unit 3 Exercises on page Unit 4 Exercises on page 33. Question #2 3. Create a Happy Face Java Applet (due next class).
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Control Structures - Part 2 Outline 5.1Introduction 5.2Essentials of Counter-Controlled Repetition.
Chapter 5 Control Structures: Part II 1 3 Used when you know in advance how many times you want the loop to be executed. 4 Requirements: 1.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled.
 2003 Prentice Hall, Inc. All rights reserved. 1 Typecasting.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
10/5: Primitives, the for loop Primitive data types –why we mention them Return to counter-controlled repetition.
10/11: do/while, Logical Operators the revised SwitchTest.java notes on JApplets the do / while repetition structure break continue Logical operators.
Review of CIS 120 Concepts: What you said you want….
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled.
1 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
1 JavaScript: Control Structures. 2 Control Structures Flowcharting JavaScript’s sequence structure.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
(c)2006 E.S.Boese All Rights Reserved.1 Drawing Shapes and Text Chapter 2 – Lecture Slides.
Chapter 2: Graphics In Java Basics of classes –instance variables –methods –overriding methods Graphics class (drawing routines) Other classes –Color –Font.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled Repetition.
2/18: Assignment Operators About Average2.java –while loop use –explicit casting –twoDigits object Assignment Operators Increment & Decrement Operators.
2/25: the switch selection structure looking at Interest.java –NumberFormat –Locale –JTextArea –use of postincrement –Math.pow( rate, year ) switch.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Introduction to Graphics. Graphical objects To draw pictures, we will use three classes of objects: –DrawingPanel : A window on the screen. –Graphics.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Part 1 Outline Counter-Controlled Repetition: Example Sentinel-Controlled Repetition:
7/19: Primitives, the for loop Primitive data types –why we mention them Return to counter-controlled repetition.
Object Oriented Programming Lecture 2: BallWorld.
9/13: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance –information hiding –classes: blueprints for objects Java Applets.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Chapter 02 Data and Expressions.
Algorithm: procedure in terms of
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 4 – Control Structures Part 1
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Advanced Programming Chapters 5 & 6: Control Structures
Structured Program
2.6 The if/else Selection Structure
Chapter 5 – Control Structures: Part 2
Chapter 5 – Control Structures: Part 2
Presentation transcript:

10/4: the for loop & the switch structure Primitive data types –why we mention them Return to counter-controlled repetition

Primitives: why we mention them recall Average2.java (p. 132) program: average = ( double ) total / gradeCounter ; cast operators are used to explicitly promote (or change) one primitive type to another. total and gradeCounter are both of type int. To return a non- int result from the equation, we need to specify a different type (ex: double ).

Primitive data types: numbers typebitsrange of values short 16-32,768 to +32,768 int 32-2,147,483,648 to +2,147,483,648 long 64-9,223,372,036,854,775,808 to +9,223,372,036,854,775,808 float E+38 to E+38 double E+308 to E+308

Primitive data types: other typebitsrange of values boolean 1true or false char 16‘\u0000’ to ‘\uFFFF’ Unicode character set byte to +127

Counter-controlled repetition: theory Counter-controlled repetition requires: 1. the name of a control variable (loop counter); 2. the initial value of the control variable; 3. the increment (or decrement) by which the control variable is modified each pass through the loop. 4. the condition that tests for the final value of the control variable.

Example: draw ten rectangles. //a while loop using counter-controlled repetition. import javax.swing.JApplet; //import JApplet class import java.awt.Graphics; //import Graphics class public class WhileCounter extends JApplet { //JApplet is superclass public void paint ( Graphics g ) //method for drawing { int count = 1; //counting repetitions int place = 25; //starting location for rectangle while ( count <= 10 ) { g.drawRect ( place, place, 40, 40 ); ++count; //increment count place += 15; //bump up place by 15 } name of control variable initial value increment condition for final value

the for repetition structure A more efficient way of creating a repetition structure: Contains all 4 elements necessary for repetition. for ( int count = 1; count <= 10; count++ ) name of control variable initial value increment condition for final value

What will be replaced. //a while loop using counter-controlled repetition. import javax.swing.JApplet; //import JApplet class import java.awt.Graphics; //import Graphics class public class WhileCounter extends JApplet { //JApplet is superclass public void paint ( Graphics g ) //method for drawing { int count = 1; //counting repetitions int place = 25; //starting location for rectangle while ( count <= 10 ) { g.drawRect ( place, place, 40, 40 ); ++count; //increment count place += 15; //bump up place by 15 }

the ‘for’ version //a ‘for’ loop alternative for counter-controlled repetition. import javax.swing.JApplet; //import JApplet class import java.awt.Graphics; //import Graphics class public class ForCounter extends JApplet { //JApplet is superclass public void paint ( Graphics g ) //method for drawing { int place = 25; //starting location for rectangle for ( int count = 1 ; count <= 10 ; ++count ) { g.drawRect ( place, place, 40, 40 ); place += 15; //bump up place by 15 }

First program of the day pg. 167 Interest.java After you successfully run the program, alter it to use a “while” loop instead of a “for” loop.

Part 2: the switch selection structure looking at Interest.java –use of postincrement –Math.pow( rate, year ) –JTextArea switch multiple-selection structure

Interest.java: use of postincrement for ( int year = 1 ; year <= 10 ; year++ ) –would a change to a preincrement cause a changed output? – No, because it executes like it is the only thing happening in a statement: year++ ;

Interest.java: Math.pow Math.pow( rate, year ); –Math class method Math.pow( x, y ); –calculates x to the y power: x y –listed in the java.lang.Math libraryjava.lang.Math

Interest.java: JTextArea JTextArea : a type of output area. we create a new object ( outputTextArea ) as an instance of the class JTextArea : instantiation. associated method: append – means add onto the JTextArea some type of String output. –the append method works similarly to saying result = result + “\n” + x + “ dollars”; (EX) View more info about JTextArea at java.sun.comjava.sun.com

the switch multiple-selection structure previously selection structures: if, if/else switch ( pizzaSlice ) { case 1: System.out.print ( "Take more" ); break; case 2: System.out.print ( “Just right” ); break; default: System.out.print ( "Have some pizza, man!" ); break;

switch multiple selection structure consists of case s with labels, including the default case break exits the structure controlling variable’s value –compared to each case label –if no match, the default case is executed can handle integers

Second Program: p. 170 SwitchTest after you get it to work, modify the program: –Ask first for a color ( pick 3 colors: black, blue, cyan, darkGray, gray, lightGray, green, magenta, orange, pink, red, white, yellow ) to use in the next step. You must import the java.awt.Color class. Use the following statement example to set the color: g.setColor ( Color.yellow ); –Then ask the user for their choice of lines, hollow rectangles, filled rectangles, hollow ovals, and filled ovals. applicable methods: drawLine, drawRect, fillRect, drawOval, fillOval.