Unit 5 sample programs
Preparing a Large File of Numbers: Ass5s2p1.java 
|
Write a program that will generate a list of n random numbers and saves it in a
text file: The first line of the file will contain the number n. Let n be the number
1000, let the random numbers be integers between 1 and 100 (inclusive)
|
// The "Ass5s2p1" class.
import java.awt.*;
import java.io.*;
class Ass5s2p1
{
static FileWriter f;
static PrintWriter output;
public static void main (String args []) throws IOException
{
f = new FileWriter ("a:rand1.txt");
output = new PrintWriter (f);
output.println (1000);
for (int i = 0 ; i < 1000 ; i++)
{
int x = (int) (Math.random () * 100 + 1);
output.println (x);
}
output.close ();
} //end main
} //end Ass5s2p1
|
Illustrating working with Tables and Files Ass5s2p2.java 
|
Write a program that will load the number file from 5.2.1 into an array of integers
and that will let you enter a number between 1 and 100. It will then print out how
often that number occurs in the file.
|
// The "Ass5s2p2" class.
import java.awt.*;
import java.io.*;
import hsa.Console;
class Ass5s2p2
{
static Console cs;
static int list [] = new int [1000];
static int maxNum = 0;
public static void main (String args []) throws IOException
{
cs = new Console ();
loadTable ();
cs.println ("Finding the Frequency of numbers in the Random File of 1000 numbers");
cs.print ("Enter a number between 1 and 100 (enter 0 to end): ");
int x = cs.readInt ();
while (x != 0)
{
int t = howManyOf (x);
cs.println("There are "+t+" occurrences of the number "+x);
cs.print ("\nEnter a number between 1 and 100 (enter 0 to end): ");
x = cs.readInt ();
}
} //end main
public static void loadTable () throws IOException
{
FileReader f = new FileReader ("a:rand1.txt");
BufferedReader input = new BufferedReader (f);
String temp = input.readLine ();
maxNum = Integer.parseInt (temp);
for (int i = 0 ; i < maxNum ; i++)
{
temp = input.readLine ();
list [i] = Integer.parseInt (temp);
}
input.close ();
} //end loadTable
public static int howManyOf (int ofThis)
{
int count = 0;
for (int i = 0 ; i < maxNum ; i++)
if (list [i] == ofThis)
count++;
return count;
} //end howManyOf
} //end Ass5s2p2
|
Illustrating Updating a Data File: Ass5s2p4.java 
|
Write methods void loadSet ( ); void saveSet ( ); void replaceOne (int newNum, int
where) that will load the file from 5.2.1 into a global array of numbers; saves it
again to the file; and replace the number in position "where" by the value of
"newNum". Use these methods in a program that will let you change the value of
the number in a specific location within the file.
|
// The "Ass5s2p4" class.
import java.awt.*;
import java.io.*;
import hsa.Console;
class Ass5s2p4
{
static Console cs;
static int list [] = new int [1000];
static int maxNum = 0;
public static void main (String args []) throws IOException
{
cs = new Console ();
int where = 0, newVal = 0;
loadSet ();
cs.println ("Change the value of a number in a specific location");
cs.println ("by picking a number between 1 and " + maxNum);
cs.print ("Enter the location of the number you wish to change (0 to end): ");
where = cs.readInt ();
while (where > 0 && where <= maxNum)
{
cs.print (" the value of that number is " + list [where] + ". Enter a new value: ");
newVal = cs.readInt ();
replaceOne (newVal, where);
cs.print ("Enter the location of the number you wish to change (0 to end): ");
where = cs.readInt ();
}
saveSet ();
cs.println ("bye");
} //end main
public static void loadSet () throws IOException
{
FileReader f = new FileReader ("a:rand1.txt");
BufferedReader input = new BufferedReader (f);
String temp = input.readLine ();
maxNum = Integer.parseInt (temp);
for (int i = 0 ; i < maxNum ; i++)
{
temp = input.readLine ();
list [i] = Integer.parseInt (temp);
}
input.close ();
} //end loadSet
public static void saveSet () throws IOException
{
FileWriter f = new FileWriter ("a:rand1.txt");
PrintWriter output = new PrintWriter (f);
output.println (maxNum);
for (int i = 0 ; i < maxNum ; i++)
output.println (list [i]);
output.close ();
} //end saveSet
public static void replaceOne (int newNum, int where)
{
if (where < maxNum && where >= 0)
list [where] = newNum;
} //end replaceOne
} //end Ass5s2p4
|
Illustrating RELATED LISTS: Ass5s3p1.java 
|
Write a method void houseList( int listItem) that loads the titles of all 12 houses
into the global String array house[12] and the apporpriate property into the global
String array property[12]. For example houseList(2) would load the house
names into house[12] and the personality discriptions into property[12]. Note that
1 stands for dates, 2 for personalities, 3 for colours, etc. Then write a program
that will let you ask for an appropriate property, and list all 12 signs with that
property. (Compare with problem 4.3.5)
|
// The "Ass5s3p1" class.
import java.awt.*;
import java.io.*;
import hsa.Console;
class Ass5s3p1
{
static Console cs;
static String houses [] = new String [12];
static String properties [] = new String [12];
public static void main (String args []) throws IOException
{
cs = new Console ();
cs.println ("Each Zodiac sign has 7 qualities to save in a file:");
cs.println (" enter 1 for DATES");
cs.println (" enter 2 for PERSONALITY");
cs.println (" enter 3 for COLOURS");
cs.println (" enter 4 for PRECIOUS STONES");
cs.println (" enter 5 for METAL");
cs.println (" enter 6 for TREES");
cs.println (" enter 7 for FLOWERS");
cs.println (" enter 8 for PLANETARY RULER OF THE HOUSE");
cs.print ("\nEnter a number between 1 to 8: ");
int choice = cs.readInt ();
if (choice > 0 && choice < 9)
{
houseList (choice);
printList (cs);
}
} //end main
public static void printList (Console c)
{
for (int i = 0 ; i < 12 ; i++)
{
cs.clear ();
c.println ("the house of " + houses [i] + " exhibits: " + properties [i]);
c.print ("\npress ENTER to continue..");
char a = c.readChar ();
c.println ("\n");
}
c.println("you have reached the end");
} //emd printList
public static void houseList (int listItem) throws IOException
{
FileReader f = new FileReader ("a:zodiac.txt");
BufferedReader input = new BufferedReader (f);
String temp;
for (int i = 0 ; i < 12 ; i++)
{
houses [i] = input.readLine ();
for (int j = 1 ; j < listItem ; j++)
temp = input.readLine ();
properties [i] = input.readLine ();
for (int j = listItem + 1 ; j < 9 ; j++)
temp = input.readLine ();
}
input.close ();
} //end houseList
} //end Ass5s3p1
|
Illustrating TWO DIMENSIONAL ARRAYS: Ass5s4p1.java 
|
The Pascal triangle turned on its side looks as follows:
1 1 1 1 1 1 1 1 1 1
1 2 3 4 5 6 7 8 9 10
1 3 6 10 15 21 28 36 45 55
1 4 10 20 35 56 84 120 165 220
1 5 15 35 70 126 210 330 495 715
1 6 21 56 126 252 462 792 1287 2002
1 7 28 84 210 462 924 1716 3003 5005
1 8 36 120 330 792 1716 3432 6435 11440
1 9 45 165 495 1287 3003 6435 12870 24310
1 10 55 220 715 2002 5005 11440 24310 48620
The first row are the numbers in p[0][0], p[0][1], p[0][2],..., p[0][9]
The familiar Pascal triangle is the triangle of numbers with p[0][0] as vertex and
the set of numbers p[0][9], p[1][8], p[2][7], p[3][6], ..., p[9][0] as base.
Note that p[0][y] is always 1, and that any other number is obtained by adding two
of its neighbours: the one above and the one to its left.
The number 15 for example is 5+10 (or 10+5, depending on which 15 you found)
Write a program that will prints this array of numbers
|
// The "Ass5s4p1" class.
import java.awt.*;
import java.io.*;
import hsa.Console;
class Ass5s4p1
{
static Console cs = new Console ();
static int list [] [] = new int [10] [10];
public static void main (String args []) throws IOException
{
//fill first row with 1
for (int col = 0 ; col < 10 ; col++)
list [0] [col] = 1;
//fill first column with 1
for (int row = 1 ; row < 10 ; row++)
list [row] [0] = 1;
//use the formula on the rest
for (int col = 1 ; col < 10 ; col++)
for (int row = 1 ; row < 10 ; row++)
list [row] [col] = list [row] [col - 1] + list [row - 1] [col];
printList ();
} //end main
public static void printList ()
{
for (int col = 0 ; col < 10 ; col++)
{
for (int row = 0 ; row < 10 ; row++)
cs.print (list [row] [col], 7);
cs.println ("");
}
} //end printList
} //end Ass5s4p1
|
Illustrating LOCATING THE LARGEST : Ass5s5p4.java 
|
Write a program that fills an array of 20 with a set of random integers between 10
and 99, prints out the list on one line, and underlines the two-digit largest number
with the symbol "==".
|
// The "Ass5s5p4" class.
import java.awt.*;
import java.io.*;
import hsa.Console;
class Ass5s5p4
{
static Console cs = new Console ();
static int list [] = new int [20];
public static void main (String args []) throws IOException
{
fillList ();
for (int i = 0 ; i < 20 ; i++)
cs.print (" " + list [i] + " ");
int n = locate ();
cs.println ("");
for (int i = 0 ; i < n ; i++)
cs.print (" ");
cs.print (" == ");
} //end main
public static int locate ()
{
int largest = list [0];
int location = 0;
for (int i = 1 ; i < 20 ; i++)
if (largest < list [i])
{
largest = list [i];
location = i;
}
return location;
} // end locate
public static void fillList ()
{
for (int i = 0 ; i < 20 ; i++)
list [i] = (int) (Math.random () * 90 + 10);
} // end fillList
} //end Ass5s5p4
|
Illustrating BUBBLE SORT: Ass5s5p9.java 
|
Write a program that uses the bubble sort to sort a set of random numbers.
|
/ The "Ass5s5p9" class.
import java.awt.*;
import java.io.*;
import hsa.Console;
class Ass5s5p9
{
static final int max = 100;
static Console cs = new Console ();
static int list [] = new int [max];
public static void main (String args []) throws IOException
{
fillList ();
cs.println ("unsorted numbers:");
printList ();
bubbleSort ();
cs.println ("sorted numbers:");
printList ();
} //end main
public static void fillList ()
{
for (int i = 0 ; i < max ; i++)
list [i] = (int) (Math.random () * 1000 + 1);
} //end fillList
public static void swap (int a, int b)
{
int temp = list [a];
list [a] = list [b];
list [b] = temp;
} //end swap
public static void bubbleSort ()
{
boolean finished = false;
while (!finished)
{
finished = true;
for (int i = 1 ; i < max ; i++)
if (list [i - 1] > list [i])
{
swap (i - 1, i);
finished = false;
}
}
} //end bubbleSort
public static void printList ()
{
for (int i = 0 ; i < max ; i++)
{
cs.print (list [i], 5);
if (i % 15 == 14)
cs.println ("");
}
cs.println ("");
} //end printList
} //end Ass5s5p9
|
Prepraring the Random.txt file for next program: 
|
Generate a set of 1000 random numbers between 1 and 1000 and save the set in
the text file: random.txt.
|
// The "Ass5s6p1prep" class.
import java.awt.*;
import java.io.*;
class Ass5s6p1prep
{
static FileWriter f;
static PrintWriter output;
public static void main (String args []) throws IOException
{
f = new FileWriter ("a:random.txt");
output = new PrintWriter (f);
int number;
//the first number is the total amount expected in the file
output.println (1000);
for (int i = 0 ; i < 1000 ; i++)
{
number = (int) (Math.random () * 1000 + 1);
output.println (number);
}
output.close ();
} //end main
} //end Ass5s6p1prep
|
Illustrating SEARCHING: Ass5s6p1.java 
|
Write a program that will first let you load the set of numbers into an array
and secondly lets you enter a number for testing to see if that number is in the file.
Use a method int positionOf(int number) that will return the position of the first
occurence of "number" in the list of 1000. If the number is not there, then return
the number -1.
|
// The "Ass5s6p1" class.
import java.awt.*;
import java.io.*;
import hsa.Console;
class Ass5s6p1
{
static Console cs = new Console ();
static int list [];
static int max = 0;
public static void main (String args []) throws IOException
{
loadList ();
cs.print ("enter a number between 1 and 1000, to see if it is on file (0 to stop): ");
int num = cs.readInt ();
int loc = -1;
while (num > 0 && num < 1001)
{
loc = locationOf (num);
if (loc < 0)
cs.println (num + " is not on file");
else
cs.println (num + " is located at position " + loc);
cs.print ("enter a number between 1 and 1000, to see if it is on file (0 to stop): ");
num = cs.readInt ();
}
} //end main
public static int locationOf (int num)
{
int location = -1;
for (int i = 0 ; location < 0 && i < max ; i++)
if (list [i] == num)
location = i;
return location;
} //end locationOf
public static void loadList () throws IOException
{
FileReader f = new FileReader ("a:random.txt");
BufferedReader input = new BufferedReader (f);
String temp = input.readLine ();
max = Integer.parseInt (temp);
list = new int [max];
for (int i = 0 ; i < max ; i++)
list [i] = Integer.parseInt (input.readLine ());
input.close ();
} // end loadList
} //end Ass5s6p1
|
|
Sponsored by ECOO and SIG-Computer Science
.
|
|
|