eng
competition

Text Practice Mode

Java Codes.

created Sep 10th 2014, 18:22 by Nehemiah Thomas


10


Rating

342 words
1 completed
00:00
// Mike Scott
// 2d array manipulation examples
 
//import
import java.awt.Color;
 
 
public class FilterExample
{
    /*
     *pre: image != null, image.length > 1, image[0].length > 1
     *    image is a rectangular matrix, neighberhoodSize > 0
     *post: return a smoothed version of image
     */
    public Color[][] smooth(Color[][] image, int neighberhoodSize)
    {    //check precondition
        assert image != null && image.length > 1 && image[0].length > 1
                && ( neighberhoodSize >  ) && rectangularMatrix( image )
                : "Violation of precondition: smooth";
 
        Color[][] result = new Color[image.length][image[0].length];
 
        for(int row = 0; row < image.length; row++)
        {    for(int col = 0; col < image[0].length; col++)
            {    result[row][col] = aveOfNeighbors(image, row, col, neighberhoodSize);
            }
        }
 
        return result;
    }
 
 
    // helper method that determines the average color of a neighberhood
    // around a particular cell.
    private Color aveOfNeighbors(Color[][] image, int row, int col, int neighberhoodSize)
    {    int numNeighbors = 0;
        int red = 0;
        int green = 0;
        int blue = 0;
 
        for(int r = row - neighberhoodSize; r <= row + neighberhoodSize; r++)
        {    for(int c = col - neighberhoodSize; c <= col + neighberhoodSize; c++)
            {    if( inBounds( image, r, c ) )
                {    numNeighbors++;
                    red += image[r][c].getRed();
                    green += image[r][c].getGreen();
                    blue += image[r][c].getBlue();
                }
            }
        }
 
        assert numNeighbors > 0;
        return new Color( red / numNeighbors, green / numNeighbors, blue / numNeighbors );
    }
import java.util.Scanner;
 
public class ScannerAndKeyboard
{
 
    public static void main(String[] args)
    {    Scanner s = new Scanner(System.in);
        System.out.print( "Enter your name: "  );
        String name = s.nextLine();
        System.out.println( "Hello " + name + "!" );
    }
} /**
 * A class to provide a simple list of integers.
 * List resizes automatically. Used to illustrate  
 * various design and implementation details of  
 * a class in Java.
 *  
 * Version 1 only contains the instance variables and
 * the constructors
 * @author scottm
 *
 */
public class IntListVer1 {
    // class constant for default size
    private static final int DEFAULT_CAP = 10;
     
    //instance variables
    private int[] iValues;
    private int iSize;
     
    /**
     * Default constructor. Creates an empty list.
     */
    public IntListVer1(){
        //redirect to single int constructor
        this(DEFAULT_CAP);
        //other statments could go here.
    }
 
    /**
     * Constructor to allow user of class to specify  
     * initial capacity in case they intend to add a lot
     * of elements to new list. Creates an empty list.
     * @param initialCap > 0
     */     
    public IntListVer1(int initialCap) {
        assert initialCap >  : "Violation of precondition. IntListVer1(int initialCap):"
            + "initialCap must be greater than 0. Value of initialCap: " + initialCap;
        iValues = new int[initialCap];
        iSize = 0;
    }
}

saving score / loading statistics ...