CSC212 Homework 4 Solutions 2014
--D. Thiebaut (talk) 08:15, 6 October 2014 (EDT)
Problem 1
/**
* evaluates RPN expressions known to be error free.
* @author D. Thiebaut
*/
import java.util.Scanner;
import java.util.Stack;
@SuppressWarnings("unchecked")
public class Hw4_1 {
static boolean debug = false; // used for debugging purposes
/**
* test validity of string as containing an operator
* @param word contains the operator (no extra spaces)
* @return true if +, *, -, or /, false otherwise
*/
private static boolean isOperator( String word ) {
String[] operators = new String[] { "+", "*", "-", "/" };
for ( int i=0; i<operators.length; i++ )
if ( word.equals(operators[i]) )
return true;
return false;
}
/**
* main entry point.
* @param args
*/
public static void main(String[] args) {
// create a stack and a line scanner
Stack stack = new Stack();
Scanner lineScanner = new Scanner( System.in );
// read lines until the end of input.
while ( lineScanner.hasNextLine() ) {
String line = lineScanner.nextLine();
// skip blank lines
if ( line.isEmpty() )
break;
// create a word scanner for each line
Scanner wordScanner = new Scanner( line );
while ( wordScanner.hasNext() ) {
String word = wordScanner.next();
//System.out.println( word );
// operate on operators
if ( isOperator( word ) ) {
int top = (int) stack.pop();
int nextTop = (int) stack.pop();
if ( word.equals( "+" ) ) stack.push( top + nextTop );
if ( word.equals( "-" ) ) stack.push( nextTop - top );
if ( word.equals( "*" ) ) stack.push( top * nextTop );
if ( word.equals( "/" ) ) stack.push( nextTop / top );
if (debug) System.out.println( "stack: " + stack );
continue;
}
// otherwise, it's a number: push it.
stack.push( (int) Integer.parseInt( word ) );
if (debug) System.out.println( "stack: " + stack );
}
// no more words in this line: print the top of stack and clear it.
System.out.println( stack.pop() );
stack.clear();
}
}
}
Problem 2
/**
* evaluates RPN arithmetic expressions on ints using a stack.
*
*/
import java.util.Scanner;
import java.util.Stack;
public class Hw4_2 {
static boolean debug = false;
/**
* given s tring containing only a possible operator, returns true if it's a valid
* one (+, *, -, /) and false otherwise
* @param word the string containing the parameter (no extra space allowed)
* @return true if *, +, - or /, false otherwise
*/
private static boolean isOperator( String word ) {
String[] operators = new String[] { "+", "*", "-", "/" };
for ( int i=0; i<operators.length; i++ )
if ( word.equals(operators[i]) )
return true;
return false;
}
/**
* displays the word ERROR.
* @param i used in debugging mode, to indicate where the error occured.
*/
private static void error( int i ) {
System.out.println( "ERROR " );// + i );
}
/**
* main entry point.
* @param args
*/
public static void main(String[] args) {
boolean error;
Stack stack = new Stack();
Scanner lineScanner = new Scanner( System.in );
// scan the lines of input
while ( lineScanner.hasNextLine() ) {
String line = lineScanner.nextLine().trim();
// skip blank lines
if ( line.isEmpty() )
continue;
// scan each word in a line and either pushes it in stack if an int
// or pop numbers from the stack to evaluate operation.
error = false;
Scanner wordScanner = new Scanner( line );
while ( wordScanner.hasNext() ) {
String word = wordScanner.next();
// --- operator? ---
if ( isOperator( word ) ) {
if ( stack.size() < 2 ) {
error( 1 );
if ( wordScanner.hasNextLine() ) wordScanner.nextLine();
error = true;
break;
}
int top = (int) stack.pop();
int nextTop = (int) stack.pop();
if ( word.equals( "+" ) ) stack.push( top + nextTop );
if ( word.equals( "-" ) ) stack.push( nextTop - top );
if ( word.equals( "*" ) ) stack.push( top * nextTop );
if ( word.equals( "/" ) ) {
if ( top != 0 )
stack.push( nextTop / top );
else {
if ( wordScanner.hasNextLine() ) wordScanner.nextLine();
stack.clear();
error( 2 );
error = true;
break;
}
}
if (debug) System.out.println( "stack: " + stack );
continue;
}
// --- no, then must be number ---
try {
stack.push( (int) Integer.parseInt( word ) );
}
catch ( NumberFormatException e ) {
error( 3 );
stack.clear();
error = true;
break;
}
if (debug) System.out.println( "stack: " + stack );
}
if ( error )
continue;
if ( stack.size() != 1 ) {
error( 4 );
stack.clear();
}
else
System.out.println( stack.pop() );
stack.clear();
}
}
}
Problem 3
/**
* Hw4_3.java
* @author thiebaut
* Reads 4 arguments from the command line, a min distance, a min number of time,
* and two files containing (t, x, y) triplets.
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Contains a pair of ints
*/
class MyPair{
public int x;
public int y;
MyPair( int xx, int yy ) { x = xx; y = yy; }
public String toString() { return "(" + x + "," + y + ")"; }
}
/**
* The class opens two files that contain the same number of lines,
* reads them, and computes the distance between points in the nth
* line of both files.
*/
public class Hw4_3 {
/**
* returns the distance between 2 points defined by (x1,y1) and
* (x2, y2)
* @param x1
* @param y1
* @param x2
* @param y2
* @return the distance as a double.
*/
private static double distance( int x1, int y1, int x2, int y2 ) {
return Math.sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) );
}
/**
* reads the file whose name is passed as a string. Returns an array list
* of MyPairs.
* @param file the file name.
* @return an arraylist of pairs of x,y ints.
*/
private static ArrayList readFile(String file ) {
Scanner in = null;
// create an arraylist of mypairs
ArrayList list = new ArrayList();
// open the file
try {
in = new Scanner( new File( file ) );
} catch (FileNotFoundException e) {
System.out.println( "File " + file + " does not exist" );
System.exit( 1 );
}
// read its contents
while ( in.hasNext() ) {
String line = in.nextLine();
Scanner wordScanner = new Scanner( line );
if ( wordScanner.hasNext() ) {
// get all 3 numbers. Get rid of time.
int time = Integer.parseInt( wordScanner.next() );
int x = Integer.parseInt( wordScanner.next() );
int y = Integer.parseInt( wordScanner.next() );
// create a new entry in array.
list.add( new MyPair( x, y ) );
}
}
return list;
}
/**
* displayList: displays the list of MyPairs. useful for debugging.
* @param caption
* @param list
*/
private static void displayList( String caption, ArrayList list ) {
System.out.print( caption );
for ( int i=0; i<list.size(); i++ ) {
MyPair p = (MyPair) list.get(i);
System.out.print( p+ " " );
}
System.out.println( );
}
/**
* computerDistances gets 2 lists of MyPairs and returns a list of double distances.
* @param l1
* @param l2
* @return an ArrayList of MyPairs.
*/
private static ArrayList computeDistances(ArrayList l1, ArrayList l2) {
ArrayList dist = new ArrayList();
for ( int i=0; i< Math.min( l1.size(), l2.size()); i++ ) {
MyPair p1 = (MyPair) l1.get( i );
MyPair p2 = (MyPair) l2.get( i );
dist.add( distance( p1.x, p1.y, p2.x, p2.y ) );
}
return dist;
}
/**
* Main entry point.
* @param args: command line arguments
*/
public static void main(String[] args) {
// check syntax
if ( args.length != 4 ) {
System.out.println( "Syntax: java Hw4_3 minDist minTim file1 file2" );
System.out.println( " where minDist is the minimum distance for contact," );
System.out.println( " minTime is the minimum amount of time for contact");
System.out.println( " where file1 and file2 are data files with 3 numbers on each line.");
return;
}
// get the command line arguments
int minDist = Integer.parseInt( args[0] );
int minTime = Integer.parseInt( args[1] );
String file1 = args[2];
String file2 = args[3];
// create arrayLists of the data in the two files.
ArrayList l1, l2;
l1 = readFile( file1 );
l2 = readFile( file2 );
// for debugging: display the two lists
//displayList( "l1 = ", l1 );
//displayList( "l2 = ", l2 );
// create a list of distances
ArrayList distances = computeDistances( l1, l2 );
// check for closeness and amount of time the data points are close (less than minDist)
int count = 0;
for ( int i = 0; i<distances.size(); i++ )
if ( (double) distances.get( i ) <= minDist )
count++;
if ( count >= minTime )
System.out.println( "CONTACT" );
else
System.out.println( "NOT" );
}
}