diff --git a/Array List.java b/Array List.java new file mode 100644 index 0000000..24026a0 --- /dev/null +++ b/Array List.java @@ -0,0 +1,208 @@ +public class MyArrayList implements Iterable +{ + /** + * Construct an empty ArrayList. + */ + public MyArrayList( ) + { + doClear( ); + } + + /** + * Returns the number of items in this collection. + * @return the number of items in this collection. + */ + public int size( ) + { + return theSize; + } + + /** + * Returns true if this collection is empty. + * @return true if this collection is empty. + */ + public boolean isEmpty( ) + { + return size( ) == 0; + } + + /** + * Returns the item at position idx. + * @param idx the index to search in. + * @throws ArrayIndexOutOfBoundsException if index is out of range. + */ + public AnyType get( int idx ) + { + if( idx < 0 || idx >= size( ) ) + throw new ArrayIndexOutOfBoundsException( "Index " + idx + "; size " + size( ) ); + return theItems[ idx ]; + } + + /** + * Changes the item at position idx. + * @param idx the index to change. + * @param newVal the new value. + * @return the old value. + * @throws ArrayIndexOutOfBoundsException if index is out of range. + */ + public AnyType set( int idx, AnyType newVal ) + { + if( idx < 0 || idx >= size( ) ) + throw new ArrayIndexOutOfBoundsException( "Index " + idx + "; size " + size( ) ); + AnyType old = theItems[ idx ]; + theItems[ idx ] = newVal; + + return old; + } + + @SuppressWarnings("unchecked") + public void ensureCapacity( int newCapacity ) + { + if( newCapacity < theSize ) + return; + + AnyType [ ] old = theItems; + theItems = (AnyType []) new Object[ newCapacity ]; + for( int i = 0; i < size( ); i++ ) + theItems[ i ] = old[ i ]; + } + + /** + * Adds an item to this collection, at the end. + * @param x any object. + * @return true. + */ + public boolean add( AnyType x ) + { + add( size( ), x ); + return true; + } + + /** + * Adds an item to this collection, at the specified index. + * @param x any object. + * @return true. + */ + public void add( int idx, AnyType x ) + { + if( theItems.length == size( ) ) + ensureCapacity( size( ) * 2 + 1 ); + + for( int i = theSize; i > idx; i-- ) + theItems[ i ] = theItems[ i - 1 ]; + + theItems[ idx ] = x; + theSize++; + } + + /** + * Removes an item from this collection. + * @param idx the index of the object. + * @return the item was removed from the collection. + */ + public AnyType remove( int idx ) + { + AnyType removedItem = theItems[ idx ]; + + for( int i = idx; i < size( ) - 1; i++ ) + theItems[ i ] = theItems[ i + 1 ]; + theSize--; + + return removedItem; + } + + /** + * Change the size of this collection to zero. + */ + public void clear( ) + { + doClear( ); + } + + private void doClear( ) + { + theSize = 0; + ensureCapacity( DEFAULT_CAPACITY ); + } + + /** + * Obtains an Iterator object used to traverse the collection. + * @return an iterator positioned prior to the first element. + */ + public java.util.Iterator iterator( ) + { + return new ArrayListIterator( ); + } + + /** + * Returns a String representation of this collection. + */ + public String toString( ) + { + StringBuilder sb = new StringBuilder( "[ " ); + + for( AnyType x : this ) + sb.append( x + " " ); + sb.append( "]" ); + + return new String( sb ); + } + + /** + * This is the implementation of the ArrayListIterator. + * It maintains a notion of a current position and of + * course the implicit reference to the MyArrayList. + */ + private class ArrayListIterator implements java.util.Iterator + { + private int current = 0; + private boolean okToRemove = false; + + public boolean hasNext( ) + { + return current < size( ); + } + + + public AnyType next( ) + { + if( !hasNext( ) ) + throw new java.util.NoSuchElementException( ); + + okToRemove = true; + return theItems[ current++ ]; + } + + public void remove( ) + { + if( !okToRemove ) + throw new IllegalStateException( ); + + MyArrayList.this.remove( --current ); + okToRemove = false; + } + } + + private static final int DEFAULT_CAPACITY = 10; + + private AnyType [ ] theItems; + private int theSize; +} + +class TestArrayList +{ + public static void main( String [ ] args ) + { + MyArrayList lst = new MyArrayList<>( ); + + for( int i = 0; i < 10; i++ ) + lst.add( i ); + for( int i = 20; i < 30; i++ ) + lst.add( 0, i ); + + lst.remove( 0 ); + lst.remove( lst.size( ) - 1 ); + + System.out.println( lst ); + } +} diff --git a/image processing.java b/image processing.java new file mode 100644 index 0000000..4bc260a --- /dev/null +++ b/image processing.java @@ -0,0 +1,64 @@ +// Java program to demonstrate +// colored to grayscale conversion + +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import javax.imageio.ImageIO; + +public class Grayscale { + public static void main(String args[]) + throws IOException + { + BufferedImage img = null; + File f = null; + + // read image + try { + f = new File( + "C:/Users/hp/Desktop/Image Processing in Java/gfg-logo.png"); + img = ImageIO.read(f); + } + catch (IOException e) { + System.out.println(e); + } + + // get image's width and height + int width = img.getWidth(); + int height = img.getHeight(); + + // convert to grayscale + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + + // Here (x,y)denotes the coordinate of image + // for modifying the pixel value. + int p = img.getRGB(x, y); + + int a = (p >> 24) & 0xff; + int r = (p >> 16) & 0xff; + int g = (p >> 8) & 0xff; + int b = p & 0xff; + + // calculate average + int avg = (r + g + b) / 3; + + // replace RGB value with avg + p = (a << 24) | (avg << 16) | (avg << 8) + | avg; + + img.setRGB(x, y, p); + } + } + + // write image + try { + f = new File( + "C:/Users/hp/Desktop/Image Processing in Java/GFG.png"); + ImageIO.write(img, "png", f); + } + catch (IOException e) { + System.out.println(e); + } + } +}