Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- //Complete the class ArrayListMethods. It consists of four short methods to manipulate an array list of strings.
- //The method header and javadoc are given to you.
- //
- //For the draft, provide the isSorted method.
- //
- import java.util.ArrayList;
- public class ArrayListMethods
- {
- ArrayList<String> list; //instance variable
- /**
- * Constructor for objects of class ArrayListMethods
- */
- public ArrayListMethods(ArrayList<String> arrayList)
- {
- // initialise instance variables
- list = arrayList;
- }
- /**
- * Determines if the array list is sorted (do not sort)
- * When Strings are sorted, they are in alphabetical order
- * Use the compareTo method to determine which string comes first
- * You can look at the String compareTo method in the Java API
- * @return true if the array list is sorted else false.
- */
- public boolean isSorted()
- {
- boolean sorted = true;
- // TODO: Determine if the array is sorted.
- for (int i = 0; i < list.size() - 1; i++)
- {
- String first = list.get(i);
- String second = list.get(i+1);
- if (first.compareTo(second) > 0)
- {
- sorted = false;
- }
- }
- return sorted;
- }
- /**
- * Replaces all but the first and last elements with the larger of its two neighbors
- * You can use the compareTo() method to determine which string is larger (larger in alphabetical
- * order).
- * Example: if the list is originally
- * ["cat", "ape", "dog", "horse", "zebra"]
- * after this method it should be:
- * ["cat", "dog", "horse", "zebra", "zebra"]
- *
- * @return a string representation of the modified array list. (do this with list.toString())
- */
- public void replaceWithLargerNeighbor()
- {
- // TODO: Replace all but the first and last elements with the larger of its to neighbors
- for (int i = 1; i < list.size() - 1; i++)
- {
- String first = list.get(i);
- String second = list.get(i+1);
- if (first.compareTo(second) > 0)
- {
- list.set(i+1, first);
- }
- else
- {
- list.set(i, second);
- }
- }
- }
- /**
- * Gets the number of duplicates in the list.
- * Be careful to only count each duplicate once. Start at index 0. (Does it match any of the other elements?)
- * Get the next word. It is at index i. (Does it match any of the words with index > i?)
- * @return the number of duplicate words in the list
- */
- public int countDuplicates()
- {
- int duplicates = 0;
- // TODO: Write the code to get the number of duplicates in the list
- for (int i = 0; i < list.size() - 1; i++)
- {
- for (int j = i+1; j < list.size(); j++)
- {
- if (list.get(i).equals(list.get(j)))
- {
- duplicates++;
- }
- }
- }
- return duplicates;
- }
- /**
- * Moves any word that starts with x, y, or z to the front of the ArrayList, but
- * otherwise preserves the order
- * Example: if the list is orginially
- * ["ape", "dog", "xantus", "zebra", "cat", "yak"]
- * after this method is called it should be
- * ["xantus", "zebra", "yak", "ape", "dog", "cat"]
- */
- public void xyzToFront()
- {
- int insertAt = 0;
- // TODO: Move any word that starts with x, y, or z to the front of the ArrayList, but otherwise preserves the order
- for (int i = 0; i < list.size(); i++)
- {
- String word = list.get(i).substring(0, 1);
- if ("xyz".contains(word))
- {
- list.add(insertAt, list.remove(i));
- insertAt++;
- }
- }
- }
- /**
- * gets the string representation of this array list
- * @returns the string representation of this array list in
- * standard collection format
- */
- public String toString()
- {
- return list.toString();
- }
- }
- ///////////////////////////////
- ///////////////////////////////////////////////
- ////////////////////////////////////////////
- //////////////////////////////////////////
- ///////////////////////////////////////////////
- //
- //Complete the class TripPlan which describes the cities that are visited by a tour conducted by Java Now Tours.
- //Keep an arraylist of cities (just the string name). Have methods to add a city, remove a city,
- //to return the names of the cities in a String, and to reverse the order of the elements in the array list.
- //
- //Notice that the reverse method is void.
- //
- //For the draft, provide the instance variable and finish the constructor.
- //For the toString method simply return the string "TripPlan["
- //
- import java.util.ArrayList;
- /**
- * A TripPlan represents a trip and holds a collection of city names.
- */
- public class TripPlan
- {
- // TODO: add instance variable here
- ArrayList<String> plan;
- /**
- * Constructs an empty trip.
- */
- public TripPlan()
- {
- // TODO: Initialize the instance variable
- plan = new ArrayList<String>();
- }
- /**
- * Add a city to the list.
- * @param cityName the city to add
- */
- public void addCity(String cityName)
- {
- // TODO: Write code to add a city to the array list instance variable
- plan.add(cityName);
- }
- /**
- * Returns a string describing the object.
- * @return a string in the format "TripPlan[cityName1,cityName2,...]"
- */
- public String toString()
- {
- String ret = plan.toString();
- return "TripPlan" + ret.replace(", ", ",");
- }
- /**
- * Removes a city form the this trip
- * @param cityName city to remove
- */
- public void removeCity(String cityName)
- {
- // TODO: Write code to remove a city to the array list instance variable
- if (plan.indexOf(cityName) != -1)
- {
- plan.remove(plan.indexOf(cityName));
- }
- }
- /**
- * Reverses the elements in the itinerary.
- */
- public void reverse()
- {
- System.out.println(plan);
- for (int i = 0; i < plan.size(); i++)
- {
- plan.add(i, plan.remove(plan.size() - 1));
- }
- }
- }
- //////////////////////////////////
- ////////////////////////////
- //////////////////////////////////
- ///////////////////////////////////
- ////////////////////////////////////
- //Create a Polygon class. A polygon is a closed shape with lines joining the corner points.
- //You will keep the points in an array list. Use object of java.awt.Point for the point.
- //Polygon will have as an instance variable an ArrayList of Points to hold the points
- //The constructor takes no parameters but initializes the instance variable
- //
- //The add method adds a Point to the polygon
- //
- //The perimeter method returns the perimeter of the polygon
- //
- //The draw method draws the polygon by connecting consecutive points and then
- //connecting the last point to the first.
- //
- //No methods headers or javadoc is provided this time. You get to try your hand at writing a class almost from scratch
- //
- //For the draft, finish the constructor.
- //Have the perimeter method return 0 and have the draw mwthod draw a
- //line from point 0, 0 to point 30, 40
- //
- import java.util.ArrayList;
- import java.awt.Point;
- public class Polygon
- {
- // TODO: provide the required constructor, instance variable, and methods
- ArrayList<Point> points;
- public Polygon()
- {
- points = new ArrayList<Point>();
- }
- public void add(Point p)
- {
- points.add(p);
- }
- public double perimeter()
- {
- Point first, second;
- double perimeter = 0.0;
- for(int i = 0; i < points.size() - 1; i++)
- {
- first = points.get(i);
- second = points.get(i+1);
- perimeter += Math.sqrt(Math.pow(second.getX() - first.getX(), 2) + Math.pow(second.getY() - first.getY(), 2));
- }
- first = points.get(0);
- second = points.get(points.size() - 1);
- perimeter += Math.sqrt(Math.pow(second.getX() - first.getX(), 2) + Math.pow(second.getY() - first.getY(), 2));
- return perimeter;
- }
- public void draw()
- {
- Point first, second;
- for(int i = 0; i < points.size() - 1; i++)
- {
- first = points.get(i);
- second = points.get(i+1);
- Line line = new Line(first.getX(), first.getY(), second.getX(), second.getY());
- line.draw();
- }
- first = points.get(0);
- second = points.get(points.size() - 1);
- Line line = new Line(first.getX(), first.getY(), second.getX(), second.getY());
- line.draw();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment