Computer Science 111 Fundamentals of Computer Programming I - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Computer Science 111 Fundamentals of Computer Programming I

Description:

Often we need many different variables that are of the ... The length of an array is stored as a public (naughty, naughty) instance variable called length. ... – PowerPoint PPT presentation

Number of Views:289
Avg rating:3.0/5.0
Slides: 15
Provided by: tomwh
Category:

less

Transcript and Presenter's Notes

Title: Computer Science 111 Fundamentals of Computer Programming I


1
Computer Science 111Fundamentals of Computer
Programming I
  • Introduction to Arrays
  • and Color

2
(No Transcript)
3
The Need for Arrays
  • Often we need many different variables that are
    of the same type and play a similar role in the
    program
  • For example, suppose we have a temperature
    reading for each day of the year and need to
    manipulate these values several times within a
    program.
  • With simple variables,
  • We would need to declare 365 variables.
  • We would not be able to loop over these variables.

4
General Comments about Arrays
  • An array is a numbered collection of variables of
    the same type.
  • The variables in an array share the same name and
    are distinguished from one another by their
    numbers or subscripts.
  • We can loop through the variables of an array by
    using a variable for the subscript.
  • The subscripts are always 0,1,, size-1

5
Referencing Array Elements
  • Suppose we have an array, temperature, for the
    temperature readings for the year.
  • The subscripts would be 0,1,,364.
  • To refer to the reading for a given day, we use
    the name of the array with the subscript in
    brackets temperature4 for the fifth day.

6
Referencing Array Elements (cont)
  • To assign the value 89 for the 150th
    day temperature149 89
  • We can loop through the elements of an array by
    varying the subscript. To set all of the values
    to 0, say for (i 0 i temperaturei 0

7
Declaring an Array
  • Like all variables, arrays must be declared
    before being used.
  • In declaring an array, we give
  • The type of the array elements
  • A pair of brackets, , to indicate an array
  • The name of the array variable
  • For example, int temperature

8
Declaring and Creating an Array
  • We can declare and create an array of a given
    size as follows
  • int temperature new int365
  • This creates temperature as an array with space
    for 365 integers.
  • Alternatively, we can specify the entries
  • double scores 2.1, 3.7, 8.8, 4.7, 5.0
  • This creates scores as an array with space for 5
    doubles and with scores 0 2.1, etc.

9
The length of an array
  • One attribute of an array object is its length
    i.e., the number of elements it can hold.
  • The length of an array is stored as a public
    (naughty, naughty) instance variable called
    length.
  • temperature.length should give 365

10
Disabling buttons
  • If we are working with a GUI, there may be times
    when we do not want the user to be able to use a
    certain button. For example, we may not want the
    user to be able to use a Hit button in blackjack
    before any cards have been dealt.
  • We can disable and enable buttons as
    follows hitButton.setEnabled(false) dealButton
    .setEnabled(true)

11
Color Constants
  • To use the java Color class, you should import
    java.awt.
  • The Color class has the following constants
  • red black
  • yellow white
  • blue gray
  • orange lightGray
  • pink darkGray
  • cyan
  • magenta

12
Using a little color with GUIs
  • You can do the following, say in a constructor
    chickenButton.setForeground(Color.yellow)
  • To color the window itself this.getContentPane(
    ).setBackground(Color.magenta)

13
More on color
  • RGB colors can be created by specifying their
    red, green and blue components. Each of these is
    specified as a whole number in the range of
    0-255.
  • The Color class has a constructor that takes 3
    such numbers and produces the corresponding
    color Color myColor new Color(138,100,86) in
    t redAmt randomInt(0,255) int greenAmt
    randomInt(0,255) int blueAmt
    randomInt(0,255) Color oldWeirdColor new
    Color(redAmt,greenAmt,blueAmt)

14
An applet a day...
Write a Comment
User Comments (0)
About PowerShow.com