You are on page 1of 1

TEST 1 } Scanner sc2 = new Scanner(System.in); if (m.

containsKey(word)){
Efficient iterative fibonacci Scanners that split lines and counts number of lines and String w; ts = m.get(word);
Import static java.math.BigInteger.*; total words Set<String> words = new TreeSet<String>(); ts.add(wpos);
Import java.math.BigInteger; Scanner sc = new Scanner(System.in); while( sc.hasNext() ){ m.put(word, ts);
Import java.util.*; Scanner words; w = sc2.next(); }
Public class FibTest{ Scanner line; if(!words.contains(w) ) else {
Public static void main(String[] argv){ String word; system.out.println(w); ts = new TreeSet<Integer>();
BigInteger n = valueOf(5125); int lc = 0, wc = 0; } ts.add(wpos);
System.out.println(“fib(“ + n + “) = “ + fib(n) ); while(sc.hasNextLine()){ return; m.put(word, ts);
Return; lc++; } }
} words = new Scanner(sc.nextLine()); // Comparator }
Public static BigInteger fib(BigInteger n){ while(words.hasNext()){ import java.util.Comparator;
BigInteger prev = ONE, curr = ZERO, next; wc++; import java.util.Arrays; for(String s : m.keySet()){
For(BigInteger i = ZERO; i.compareTo(n) > ZERO; word = words.next(); System.out.printf("%s", s);
i = i.add(ONE) ){ } class Pair implements Comparator<Pair>{ for(int i : m.get(s))
next = curr.add(prev); } private int s; System.out.printf(" %d", i);
prev = curr; System.out.printf("\n#10\nwords = %d lines = private int t; System.out.println();
curr = next; %d\n", wc, lc); }
} return; public Pair(int s, int t){ }
Return curr; } this.s = s; }
} this.t = t; import java.io.*;
} TEST 2
Interface programming } class IO1 {
Recursive Factorial Data structure overlapping public int compare(Pair P1, Pair P2){ public static void main(String[] argv) throws
import java.math.BigInteger; Set<T> = new TreeSet<T>(); int r = 0; IOException{
import static java.math.BigInteger.*; if (P1.s < P2.s) r =-1 ; InputStream is = System.in;
import java.util.Scanner; Declaration for a fucked up map else if (P1.s > P2.s) r = 1; OutputStream os = System.out;
Map<String, HashMap<Integer, LinkedList<String>>> else if (P1.s == P2.s) int c;
public class Fact5 { map = new TreeMap<String, HashMap<Integer, if (P1.t < P2.t) r =-1 ; while((c = is.read()) != -1){
public static void main(String[] argv){ LinkedList<String>>>(); else if (P1.t > P2.t) r = 1; os.write(c);
BigInteger n; return r; }
Scanner sc = new Scanner(System.in); Linked list iterator } }
while(sc.hasNextBigInteger()) LinkedList<Integer> LI = new LinkedList<Integer>(); }
System.out.println(fact(sc.nextBigInteger())); ListIterator it = LI.listIterator(); public String toString(){
} while(it.hasNext() ) return String.format("(%d, %d)", s, t); import java.io.*;
System.out.printf(“%s “, it.next() ); } class IO1a {
public static BigInteger fact(BigInteger n){ while(it.hasPrevious() ) } public static void main(String[] argv) throws
BigInteger r = ONE; System.out.print(“ %s “, it.previous() ); class C { IOException{
for(BigInteger i = ONE; i.compareTo(n) < 0; public static void main(String[] argv){ InputStream is = System.in;
i = i.add(ONE)){ Intersection of two sets Pair a = new Pair(1, 4), OutputStream os = System.out;
r = r.multiply(i); Set<T> A = new HashSet<T>(); b = new Pair(2, 6), c = new Pair(1, 4); int c;
} Set<T> B = new HashSet<T>(); byte[] b = new byte[10];
return r; Set<T> B = new HashSet<T>(); System.out.println("a.compare("+a+", " + b+ ") = " while((c = is.read(b)) > 0){
} + a.compare(a,b)); os.write(b, 0, b.length);
} For(T x : A){ System.out.println("a.compare("+a+", " + c+ ") = " }
If(B.contains(x) ) + a.compare(a,c)); }
Arbitrary number of double arguments and returns sum C.add(x);
of squares. System.out.println("a.compare("+b+", " + c+ ") = " }
} + a.compare(b,c));
public static double ssq(double ... D){
double r = 0.0; Scanners to read text a line at a time, and keeps track of import java.io.*;
for(double d: D){ the lines on which each word occurs and stores them in Pair[] p = {a, b, c, new Pair(1, 3)}; class IO1b {
r += d*d; a map. public static void main(String[] argv) throws
} Map<String, TreeSet<Integer>> m = new for(int i=0;i<p.length;++i) System.out.println(p[i]); IOException{
return r; TreeMap<String, TreeSet<Integer>>(); System.out.println(); InputStream is = System.in;
} Arrays.sort(p, a); OutputStream os = System.out;
Int lc = 0; for(int i=0;i<p.length;++i) System.out.println(p[i]); if(is.markSupported()){
Calendar objects and setting them to a date. Scanner sc = new Scanner(System.in); System.out.println(); System.out.printf("mark supported");
Calendar c1 = Calendar.getInstance(); Scanner wds; }
Calendar c2 = new GregorianCalendar While(sc.hasNextLine() ){ return; else{
(2004, Calendar.SEPTEMBER, 9); Lc++; } System.out.printf("mark not supported");
c1.set(Calendar.MONTH, Calendar.DECEMBER); Wds = new Scanner(sc.nextLine() ); } }
c1.set(Calendar.DAY_OF_MONTH, 22); While(wds.hasNext() ){ int c;
c1.set(Calendar.YEAR, 1956); import java.util.EnumSet;
String wd = wds.next(); enum Colour { WHITE, RED, GREEN, BLUE, ORANGE, byte[] b = new byte[10];
System.out.println("\n#3"); If(m.containsKey(wd) ) while((c = is.read(b)) > 0){
System.out.printf("%tD%n", c1); VIOLET, BLACK }
m.get(wd).add(lc); // EnumSet os.write(b, 0, b.length);
System.out.printf("%tD%n\n", c2); else{ }
//
Package stuff ts = new TreeSet<Integer>(); class A { is.close();
com.company.quality ts.add(lc); public static void main(String[] argv){ os.close();
package com.company.quality m.put(wd, ts); // no constructor - use static method }
c:\javac } // (allOf, noneOf, range, of }
cp com.company.quality.MyProg }
Overloaded equals with name object. } EnumSet<Colour> ES = EnumSet.allOf(Colour.class);
public boolean equals(Object obj){ Write a function object (a comparator) which can be
boolean r = false; used to compare strings by length where strings of a for(Colour c : ES) System.out.printf(" %s ", c);
if(this == obj) r = true; given length are compare in the usual way. Declare a System.out.printf("%n");
else if(obj == NULL) r = false; treeset of strings in such a way that a set iterator will
else if(getClass() != obj.getClass()) r = false; give the elements in the above order. EnumSet<Colour> none =
else{ class Q4{ EnumSet.noneOf(Colour.class);
Name n = (Name) obj; public static void main(String[] argv){ EnumSet<Colour> range
r = ( last.equals(n.last) && first.equals(n.first) && Set<String> ts = new TreeSet<String>(new Comp() ); = EnumSet.range(Colour.GREEN, Colour.VIOLET);
mid.equals(n.mid) ); Scanner sc = new Scanner(System.in);
} Scanner wds; EnumSet<Colour> BW = EnumSet.of(Colour.WHITE,
return r; int lc = 0; Colour.BLACK);
} String word;
while(sc.hasNextLine() ) { for(Colour c : none) System.out.printf(" %s ", c);
public Name implements Comparable <Name>{ lc++; System.out.printf("%n");
... wds = new Scanner(sc.nextLine() );
} while(wds.hasNext() ){ for(Colour c : range) System.out.printf(" %s ", c);
Stack interface with funtions word = wds.next();
class StackEmptyException extends Exception{ ts.add(word); System.out.printf("%n");
interface Stack<T>{ } for(Colour c : BW) System.out.printf(" %s ", c);
void push(T++); for(String wd : ts.subset(“aaaa “, “bbbb”) ) System.out.printf("%n");
void pop() throws StackEmptyException; System.out.printf(“%s : “, wd);
T top() throws StackEmptyException; System.out.printf(“\n “); EnumSet<Colour> S = none;
boolean empty(); } S.add(Colour.WHITE);
int size(); } S.add(Colour.VIOLET);
}
} Class Comp implements Comparator<String>{ for(Colour c : S) System.out.printf(" %s ", c);
public int Compare(String s, String t){ System.out.printf("%n");
Enumeration junk. int r = 1;
class Q8{ if(s.equals(t) ) r = 0; return;
public static void main(String[] argv){ else if(s.length() <t.length() ) r = -1; }
for(ClassYear cy : ClassYear.values() ){ else if(s.length() == t.length() ){ }
System.out.print(cy); int rr = s.compareTo(t);
System.out.println(“( “ + cy.getCount() + “)”); import java.util.*;
if(rr < 0) r = -1; class TryMap3 {
} else r = 1;
} public static void main(String[] argv){
} Scanner sc = new Scanner(System.in);
} return r;
enum ClassYear{ Map<String, TreeSet<Integer>> m =
} new HashMap<String, TreeSet<Integer>>();
FRESHMAN(3123), SOPHOMORE(2672),
JUNIOR(2213), SENIOR(1823); Write a main function which reads the file whose name
private int count = 0; is the first command line parameter a word at a time, String word;
private ClassYear(int n) {count = n;} and puts the words in a TreeSet and also reads words int wpos = 0;
public int getCount() {return count;} from standard input until end of file printing the words,
public int setCount(int count) { one per line, which do not occur in the file read in first. TreeSet<Integer> ts;
this.count = count; public static void main(String[] argv) throws while(sc.hasNext()){
} FileNotFoundException{ wpos++;
Scanner sc = new Scanner(new File(argv[0]) ); word = sc.next();

You might also like