CSE 214 Computer Science II (Spring 2015)
RECITATION 2 – OOP and Algorithm Analysis
Objective:
[Note: The code sniipet can be found at http://www3.cs.stonybrook.edu/
public class GenericMethodDemo
{
// generic method printValues
public static < E > void printValues( E inputValue )
{
// Display Value
}
public static void main( String args[] )
{
// Create values of integer, float and Character
System.out.println( "\n Integer value is:" );
// call the printValues function
System.out.println( "\n Float value is:" );
// call the printValues function
System.out.println( " \n Character value is " );
// call the printValues function
}
}
[Note: The code snippet can be found at http://www3.cs.stonybrook.edu/
class GenericClassDemo<T> {
private T t;
public void insert(T t) {
this.t = t;
}
public T get() {
return t;
}
}
public class Main {
public static void main(String[] args) {
GenericClassDemo <Integer> Obj = new GenericClassDemo <Integer>();
//Insertion of value
Obj.insert(12);
//Display value
Integer i = Obj.get();
System.out.println(i);
}
}
a) 678912 b) 3n + 9n4+ 2n5 c) n(log2 n)2 + 2n
d) (log2 n)2 + n! e) 20n+6 f) 2n + n10
for(int i = 1; i <= n; i++)
{ d=d+d*5;
}
for(int i=0;i<n;i++){
for(int j = 0; j < i; j++)
{ d = d – 5;
}
}
for(int i = 1; i <= n; i++)
{ for(int j = 1; j <= i; j++)
d = d + i + j;
d = d + 2;
}
for(int k = 1; k < n; k = k*2)
{ d = d – 5;
}
long startTime=System.currentTimeMillis();
long endTime=System.currentTimeMillis();
long elapsed=endTime-startTime
public static String repeat1(char c, int n)
{
String answer=””;
for(int j=0;j<n;j++)
answer+=c;
return answer;
}
public static String repeat2(char c, int n)
{
StringBuilder sb= new StringBuilder();
for(int j=0;j<n;j++)
sb.append(c);
return sb.toString();
}
n |
repeat1 (in ms) |
repeat2 (in ms) |
10000 |
|
|
50000 |
|
|
100000 |
|
|
200000 |
|
|
6000000 |
|
|