1. (12 points) Given the method x() below, write the method sort() that puts the entries in the array t in ascending order ("Apple", "Toy", "alfalfa", "open",...). You may use any algorithm you wish for the sort. void x(){ String t[]={"sand","open","Apple","tiger","orange","Toy","alfalfa"}; sort(t); } ============================================================================= void sort(String t[]){ //a quick and dirty bubble sort (very inefficient) String temp; for(int j=0;j0){ temp=t[k]; t[k]=t[k+1]; t[k+1]=temp; } } } ============================================================================= 2. (12 points) Assume you are given the class Util below. Write the class MySqrtException (a checked exception). Then write the code for the method x(), which is passed an array of doubles and replaces each double with its square root by calling MySqrt. The code in the main() method in class Util shows how x() is called. If an exception is thrown, an error message should be displayed and then the program should continue. class Util{ public static double mySqrt(double x) throws MySqrtException{ if(x<0) throw new MySqrtException("Negative argument: "+x); if(x==0) return 0; double guess; guess=x/2; for(int j=0;j<7;j++) guess=(x+guess*guess)/2/guess; return guess; } public static void main(String arg[]){ double v[]={2.4, 3.7, 8, 9.2, 1.6}; x(v); } } ============================================================================= static void x(double v[]){ for(int j=0;jhigh) return 0; if(low==high) return x[low]; return x[low]+x[high]+sum(x,low+1, high-1); } ============================================================================= 4. (12 points) Assume the file ints.dat contains only ints. Write a program that reads ints in the file ints.dat and prints out their sum. ============================================================================= import java.io.*; import java.util.Scanner; public static void main(String afg[]){ int x,sum; sum=0; try{ Scanner sc=new Scanner(new FileInputStream("ints.dat")); while(sc.hasNextInt()){ x=sc.nextInt(); sum+=x; } System.out.println("The sum is "+sum); } catch(IOException e){ System.out.println("Failure to open the file 'ints.dat'"); } } ============================================================================= 5. (12 points) Assume class Node, defined below, is used to construct a binary tree with no repeated entries and having the property that the entry at any node is greater than all entries in the subtree whose root is the left child and less than all entries in the subtree whose root is the right child. Write the methods isContainedIn() and hasTheSameEntries(). The method isContainedIn() returns true if and only if all the entries in the first tree are in the second tree. The method hasTheSameEntries() returns true if and only if both trees have the same entries. Hints: isContainedIn() could call a simpler method that asks whether an int is in the tree. Below I show how the methods are called. class Node{ public int entry; public Node child[2]; } public static void main(String arg[]){ Node t1,t2; // ..... //..... //code for building trees with roots t1 and t2 System.out.println("It is "+isContainedIn(t1,t2)+ " that all the entries in the first tree are in the second tree"); System.out.println("It is "+hasTheSameEntries(t1,t2)+ " that both trees have the same entries"); } ============================================================================== static boolean inTree(int j,Node rt){ if(rt==null) return false; if(j==rt.k) return true; if(j"; temp = temp.next; } return t + "null"; } } ========================================================================== 9. (10 points) I started writing a class called Stack whose purpose is to keep track of characters that are placed onto a Stack data structure. class Stack{ char [] sign; int elementsInArray; public Stack(){ sign = new char[10]; elementsInArray = 0; } ========================================================================== class Stack{ char [] sign; int elementsInArray; public Stack(){ sign = new char[10]; elementsInArray = 0; } public void push(char c){ Check.err(elementsInArray < sign.length,"Array Full"); sign[elementsInArray] = c; elementsInArray++; } public String toString(){ String temp = ""; for (int i = 0; i < elementsInArray; i++) temp = temp + sign[i] + " "; return temp; } } ========================================================================== class Pizza{ private char size; private int [] toppings; public Pizza(char s, int cheese, int pepperoni, int ham){ size = s; toppings = new int[3]; toppings[0] = cheese; toppings[1] = pepperoni; toppings[2] = ham; } public Pizza(Pizza p){ size = p.size; toppings = new int[3]; for(int i = 0; i < toppings.length; i++){ toppings[i] = p.toppings[i];} } public Pizza clone(){ return new Pizza(this);} public String toString(){ return getType() + "(" + size + ", Cheese: " + toppings[0] + ", Pepperoni: " + toppings[1] + ", Ham: " + toppings[2] + ", Cost: " + pizzaCost()+ ")";} public double pizzaCost(){ double cost = 0; if(size == 'S') cost = 10; else if(size == 'M') cost = 12; else if(size == 'L') cost = 14; for(int i = 0; i < toppings.length; i++) cost = cost + (toppings[i] * 2); return cost;} private String getType(){ return "Regular Pizza";} } class Sicilian extends Pizza{ int addedCost; public Sicilian(Pizza p, int aCost){ super(p); addedCost = aCost;} public Sicilian(Sicilian s){ super(s); addedCost = s.addedCost;} public Sicilian clone(){ return new Sicilian(this);} public double pizzaCost(){ return addedCost + super.pizzaCost();} public String getType(){ return "Sicilian Pizza";} }