CSCI Problem Solving, Programming and Computers Spring, 2016 Assignment 2 {Inheritance, Interfaces, Arrays}

Similar documents
CSCI Problem Solving, Programming and Computers Spring, 2016 Assignment 2 {Inheritance, Interfaces, Arrays}

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved

2.8. Decision Making: Equality and Relational Operators

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

download instant at

CS141 Programming Assignment #6

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

Java for Non Majors Spring 2018

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

Multidimensional Arrays. CSE 114, Computer Science 1 Stony Brook University

Arrays. Eng. Mohammed Abdualal

CSCI 355 Lab #2 Spring 2007

Supplementary Test 1

Midterm Examination (MTA)

CSCI 355 LAB #2 Spring 2004

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Rules and syntax for inheritance. The boring stuff

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

a) Answer all questions. b) Write your answers in the space provided. c) Show all calculations where applicable.

Introduction to Computer Science Unit 2. Exercises

CSCI 2010 Principles of Computer Science. Basic Java Programming. 08/09/2013 CSCI Basic Java 1

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Objectives of the lesson

Chapter 6 Lab Classes and Objects

Weighted Graphs and Applications. CSE260, Computer Science B: Honors Stony Brook University

Chapter 6 Lab Classes and Objects

RANDOM NUMBER GAME PROJECT

Lecture 13 & 14. Single Dimensional Arrays. Dr. Martin O Connor CA166

Chapter 7 Multidimensional Arrays

Java Assignment 3: Loop Practice Ver 3.0 Last Updated: 12/1/2015 8:57 AM

Control Statements: Part 1

The Java language has a wide variety of modifiers, including the following:

ITERATION WEEK 4: EXMAPLES IN CLASS

AP Computer Science Unit 1. Writing Programs Using BlueJ

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Object-Oriented Concepts

Computer Science II (20082) Week 1: Review and Inheritance

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

Manipulating One-dimensional Arrays

CS 1316 Exam 1 Summer 2009

13 th Windsor Regional Secondary School Computer Programming Competition

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Selection Statements and operators

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

Programming with Java

Chapter 6 Arrays, Part D 2d Arrays and the Arrays Class

Circular Linked List Assignment

University of Palestine. Mid Exam Total Grade: 100

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Inheritance (Part 5) Odds and ends

This exam is open book. Each question is worth 3 points.

Full file at Chapter 2 - Inheritance and Exception Handling

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

1. What does the following code fragment write to the monitor?

CS 113 PRACTICE FINAL

AP Computer Science Unit 1. Programs

Distributed Systems Recitation 1. Tamim Jabban

Arrays Classes & Methods, Inheritance

Software Practice 1 Basic Grammar

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Module Contact: Dr Gavin Cawley, CMP Copyright of the University of East Anglia Version 1

Fundamentals of Programming Data Types & Methods

Chapter 5 Methods. public class FirstMethod { public static void main(string[] args) { double x= -2.0, y; for (int i = 1; i <= 5; i++ ) { y = f( x );

MORE OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 4 09/01/2011

Java Coding 3. Over & over again!

Final Exam Practice. Partial credit will be awarded.

JAVA Programming Language Homework I - OO concept

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014

Introduction to Java Applications

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 45 Obtained Marks:

Questions Answer Key Questions Answer Key Questions Answer Key

Section 2.2 Your First Program in Java: Printing a Line of Text

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Two-Dimensional Arrays

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

Introduction to Computer Science Unit 5. Programs: Strings

Java Bootcamp - Villanova University. CSC 2014 Java Bootcamp. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Distributed Systems Recitation 1. Tamim Jabban

Bjarne Stroustrup. creator of C++

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name

Administrivia. Last modified: Fri Aug 25 10:59: CS61B: Lecture #2 1

CS 101 Fall 2006 Midterm 3 Name: ID:

Object Oriented Programming. Java-Lecture 6 - Arrays

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

Array. Array Declaration:

CS 101 Exam 2 Spring Id Name

final int CHICAGO = 0; final int BOSTON = 1; final int MIAMI = 4;

For example, when your method is added to the class with the main method below, the output should be:

Computer Science II (20073) Week 1: Review and Inheritance

JAVA Ch. 4. Variables and Constants Lawrenceville Press

Transcription:

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.