// Question 1:

// 1.1 compile error, show is not static

// 1.2 output: m of SuperP2

// 1.3 output:  1
//              2

// 1.4 output:  9

// 1.5 output: 1,234,567,890

// Question 2:

import java.util.*;

class MySortedArrayList extends ArrayList<Integer> {
    
    public boolean add(Integer elm){
        for (int i = 0; i < size(); i++){
            if (elm <= get(i)){
                if (elm.equals(get(i)))
                    return false;
                add(i, elm);
                return true;
            }
        }
        super.add(elm);
        return true;
    }

    private void addLast(Integer elm){
        super.add(elm);
    }

    public MySortedArrayList union(MySortedArrayList lst){
        MySortedArrayList res = new MySortedArrayList();
        for (Integer elm : this){
            res.add(elm);
        }
        for (Integer elm : lst){
            res.add(elm);
        }
        return res;
    }
        
    /*
      more efficient version, extra 5 points
    public MySortedArrayList union(MySortedArrayList lst){
        MySortedArrayList res = new MySortedArrayList();
        int i = 0;
        int j = 0;
        while (i < size() && j < lst.size()){
            if (get(i).equals(lst.get(j))){
                res.addLast(get(i));
                i++; j++;
            } else if (get(i) > lst.get(j)){
                res.addLast(lst.get(j));
                j++;
            } else {
                res.addLast(get(i));
                i++;
            }
        }
        while (i < size()){
            res.addLast(get(i));
            i++;
        }
        while (j < lst.size()){
            res.addLast(lst.get(j));
            j++;
        }
        return res;
    }
    */

// Question 3:
    
public class ComparableRational extends Rational implements Comparable<ComparableRational> {

    public ComparableRational() {
        super(0, 1);
    }

    public ComparableRational(long numerator, long denominator) {
        super(numerator, denominator);
    }

    public int compareTo(ComparableRational o){
        long p1  = getNumerator()*o.getDenominator();
        long p2 =  getDenominator()*o.getNumerator();
        if (p1 > p2)
            return 1;
        else if (p1 == p2)
            return 0;
        else
            return -1;
    }
}

// Question 4:
    public static ArrayList<String> hyperLinks(Scanner sc) throws Exception {
        ArrayList<String> links = new ArrayList<>();
        while (sc.hasNextLine()){
            String line = sc.nextLine();
            int start = line.indexOf("<a href=\"");
            if (start != -1){
                int end = line.indexOf("\"", start+9);
                links.add(line.substring(start+9, end));
            }
        }
        return links;
    }

// Question 5:

// 5.1 45

// 5.2 the function returns the sum of the digits in x

// 5.3
    public static BigInteger f(BigInteger x){
        BigInteger s = BigInteger.ZERO;
        while (!x.equals(BigInteger.ZERO)){
            s = s.add(x.mod(BigInteger.TEN));
            x = x.divide(BigInteger.TEN);
        }
        return s;
    }
	
	

	
