Advertisement
Guest User

CODE HS

a guest
Feb 24th, 2017
3,723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.58 KB | None | 0 0
  1. //ROADTRIP
  2. /*
  3.  * This class stores information about a location on Earth.  Locations are
  4.  * specified using latitude and longitude.  The class includes a method for
  5.  * computing the distance between two locations.
  6.  *
  7.  * This implementation is based off of the example from Stuart Reges at
  8.  * the University of Washington.
  9.  */
  10.  
  11. public class GeoLocation
  12. {
  13.     // Earth radius in miles
  14.     public static final double RADIUS = 3963.1676;  
  15.  
  16.     private double latitude;
  17.     private double longitude;
  18.     private String name;
  19.    
  20.     /**
  21.      * Constructs a geo location object with given latitude and longitude
  22.      */
  23.     public GeoLocation(String name, double theLatitude, double theLongitude)
  24.     {
  25.         latitude = theLatitude;
  26.         longitude = theLongitude;
  27.         this.name = name;
  28.     }
  29.  
  30.     /**
  31.      * Returns the latitude of this geo location
  32.      */
  33.     public double getLatitude()
  34.     {
  35.         return latitude;
  36.     }
  37.  
  38.     /**
  39.      * returns the longitude of this geo location
  40.      */
  41.     public double getLongitude()
  42.     {
  43.         return longitude;
  44.     }
  45.  
  46.     // returns a string representation of this geo location
  47.     public String toString()
  48.     {
  49.         return name + " (" + latitude + ", " + longitude + ")";
  50.     }
  51.  
  52.     // returns the distance in miles between this geo location and the given
  53.     // other geo location
  54.     public double distanceFrom(GeoLocation other)
  55.     {
  56.         double lat1 = Math.toRadians(latitude);
  57.         double long1 = Math.toRadians(longitude);
  58.         double lat2 = Math.toRadians(other.latitude);
  59.         double long2 = Math.toRadians(other.longitude);
  60.         // apply the spherical law of cosines with a triangle composed of the
  61.         // two locations and the north pole
  62.         double theCos = Math.sin(lat1) * Math.sin(lat2) +
  63.             Math.cos(lat1) * Math.cos(lat2) * Math.cos(long1 - long2);
  64.         double arcLength = Math.acos(theCos);
  65.         return arcLength * RADIUS;
  66.     }
  67.    
  68.     public String getName()
  69.     {
  70.         return name;
  71.     }
  72. }
  73.  
  74. //ROADTRIP PART 2
  75. import java.util.*;
  76.  
  77. public class RoadTrip
  78. {
  79.     public ArrayList<GeoLocation> stops = new ArrayList<GeoLocation>();
  80.    
  81.     public void addStop(String name, double latitude, double longitude)
  82.     {
  83.         GeoLocation temp = new GeoLocation(name, latitude, longitude);
  84.         stops.add(temp);
  85.     }
  86.    
  87.     public int getNumberOfStops()
  88.     {
  89.         return stops.size();
  90.     }
  91.    
  92.     public double getTripLength()
  93.     {
  94.         double total = 0;
  95.         for (int i = 0; i < stops.size() - 1; i++)
  96.         {
  97.             total += (stops.get(i).distanceFrom(stops.get(i + 1)));
  98.         }
  99.        
  100.         return total;
  101.     }
  102.    
  103.     public String toString()
  104.     {
  105.         String returnString = "";
  106.         for (int i = 0; i < stops.size(); i++)
  107.         {
  108.              returnString += (i + 1) + ". " + stops.get(i).getName()
  109.              + " (" + stops.get(i).getLatitude() + ", " +
  110.              stops.get(i).getLongitude() + ")\n";
  111.         }
  112.         return returnString;
  113.     }
  114. }
  115.  
  116.  
  117. //EXPANDINGARRAY
  118. public class ExpandingArray
  119. {
  120.     private static final int STARTING_SIZE = 10;
  121.     private int[] arr;
  122.     private int currentSize;
  123.     private int numElements;
  124.    
  125.     public ExpandingArray()
  126.     {
  127.         arr = new int[STARTING_SIZE];
  128.         currentSize = STARTING_SIZE;
  129.         numElements = 0;
  130.     }
  131.    
  132.     // Remove the element at index `index` and shift
  133.     // all subsequent elements to the left.
  134.     public int remove(int index)
  135.     {
  136.         // your code here
  137.         int[] a = new int[arr.length - 1];
  138.        
  139.         for (int i = 0; i < index; i++)
  140.         {
  141.             a[i] = arr[i];
  142.         }
  143.         for (int i = index + 1; i < arr.length;i++)
  144.         {
  145.             a[i - 1] = arr[i];
  146.         }
  147.         numElements--;
  148.         arr = a;
  149.         return arr[index];
  150.     }
  151.    
  152.     // Add the int `element` at the `index` in the array.
  153.     // You'll need to shift everything one index to the right
  154.     // after this index.
  155.     public void add(int index, int element)
  156.     {
  157.         // your code here
  158.         int[] a = new int[arr.length + 1];
  159.        
  160.         for (int i = 0; i < index; i++)
  161.         {
  162.             a[i] = arr[i];
  163.         }
  164.        
  165.         for (int i = index - 1; i < arr.length; i++)
  166.         {
  167.             a[i+ 1] = arr[i];
  168.         }
  169.        
  170.         a[index] = element;
  171.         numElements++;
  172.         arr = a;
  173.     }
  174.    
  175.     // Return the number of elements in your array.
  176.     public int size()
  177.     {
  178.         // your code here
  179.         return arr.length;
  180.     }
  181.    
  182.     private boolean isFull()
  183.     {
  184.         return numElements == currentSize;
  185.     }
  186.    
  187.     private void expand()
  188.     {
  189.         System.out.println("Expanding");
  190.         int newSize = currentSize * 2;
  191.         int[] newArray = new int[newSize];
  192.        
  193.         // Copy over old elements
  194.         for(int i = 0; i < currentSize; i++)
  195.         {
  196.             newArray[i] = arr[i];
  197.         }
  198.        
  199.         currentSize = newSize;
  200.         arr = newArray;
  201.     }
  202.    
  203.     public int get(int index)
  204.     {
  205.         return arr[index];
  206.     }
  207.    
  208.     public void add(int x)
  209.     {
  210.         if(isFull())
  211.         {
  212.             expand();
  213.         }
  214.         arr[numElements] = x;
  215.         numElements++;
  216.     }
  217.    
  218.     public String toString()
  219.     {
  220.         String str = "{";
  221.         for (int i=0; i < numElements; i++) {
  222.             str += arr[i] + ", ";
  223.         }
  224.         if (str.length() > 0 && str.charAt(str.length()-2)==',') {
  225.             str = str.substring(0, str.length()-2);
  226.             str += "}";
  227.         }
  228.         return str;
  229.     }
  230. }
  231.  
  232.  
  233.  
  234.  
  235. // SUMMERREADING
  236. public List<Book> filterBooks(List<Book> readingList, String author)
  237. {
  238.     // Remove all Books from the readingList that are not
  239.     // written by the given author. Return the resulting List
  240.     List<Book> myList = new ArrayList<>();
  241.    
  242.     for (int i = 0; i < readingList.size(); i++)
  243.     {
  244.         if (readingList.get(i).getAuthor().equals(author))
  245.         {
  246.             myList.add(readingList.get(i));
  247.         }
  248.     }
  249.     return myList;
  250. }
  251.  
  252. //ICECREAM
  253. public ArrayList<String> getAllChoices(String[] flavors, String[] toppings)
  254. {
  255.     ArrayList<String> list = new ArrayList<String>();
  256.     for(int i = 0; i < flavors.length; i ++)
  257.     {
  258.         for(int x = 0; x < toppings.length; x++)
  259.         {
  260.             String flavors1 = flavors[i];
  261.             String toppings1 = toppings[x];
  262.             list.add(flavors1 + " with " + toppings1 + " on top");
  263.         }
  264.     }
  265.     return list;
  266. }
  267.  
  268. //5.7.8 SUMROWS
  269. public int sumRow(int[][] matrix, int row)
  270. {
  271.     int sum = 0;
  272.     for (int i = 0; i < matrix[row].length; i++)
  273.     {
  274.         sum += matrix[row][i];
  275.     }
  276.     return sum;
  277. }
  278.  
  279. //5.8.4 WORDCOUNT
  280. import java.util.*;
  281.  
  282. public class WordCounts extends ConsoleProgram
  283. {
  284.     public void run()
  285.     {
  286.         HashMap <String, Integer> count = new HashMap<String, Integer>();
  287.  
  288.         String str = readLine("Enter a string: ");
  289.         String[] arr = str.split(" ");
  290.  
  291.         for (String element : arr)
  292.         {
  293.             String e = element.toLowerCase();
  294.             if (count.containsKey(e))
  295.             {
  296.                 count.put(e, count.get(e) + 1);
  297.             }
  298.             else
  299.             {
  300.                 count.put(e, 1);
  301.             }
  302.         }
  303.  
  304.         printSortedHashMap(count);
  305.     }
  306.  
  307.     private void printSortedHashMap(HashMap<String, Integer> wordCount)
  308.     {
  309.         Object[] keys = wordCount.keySet().toArray();
  310.         Arrays.sort(keys);
  311.    
  312.         for (Object word : keys)
  313.         {
  314.             int val = wordCount.get(word);
  315.             System.out.println(word + ": " + val);
  316.         }
  317.     }
  318. }
  319.  
  320. //5.9.4 BINARY
  321. public int binaryToDecimal(String binaryString)
  322. {
  323.     int numPlaces = binaryString.length();
  324.     int currentExponent = numPlaces - 1;
  325.     int decimalValue = 0;
  326.    
  327.     for(int i = 0; i < binaryString.length(); i++)
  328.     {
  329.         int placeValue = (int) Math.pow(2, currentExponent);
  330.         char currentDigit = binaryString.charAt(i);
  331.         int digitValue = Character.getNumericValue(currentDigit);
  332.        
  333.         if(i != binaryString.length() - 1)
  334.         {
  335.             System.out.print(" + ");
  336.         }
  337.        
  338.         decimalValue += digitValue * placeValue;
  339.         currentExponent--;
  340.     }
  341.    
  342.     return decimalValue;
  343. }
  344.  
  345. //5.9.8 ANOTHER BINARY
  346. public String binaryToText(String binary)
  347. {
  348.     String text = "";
  349.     ArrayList<String> bits = new ArrayList<String>();
  350.    
  351.     for (int i = 0; i <= binary.length() - 8; i += 8)
  352.     {
  353.         bits.add(binary.substring(i, i + 8));
  354.     }
  355.    
  356.     for (String element : bits)
  357.     {
  358.         int num = binaryToDecimal(element);
  359.         char letter = (char) num;
  360.         text += letter;
  361.     }
  362.     return text;
  363. }
  364.  
  365. public int binaryToDecimal(String binaryString)
  366. {
  367.     int numPlaces = binaryString.length();
  368.     int currentExponent = numPlaces - 1;
  369.     int decimalValue = 0;
  370.    
  371.     for(int i = 0; i < binaryString.length(); i++)
  372.     {
  373.         int placeValue = (int) Math.pow(2, currentExponent);
  374.         char currentDigit = binaryString.charAt(i);
  375.         int digitValue = Character.getNumericValue(currentDigit);
  376.         System.out.print(digitValue + " * (" + placeValue + ")");
  377.        
  378.         if(i != binaryString.length() - 1)
  379.         {
  380.             System.out.print(" + ");
  381.         }
  382.    
  383.         decimalValue += digitValue * placeValue;
  384.         currentExponent--;
  385.     }
  386.     System.out.println(" = " + decimalValue);
  387.     return decimalValue;
  388. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement