CSC 1051 Algorithms and Data Structures I. Final Examination December 17, Name:

Similar documents
CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

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

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name: KEY. Question Value Score

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 26, Name: Key

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name: Question Value Score

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

CSC Algorithms and Data Structures I. Midterm Examination February 25, Name:

CSC 1051 Algorithms and Data Structures I. Final Examination December 18, Name:

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 9, Name: KEY

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 2, Name:

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 11, Name: KEY

CSC 1051 Algorithms and Data Structures I. Final Examination December 20, Name: KEY. Question Value Score

Happy Cinco de Mayo!!!!

Happy Cinco de Mayo!!!!

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 6, Name:

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Arrays - Review. Initializer Lists. The for-each Loop. Arrays, Part 2. Dr. Papalaskari 1. CSC 1051 Data Structures and Algorithms I

Where do objects come from? Good question!

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

COE 211/COE 212 Computer/Engineering Programming. Welcome to Exam II Thursday December 20, 2012

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Iterators and File Input

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

Second Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 11 November 2010

Arrays and File Input

Arrays - Review. Arrays as Parameters. Arrays as Parameters. Arrays and File Input. Dr. Papalaskari 1. CSC 1051 Data Structures and Algorithms I

Arrays and File Input

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

CSC 1051 Arrays - Review questions

Final Exam CS 152, Computer Programming Fundamentals December 9, 2016

class declaration Designing Classes part 2 Getting to know classes so far Review Next: 3/18/13 Driver classes:

CS1083 Week 2: Arrays, ArrayList

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

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1

COE 212 Engineering Programming. Welcome to Exam II Monday May 13, 2013

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

Designing Classes part 2

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

CSC 1051 Data Structures and Algorithms I

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

COE 212 Engineering Programming. Welcome to Exam II Thursday April 21, Instructors: Dr. Salim Haddad Dr. Joe Tekli Dr. Wissam F.

CS 101 Fall 2006 Midterm 3 Name: ID:

CSC 1051 Data Structures and Algorithms I

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 1, Name: KEY A

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A

COE 212 Engineering Programming. Welcome to the Final Exam Tuesday December 15, 2015

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

First Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 7 October 2010

Final Exam Practice. Partial credit will be awarded.

COE 211 Computer Programming. Welcome to Exam I Tuesday March 13, 2018

Java Review. Fundamentals of Computer Science

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

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) Midterm Examination

Algorithms and Conditionals

Assignment 1 due Monday at 11:59pm

Advanced Java Concept Unit 1. Mostly Review

// constructor: takes a String as parameter and creates a public Cat (String x) { name = x; age = 0; lives = 9; // cats start out with 9 lives }

Name CIS 201 Midterm II: Chapters 1-8

Chapter 3. Selections

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

CSC 1051 Data Structures and Algorithms I

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

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

Java Coding 3. Over & over again!

Chapter. We've been using predefined classes. Now we will learn to write our own classes to define objects

CSCI 1226 A Test #1. Wednesday, 10 October, 2018 Name: Student #: General Instructions Read and follow all directions carefully.

CS 113 MIDTERM EXAM 2 SPRING 2013

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

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

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

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

Introduction to Arrays

Practice Midterm 1. Problem Points Score TOTAL 50

CSE 1223: Exam II Autumn 2016

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ Test 2. Question Max Mark Internal External

Java Flow of Control

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016

Computer Programming, I. Laboratory Manual. Final Exam Solution

SAMPLE EXAM Final Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 16 December 2010

Practice Midterm 1 Answer Key

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Problem Grade Total

Java I/O and Control Structures

Data Representation and Applets

Object Oriented Programming with Java

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

Data Representation and Applets

Data Representation and Applets

Name: Checked: Access the Java API at the link above. Why is it abbreviated to Java SE (what does the SE stand for)?

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2012

Chapter 6 Lab Classes and Objects

Transcription:

CSC 1051 Algorithms and Data Structures I Final Examination December 17, 2013 Name: Question Value 1 10 Score 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 20 TOTAL 100 Please answer questions in the spaces provided. If you make a mistake or for some other reason need more space, please use the back of this page or the extra blank page at the end and clearly indicate where the answer can be found. Good luck and best wishes for the holidays and the New Year!

1. ( / 10). (a) Show the output produced by the following code fragment: String mess = ""; char[] grades = 'A', 'B', 'C', 'D', F'; for (char ch: grades) mess = ch + mess; System.out.println (mess); (b) Show the output produced by the following code fragment: char[] grades = 'A', 'B', 'C', 'D', F'; for (char ch: grades) System.out.print ((char) (ch + 1)); (c) Suppose an int array ratings contains values in the range 0-3. Write a code fragment that creates an array count that contains the frequency of occurrences of 0 s, 1 s, 2 s and 3 s in the ratings array, and then prints this information. For example, if the array ratings has these contents: ratings 0 1 2 3 4 5 6 2 3 2 1 0 2 2 Your code should create the array count with the following contents: count 0 1 2 3 1 1 4 1 and the output would be: Count for 0: 1 Count for 1: 1 Count for 2: 4 Count for 3: 1 Write your code fragment below. Assume the array ratings is already initialized to some values. Your code should work for any size array.

2. ( / 10) Draw diagrams showing the contents of the arrays after execution of the following code fragment: a) int[] a = 3, 5, 2; int[] b = new int[8]; for (int i=0; i<a.length; i++) b[i] = a[i]; for (int i=b.length - 1; i >= a.length; i--) b[i] = a[0]; b) int[][] table = new int[4][5]; for (int i=0; i < 3; i++) table[i][i+1] = i + 10; c) char[][] table = new char[3][3]; String sample = "Relax. Exams can be fun. "; int count = 0; for (int i=0; i < 3; i++) for (int j=0; j < 3; j++) table[i][j] = sample.charat(count); count++;

3. ( / 10) Show what gets printed with the file contents shown below. //********************************************************* **** // SomethingToDoWithFiles.java Author: MAP //********************************************************* **** import java.util.scanner; public class SomethingToDoWithFiles public static void main(string[] args) throws IOException File myfile1 = new File("sample1.inp"); filescan1 = new Scanner (myfile1); int count = 0; while (filescan1.hasnext()) String line = filescan1.nextline(); Scanner scanline = new Scanner(line); String[] word = new String[3]; for (int i = 0; i< 3; i++) word[i] = scanline.next(); System.out.println(count + " " + word[2])); count++; System.out.println("All done " + count); sample1.inp do re mi re mi fa mi fa sol

4. ( / 10) (a) Write the Java code for a method named counta() that accepts a String parameter named str. The method should compute and return the number of occurrences of the letter A in str. For example, if the method is invoked with the String HAPPY BIRTHDAY, it should return 2. (b) Assume counta() is defined as a static method in some class, and that a String has already been obtained from the user and stored in a variable named msg. Write a code fragment that could be used in the main() method of that class to invoke counta() so as to print the number of A s in msg as follows: The message contains 2 occurrences of the letter A. (In this example, we assume msg= HAPPY BIRTHDAY but your code should work for any String.) Note: Be sure that your code, above, always use singular/plural appropriately.

5. ( / 10) Review the program and answer the questions below: import java.awt.*; import javax.swing.japplet; public class FinalApplet extends JApplet public void paint( Graphics page) Color mycolor = new Color(100, 100, 100); // what s this color? page.setcolor( mycolor); page.fillrect(0, 0, 300, 200); page.setcolor( Color.black ); page.drawline(0, 200, 300, 0); page.setcolor( Color.red); page.filloval(-50,-50,100,100); a) For each identifier listed in the table, specify what it refers to choose one of the following: Class... Method... Object... Package (Note: some of these may occur more than once) java.awt JApplet Graphics page Color mycolor setcolor filloval b) What do you think the mycolor looks like? c) Draw a sketch of the image displayed by the applet indicate the color of each figure or line. Be sure to label the coordinate system.

6. ( / 10) Suppose you look up a class in the Java API and find something that looks like the following (NOTE: this is a made- up class.): java.bogus Class Awesome java.lang.object java.bogus.awesome public class Awesome extends Object This is a made-up class. It does not matter what it actually does, i am just trying to see if you know how to use it. Constructor Summary Awesome(int x) Creates a new Awesome object of size x. Method Summary int computate(boolean x, char y) Mystery method 1. void updator(int x) Mystery method 2. getth a) What import statement do you need to include in your program in order to use this class? b) Write some code to declare variables for two objects of this class, named thing1 and thing2 (use any values of the appropriate type in the constructor).

(Question 6, continued) c) For each of the methods listed in the method summary, state its name, return type and the number and types of parameters it requires. Method name: Method return type: Required paramaters for the method: (how many? of what type(s)?) Method name: Method return type: Required paramaters for the method: (how many? of what type(s)?) d) Suppose you are writing a driver class that uses Awesome and you have already declared and instantiated objects thing1 and thing2. For each of the following, check the appropriate box to indicate whether it represents a valid Java statement. Valid Java statement? Yes No Awesome.updator (5); thing1.updator (int x); thing1. computate (boolean x, char y); thing1. computate (false, 'y'); thing2.updator(); thing2.updator(0); if (thing2.computate(true, 'y') > thing1.computate(true, 'y')) System.out.println( ok ); thing1.computate (false, 'y'); same as previous thing2.updator(1) = 2; System.out.println(thing1.computate(false, 'y'));

7. ( / 10) Suppose you have a Passenger class defined as follows to represent passengers in the Titanic: public class Passenger // instance data private int status; private boolean child; private char sex; private boolean survivor; // constructor public Passenger(int status, boolean child, char sex, boolean survivor) this.status = status; this.child = child; this.sex = sex; this.survivor = survivor; // tostring() returns String description of Passenger public String tostring() String pass = ""; switch (status) case 1: pass += "1st class\t"; break; case 2: pass += "2nd class\t"; break; case 3: pass += "3rd class\t"; break; case 4: pass += "crew\t"; break; pass += (child? "child": "adult") + "\t"; pass += ((sex == 'm')? "male" : "female") + "\t"; pass += (survivor? "survived": "perished"); return pass; On the next page, write the code for a PassengerData class to represent the list of passengers of the Titanic and to keep track of total number of passengers and survivors, using an array of Passenger objects. Some bits of the code and comments are already written. Use these comments as guidelines for your code, in the spaces provided on the next page. Note that you only need to implement the constructor and addpassenger() methods, which should initialize and update, respectively, the array and the two counts (i.e., count and numsurvivors).

(Question 7, continued) public class PassengerData private Passenger[] collection; private int count; private int numsurvivors; //------------------------------------------------------ // Constructor: Creates an initially empty collection // with space for up to 5000 passengers. //------------------------------------------------------ //------------------------------------------------------ // Method addpassenger(): Adds a Passenger to the // collection. //------------------------------------------------------ public void addpassenger (int status, boolean child, char sex, boolean survivor)

8. ( / 10) Construct an algorithm that inputs a number num and prints all its factors (i.e., all the values of x in the range 2 (num- 1) such that num is divisible by x). After the factors are printed, print a goodbye message. Note: You are NOT being asked to print the only prime factors (that one is more challenging). Example: If num (i.e., the input) is 20, the algorithm should print: The factors of 20 are: 2 4 5 10 Goodbye Directions: Write your algorithm by rearranging and structuring elements chosen from the list below, using indentation to show structure. Do not use anything else and note that not all of these are needed, but you may use one of them more than once, if necessary. input num input x x = 1 x = 2 x = x + 1 if (num % x == 0) if (num % 2 == 0) if (num % 2 == x) else while (x < num) while (x <= num) while (num %2 == x) while (num % 2 == 0) print The factors of num are: print x print num print Goodbye

9. ( / 20) Write a complete Java program consisting of a class Book and a driver class named Bookshelf. The Book class should contain instance variables representing information such as the following: Example: Ø Title: A Confederacy of Dunces Ø Author: John Kennedy Toole Ø Average Customer Review: 4.2 Ø Price: $11.48 Ø Quantity in stock: 52 The Book class should have a constructor, accessor and mutator methods for the average customer review score, and a tostring() method. Do not write code for any additional methods (no need to write accessors and mutators for all instance variables). The driver class Bookshelf should implement the following algorithm: o Instantiate three variables of the Book class named item1, item2, item3 (make up values for the data) o Print the info of item1, item2, item3 o Change the average customer review score for item2 (make up a new value) o Print the info of item1, item2, item3 Notes: It is NOT necessary to include comments with your code, but be sure to use good indentation. Reminder: Formatting a number as a currency: Example: (assumes import java.text.numberformat; ) NumberFormat fmt = NumberFormat.getCurrencyInstance(); System.out.println(fmt.format(3.4)); è Prints: $3.40 Write the complete code for the two classes in the next two pages. è è