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

Similar documents
Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

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

Programming with Java

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

CS1150 Principles of Computer Science Math Functions, Characters and Strings (Part II)

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

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

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

Using Java Classes Fall 2018 Margaret Reid-Miller

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

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

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

Computational Expression

What did we talk about last time? Math methods boolean operations char operations


Full file at

Chapter 4 Mathematical Functions, Characters, and Strings

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Midterms Save the Dates!

CST242 Strings and Characters Page 1

COE 212 Engineering Programming. Welcome to Exam I Tuesday November 11, 2014

Class Library java.lang Package. Bok, Jong Soon

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Final Exam Practice. Partial credit will be awarded.

ing execution. That way, new results can be computed each time the Class The Scanner

Eng. Mohammed Abdualal

Chapter 4 Classes in the Java Class Libraries

Object Oriented Programming. Java-Lecture 1

Appendix 3. Description: Syntax: Parameters: Return Value: Example: Java - String charat() Method

Introduction to Computer Science Unit 2. Notes

String. Other languages that implement strings as character arrays

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

More on variables and methods


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

Introduction to Java Unit 1. Using BlueJ to Write Programs

Problem Grade Total

Creating Strings. String Length

Introduction to Computer Science Unit 2. Notes

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

H212 Introduction to Software Systems Honors

What did we talk about last time? Examples switch statements

CSE 114 Midterm 1. Please leave one seat between yourself and each of your neighbors.

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

AP Computer Science Unit 1. Writing Programs Using BlueJ

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

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

Lab 14 & 15: String Handling

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

CS 1301 Ch 8, Part A

A variable is a name for a location in memory A variable must be declared

Computer Science 145 Midterm 1 Fall 2016

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

Chapter 12 Strings and Characters. Dr. Hikmat Jaber

AP Computer Science Unit 1. Writing Programs Using BlueJ

Faculty of Science COMP-202A - Foundations of Computing (Fall 2013) - All Sections Midterm Examination

Question: Total Points: Score:

OOP-Lecture Java Loop Controls: 1 Lecturer: Hawraa Sh. You can use one of the following three loops: while Loop do...while Loop for Loop

Lesson:9 Working with Array and String

CS 101 Exam 1 Spring 200 Id Name

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

double float char In a method: final typename variablename = expression ;

AP Computer Science A

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

STUDENT LESSON A10 The String Class

COE 212 Engineering Programming. Welcome to Exam I Thursday June 21, Instructor: Dr. Wissam F. Fawaz

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Fundamentals of Programming Data Types & Methods

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

Faculty of Science COMP-202A - Foundations of Computing (Fall 2012) - All Sections Midterm Examination

Mid Term Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Total Marks: 50 Obtained Marks:

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2009) - All Sections Final Examination

Midterms Save the Dates!

x++ vs. ++x x=y++ x=++y x=0; a=++x; b=x++; What are the values of a, b, and x?

PART A (5 pts): Assume an ArrayList named values contains the following eight numbers:

Java s String Class. in simplest form, just quoted text. used as parameters to. "This is a string" "So is this" "hi"

String is one of mostly used Object in Java. And this is the reason why String has unique handling in Java(String Pool). The String class represents

Basic Computation. Chapter 2

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

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

Practice Midterm 1. Problem Points Score TOTAL 50

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

Java Flow of Control

AP CS Unit 3: Control Structures Notes

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

We now start exploring some key elements of the Java programming language and ways of performing I/O

COMP 401 Midterm. Tuesday, Oct 18, pm-3:15pm. Instructions

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

String related classes

Control Statements: Part 1

"Hello" " This " + "is String " + "concatenation"

AP Computer Science Unit 1. Programs

CSE 131S Introduction to Computer Science Summer SON Exam I

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

Interaction with Android

Midterm Exam 2 Thursday, November 15th, points (15% of final grade) Instructors: Jim Williams and Marc Renault

CS 101 Fall 2006 Midterm 3 Name: ID:

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

Transcription:

First Name (Print): Last Name (Print): Student Number: The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II 2016 Instructor: Dr. Bowen Hui Tuesday, April 19, 2016 Time: 6:00pm - 8:00pm Instructions: This is a closed book exam. No calculators are allowed. Turn off your cell phone. Have your student ID card out on your desk. This exam has 16 pages (including this cover page). Question Score Possible Marks Part 1 6 Part 2 Question 1 6 Part 2 Question 2 11 Part 2 Question 3 8 Part 2 Question 4 8 Part 3 16 Total 52 (+ 3 bonus marks)

Student Name: 1 Part 1: Multiple Choice (6 points) Circle one answer for each question below. 1. The ASCII value for character A is 65. What does the expression System.out.println( "A" + 1 ) display? (a) A1 (b) 66 (c) B (d) Syntax error (illegal expression) 2. Suppose int i = 5, which of the following can NOT be used as an index for array double[] t = new double[100]? (a) i (b) ( int )( Math.random() * 10 ) (c) ( int )( Math.random() * -1 ) (d) 90 + i 3. Assuming the loop body does not modify hc below, how many iterations will the following loop execute? for (int hc = 0; hc <= n; hc++) // iteration (a) 2*n (b) n (c) n - 1 (d) n + 1 4. A method that does not return any output should hae which return type in the method header? (a) int (b) double (c) boolean (d) void 5. To declare a constant MAX LENGTH inside a class, you write: (a) final static MAX LENGTH = 99.98; (b) static double MAX LENGTH = 99.98; (c) final double MAX LENGTH = 99.98; (d) final int double MAX LENGTH = 99.98; 6. Which method header should be used if you want to provide an accessor method for a boolean attribute finished? (a) public void getfinished() (b) public boolean getfinished() (c) public boolean isfinished() (d) public void isfinished()

Student Name: 2 Part 2: Short Answers Question 1. (6 points) Write the Java code inside a main method that simulates a simple slot machine where three numbers between 0 and 9 (inclusive) are randomly selected and printed side by side. Based on these numbers, your program will display the output as follows: Print Winner! if all three numbers are the same Print Almost there! if any two numbers are the same Print Better luck next time! otherwise The game will repeat unless the user enters no. Sample output: 8, 6, 2 Better luck next time! Play again? yes 7, 1, 7 Almost there! Play again? no Grading scheme: [1 pt] Generating random numbers [1 pt] All the print statements and proper output [1 pt] Comparison when all 3 numbers are the same [1 pt] Comparison when any 2 numbers are the same [1 pt] Repeating the game [1 pt] Checking user input for ending the game

Student Name: 3 This page is intentionally left blank.

Student Name: 4 Question 2. [11 points] You are given the start of a Java program that helps a hospital analyze the flow of patients through the emergency room below: import java.util.scanner; public class HospitalFlow public static void main( String[] args ) // constants final int NUMDAYS = 20; // data over 20 days final int NUMHOURS = 24; // 24 hours in a day int[][] data = new int[ NUMHOURS ][ NUMDAYS ]; Scanner input = new Scanner( System.in ); int hour; int nump; // read input from user for( int hr=0; hr<numhours; hr++ ) System.out.println( "For hour " + hr + ": " ); for( int dd=0; dd<numdays; dd++ ) System.out.print( "Enter number of patient on day " + (dd+1) + ": " ); nump = input.nextint(); data[ hr ][ dd ] = nump; // analyze total number of patients per hour //... // report busiest hour of day //... This program asks the user to input the number of patients reporting in the emergency room over 20 days for each hour of the day, from midnight (hour 0), to 11pm (hour 23). This information is then stored as a 2D array called data. There are two steps missing from this program which you will have to complete in this question. Continue on the next page...

Student Name: 5 Part a. Continuing with the above program, write the Java code that tallies up the total number of patients per hour. In particular: Create a new 1D array called dataoverdays that will store the total number of patients for each hour of the day Go through the data array, for each hour of the day, sum up the number of patients for that hour Record this sum into the appropriate hour slot of the dataoverdays Grading scheme: [1 pt] Declare and create dataoverdays [1 pt] Loop through each hour of the array [2 pts] Compute the sum for that hour [1 pt] Store the sum into dataoverdays Continue on the next page...

Student Name: 6 Part b. Continuing on from part (a), write the Java code analyzes the array dataoverdays and report the busiest hour in the day and how many patients there were in total. Sample output (some output omitted due to limited space): For hour 0: Enter number of patient on day 1: 3 Enter number of patient on day 2: 5... Enter number of patient on day 20: 3 For hour 1: Enter number of patient on day 1: 3 Enter number of patient on day 2: 2... Enter number of patient on day 20: 1... For hour 23: Enter number of patient on day 1: 0 Enter number of patient on day 2: 2... Enter number of patient on day 20: 1 Over the past 20 days, the busiest hour in the day is hour 14 which had 189 patients in total. Grading scheme: [1 pt] Keep track of the busiest hour [1 pt] Keep track of the total number of patients at busiest hour [2 pts] Find the busiest hour in dataoverdays [1 pt] Update the proper variables [1 pt] Display analysis results as shown in the sample output above

Student Name: 7 Question 3. (8 points) Given the following Birthday class, you will see that it has the day, month, and year of birth as attributes. This class also has various methods, such as a constructor that initializes these attributes, and an accessor method for each attribute. public class Birthday // attributes private int day; private int month; private int year; // constructor public Birthday( int dd, int mm, int yy ) day = dd; month = mm; year = yy; // accessors public int getday() return day; public int getmonth() return month; public int getyear() return year; For this question, you are asked to add a method called comparetoanother to this class that takes a Birthday object as input and returns an integer number as output. The purpose of this method is to compare itself to the input birthday object and see which birthday occurs earlier. In particular, the method will return an integer output as follows: Return 0 if this birthday and the input object occur on the same date Return 1 if this birthdate occurs earlier than the input object birthdate Return -1 if this birthdate occurs later than the input object birthdate Grading scheme: [2 pts] Method header [1 pt] Logic works when both birthdays are the same [1 pt] Logic works when input birthday is earlier [1 pt] Logic works when input birthday is later [1 pt] Correct return value [1 pt] Use of accessor methods as needed [1 pt] Overall syntax

Student Name: 8 This page is intentionally left blank.

Student Name: 9 Question 4. (8 points) You are given the following Player class: public class Player private String name; private int highscore; public Player( String alias, int score ) name = alias; highscore = score; public String getname() return name; public int gethighscore() return highscore; For this question, define a TestPlayer class that has a main method. In the main method, create an array of Player objects called scoreboard and determine which Player objects have above average highscores as follows: In your array, create 4 Player objects such that: The 1st player is called tommy and has a highscore of 342 The 2nd player is called jonny and has a highscore of 149 The 3rd player is called cindy and has a highscore of 241 The 4th player is called sarah and has a highscore of 329 Compute the average highscore of the Player objects in your array and display it Go through each Player objects in your array and display the names of the ones whose highscore are above the computed average Sample output: The average high score is 265.25 Players with above average high score are: tommy sarah Grading scheme: [1 pt] Class definition and structure [1 pt] Declare and create the scoreboard array [2 pts] Creating 4 Player objects into the array [1 pt] Computing the average highscore [1 pt] Finding the players with above average highscore [1 pt] Use of accessor methods [1 pt] Displaying output as required

Student Name: 10 This page is intentionally left blank.

Student Name: 11 Part 3 Long Answer [16 points] In this question, write a Java program that records app reviews. To simplify the problem, the Review class has been provided to you as follows: public class Review private String comment; private int rating; public Review( String c, int r ) comment = c; rating = r; public String tostring() String str = ""; str += "(" + rating + "/5): "; str += comment + "\n"; return str; You will need to use the above Review class in completing this question. Create an App class that has a name, a maximum of 100 reviews, and an array of reviews. You may have additional attributes as needed to make your program work. Ensure your App class has the following methods: A constructor method that initializes the name of the app. A method that enables the submission of a review to be added to the app. submitreview, and must take a Review object as input. This method should be called The tostring method that concatenates the name of the app, along with the reviews available for the app. Lastly, create a TestApp class with a main method. In the main method, include the following: Create 3 reviews based on the class definition above Create an app (give it a meaningful name) Submit the reviews you created to this app Display the app info Sample output: App: The New Solitaire (5/5): awesome (4/5): pretty good (1/5): awesome: sucks

Student Name: 12 Grading scheme: [1 pt] Overall class definition and structure [2 pts] App class: attribute definitions [2 pts] App class: constructor method [2 pts] App class: submitreview method [2 pts] App class: tostring method [2 pts] App class: functional and works [1 pt] TestApp class: define Review objects [2 pts] TestApp class: create app and call its submitreview [2 pts] TestApp class: call app s tostring and display output

Student Name: 13 This page is intentionally left blank.

Student Name: 14 Common Methods and Definitions From the Math class: double random() Returns a double value greater than or equal to 0.0 and less than 1.0 int round( float a ) Returns the closest int value to a, with ties rounding up double pow( double a, double b ) Returns the value of a raised to the power of b double exp( double a ) Returns Euler s number e raised to the power of a double sqrt( double a ) Returns the correctly rounded positive square root of a From the Character class: boolean isdigit( char ch ) Returns true if ch is a digit, and false otherwise boolean isletter( char ch ) Returns true if ch is a letter, and false otherwise boolean isletterordigit( char ch ) Returns true if ch is a letter or a digit, and false otherwise boolean islowercase( char ch ) Returns true if ch is a lowercase letter, and false otherwise boolean isuppercase( char ch ) Returns true if ch is an uppercase letter, and false otherwise From the String class: char charat( int index ) Returns the character at position index of the string int indexof( int ch ) Returns the index position within the string of the first occurrence of ch int lastindexof( int ch ) Returns the index position within the string of the last occurrence of ch int length() Returns the number of characters in the string boolean startswith( String prefix ) Returns true if the string starts with prefix, and false otherwise boolean endswith( String suffix ) Returns true if the string ends with suffix, and false otherwise boolean contains( String str ) Returns true if the string contains str, and false otherwise int compareto( String str ) Returns an integer result for comparing two strings lexicographically int comparetoignorecase( String str ) Returns an integer result for comparing two strings lexicographically while ignoring case differences boolean equals( Object stringobject ) Returns true if the string is the same as stringobject, and false otherwise boolean equalsignorecase( String str ) Returns true if the string is the same as str, and false otherwise String substring( int beginindex ) Returns a new string that is a substring starting at position beginindex of this string String substring( int beginindex, endindex ) Returns a new string that is a substring starting at position beginindex and up to endindex of this string

Student Name: 15 String tolowercase() Converts all the characters in this string to lower case String touppercase() Converts all the characters in this string to upper case From the Scanner class: Scanner Scanner( InputStream source ) Creates a new Scanner object from the specified source int nextint() Scans the next token of the input as an int double nextdouble() Scans the next token of the input as a double String next() Returns the next complete token as a String String nextline() Returns everything in the current line as a String From the Random class: Random Random() Creates a new Random object int nextint() Returns a randomly generated integer int nextint( int n ) Returns a randomly generated integer between 0 and n-1 inclusive double nextdouble() Returns a randomly generated decimal number between 0 and 1 inclusive