Error
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.

static

Find Min/Max of a Porabola
Monday, 30 January 2012 15:52
// Short snippet that takes 3 integers and gives you the min/max location of a parabola


public class mainClass2 {
//Finds parabola min/max
public static void main(String args[]){
int a = 29;
int b = -31;
int c = 99;

float x = findX(a,b);
float y = findY(a,b,c,x);

if (a < 0){
System.out.println("max: x="+ x + " y=" + y);
}
else{
System.out.println("min: x="+ x + " y=" + y);
}
}

public static float findX(float a, float b){
if (a == 0){
return -1;
}
else{
return (-b)/(2*a);
}
}
public static float findY(float a, float b, float c, float x){
return a*(x*x) + (b*x) + c;
}
}

Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/g-7PpoyvgIg/14557

 
Prob 3
Friday, 14 October 2011 18:41

import java.math.*;
import java.util.*;

public class Prob3{
public static void main(String[] argsv){
System.out.println("The largest prime factor of the number 600851475143 is " + greatestFac(primeFactors(600851475143l)));
}

private static ArrayList primeFactors(Long n){
ArrayList facs = new ArrayList();
long prime = 2;
while(((double)n/(double)prime) != 1.0){
if(n % prime == 0){
n = n / prime;
facs.add(prime);
prime = 1;
}
prime = nextPrime(prime);
}
facs.add(prime);
return facs;
}

private static long greatestFac(ArrayList list){
long high = list.get(0);
for(int i = 1; i < list.size(); i++){
if(list.get(i) > high) high = list.get(i);
}
return high;
}


// Find the next prime number after n (n + 1)
private static long nextPrime(long n){
n++; // remove if implementation details change (pass n + 1)
while(!isPrime(n)){
n++;
}
return n;
}

private static boolean isPrime(long n){
long limit = (long) Math.sqrt(n);
for(long i = 2; i <= limit; i++){
if(n % i == 0){
return false;
}
}
return true;
}

} // end class Prob3

Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/IgzOSHaWGaI/13703

 
C# code to test if an object is serializable
Tuesday, 02 August 2011 15:20
The code below contains utility methods and an example test method to check if a class is truly serializable by cloning it using serialization then comparing the original to the clone.


public static Stream Serialize(object source)
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new MemoryStream();
formatter.Serialize(stream, source);
return stream;
}

public static T Deserialize(Stream stream)
{
IFormatter formatter = new BinaryFormatter();
stream.Position = 0;
return (T)formatter.Deserialize(stream);
}

public static T CloneBySerialization(T source)
{
return Deserialize(Serialize(source));
}

public static void AssertRoundTripSerializationIsPossible(T source)
{ // assumes T implements Equals
T clone = CloneBySerialization(source);
Assert.AreEqual(source, clone, "Failed round-trip serialization, clone not equal to source");
Assert.IsFalse(ReferenceEquals(source, clone), "Failed round-trip serialization, clone points to souce");
}

[TestMethod]
public void ClassShouldBeSeralizable()
{
Foo original = new Foo("bar");
AssertRoundTripSerializationIsPossible(original);
}

Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/QBlm0HoTlhc/13455

 
ArrayListDemo.java
Friday, 29 July 2011 05:32
package questions;

import java.util.*;
/**
* @author Bharat Verma
*/
public class ArrayListDemo
{
public static void printArrayList (ArrayList p_arraylist)
{
System.out.println("------------------------------------------");
System.out.println("Content of ArrayList : "+p_arraylist);
System.out.println("Size of ArrayList = "+p_arraylist.size());
System.out.println("------------------------------------------");
}
public static void main(String[] args)
{
ArrayList sampleArrayList =new ArrayList(); // you can specify the object type

Integer intr = new Integer(729);
String str ="Bharat";
double dtr = 169.721;

printArrayList(sampleArrayList);

sampleArrayList.add(intr);
sampleArrayList.add(str);
sampleArrayList.add(dtr);

printArrayList(sampleArrayList);

Integer i5=new Integer(50);
sampleArrayList.add(3, i5); // using add(int index, object obj)

printArrayList(sampleArrayList);

sampleArrayList.remove(3); // using remove()

printArrayList(sampleArrayList);

Object a=sampleArrayList.clone(); // using clone

System.out.println("The clone is: " + a);

printArrayList(sampleArrayList);

// iteration using iterator
Iterator iter = sampleArrayList.iterator();
System.out.println("Using Iterator");
while (iter.hasNext())
{
System.out.println(iter.next());
}

// using for loop and indexes
System.out.println("Using for loop and index");
for (int i=0; i {
System.out.println(sampleArrayList.get(i));
}
}
}

Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/ERJ_5mp35vg/13419

 
arraylist demo
Friday, 29 July 2011 05:31
package questions;

import java.util.*;
/**
* @author Bharat Verma
*/
public class ArrayListDemo
{
public static void printArrayList (ArrayList p_arraylist)
{
System.out.println("------------------------------------------");
System.out.println("Content of ArrayList : "+p_arraylist);
System.out.println("Size of ArrayList = "+p_arraylist.size());
System.out.println("------------------------------------------");
}
public static void main(String[] args)
{
ArrayList sampleArrayList =new ArrayList(); // you can specify the object type

Integer intr = new Integer(729);
String str ="Bharat";
double dtr = 169.721;

printArrayList(sampleArrayList);

sampleArrayList.add(intr);
sampleArrayList.add(str);
sampleArrayList.add(dtr);

printArrayList(sampleArrayList);

Integer i5=new Integer(50);
sampleArrayList.add(3, i5); // using add(int index, object obj)

printArrayList(sampleArrayList);

sampleArrayList.remove(3); // using remove()

printArrayList(sampleArrayList);

Object a=sampleArrayList.clone(); // using clone

System.out.println("The clone is: " + a);

printArrayList(sampleArrayList);

// iteration using iterator
Iterator iter = sampleArrayList.iterator();
System.out.println("Using Iterator");
while (iter.hasNext())
{
System.out.println(iter.next());
}

// using for loop and indexes
System.out.println("Using for loop and index");
for (int i=0; i {
System.out.println(sampleArrayList.get(i));
}
}
}

Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/ZT0n9RBlH_k/13417

 
Start
Prev
1


Page 1 of 2
Taxonomy by Zaragoza Online