Code Samples

This page contains the code we did in class and other similar examples. Follow this page as a reference. Codes for recent labs will be at the beginning.

Streams and Files

import java.io.IOException;
import java.io.EOFException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.FileInputStream;
import java.io.ObjectInputStream;

public class CalendarSol {
	public static void main(String[] args) {
		Event party = new Event("Fun Party", "2016-03-03 22:00", "French Quarter", "It's Mardi Gras");
		Event test = new Event("CSCI Party", "2016-03-03 22:00", "UNO", "Because Rocket League");
		ObjectOutputStream output = null;

		// Most likely you would have all your objects in some sort of data structure like an ArrayList. You would just serialize the ArrayList instead of individual objects.
		try {
            FileOutputStream fos = new FileOutputStream("calendarFile.ser");

            // ObjectOutputStream does the conversion, FileOutputStream pushes the data
			output = new ObjectOutputStream(fos);
			output.writeObject(party);
			output.writeObject(test);
			output.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		ObjectInputStream input = null;
		Event event1 = null;

		try {
            FileInputStream fis = new FileInputStream("calendarFile.ser");

            // ObjectInputStream does the conversion, FileInputStream pulls the data
            input = new ObjectInputStream(fis);
			do {
				event1 = (Event) input.readObject();
				System.out.println(event1);
			} while (event1 != null);
			input.close();
		} catch (ClassNotFoundException e){ // Must include
			e.printStackTrace();
		} catch (IOException e){// Must include
			if (e instanceof EOFException) {
				System.out.print("Reached end of file, " + e);
			} else {
				e.printStackTrace();
			}
		}

	}
}

Strings

import java.util.Arrays;

public class StringsExample {
    public static boolean containsAllVowel(String s) {
        String vowels = "aeiou";

        for (int i = 0; i < vowels.length(); i++) {
            if (s.indexOf(vowels.charAt(i)) == -1) {
                return false;
            }
        }

        return true;
    }

    public static boolean containsOnlyUniqueCharacters(String s) {
        int[] count = new int[26];

        for (int i = 0; i < s.length(); i++) {
            count[s.charAt(i) - 'a']++;
        }

        for (int i = 0; i < count.length; i++) {
            if (count[i] > 1) {
                return false;
            }
        }

        return true;
    }

    public static String sortString(String s) {
        char[] chars = s.toCharArray();
        Arrays.sort(chars);
        return new String(chars);
    }
}

Javadoc Example

You will use javadoc -d foldername *.java to generate the javadoc files. Below is an example of javadoc comments we wrote in class (will be updated after Tuesday class). A more detailed example can be found here: http://www.docjar.net/html/api/java/util/Collections.java.html

/**
* This class has some helpful math utility method that helps add, and divide the numbers.
*/
/**
 * <p>
 * This is a math utility class that has some basic math functions. It can add,
 * divide and also handles exception.
 * </p>
 *
 * <p>
 * This might not be as useful as your own implementation
 * </p>
 *
 *
 * @author Manisha Panta
 * @since 1.0.0
 */

public class MathUtil {

    /**
     * This method adds two number and returns the sum.
     *
     * @param a The first number to add
     * @param b The second number to add
     * @return The sum of the two numbers
     *
     */
    public int add(int a, int b) {
        return a + b;
    }

    /**
     * This method divides two number.
     *
     * @param a The first number
     * @param b The second number
     * @return divide
     *
     * @deprecated Use the new method instead. This method might lead to unexpected
     *             bugs.
     * @see newDivide
     */
    public int divide(int a, int b) {
        return a / b;
    }

    /**
     * This method divides two number.
     *
     * @param a The first number
     * @param b The second number
     * @return divide
     *
     * @throws ArithmeticException if b==0
     */
    public int newDivide(int a, int b) throws ArithmeticException {
        if (b == 0)
            throw new ArithmeticException("Cannot divide by zero");

        return a / b;
    }
}

Javadoc Example 2

/**
* An object designed to model a circle. 
* It has a radius and circumference and can calculate the area a cylinder given a height and radius.
* 
* @author Anthony Marchiafava
* @version 1.0
*/
public class Circle{
	//circumference
	private double circ;
	
	//radius
	private double radius;
	
	//constructs a Circle
    /**
    * Makes a Circle object.
    * @param radius The radius of the circle.
    */	
    public Circle(double radius){
		this.radius = radius;
		this.circ = 2*Math.PI*radius;
	}
	
	//sets the circ of the Circle
    /**
    * Sets the circumference of the circle.
    * @param circ The new circumference of the circle.
    */
	public void setCirc(double circ){
		this.circ = circ;
	}
	
    /**
    * Returns the circumference of the circle.
    * @return The circumference of the cirlce.
    *
    */
	//gets the circ of the Circle
	public double getCirc(){
		return circ;
	}

    /**
    * Calculates the area of a cylinder. The area of the cylinder would be 2*pi*radius*height + 2*pi*r^2.
    * @param height The height of the cylinder we want to use.
    * @param radius The radius of the cylinder we want to use.
    * @return the area of the cylinder.
    */
	//returns the area of the cylinder that would be produced if the circle became three dimensional
	public double calculateArea(double height, double radius){
		return 2*Math.PI*radius*height+2*Math.PI*radius*radius;
	}
}

Hello World

public class HelloWorld{
    public static void main(String args[]){
        System.out.println("Hello World");
    }
}