7/19: Primitives, the for loop Primitive data types –why we mention them Return to counter-controlled repetition.

Slides:



Advertisements
Similar presentations
Introduction to C Systems Programming Concepts. Introduction to C A simple C Program A simple C Program –Variable Declarations –printf ( ) Compiling and.
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Variables and Operators
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
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.
 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
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:
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.
Introduction to C Systems Programming. Systems Programming: Introduction to C 2 Systems Programming: 2 Introduction to C  A ‘C’ Program –Variable Declarations.
CS102 Data Types in Java CS 102 Java’s Central Casting.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
2015/8/221 Data Types & Operators Lecture from (Chapter 3,4)
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Control Structures - Part 2 Outline 5.1Introduction 5.2Essentials of Counter-Controlled Repetition.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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 Typecasting.
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
10/5: Primitives, the for loop Primitive data types –why we mention them Return to counter-controlled repetition.
Java Simple Types CSIS 3701: Advanced Object Oriented Programming.
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….
2/4: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance Java Applets –what is an applet vs. an application? –where do applets.
9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure 4.6 The if / else Selection Structure 4.7.
 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.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Computer Programming Control Structure
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
 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.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
10/4: the for loop & the switch structure Primitive data types –why we mention them Return to counter-controlled repetition.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
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.
2/4: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance –information hiding –classes: blueprints for objects Java Applets.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Object Oriented Programming Lecture 2: BallWorld.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
9/13: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance –information hiding –classes: blueprints for objects Java Applets.
Topics introduced today (these topics would be covered in more detail in later classes) – Primitive Data types Variables Methods “for” loop “if-else” statement.
Chapter 02 Data and Expressions.
Primitive/Reference Types and Value Semantics
- Standard C Statements
Yanal Alahmad Java Workshop Yanal Alahmad
Explicit and Implicit Type Changes
Unit-2 Objects and Classes
null, true, and false are also reserved.
Java I.
Computers & Programming Languages
An Introduction to Java – Part I, language basics
Java Programming Review 1
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Java’s Central Casting
Dale Roberts, Lecturer IUPUI
Chapter 5 – Control Structures: Part 2
Chapter 5 – Control Structures: Part 2
Presentation transcript:

7/19: Primitives, the for loop 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 }

Using for loops for animation: pt. 1 //a for loop as an alternative for counter-controlled repetition. import javax.swing.JApplet; //import JApplet class import java.awt.Graphics; //import Graphics class import java.awt.Color; //import Color class public class ForCounter3 extends JApplet { //JApplet is superclass public void paint ( Graphics g ) //method for drawing { int xPos = 25; //starting x location for rectangle int yPos = 25; //starting y location for rectangle boolean reverseX = false; //"which way to go" trigger boolean reverseY = false; //"which way to go" trigger get java file: ForCounter3

Using for loops for animation: pt. 2 for ( int count = 1 ; count <= 200 ; ++count ) { g.setColor(Color.white); g.drawRect ( xPos, yPos, 40, 40 ); if ( xPos < 300 && reverseX == false ) xPos += 15; //bump up xPos by 15 if xPos < 300 else { xPos -= 5; //reduce xPos by 12 if xPos >= 300 reverseX = true ; if ( xPos < 10 ) reverseX = false ; } get java file: ForCounter3

Using for loops for animation: pt. 3 if ( yPos < 250 && reverseY == false ) yPos += 10; //bump up yPos by 10 if yPos < 250 else { yPos -= 7; //reduce yPos by 7 if yPos >= 250 reverseY = true; if ( yPos < 10 ) reverseY = false; } g.setColor(Color.blue); g.drawRect ( xPos, yPos, 40, 40 ); //just to slow it down and look like animation for ( byte i = 1 ; i < 100 ; i++ ) repaint(); } get java file: ForCounter3

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. Tomorrow: Midterm Exam! –Everything through today’s lecture is fair game.