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

Size: px
Start display at page:

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

Transcription

1 CSC 1051 Algorithms and Data Structures I Final Examination May 2, 2016 Name: Question Value Score TOTAL 100 Please answer questions in the spaces provided. Please be legible. If you make a mistake or need more space, use backs of pages - clearly indicate where the answer can be found. Good luck and best wishes for a great summer!

2 1. ( / 10) What gets printed? Please show output as it will appear, or indicate NO OUTPUT. If there is an infinite loop, be sure to show some lines of the output followed by INFINITE LOOP. int a = 4; do a--; System.out.println(a); while (a < 4) Output: INFINITE LOOP String[] notes = "do", "re", "mi"; for (String word : notes) System.out.println ("Tra la la " + word); Output: tra la la do tra la la re tra la la mi int size = 10; do System.out.print(size + " => "); int category = size / 5; switch(category) case 0: System.out.println ("S"); break; case 1: System.out.println ("M"); break; default: System.out.println ("L"); size = size - 2; while (size > 2); Output: 10 => L 8 => M 6 => M 4 => S int a = 4; for (int a = 4; a < 4; a--) System.out.println(a); Output: NO OUTPUT

3 2. ( / 10) Suppose you are drawing in a Graphics context at position (x,y) an emoji that is controlled by a boolean variable happy. When happy is true, it should look like it is smiling; when false, it should look like it is frowning. Assume that the face and eyes are already drawn, so you just need to draw the mouth as an arc facing up or down. Here is the code using if/else: if (happy) page.drawarc (x+15, y+30, 20, 10, 180, 180); else page.drawarc (x+15, y+30, 20, 10, 0, 180); * Rewrite the above code using the conditional operator. (i.e., in a single line of code, without using if/else). page.drawarc (x+15, y+30, 20, 10, happy? 180: 0, 180); b) Suppose you would like to generate a random inspirational message. You start by creating an array containing a list of possible messages: String[] mycliche = "Life is beautiful", "Keep calm and carry on", "Live, laugh, love", "Today is the first day of the rest of your life" ; // add your own, if you feel like it! Write a code fragment that uses a randomly generated index into this array to print one of the above messages. Your choice whether to use an object from the Random class or the random() method from the Math class. Be sure your code works fine if you add more messages to the array (for example, if you add a fifth message in the array above). Random rand = new Random(); int choice= rand.nextint(mycliche.length); System.out.println(myCliche[choice]);

4 3. ( / 10) Consider the following program: //************************************************************** // SomethingToDoWithFiles.java Author: MAP //************************************************************** import java.util.scanner; import java.io.*; public class SomethingToDoWithFiles public static void main (String[] args) throws IOException String line1, line2; Scanner filescan1, filescan2; File myfile1 = new File("sample1.txt"); filescan1 = new Scanner (myfile1); File myfile2 = new File("sample2.txt"); filescan2 = new Scanner (myfile2); outfile = new PrintWriter("data-out.txt"); while (filescan1.hasnext() && filescan2.hasnext()) line1 = filescan1.nextline(); line2 = filescan2.nextline(); outfile.print(line1.charat(0) + "**" + line2.charat(0)); outfile.println(filescan2.hasnext()? "Down!": "Up!"); outfile.close(); System.out.println(fileScan2.hasNext()? "Up!": "Down!"); a) Suppose the file sample1.txt and sample2.txt are as shown above. Show the OUTPUT and the contents of the file data-out.txt after execution of the program. OUTPUT: data-out.txt Down! sample1.txt down came the rain and washed the spider out. d**ot**aw**aup! sample2.txt out came the sun and washed out all the rain b) Give an example of a situation that could cause IOExceptions in the above code. file not found for the Scanner cannot do input (OR bad file name for the PrintWriter cannot create the file)

5 4. ( / 10) a) Write a Java method max() with three parameters of type double that returns a value of type double that is the largest of the three given values. For example, max(35.2, 45.7, 22.8) should return Note that the method should not print anything. public double max(double a, double b, double c) double max = a; if (b > max) max = b; if (c > max) max = c; return max; a) Write a Java method countvowels() with one parameter, a String that returns the number of vowels (an integer) in the given String. For example, countvowels("the end is near") should return 5. Note that the method should not print anything. public int countvowels(string word) int count = 0; for (char c: word) if (c == a c == e c == i c == o c == u ) count ++; return count;

6 5. ( / 10) a) Trace through the following code and show what gets printed. int[] a = 100, 200, 300, 400; int[] b = 1000, 2000, 3000, 4000; int[] c = b; for (int i=0; i<a.length; i++) b[i] = a[i]; a[1] = 4; b[2] = 5; c[3] = 6; for (int x: a) System.out.print(x + " "); System.out.println(); for (int x: b) System.out.print(x + " "); System.out.println(); for (int x: c) System.out.print(x + " "); System.out.println(); Output: b) Suppose an array of int named list has been declared, instantiated, and initialized. Write a code fragment to print the contents of array list backwards. for (int i = list.length - 1; i >=0; i--) System.out.println(list[i]);

7 6. ( / 10) Consider the following program: // ProductCodes.java Author: Lewis/Loftus import java.util.scanner; public class ProductCodes // // Counts the number of product codes that are entered with a // zone of R and and district greater than // public static void main(string[] args) String code; char zone; int district, valid = 0, banned = 0; Scanner scan = new Scanner(System.in); System.out.print("Enter product code (XXX to quit): "); code = scan.nextline(); while (!code.equals("xxx")) try à zone = code.charat(9); catch à district = Integer.parseInt(code.substring(3, 7)); valid++; if (zone == 'R' && district > 2000) banned++; System.out.print("Enter product code (XXX to quit): "); code = scan.nextline(); System.out.println("# of valid codes entered: " + valid); System.out.println("# of banned codes entered: " + banned); a) Give a examples of product code inputs that would cause the program to _ TTTTTTTTTTTT throw a NumberFormatException TTT throw a StringIndexOutOfBoundsException AAA3446RRR NOT throw any exception but be counted as banned XXX cause the program to terminate, without error. b) Suppose you would like to catch and handle NumberFormatException by skipping this input using the following catch clause: catch (NumberFormatException exception) System.out.println("Bad code: " + code); Annotate ProductCodes.java to show exactly how the try/catch block should be placed.

8 7. ( / 10) Consider the following program: public class TwoDArray public static void main (String[] args) int[][] table = new int[3][9]; for (int row=0; row < table.length; row++) for (int col=0; col < table[row].length; col++) table[row][col] = row * 10 + col; System.out.print ("# \t"); for (int col=0; col < table[0].length; col++) System.out.print (col + "\t"); System.out.println(); System.out.print (" "); for (int col=0; col < table[0].length; col++) System.out.print ("-----"); System.out.println(); for (int row=0; row < table.length; row++) System.out.print (row + " \t"); for (int col=0; col < table[row].length; col++) System.out.print (table[row][col] + "\t"); System.out.println(); We would like to modify the program to: (1) change array size; and (2) label rows/columns so that the output would now look EXACTLY like this: # a) In the above output, circle entries corresponding to table[0][2] and table[2][3]. b) Annotate the code so that it works as described in (1) and (2) above

9 8. ( / 10) Complete the java code below so that it prints a table for the Investment problem: You put an initial amount into a bank account that earns 5% interest per year. Show the yearly returns on your investment until it doubles. For example, if the user inputs as the initial amount, a table such as the one shown here would be printed. import java.text.numberformat; import java.util.scanner; public class Investment public static void main (String[] args) Scanner scan = new Scanner(System.in); year interest balance 0 $10, $ $10, $ $11, $ $ $ $12, (keep going until balance >= $20000) System.out.print("Enter initial balance:"); double initial = scan.nextdouble(); int year = 0; double rate = 0.05; double balance = initial; NumberFormat money = NumberFormat.getCurrencyInstance(); System.out.println("year\tinterest\tbalance"); System.out.println (year + "\t" + " " + "\t" + money.format(balance)); double interest; double target = 2 * initial; while (balance < target) year++; interest = balance * rate; balance += interest; System.out.println (year + "\t" + money.format(interest) + "\t" + money.format(balance));

10 9. ( / 10) Write a complete Java program consisting of a class Shoe and a client class named Wishlist. The Shoe class should contain instance variables representing the following shoe data: manufacturer- a String, eg "Sofft" or "Steve Madden" name - a String, eg "Madeline" or "Realove" price - a f (use double), eg: SKU number - an integer, eg: (SKU is displayed right above price in picture below) The Shoe class should have a constructor, accessor and mutator methods for the price, and a tostring() method. Do not write code for any additional methods (no need to write accessors and mutators for all the other instance variables). The client class Wishlist should implement the following algorithm: o Instantiate three variables of the Shoe class named s1, s2, s3 (make up values for the data) o Print the info of s1, s2, s3 o Change the price of s1 to $29.99 o Print the total price of s1, s2, s3 Write the complete code for the two classes in the next two pages. Notes: It is NOT necessary to include comments with your code, but be sure to use good indentation. Formatting a number as a currency: if you need a reminder, see previous question Write the complete code for the two classes in the next two pages. èè

11 import java.text.numberformat; public class Shoe private String manufacturer; private String name; private double price; private int sku; // Constructor: with all the data public Shoe(String m, String n, double p, int s) manufacturer = m; name = n; price = p; sku = s; public String tostring() NumberFormat fmt = NumberFormat.getCurrencyInstance(); return name + " by " + manufacturer + ", SKU: " + sku + " " + fmt.format(price); public void setprice(double p) price = p; public double getprice() return price;

12 import java.text.numberformat; public class Wishlist public static void main(string[] args) Shoe s1, s2, s3, s4; s1 = new Shoe("Nike", "Air", 80, 833); s2 = new Shoe("Asics", "Gel Nimbus", 100, 329); s3 = new Shoe("Madden", "Realove", 30, 440); System.out.println(s1); System.out.println(s2); System.out.println(s3); s1.setprice(29.99); NumberFormat fmt = NumberFormat.getCurrencyInstance(); System.out.println("Total price of shoes"); double total = s1.getprice() + s2.getprice() + s3.getprice(); System.out.println(fmt.format(total));

13 10. ( / 10) Draw the UML class diagram for the previous problem (Shoe + Wishlist). Wishlist main(args: String[]): void Shoe manufacturer: String name: String price: double sku: int tostring: String getprice(): double setprice(x: double): void

14

15

16

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

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name: CSC 1051 Algorithms and Data Structures I Final Examination May 2, 2016 Name: Question Value Score 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 10 TOTAL 100 Please answer questions in the spaces provided.

More information

Happy Cinco de Mayo!!!!

Happy Cinco de Mayo!!!! CSC 1051 Algorithms and Data Structures I Happy Cinco de Mayo!!!! Final Examination May 5, 2018 Name: Question Value Score 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 20 TOTAL 100 Please answer questions

More information

Happy Cinco de Mayo!!!!

Happy Cinco de Mayo!!!! CSC 1051 Algorithms and Data Structures I Happy Cinco de Mayo!!!! Final Examination May 5, 2018 Name: KEY Question Value Score 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 20 TOTAL 100 Please answer questions

More information

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

CSC 1051 Algorithms and Data Structures I. Final Examination December 18, Name: CSC 1051 Algorithms and Data Structures I Final Examination December 18, 2015 Name: Question Value Score 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 10 TOTAL 100 Please answer questions in the spaces

More information

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

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name CSC 1051 Algorithms and Data Structures I Final Examination May 12, 2017 Name Question Value Score 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 10 TOTAL 100 Please answer questions in the spaces provided.

More information

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 12, Name: KEY. Question Value Score CSC 1051 Algorithms and Data Structures I Final Examination May 12, 2017 Name: KEY Question Value Score 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 10 TOTAL 100 Please answer questions in the spaces

More information

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

CSC 1051 Algorithms and Data Structures I. Final Examination December 20, Name: KEY. Question Value Score CSC 1051 Algorithms and Data Structures I Final Examination December 20, 2016 Name: KEY Question Value Score 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 10 TOTAL 100 Please answer questions in the

More information

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

CSC 1051 Algorithms and Data Structures I. Final Examination December 17, Name: 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.

More information

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

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name: CSC 1051 Algorithms and Data Structures I Final Examination May 2, 2015 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.

More information

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

CSC Algorithms and Data Structures I. Midterm Examination February 25, Name: CSC 1051-001 Algorithms and Data Structures I Midterm Examination February 25, 2016 Name: Question Value Score 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 10 TOTAL 100 Please answer questions in the

More information

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A CSC 1051 Algorithms and Data Structures I Midterm Examination February 25, 2016 Name: KEY A Question Value Score 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 10 TOTAL 100 Please answer questions in

More information

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 11, Name: KEY CSC 1051 Algorithms and Data Structures I Midterm Examination October 11, 2018 Name: KEY Question Value Score 1 20 2 20 3 20 4 20 5 20 TOTAL 100 Please answer questions in the spaces provided. If you make

More information

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1 File I/O and Exceptions CSC 1051 Data Structures and Algorithms I I/O Streams Programs read information from input streams and write information to output streams Dr. Mary-Angela Papalaskari Department

More information

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1 File I/O and Exceptions CSC 1051 Data Structures and Algorithms I I/O Streams Programs read information from input streams and write information to output streams Dr. Mary-Angela Papalaskari Department

More information

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

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Last Class CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in this

More information

File I/O and Exceptions

File I/O and Exceptions File I/O and Exceptions CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some

More information

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1 File I/O and Exceptions CSC 1051 Data Structures and Algorithms I I/O Streams Programs read information from input streams and write information to output streams Dr. Mary-Angela Papalaskari Department

More information

COMP 202 File Access. CONTENTS: I/O streams Reading and writing text files. COMP File Access 1

COMP 202 File Access. CONTENTS: I/O streams Reading and writing text files. COMP File Access 1 COMP 202 File Access CONTENTS: I/O streams Reading and writing text files COMP 202 - File Access 1 I/O Streams A stream is a sequence of bytes that flow from a source to a destination In a program, we

More information

I/O Streams. COMP 202 File Access. Standard I/O. I/O Stream Categories

I/O Streams. COMP 202 File Access. Standard I/O. I/O Stream Categories CONTENTS: I/O streams COMP 202 File Access Reading and writing text files I/O Streams A stream is a sequence of bytes that flow from a source to a destination In a program, we read information from an

More information

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

Java Bootcamp - Villanova University. CSC 2014 Java Bootcamp. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Arrays CSC 2014 Java Bootcamp Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Some slides in this presentation are adapted from the slides accompanying Java Software Solutions

More information

Arrays and File Input

Arrays and File Input Arrays and File Input CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides

More information

Arrays and File Input

Arrays and File Input Arrays and File Input CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides

More information

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

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 CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides

More information

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 26, Name: Key CSC 1051 Algorithms and Data Structures I Midterm Examination February 26, 2015 Name: Key Question Value 1 10 Score 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 10 TOTAL 100 Please answer questions in the

More information

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 9, Name: KEY CSC 1051 Algorithms and Data Structures I Midterm Examination October 9, 2014 Name: KEY Question Value Score 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 10 TOTAL 100 Please answer questions in the

More information

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name: CSC 1051 Algorithms and Data Structures I Midterm Examination October 7, 2013 Name: Question Value Score 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 10 TOTAL 100 Please answer questions in the spaces

More information

Exceptions Chapter 10. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Exceptions Chapter 10. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Exceptions Chapter 10 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Exceptions: The purpose of exceptions Exception messages The call stack trace The try-catch statement Exception

More information

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

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Arrays, Part 2 CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in

More information

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

Arrays - Review. Initializer Lists. The for-each Loop. Arrays, Part 2. Dr. Papalaskari 1. CSC 1051 Data Structures and Algorithms I Arrays, Part 2 Arrays - Review Declaration: double[] scores element type Instantiation: = new double[10]; CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences

More information

H212 Introduction to Software Systems Honors

H212 Introduction to Software Systems Honors Introduction to Software Systems Honors Lecture #07: September 21, 2015 1/30 We explained last time that an array is an ordered list of values. Each value is stored at a specific, numbered position in

More information

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O 1 Sending Output to a (Text) File import java.util.scanner; import java.io.*; public class TextFileOutputDemo1 public static void

More information

Selection Statements and operators

Selection Statements and operators Selection Statements and operators CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

More information

CSC 1051 Data Structures and Algorithms I

CSC 1051 Data Structures and Algorithms I Repetition CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in this

More information

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1 CSC 1051 Algorithms and Data Structures I Midterm Examination February 24, 2014 Name: KEY 1 Question Value Score 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 10 TOTAL 100 Please answer questions in

More information

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website: Repetition CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in this

More information

Recitation: Loop Jul 7, 2008

Recitation: Loop Jul 7, 2008 Nested Loop Recitation: Loop Jul 7, 2008 1. What is the output of the following program? Use pen and paper only. The output is: ****** ***** **** *** ** * 2. Test this program in your computer 3. Use "for

More information

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

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Arrays, Part 2 CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in

More information

Arrays - Review. Two-Dimensional Arrays. Arrays, Part 2. Dr. Papalaskari 1. CSC 1051 Data Structures and Algorithms I

Arrays - Review. Two-Dimensional Arrays. Arrays, Part 2. Dr. Papalaskari 1. CSC 1051 Data Structures and Algorithms I Arrays, Part 2 CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in

More information

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

COE 212 Engineering Programming. Welcome to Exam II Monday May 13, 2013 1 COE 212 Engineering Programming Welcome to Exam II Monday May 13, 2013 Instructors: Dr. Randa Zakhour Dr. Maurice Khabbaz Dr. George Sakr Dr. Wissam F. Fawaz Name: Solution Key Student ID: Instructions:

More information

7.3 Arrays of Strings (Objects) 7.3 Arrays of Strings (Objects) 7.3 Tunes.java. 7.3 Arrays of Objects 9/11/13. ! A UML diagram for the Tunes program

7.3 Arrays of Strings (Objects) 7.3 Arrays of Strings (Objects) 7.3 Tunes.java. 7.3 Arrays of Objects 9/11/13. ! A UML diagram for the Tunes program 7.3 Arrays of Strings (Objects)! The elements of an array can be object references! The following declara6on reserves space to store 5 references to String objects String[] words = new String[5];! Ini6ally

More information

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

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Arrays, Part 2 CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in

More information

System.out.print("Enter sales for salesperson " + i + ": "); sales[i] = scan.nextint();

System.out.print(Enter sales for salesperson  + i + : ); sales[i] = scan.nextint(); Tracking Sales File Sales.java contains a Java program that prompts for and reads in the sales for each of 5 salespeople in a company. It then prints out the id and amount of sales for each salesperson

More information

Lab 13. Name: Checked:

Lab 13. Name: Checked: Lab 13 Name: Checked: Objectives: Learn how to input data from text files using Scanner. We will also use Scanner in other novel ways, including reading data directly from a webpage! Preparation: Processing

More information

CSC 1051 Data Structures and Algorithms I

CSC 1051 Data Structures and Algorithms I Repetition CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in this

More information

CSC 1051 Data Structures and Algorithms I

CSC 1051 Data Structures and Algorithms I Repetition CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in this

More information

CSC 1051 Arrays - Review questions

CSC 1051 Arrays - Review questions CSC 5 Arrays - Review questions ) Given the following declarations: int a = 2; int b = 3; int c = 5; double x = 2.5; double y =.; double z = 4.32; double[] list = 2., 3.8,.4}; Show what value is assigned

More information

Chapter 3. Selections

Chapter 3. Selections Chapter 3 Selections 1 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator 6. The Switch Statement 7. Useful Hints 2 1. Flow of

More information

Where do objects come from? Good question!

Where do objects come from? Good question! Designing Classes CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Where do objects

More information

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

COE 211/COE 212 Computer/Engineering Programming. Welcome to Exam II Thursday December 20, 2012 1 COE 211/COE 212 Computer/Engineering Programming Welcome to Exam II Thursday December 20, 2012 Instructor: Dr. George Sakr Dr. Wissam F. Fawaz Dr. Maurice Khabbaz Name: Student ID: Instructions: 1. This

More information

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

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Designing Classes CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Where do objects

More information

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

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Designing Classes CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Where do objects

More information

Selection Statements and operators

Selection Statements and operators Selection Statements and operators CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

More information

Iterators and File Input

Iterators and File Input Iterators and File Input CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some

More information

Arrays, Part 3: Multidimensional arrays

Arrays, Part 3: Multidimensional arrays Arrays, Part 3: Multidimensional arrays CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

More information

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

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007 Name: Please fill in your Student Number and Name. Student Number : Student Number: University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ 2007 Final Examination Question Max

More information

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 1, Name: KEY A CSC 1051 Algorithms and Data Structures I Midterm Examination March 1, 2018 Name: KEY A Question Value Score 1 20 2 20 3 20 4 20 5 20 TOTAL 100 Please answer questions in the spaces provided. If you make

More information

JAVA Ch. 4. Variables and Constants Lawrenceville Press

JAVA Ch. 4. Variables and Constants Lawrenceville Press JAVA Ch. 4 Variables and Constants Slide 1 Slide 2 Warm up/introduction int A = 13; int B = 23; int C; C = A+B; System.out.print( The answer is +C); Slide 3 Declaring and using variables Slide 4 Declaring

More information

Chapter 7. Arrays are objects that help us organize large amounts of information

Chapter 7. Arrays are objects that help us organize large amounts of information Arrays 5 TH EDITION Chapter 7 Lewis & Loftus java Software Solutions Foundations of Program Design 2007 Pearson Addison-Wesley. All rights reserved Arrays Arrays are objects that help us organize large

More information

CS1083 Week 2: Arrays, ArrayList

CS1083 Week 2: Arrays, ArrayList CS1083 Week 2: Arrays, ArrayList mostly review David Bremner 2018-01-08 Arrays (1D) Declaring and using 2D Arrays 2D Array Example ArrayList and Generics Multiple references to an array d o u b l e prices

More information

Java Coding 3. Over & over again!

Java Coding 3. Over & over again! Java Coding 3 Over & over again! Repetition Java repetition statements while (condition) statement; do statement; while (condition); where for ( init; condition; update) statement; statement is any Java

More information

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

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1 Designing Classes Where do objects come from? CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

More information

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

Module Contact: Dr Gavin Cawley, CMP Copyright of the University of East Anglia Version 1 UNIVERSITY OF EAST ANGLIA School of Computing Sciences Main Series UG Examination 2017-18 PROGRAMMING 1 CMP-4008Y Time allowed: 2 hours Answer FOUR questions. All questions carry equal weight. Notes are

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 13 Two Dimensional Arrays Outline Problem: How do store and manipulate data in tabular format Two-dimensional arrays easy access with 2 indices This

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 13 Two Dimensional Arrays Outline Problem: How do store and manipulate data in tabular format Two-dimensional arrays easy access with 2 indices This

More information

Java I/O and Control Structures

Java I/O and Control Structures Java I/O and Control Structures CSC 2014 Java Bootcamp Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Some slides in this presentation are adapted from the slides accompanying

More information

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 2, Name: CSC 1051 Algorithms and Data Structures I Midterm Examination March 2, 2017 Name: Question Value Score 1 10 2 10 3 20 4 20 5 20 6 20 TOTAL 100 Please answer questions in the spaces provided. If you make

More information

COE 212 Engineering Programming. Welcome to Exam II Tuesday November 28, 2018

COE 212 Engineering Programming. Welcome to Exam II Tuesday November 28, 2018 1 COE 212 Engineering Programming Welcome to Exam II Tuesday November 28, 2018 Instructors: Dr. Dima El-khalil Dr. Jawad Fahs Dr. Joe Tekli Dr. Wissam F. Fawaz Name: Student ID: Instructions: 1. This exam

More information

Java I/O and Control Structures Algorithms in everyday life

Java I/O and Control Structures Algorithms in everyday life Introduction Java I/O and Control Structures Algorithms in everyday life CSC 2014 Java Bootcamp Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Source: http://xkcd.com/627/

More information

Algorithms and Conditionals

Algorithms and Conditionals Algorithms and Conditionals CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

More information

COMP 202 File Access. CONTENTS: I/O streams Reading and writing text files. COMP 202 File Access 1

COMP 202 File Access. CONTENTS: I/O streams Reading and writing text files. COMP 202 File Access 1 COMP 202 File Access CONTENTS: I/O streams Reading and writing text files COMP 202 File Access 1 I/O Streams A stream is a sequence of bytes that flow from a source to a destination In a program, we read

More information

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 6, Name: CSC 1051 Algorithms and Data Structures I Midterm Examination October 6, 2016 Name: Question Value Score 1 20 2 20 3 20 4 20 5 20 TOTAL 100 Please answer questions in the spaces provided. If you make a

More information

Java Flow of Control

Java Flow of Control Java Flow of Control SEEM 3460 1 Flow of Control Unless specified otherwise, the order of statement execution through a method is linear: one statement after another in sequence Some programming statements

More information

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

CSCI 2010 Principles of Computer Science. Basic Java Programming. 08/09/2013 CSCI Basic Java 1 CSCI 2010 Principles of Computer Science Basic Java Programming 1 Today s Topics Using Classes and Objects object creation and object references the String class and its methods the Java standard class

More information

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

COE 211 Computer Programming. Welcome to Exam I Tuesday March 13, 2018 1 COE 211 Computer Programming Welcome to Exam I Tuesday March 13, 2018 Instructors: Dr. Dima El-khalil Dr. Wissam F. Fawaz Name: Student ID: Instructions: 1. This exam is Closed Book. Please do not forget

More information

CS Computers & Programming I Review_01 Dr. H. Assadipour

CS Computers & Programming I Review_01 Dr. H. Assadipour CS 101 - Computers & Programming I Review_01 Dr. H. Assadipour 1. What is the output of this program? public class Q_01 public static void main(string [] args) int x=8; int y=5; double z=12; System.out.println(y/x);

More information

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

COE 212 Engineering Programming. Welcome to Exam II Thursday April 21, Instructors: Dr. Salim Haddad Dr. Joe Tekli Dr. Wissam F. 1 COE 212 Engineering Programming Welcome to Exam II Thursday April 21, 2016 Instructors: Dr. Salim Haddad Dr. Joe Tekli Dr. Wissam F. Fawaz Name: Student ID: Instructions: 1. This exam is Closed Book.

More information

Name: Checked: Preparation: Write the output of DeckOfCards.java to a text file Submit through Blackboard by 8:00am the morning of Lab.

Name: Checked: Preparation: Write the output of DeckOfCards.java to a text file Submit through Blackboard by 8:00am the morning of Lab. Lab 14 Name: Checked: Objectives: Practice handling exceptions and writing text files. Preparation: Write the output of DeckOfCards.java to a text file Submit through Blackboard by 8:00am the morning of

More information

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a Spring 2017 Name: TUID: Page Points Score 1 28 2 18 3 12 4 12 5 15 6 15 Total: 100 Instructions The exam is closed book, closed notes. You may not use a calculator, cell phone, etc. i Some API Reminders

More information

STUDENT LESSON A12 Iterations

STUDENT LESSON A12 Iterations STUDENT LESSON A12 Iterations Java Curriculum for AP Computer Science, Student Lesson A12 1 STUDENT LESSON A12 Iterations INTRODUCTION: Solving problems on a computer very often requires a repetition of

More information

Introduction to Arrays

Introduction to Arrays Introduction to Arrays CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some

More information

Selection and Repetition Revisited

Selection and Repetition Revisited Selection and Repetition Revisited CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

More information

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

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007 Name: Please fill in your Student Number and Name. Student Number : Student Number: University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ 2007 Supplementary Examination Question

More information

Conditionals and Loops Chapter 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Conditionals and Loops Chapter 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Conditionals and Loops Chapter 4 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Flow of control Boolean expressions if and switch statements Comparing data while, do, and for

More information

Name: Checked: Preparation: Investment Calculator with input and output to text files Submit through Blackboard by 8:00am the morning of Lab.

Name: Checked: Preparation: Investment Calculator with input and output to text files Submit through Blackboard by 8:00am the morning of Lab. Lab 14 Name: Checked: Objectives: Practice handling exceptions and writing text files. Preparation: Investment Calculator with input and output to text files Submit through Blackboard by 8:00am the morning

More information

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class. Name: Covers Chapters 1-3 50 mins CSCI 1301 Introduction to Programming Armstrong Atlantic State University Instructor: Dr. Y. Daniel Liang I pledge by honor that I will not discuss this exam with anyone

More information

Full file at

Full file at Chapter 2 Console Input and Output Multiple Choice 1) Valid arguments to the System.out object s println method include: (a) Anything with double quotes (b) String variables (c) Variables of type int (d)

More information

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

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1 Designing Classes Where do objects come from? CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

More information

Loops. CSE 114, Computer Science 1 Stony Brook University

Loops. CSE 114, Computer Science 1 Stony Brook University Loops CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Motivation Suppose that you need to print a string (e.g., "Welcome to Java!") a user-defined times N: N?

More information

Tutorial 06. Conditional statement: if then, if else, switch

Tutorial 06. Conditional statement: if then, if else, switch College of Computer and Infmation Sciences CSC111 Computer Programming I Exercise 1: Tutial 06 Conditional statement: if then, if, switch What is the output of each of the following code fragments? (given

More information

This Week s Agenda (Part A) CS121: Computer Programming I. The Games People Play. Data Types & Structures. The Array in Java.

This Week s Agenda (Part A) CS121: Computer Programming I. The Games People Play. Data Types & Structures. The Array in Java. CS121: Computer Programming I A) Collections B) File I/O & Error Handling Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use

More information

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

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003 1.00 Introduction to Computers and Engineering Problem Solving Quiz 1 March 7, 2003 Name: Email Address: TA: Section: You have 90 minutes to complete this exam. For coding questions, you do not need to

More information

Problem Grade Total

Problem Grade Total CS 101, Prof. Loftin: Final Exam, May 11, 2009 Name: All your work should be done on the pages provided. Scratch paper is available, but you should present everything which is to be graded on the pages

More information

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

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) Midterm Examination First Name: Last Name: McGill ID: Section: Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) Midterm Examination Monday, March 9, 2009 Examiners: Mathieu Petitpas [Section 1] 18:30

More information

H212 Introduction to Software Systems Honors

H212 Introduction to Software Systems Honors Introduction to Software Systems Honors Lecture #04: Fall 2015 1/20 Office hours Monday, Wednesday: 10:15 am to 12:00 noon Tuesday, Thursday: 2:00 to 3:45 pm Office: Lindley Hall, Room 401C 2/20 Printing

More information

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

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination First Name: Last Name: McGill ID: Section: Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination Tuesday, November 4, 2008 Examiners: Mathieu Petitpas [Section 1] 18:30

More information

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters Outline Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters Variable Length Parameter Lists split() Method from String Class Integer & Double Wrapper

More information

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below. C212 Early Evaluation Exam Mon Feb 10 2014 Name: Please provide brief (common sense) justifications with your answers below. 1. What is the type (and value) of this expression: 5 * (7 + 4 / 2) 2. What

More information

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

Chapter. We've been using predefined classes. Now we will learn to write our own classes to define objects Writing Classes 4 Chapter 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design 2007 Pearson Addison-Wesley. All rights reserved Writing Classes We've been using predefined

More information

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

COE 212 Engineering Programming. Welcome to Exam I Thursday June 21, Instructor: Dr. Wissam F. Fawaz 1 COE 212 Engineering Programming Welcome to Exam I Thursday June 21, 2018 Instructor: Dr. Wissam F. Fawaz Name: Student ID: Instructions: 1. This exam is Closed Book. Please do not forget to write your

More information

WOSO Source Code (Java)

WOSO Source Code (Java) WOSO 2017 - Source Code (Java) Q 1 - Which of the following is false about String? A. String is immutable. B. String can be created using new operator. C. String is a primary data type. D. None of the

More information