Contents

  1. j1.java
  2. j2.java
  3. j3.java
  4. j4.java
  5. j5.java
  6. j6.java
  7. j7.java
  8. j8.java
  9. j9.java
  10. j10.java
  11. j11.java
  12. j12.java
  13. j13.java

j1.java 1/13

[
top][prev][next]
/** j1 - type hello world to screen 10 times
 */

public class j1 {

    public static void main( String[] args ) {

        for ( int i = 0; i < 10; i++ )
            System.out.println("Hello World");
    }
}

j2.java 2/13

[
top][prev][next]
/** j2 - type _______ to screen 10 times
 */

import java.io.*;

public class j2 {

    public static void main( String[] args ) throws IOException {

        // put a filter in front of System.in so I can read lines
        // old style:
        // DataInputStream in = new DataInputStream( System.in );
        
        BufferedReader in = new BufferedReader (
                            new InputStreamReader( System.in ));
        
        String input;

        input = in.readLine();
        
        for ( int i = 0; i < 10; i++ )
            System.out.println( input );
    }
}

j3.java 3/13

[
top][prev][next]
/** j3 - write I hate ___  (where item comes from input)
 */

import java.io.*;

public class j3{

    public static void main( String[] args ) throws IOException {

        // put a filter in front of System.in so I can read lines
        // old style:
        // DataInputStream in = new DataInputStream( System.in );
        
        BufferedReader in = new BufferedReader (
                            new InputStreamReader( System.in ));
        
        String input;

        input = in.readLine();
        
        for ( int i = 0; i < 10; i++ )
            System.out.println( "I hate " + input );
    }
}

j4.java 4/13

[
top][prev][next]
/** j4 - read and write unless it is 'P'
 *  (C Style Solution)
 */

import java.io.*;

public class j4 {

    public static void main( String[] args ) throws IOException {

        int input;

        input = System.in.read();
        
        if ( input != 'P' ) {
            //    System.out.println( "input = " + input );
            System.out.println( (char) input );
        }
    }
}

j5.java 5/13

[
top][prev][next]
/** j5 - read and write till it is 'P'
 *  (modern java)
 */

import java.io.*;

public class j5 {

    public static void main( String[] args ) throws IOException {

        int input;

        InputStreamReader in = new InputStreamReader( System.in );

        while ( true ) {
            input = in.read();

            if ( input == 'P' )
                break;
            
            System.out.print( (char) input );
        }
        // one extra newline
        System.out.println( "" );
    }
}

j6.java 6/13

[
top][prev][next]
/** j6 - read and write till it is 'P'
 *  (modern java)
 */

import java.io.*;

public class j6 {

    public static void main( String[] args ) throws IOException {

        int input;

        InputStreamReader in = new InputStreamReader( System.in );

        do {
            input = in.read();

            if ( input != 'P' )
               System.out.print( (char) input );

        } while ( input != 'P') ;
        
        // one extra newline
        System.out.println( "" );
    }
}

j7.java 7/13

[
top][prev][next]
/** j7 - read from TEST.IN and write till it is 'P'
 *  (modern java)
 */

import java.io.*;

public class j7 {

    public static void main( String[] args ) throws IOException {

        String input;

        BufferedReader in = new BufferedReader(
                                 new FileReader( "TEST.IN" ) );

        while ( (input = in.readLine() ) != null )
            System.out.println( input );
        
    }
}

j8.java 8/13

[
top][prev][next]
/** j8 - read from TEST.IN and write to TEST.OUT till it is 'P'
 *  (modern java)
 */

import java.io.*;

public class j8 {

    public static void main( String[] args ) throws IOException {

        String input;

        BufferedReader in = new BufferedReader(
                                 new FileReader( "TEST.IN" ) );

        PrintWriter out = new PrintWriter(
                                 new FileWriter( "TEST.OUT" ) );

        while ( (input = in.readLine() ) != null ) {
            out.println( input);
        }
        out.flush();
        out.close();
    }
}

j9.java 9/13

[
top][prev][next]
/** j9 - read from commandline supplied file and write to TEST.OUT
 *    (modern java)
 */

import java.io.*;

public class j9 {

    public static void main( String[] args ) throws IOException {

        String input;

        if ( args.length != 1 ) {
            System.out.println( "Usage: java j9 inputfile" );
            return;
        }

        BufferedReader in = new BufferedReader(
                                 new FileReader( args[0] ) );

        PrintWriter out = new PrintWriter(
                                 new FileWriter( "TEST.OUT" ) );

        while ( (input = in.readLine() ) != null ) {
            out.println( input);
        }
        out.flush();
        out.close();
    }
}

j10.java 10/13

[
top][prev][next]
/** j10 - read from stdin/write stdout (observe redirection)
 *    output (modern java)
 */

import java.io.*;

public class j10 {

    public static void main( String[] args ) throws IOException {

        String input;

        BufferedReader in = new BufferedReader( 
                                 new InputStreamReader( System.in ) );

        PrintWriter out = new PrintWriter(
                                 new OutputStreamWriter( System.out ) );

        while ( (input = in.readLine() ) != null ) {
            out.println( input);
            // flush each line for human output, should skip otherwise
            out.flush();
        }
        // not really needed if out.flush() in the loop above
        out.flush();
        out.close();
    }
}

j11.java 11/13

[
top][prev][next]
/** j11 - magnitude function
 *    
 */

public class j11 {

    public static void main( String[] args ) {
        double x = 3.0;
        double y = 4.0;

        System.out.println( "Answer = " + mymag( x, y ) );
        
    }

    public static double mymag( double x, double y ) {
        return Math.sqrt( x*x + y*y );
    }
}

j12.java 12/13

[
top][prev][next]
/** j12 - magnitude function with inputs
 *    
 */

import java.io.*;

public class j12 {

    public static void main( String[] args ) throws IOException {
        double x;
        double y;
        String input;

        BufferedReader in = new BufferedReader(
                              new InputStreamReader ( System.in ));

        System.out.print( "x = " );
        System.out.flush();
        input = in.readLine();

        x = Double.valueOf( input ).doubleValue();

        System.out.print( "y = " );
        System.out.flush();
        input = in.readLine();

        y = Double.valueOf( input ).doubleValue();
        
        
        System.out.println( "Answer = " + mymag( x, y ) );
        
    }

    public static double mymag( double x, double y ) {
        return Math.sqrt( x*x + y*y );
    }
}

j13.java 13/13

[
top][prev][next]
/** j13 - magnitude function with inputs, add outer loop
 *    
 */

import java.io.*;

public class j13 {

    public static void main( String[] args ) throws IOException {
        double x;
        double y;
        String input;

        BufferedReader in = new BufferedReader(
                              new InputStreamReader ( System.in ));

        do {
            
            System.out.print( "x = " );
            System.out.flush();
            input = in.readLine();

            x = Double.valueOf( input ).doubleValue();

            System.out.print( "y = " );
            System.out.flush();
            input = in.readLine();

            y = Double.valueOf( input ).doubleValue();
        
        
            System.out.println( "Answer = " + mymag( x, y ) );

            System.out.print( "Do you wish to continue [Y/n]" );

            input = in.readLine();

        } while ( ! ( input.startsWith("n") || input.startsWith("N") ));
        
    }

    public static double mymag( double x, double y ) {
        return Math.sqrt( x*x + y*y );
    }
}

Generated by GNU enscript 1.6.1.