// Description: 1) Input the characters(bytes) from a file containing
// normal (ASCII) text, with both upper and lower case.
// 2) For each byte, determine if it's a letter. Convert
// it to UPPER CASE. Then "count" it. That is, count
// how many how many 'A's, 'B's, 'C's, etc. All upper
// case, makes the counting easier. Count the letters in
// the entire file.
// 3) Output the results of the counting as a table which
// looks like:
// Letter Frequency
// A 23
// B 14
// etc...
// 4) Design your program so the file name is input.
import java.io.* ;
public class CountReader
{
Reader in = null;
int rread = 0, charCount=0;
boolean whiteSpace = false;
public CountReader (Reader r)
{
in = r;
}
/**
* Implementation for parent's read method. Counts chars.
*/
public int read(char[] array, int[] cnt) throws IOException
{
if (array == null)
throw new IOException("Null array");
// Do actual read
rread = in.read(array);
// Now count
charCount += rread; // Increment character count
char c;
for (int i = 0; i < rread; i++)
{
c = Character.toUpperCase(array[i]);
if( Character.isLetter(c) )
cnt[(int)c-65]++;
}
return rread;
}
public void close() throws IOException
{
in.close();
}
public int getCharCount()
{
return charCount;
}
/** Test driver */
public static void main(String args[]) throws Exception
{
CountReader r = new CountReader(new FileReader("sample.txt"));
char c[] = new char[4096];
int re = 0;
int[] lcount = new int[26];
for(int i=0; i < 26; i++)
lcount[i] = 0;
while ((re = r.read(c, lcount)) != -1)
System.out.print(new String(c, 0, re));
r.close();
System.out.println("\n\nRead chars: " + r.getCharCount());
for(int i=0; i < 26; i++)
System.out.println( " " + (char)(i+65) + '\t' + lcount[i] );
}
}
/* Output
C:\>a:
A:\>java CountReader
If we looked only by
what we know,
we could not turn our
heads:
If we were at the
mercy of what
we understand,
our eyes could not see:
discovery is
praise &
understandig is
celebration:
Read chars: 195
A 8
B 2
C 5
D 9
E 21
F 3
G 1
H 4
I 8
J 0
K 2
L 5
M 1
N 10
O 13
P 1
Q 0
R 10
S 9
T 10
U 7
V 1
W 9
X 0
Y 5
Z 0
*/
/*___________________________*/
Description: 1) Retrieving numbers from the command line (Dos Prompt)
Write a program which will compute the distance
between two cartesian points. The four numbers
must be of type double. The numbers parameters will
be input as: x1, y1, x2, y2. Each number(coordinate
will be stored as a separate String in the array "args[]".
*/
import java.io.*;
public class Point
{
public static void main(String[] args) throws IOException
{
double xcoor1 = 0, xcoor2 = 0, ycoor1 = 0, ycoor2 = 0, distance;
if(args.length != 4)
System.out.println("Usage: java lab5b x1 y1 x2 y2 ");
else
{
xcoor1 = Double.parseDouble(args[0]);
ycoor1 = Double.parseDouble(args[1]);
xcoor2 = Double.parseDouble(args[2]);
ycoor2 = Double.parseDouble(args[3]);
distance = Math.sqrt((xcoor2 - xcoor1)* (xcoor2 - xcoor1) +
(ycoor2 - ycoor1) * (ycoor2 - ycoor1));
System.out.println("The distance from point a to point b is "
+ distance);
}
}
}
/* OUTPUT
A:\>javac Point.java
A:\>java Point 2 2 4 2
The distance from point a to point b is 2.0
A:\>java Point 45.7 12.3 -6.7 9.75
The distance from point a to point b is 52.46201006442663
*/
Home
Next
Previous