Todo - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

Todo

Description:

To-do. Review Quiz 5 & Project 5 questions. More exercises. Diagram memory usage of arrays. Arrays of primitive-type variables. Arrays of objects ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 9
Provided by: name9
Category:
Tags: todo | usage

less

Transcript and Presenter's Notes

Title: Todo


1
To-do
  • Review Quiz 5 Project 5 questions
  • More exercises
  • Diagram memory usage of arrays
  • Arrays of primitive-type variables
  • Arrays of objects
  • Programming exercises on arrays

2
Array variables
  • Array variables are references
  • double a -1.0, 4.5, 0.5
  • char b A, a, B, D
  • char c b //copy reference b to c

-1.0
A
a0
a
b
4.5
a
a1
0.5
B
a2
c
D
3
Diagram the memory usage of the following.
  • int a0 -2, 3, 4
  • int a1 new int6
  • Array.Copy(a0, a1, 3)
  • int a2 a1
  • int a3

4
Arrays of objects
  • If variable a refers to an array of objects, then
    each element in a is also a reference.
  • Create an array of objects
  • //First new operator allocate memory for the
    array of references Point pts new Point2
  • //Following new operator allocate memory for the
    objects referred // to by the array
  • pts0 new Point(1.0, 2.0)
  • pts1 new Point(3.0, 4.0)

null
pts0
pts
null
pts1
x 1.0 y 2.0
pts0
pts
pts1
x 3.0 y 4.0
5
Diagram the final memory usage of the following.
  • BankAccount account new BankAccount3
  • account1 new BankAccount(25.0)

6
Exercise rolling a dice
  • Simulate the rolling of one dice. Output the
    occurrence of each outcome for 100 tosses.
  • Use a Random object to create random numbers 1
    through 6.
  • Random r new Random()
  • int outcome r.Next(1, 7)
  • Use an array to store the occurrence times of
    each outcome
  • int result new int7 //Only the last 6 are
    used
  • //result1 occurrence of 1
  • //result2 occurrence of 2
  • //
  • //result6 occurrence of 6

7
Exercie Fibonacci series
  • Fibonacci series starts with 0 and 1, each new
    number in the series is simply the sum of the two
    preceeding it.
  • 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, . .
    .
  • Using arrays, compute the first 50 Fibonacci
    numbers, then out them.

8
Exercise class LineSegment
  • Instance variable an array of two Point objects
  • Point points new Point2
  • // Use the class Point in example 12
  • One constructor with 2 arguments to initialize
    points
  • One method returning the length of the segment
  • One method returning the mid-point of the segment
  • ToString method returning a string representing
    the segment
Write a Comment
User Comments (0)
About PowerShow.com