CSCI 160 - Problem Solving, Programming and Computers Spring, 2016 Assignment 2 Inheritance, Interfaces, Arrays Q1. [20 points] Q1. TXT Short answer questions Fill in the blanks in each of the following statements: 1. If a class contains at least one abstract method, it s a(n) class. 2. Classes from which objects can be instantiated are called classes. 3. involves using a superclass variable to invoke methods on superclass and subclass objects, enabling you to program in the general. 4. Methods that are not interface methods and that do not provide implementations must be declared using keyword. 5. An array that uses two indices is referred to as a(n) array. State whether each of the statements that follows is true or false. If false, explain why. 6. [TRUE / FALSE] All methods in an abstract class must be declared as abstract methods. 7. [TRUE / FALSE] Invoking a subclass-only method through a subclass variable is not allowed. 8. [TRUE / FALSE] If a superclass declares an abstract method, a subclass must implement that method. Why? 9. [TRUE / FALSE] An object of a class that implements an interface may be thought of as an object of that interface type. 10. [TRUE / FALSE] An individual array element that s passed to a method and modified in that method will contain the modified value when the called method completes execution.
Q2. [30 points] FindDistance.java Write a program FindDistance to display the distance between two cities. Sample program run is shown (user input bolded): >This program finds the distance between two cities. Supported cities: Atlanta, Boston, Chicago, Houston, Los Angeles, New York, San Francisco, Washington. Enter starting city: San Francisco Enter destination city: Atlanta The distance from San Francisco to Atlanta is 2480 miles. The user will be asked to re-enter the name of a city if it doesn t match any of the names supported by the program. A normal two-dimensional array will be used to store the distances between cities. A ragged array could be used instead. The city names are maintained in array CITY_NAMES. The distance between each two cities are maintained in array DISTANCE. For example distance between Atlanta and Boston is 1110. Note that index of Atlanta is 0 and Boston is 1 in CITY_NAMES. So DISTNCE[0][1] is the distance between two cities. public class FindDistance private static final String[] CITY_NAMES = "Atlanta", "Boston", "Chicago", "Houston", "Los Angeles", "New York", "San Francisco", "Washington"; private static final int[][] DISTANCES = 0, 1110, 710, 790, 2190, 850, 2480, 620, 1110, 0, 1000, 1830, 3020, 210, 3130, 450, 710, 1000, 0, 1090, 2050, 810, 2170, 710, 790, 1830, 1090, 0, 1540, 1610, 1910, 1370, 2190, 3020, 2050, 1540, 0, 2790, 390, 2650, 850, 210, 810, 1610, 2790, 0, 2930, 240, 2480, 3130, 2170, 1910, 390, 2930, 0, 2840, 620, 450, 710, 1370, 2650, 240, 2840, 0; public static void main(string[] args) //TODO: Display initial message, including a list of legal cities // TODO:Call getcitycode to obtain codes for starting city and destination city //TODO: Display distance between chosen cities private static int getcitycode(string prompt) // TODO: method getcitycode:prompts user to enter city name; returns corresponding city code(index of city in CITY_NAMES). If city name is not recognized, allows user to enter another name.
Q3. [20 points] StringReverse.java Write a program StringReverse with following methods to reverse a string:. For example, reverse("hello!") returns the string "!olleh. 1. a recursive method reverse(string text) that reverses a string 2. A iterative method reverse2(string text) that reverses a string public class StringReverse public static String reverse(string text) //TODO public static String reverse2(string text) //TODO public static void main(string[] args) System.out.println("reverse-recursive:") System.out.println(StringReverse.reverse("Hello!")); System.out.println("Expected:!olleH"); System.out.println(StringReverse.reverse("Odd")); System.out.println("Expected: ddo"); System.out.println("reverse-iterative:") System.out.println(StringReverse.reverse2("Hello!")); System.out.println("Expected:!olleH"); System.out.println(StringReverse.reverse2("Odd")); System.out.println("Expected: ddo"); Q2. [30 points] LinearSearch.java Write a program LinearSearch that implements a method to search a value(key) in an array by scanning through elements left to right in a linear fashion. If the key is found its index should be return, otherwise -1 should be returned. Provide: 1. Iterative linear search method linearsearch(int key) 2. Recursive linear search method recursivelinearsearch(int key) import java.util.random; import java.util.scanner; import java.util.arrays; public class LinearArray private int[] data; // array of values private static Random generator = new Random(); public LinearArray( int size ) //populate array with random values in range 10-99
data = new int[ size ]; for ( int i = 0; i < size; i++ ) data[ i ] = 10 + generator.nextint( 90 ); public String tostring() return Arrays.toString(data); /*Recursive method*/ public int recursivelinearsearch( int search, int start ) //TODO /*Iterative method*/ public int linearsearch( int searchkey ) //TODO public static void main( String[] args ) Scanner input = new Scanner( System.in ); int key; // search key int index; // location of search key in array // create array and output it LinearArray searcharray = new LinearArray( 10 ); System.out.println( searcharray + "\n" ); // print array // get input from user System.out.print( "Please enter an integer value (-1 to quit): " ); key = input.nextint(); // read first int from user // recursive search index = searcharray.recursivelinearsearch( key,0 ); System.out.println("Recursive search:"); if ( index == -1 ) // integer was not found System.out.println( "The integer " + key + " was not found.\n" ); else // integer was found System.out.println( "The integer " + key + " was found in index " + index + ".\n" ); //iterative search index = searcharray.linearsearch( key ); System.out.println("Iterative search:"); if ( index == -1 ) // integer was not found System.out.println( "The integer " + key + " was not found.\n" ); else // integer was found System.out.println( "The integer " + key + " was found in index " + index + ".\n" ); // end main
Note: Marks will be assigned for proper comments and output. In addition to comments in your program, also include a file header comment at the top of each program file that includes your name, assignment number, and a description of the program: //********************************************************** // AssignmentNo (Enter Assignment number): (Enter ClassName) // Author(s): (Enter your full name here) // Description: (Enter Description) //********************************************************* Online Submission Instructions: Copy the folder <<yourname>>asg3 (e.g. JohnDoeAsg3) containing answer files, into /usr/people/handin/cs160 before midnight of the date indicated in class website.