CS230 Assignment 2

Assignment 2

 

Goals

  • Define and use your first Java classes
  • Understand better the difference between static and instance methods
  • Refresh your memory on how to use recursion
  • Examine more of the String class methods
 

DUE: Thursday Sep 19 at 11:59pm

  • How to turn in your assignment
  • Cover page for submission.
  • For Task 1 and 2, click on the class name link in the description to find java documentation that may be helpful. Note: Improving instance variable names is allowed.
  • For this assignment, you will work on the tasks alone. There is no task to work with a partner this time. (If you already found a partner, keep them for next assignment). Answer N/A to the questions about partners in the cover page.
  • Remember that for this and all assignments, you could use a 48 hour extension without asking for permission. However, if those two extra days are not sufficient for you to complete the assignment, you are required to contact the instructor and discuss a plan for completing the assignment before the assignment is due. We will work together to make sure that plan is a reasonable and effective so that it supports both your learning and your health.
 

Task 1: Points and Distances

  • Consult this Point documentation that may be helpful.

Create a class Point that keeps track of an x coordinate and a y coordinate on the Cartesian plane, both of type double. Provide getter and setter methods, and these methods:

  • findDistance() - an instance method that takes as input a Point, computes and returns its distance from the current Point using the Pythagorean formula

  • areEquidistant() - an instance predicate method that takes as input two Point objects, and returns true if the difference in distance from the current Point to each of those inputs is within TOLERANCE. Define TOLERANCE as a final value of 0.01,

  • readPoint() - a static method that reads the x and y coordinates from the keyboard, and calls the constructor to create a new Point instance, which is returned,

  • main() - creates three instances of type Point, whose coordinates are inputed by the user. Compute, and report if any of the three points is equidistant to the other two.

Reminders:

  • As with any class you write, define a toString() method to print the contents of the class in a nice way.
  • The main method should be short and high level, it should consist primarily of calling other methods.
  • Choose your testing cases carefully to thoroughly test the methods you defined.
  • We recommend that you start by implementing the rest of the methods before you implement readPoint. The reason is that you can hard-code specific objects' instance variables through the constructor (e.g., new Point(1.0, 2.0)) and use these objects to implement and test the rest of the methods. Once you are done, the readPoint method will be easier to implement and test. Doing otherwise you will find that testing is very time-consuming because you need to do through the keyboard input every time you test.
 

Task 2: Flights and Stopovers

  • Consult this Flight documentation that may be helpful.

Implement a class called Flight to represents an airline flight. Include instance data to represent the name of the airline, the flight number, and the flight origin and destination cities. Provide getter and setter methods, and these methods:

  • Flight constructor - define a constructor that takes as input the information to initialize the instance data,

  • readFlight() - a static method that takes as input a Scanner and outputs a Flight object specified by the user,

  • stopOver() - a static predicate method that takes as input two Flight objects and returns true if the first Flight object's destination matches the second Flight object's origin,

  • main() - reads a couple of flights and determines whether they share a stopover city, that is, whether the destination city of one flight is the origin city of the other.

Reminders:

  • Define a toString() method to print the contents of the class in a nice way. This will be helpful when reporting stopover information in main()
 

Task 3: Fun with Strings

  • Consult this StringOps documentation that may be helpful.

The aims of this problem are

  1. to practice working with Java's built-in String class and methods,
  2. to implement two new methods that will operate on Strings.

For this task, your solution must involve recursion. You may create own private helper methods as necessary.

Define a new class named StringOps and include definitions of the following methods:

  • public static String removeChar(String s, char ch) - returns a new string that is constructed from the input string s, with the first occurrence of the character ch removed. If the character ch is not contained in the input string S, then this method returns the original string.
    • Example: the method call removeChar("java", 'a') returns the string "jva", and the method call removeChar("java", 'q') returns "java"
  • public static void testAnagrams (String word1, String word2) - determines whether the two input strings word1 and word2 are anagrams and prints the outcome to the user. An anagram is defined as two words that have exactly the same letters, but in a different order
    • Example: the method call testAnagrams("melon", "lemon"); might result in the following printout: "melon and lemon are anagrams", while the method call testAnagrams("hello", "world"); might result in: "hello and world are not anagrams"
    • Assume that a word is not an anagram of itself, so the pair of words (hello, hello) are not anagrams.
    • Assume that both input strings consist entirely of lowercase letters.
    • Hint: Your removeChar() method will be helpful in implementing testAnagrams()
  • main() - contains code to test all of your methods