Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //ROADTRIP
- /*
- * This class stores information about a location on Earth. Locations are
- * specified using latitude and longitude. The class includes a method for
- * computing the distance between two locations.
- *
- * This implementation is based off of the example from Stuart Reges at
- * the University of Washington.
- */
- public class GeoLocation
- {
- // Earth radius in miles
- public static final double RADIUS = 3963.1676;
- private double latitude;
- private double longitude;
- private String name;
- /**
- * Constructs a geo location object with given latitude and longitude
- */
- public GeoLocation(String name, double theLatitude, double theLongitude)
- {
- latitude = theLatitude;
- longitude = theLongitude;
- this.name = name;
- }
- /**
- * Returns the latitude of this geo location
- */
- public double getLatitude()
- {
- return latitude;
- }
- /**
- * returns the longitude of this geo location
- */
- public double getLongitude()
- {
- return longitude;
- }
- // returns a string representation of this geo location
- public String toString()
- {
- return name + " (" + latitude + ", " + longitude + ")";
- }
- // returns the distance in miles between this geo location and the given
- // other geo location
- public double distanceFrom(GeoLocation other)
- {
- double lat1 = Math.toRadians(latitude);
- double long1 = Math.toRadians(longitude);
- double lat2 = Math.toRadians(other.latitude);
- double long2 = Math.toRadians(other.longitude);
- // apply the spherical law of cosines with a triangle composed of the
- // two locations and the north pole
- double theCos = Math.sin(lat1) * Math.sin(lat2) +
- Math.cos(lat1) * Math.cos(lat2) * Math.cos(long1 - long2);
- double arcLength = Math.acos(theCos);
- return arcLength * RADIUS;
- }
- public String getName()
- {
- return name;
- }
- }
- //ROADTRIP PART 2
- import java.util.*;
- public class RoadTrip
- {
- public ArrayList<GeoLocation> stops = new ArrayList<GeoLocation>();
- public void addStop(String name, double latitude, double longitude)
- {
- GeoLocation temp = new GeoLocation(name, latitude, longitude);
- stops.add(temp);
- }
- public int getNumberOfStops()
- {
- return stops.size();
- }
- public double getTripLength()
- {
- double total = 0;
- for (int i = 0; i < stops.size() - 1; i++)
- {
- total += (stops.get(i).distanceFrom(stops.get(i + 1)));
- }
- return total;
- }
- public String toString()
- {
- String returnString = "";
- for (int i = 0; i < stops.size(); i++)
- {
- returnString += (i + 1) + ". " + stops.get(i).getName()
- + " (" + stops.get(i).getLatitude() + ", " +
- stops.get(i).getLongitude() + ")\n";
- }
- return returnString;
- }
- }
- //EXPANDINGARRAY
- public class ExpandingArray
- {
- private static final int STARTING_SIZE = 10;
- private int[] arr;
- private int currentSize;
- private int numElements;
- public ExpandingArray()
- {
- arr = new int[STARTING_SIZE];
- currentSize = STARTING_SIZE;
- numElements = 0;
- }
- // Remove the element at index `index` and shift
- // all subsequent elements to the left.
- public int remove(int index)
- {
- // your code here
- int[] a = new int[arr.length - 1];
- for (int i = 0; i < index; i++)
- {
- a[i] = arr[i];
- }
- for (int i = index + 1; i < arr.length;i++)
- {
- a[i - 1] = arr[i];
- }
- numElements--;
- arr = a;
- return arr[index];
- }
- // Add the int `element` at the `index` in the array.
- // You'll need to shift everything one index to the right
- // after this index.
- public void add(int index, int element)
- {
- // your code here
- int[] a = new int[arr.length + 1];
- for (int i = 0; i < index; i++)
- {
- a[i] = arr[i];
- }
- for (int i = index - 1; i < arr.length; i++)
- {
- a[i+ 1] = arr[i];
- }
- a[index] = element;
- numElements++;
- arr = a;
- }
- // Return the number of elements in your array.
- public int size()
- {
- // your code here
- return arr.length;
- }
- private boolean isFull()
- {
- return numElements == currentSize;
- }
- private void expand()
- {
- System.out.println("Expanding");
- int newSize = currentSize * 2;
- int[] newArray = new int[newSize];
- // Copy over old elements
- for(int i = 0; i < currentSize; i++)
- {
- newArray[i] = arr[i];
- }
- currentSize = newSize;
- arr = newArray;
- }
- public int get(int index)
- {
- return arr[index];
- }
- public void add(int x)
- {
- if(isFull())
- {
- expand();
- }
- arr[numElements] = x;
- numElements++;
- }
- public String toString()
- {
- String str = "{";
- for (int i=0; i < numElements; i++) {
- str += arr[i] + ", ";
- }
- if (str.length() > 0 && str.charAt(str.length()-2)==',') {
- str = str.substring(0, str.length()-2);
- str += "}";
- }
- return str;
- }
- }
- // SUMMERREADING
- public List<Book> filterBooks(List<Book> readingList, String author)
- {
- // Remove all Books from the readingList that are not
- // written by the given author. Return the resulting List
- List<Book> myList = new ArrayList<>();
- for (int i = 0; i < readingList.size(); i++)
- {
- if (readingList.get(i).getAuthor().equals(author))
- {
- myList.add(readingList.get(i));
- }
- }
- return myList;
- }
- //ICECREAM
- public ArrayList<String> getAllChoices(String[] flavors, String[] toppings)
- {
- ArrayList<String> list = new ArrayList<String>();
- for(int i = 0; i < flavors.length; i ++)
- {
- for(int x = 0; x < toppings.length; x++)
- {
- String flavors1 = flavors[i];
- String toppings1 = toppings[x];
- list.add(flavors1 + " with " + toppings1 + " on top");
- }
- }
- return list;
- }
- //5.7.8 SUMROWS
- public int sumRow(int[][] matrix, int row)
- {
- int sum = 0;
- for (int i = 0; i < matrix[row].length; i++)
- {
- sum += matrix[row][i];
- }
- return sum;
- }
- //5.8.4 WORDCOUNT
- import java.util.*;
- public class WordCounts extends ConsoleProgram
- {
- public void run()
- {
- HashMap <String, Integer> count = new HashMap<String, Integer>();
- String str = readLine("Enter a string: ");
- String[] arr = str.split(" ");
- for (String element : arr)
- {
- String e = element.toLowerCase();
- if (count.containsKey(e))
- {
- count.put(e, count.get(e) + 1);
- }
- else
- {
- count.put(e, 1);
- }
- }
- printSortedHashMap(count);
- }
- private void printSortedHashMap(HashMap<String, Integer> wordCount)
- {
- Object[] keys = wordCount.keySet().toArray();
- Arrays.sort(keys);
- for (Object word : keys)
- {
- int val = wordCount.get(word);
- System.out.println(word + ": " + val);
- }
- }
- }
- //5.9.4 BINARY
- public int binaryToDecimal(String binaryString)
- {
- int numPlaces = binaryString.length();
- int currentExponent = numPlaces - 1;
- int decimalValue = 0;
- for(int i = 0; i < binaryString.length(); i++)
- {
- int placeValue = (int) Math.pow(2, currentExponent);
- char currentDigit = binaryString.charAt(i);
- int digitValue = Character.getNumericValue(currentDigit);
- if(i != binaryString.length() - 1)
- {
- System.out.print(" + ");
- }
- decimalValue += digitValue * placeValue;
- currentExponent--;
- }
- return decimalValue;
- }
- //5.9.8 ANOTHER BINARY
- public String binaryToText(String binary)
- {
- String text = "";
- ArrayList<String> bits = new ArrayList<String>();
- for (int i = 0; i <= binary.length() - 8; i += 8)
- {
- bits.add(binary.substring(i, i + 8));
- }
- for (String element : bits)
- {
- int num = binaryToDecimal(element);
- char letter = (char) num;
- text += letter;
- }
- return text;
- }
- public int binaryToDecimal(String binaryString)
- {
- int numPlaces = binaryString.length();
- int currentExponent = numPlaces - 1;
- int decimalValue = 0;
- for(int i = 0; i < binaryString.length(); i++)
- {
- int placeValue = (int) Math.pow(2, currentExponent);
- char currentDigit = binaryString.charAt(i);
- int digitValue = Character.getNumericValue(currentDigit);
- System.out.print(digitValue + " * (" + placeValue + ")");
- if(i != binaryString.length() - 1)
- {
- System.out.print(" + ");
- }
- decimalValue += digitValue * placeValue;
- currentExponent--;
- }
- System.out.println(" = " + decimalValue);
- return decimalValue;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement