Lec 17 Using Nested Loops and Objects in an Applet Class

Slides:



Advertisements
Similar presentations
DONT PANIC!! Lots of new notions coming in these slides Dont worry if not all of it makes perfect sense Well meet most of this stuff again in detail later.
Advertisements

CPSC 388 – Compiler Design and Construction
Arrays.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Programs Involving Repetition Drawing Grass Drawing grids Printing marks on a ruler Repeatedly rolling dice in craps game.
Writing methods and Java Statements. Java program import package; // comments and /* … */ and /** javadoc here */ public class Name { // instance variables.
Repetition Structures: For Loop Constants CSC 1401: Introduction to Programming with Java Week 5 Wanda M. Kunkle.
Lecture 2: Variables and Expressions Yoni Fridman 6/29/01 6/29/01.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Java Unit 9: Arrays Declaring and Processing Arrays.
Review Blocks of code {.. A bunch of ‘statements’; } Structured programming Learning Processing: Slides by Don Smith 1.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Lec 19 Array Intro. Agenda Arrays—what are they Declaring Arrays Constructing Arrays Accessing Array cells.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Java Loops (Java: An Eventful Approach, Ch 7 and 13), Slides Credit: Bruce, Danyluk and Murtagh CS 120 Lecture 16 6 November 2012.
Sahar Mosleh California State University San MarcosPage 1 A for loop can contain multiple initialization actions separated with commas Caution must be.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
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.
ECE122 Feb. 22, Any question on Vehicle sample code?
Introduction to Programming Writing Java Beginning Java Programs.
Copyright Curt Hill Variables What are they? Why do we need them?
In Java.... Variables contain the values of primitive data types Value Types.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Arithmetic, Class Variables and Class Methods Week 11
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
CSC 142 Computer Science II Zhen Jiang West Chester University
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
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.
CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
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.
(Java Looping and Conditional Statements). Flow of control Sequential Executes instructions in order Method Calls Transfer control to the methods, then.
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Elementary Programming
The switch Statement, and Introduction to Looping
Yanal Alahmad Java Workshop Yanal Alahmad
Lecture 2: Data Types, Variables, Operators, and Expressions
Variables and Arithmetic Operators in JavaScript
Chapter 3: Using Methods, Classes, and Objects
University of Central Florida COP 3330 Object Oriented Programming
Chapter 3 Assignment Statement
Java Programming: Guided Learning with Early Objects
Type Conversion, Constants, and the String Object
Using local variable without initialization is an error.
Type Conversion, Constants, and the String Object
Unit-1 Introduction to Java
Chapter 1: Computer Systems
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Programs Involving Repetition
CSC240 Computer Science III
Chapter 2: Java Fundamentals
Control structures Chapter 3.
Fundamentals of visual basic
Recap Week 2 and 3.
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s will be added to the course web site by the end of the week. Labs start next week in JEFF 155:
Programs and Classes A program is made up from classes
Control structures Chapter 3.
Chapter 2 Programming Basics.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Java Programming Language
Control structures Chapter 3.
Chap 2. Identifiers, Keywords, and Types
Agenda Warmup Lesson 2.2 (parameters, etc)
Loops CGS3416 Spring 2019 Lecture 7.
Looping and Repetition
Presentation transcript:

Lec 17 Using Nested Loops and Objects in an Applet Class

Agenda More Applet stuff Drawing a Brick Wall Using objects in Applets Constants (final) While Loops again Nested Loops Using objects in Applets Point object to locate LL corner of wall (and move it) Default values and null

Introduction to final You can make sure that a variable never gets changed once it's initialized (i.e. contstants) Use final in the variable declaration to do that It means: ``Once this variable gets a value, it's final!'' Let's try it out in an Applet that draws Bricks of certain height and width, all the same (constant) size We use the all caps convention when declaring names for constants Use underscores to separate multiple words in the variable name Can also combine static and final to get a class constant Constants can be made public (this is the one exception to the ``must be private'' rule for instance and class variables) If it's a constant, we know nobody will mess it up by accident (especially ourselves)

Constants that pin down the size of a Brick public static final int BRICK_WIDTH=30; public static final int BRICK_HEIGHT=10; ... public void paint(Graphics g) { g.fillRect(100,100,BRICK_WIDTH,BRICK_HEIGHT); g.fillRect(140,100,BRICK_WIDTH,BRICK_HEIGHT); g.fillRect(180,100,BRICK_WIDTH,BRICK_HEIGHT); g.fillRect(220,100,BRICK_WIDTH,BRICK_HEIGHT); } BUT this is kind of silly, we could use a loop to draw a row of bricks

The while Loop A control construct for specifying repetition General Structure: while (condition) { //Statements to be repeated }

The Counting while loop Counting up int i=initialValue; while(i<endValue){ //statements to be repeated i++; } We'll use while loops to draw our bricks

And, now that that works What if we want the wall to move around with the arrow keys. Need instance variables, x and y OR a Point for lowerLeft corner of wall. Let's use Point since that lets us talk about null

Default values Every variable defaults to some fixed value For primitive variables: Variables of numerical type (double, float, int, byte, short, long) all default to 0 boolean variables default to false For reference (i.e. object) variables: Variables of any class type default to null Default values are a moot point for: local variables (which are required to be initialized before being used) parameters (they are always initialized by the call to the method) If you always initialize your static variables and instance variables, you don't have to worry about this at all

null If a variable is null then it points to no object at all So if you try calling a method on a variable that points to null, you get a runtime error You can set a reference variable to null using an assignment statement if you don't want it to point to anything Think of null as being a fixed point in memory with nothing there Primitive variables cannot be assigned null. The concept null only makes sense when talking about reference variables

Lab17TreasureApplet Redo Treasure Island as an applet class