CS117 Programming Methodology
Fall 1999
Analyze the program below and write a complete simulation table, as described in class, for the program’s execution from start to finish. Make sure that the following are made clear in your simulation table:
1. Each scope is contained in a column of the table, headed by the name of the object, file, or function that defines the scope. Draw vertical ines to separate the columns.
2. Each scope column has columns within it for each variable defined in the scope. Constants can be omitted.
3. There is a column for output.
4. If the program reads input, that the input values are made explicit in a separate section (not in the table).
5. There is only one change of value per row in the table. i.e. each assignment changes one value only, and each row corresponds to a separate assignment.
6. It is acceptable to ignore a scope, whether function, file or object, that does not declare new variable names.
7. Function call and return is indicated by horizontal lines in that function's scope column .
· the default constructor and when it is called
· static member functions
· calling a member function for an object
· the increment and decrement operators ++
· array indexing with [...]
· the standard output stream System.out
· passing an object to a method by reference (BE CAREFUL HERE)
· class constants using the final and static keywords
Hand in your completed assignment to me (RTH) in class on Monday, November 15th. in class.
import
java.io.*;
class N
{
public static void normalize(C c) {
int[] a = c.getArray();
int max = a[c.getMaxValInd()];
for (int i = 0; i < C.SIZE; i++)
a[i] = a[i] * C.BIG / max;
}
}
class C
{
public static final int SIZE = 3;
public static final int BIG = 256;
private int maxValInd, minValInd;
private int[] arr;
public
C() {
arr = new int[SIZE];
arr[0] = 140;
arr[1] = 221;
arr[2] = 56;
}
public int[] getArray() { return arr; }
public int getMaxValInd() { return maxValInd;
}
public void findMax() {
int val = 0;
for (int i = 0; i < SIZE; i++)
if (arr[i] > val) {
val = arr[i];
maxValInd = i;
}
}
public void findMin() {
int val = BIG;
for (int i = 0; i < SIZE; i++)
if (arr[i] < val) {
val = arr[i];
minValInd = i;
}
}
public void printRange() {
System.out.print("Range is ");
System.out.print(arr[minValInd]);
System.out.print(" to ");
System.out.println(arr[maxValInd]);
}
public static void main(String[] args) {
C c = new C();
c.findMax();
N.normalize(c);
c.findMin();
c.printRange();
}
}