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

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

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

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

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

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

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

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

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

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

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. Final Examination May 2, Name: Question Value Score

CSC 1051 Data Structures and Algorithms I

CSC 1051 Data Structures and Algorithms I

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

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

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

CSC 1051 Data Structures and Algorithms I

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

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

Chapter 2 Exercise Solutions

Using Java Classes Fall 2018 Margaret Reid-Miller

COMP 202 Java in one week

Java Coding 3. Over & over again!

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU. Week 21/02/ /02/2007 Lecture Notes: ASCII

Chapter 3. Selections

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

AP CS Unit 3: Control Structures Notes

Practice Midterm 1. Problem Points Score TOTAL 50

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Happy Cinco de Mayo!!!!

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

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

COMP 202. Java in one week

Practice Midterm 1 Answer Key

Algorithms and Conditionals

Java I/O and Control Structures

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

Introduction to Computer Science Unit 2. Notes

CS111: PROGRAMMING LANGUAGE II

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

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

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

Java I/O and Control Structures Algorithms in everyday life

Methods CSC 121 Fall 2016 Howard Rosenthal

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

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

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

Methods CSC 121 Spring 2017 Howard Rosenthal

Using Classes and Objects

Happy Cinco de Mayo!!!!

Fundamentals of Programming Data Types & Methods

Data Representation and Applets

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

4. Java Project Design, Input Methods

DUBLIN CITY UNIVERSITY

Programming with Java

DUBLIN CITY UNIVERSITY

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

Introduction to Computer Science Unit 2. Notes

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

Oct Decision Structures cont d

Applets and the Graphics class

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

COMP102: Test 1 Model Solutions

Controls Structure for Repetition

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Classes and Objects Part 1

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

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

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

CS110: PROGRAMMING LANGUAGE I

Midterm Examination (MTA)

Term 1 Unit 1 Week 1 Worksheet: Output Solution

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

Object Oriented Programming. Java-Lecture 1

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

CS111: PROGRAMMING LANGUAGE II

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

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

Object-Based Programming. Programming with Objects

Selection Statements and operators

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

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

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Selection Statements and operators

CSCI 1226 Sample Midterm Exam

Computer Science 145 Midterm 1 Fall 2016

Lecture Static Methods and Variables. Static Methods

Java Classes: Random, Character A C S L E C T U R E 6

Data Representation and Applets

H212 Introduction to Software Systems Honors

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

Using Classes and Objects

Classes. Classes as Code Libraries. Classes as Data Structures

Data Representation and Applets

Java By Abstraction - Test-B (Chapters 1-6)

Computer Science 145

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Transcription:

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 the spaces provided. If you make a mistake or for some other reason need more space, please use the back of pages and clearly indicate where the answer can be found. Good luck!

1. [ /10] What output is produced by the following program? public class TraceThis public static void main (String[] args) int x = 10; int a = 20; int b = 30; x = a; a = b; b = 40; System.out.println("What gets printed? "); System.out.println("x = " + x + " a = " + a + " b = " + b); System.out.print ("Let\'s do this \none\n\"last time\" "); System.out.println (".\nplease count the \"\\\\\"s carefully!"); System.out.println ("Version 1 numbers: " + 4 + 6); System.out.println ("Version 2 numbers: " + "4 + 6"); System.out.println ("Version 3 numbers: " + (4 + 6)); Output: What gets printed? x = 20 a = 30 b = 40 Let's do this one "last time". Please count the "\\"s carefully! Version 1 numbers: 46 Version 2 numbers: 4 + 6 Version 3 numbers: 10

2. ( / 10) a) Which of the following code fragments correctly executes three assignments if the condition is true? Mark the correct one and explain your answer, briefly. i) if (x < 0) a = b * 2; y = x; z = a y; ii) if (x < 0) // This is the only one that works // because it uses the braces correctly a = b * 2; y = x; z = a y; iii) if (x < 0) a = b * 2; y = x; z = a y; iv) if (x < 0) a = b * 2; y = x; z = a y ; b) Which of the code fragments below will add 1 to x if x is positive and subtract 1 from x if x is negative, but leave x alone if x is 0? Explain your answer, briefly. i) if (x > 0) x++; else x--; ii) if (x == 0) x = 0; else x++; x--; iii) x++; x--; iv) if (x > 0) x++; else if (x < 0) // This is the only one that works because it x--; //leaves the x unchanged when it is zero.

3. ( / 10) What gets printed? Please show output as it will appear, or indicate NO OUTPUT, or show some of the output followed by INFINITE LOOP. int a = 0; while (a > 0) System.out.println(a); a = a + 3; Output: NO OUTPUT int a = 5; while (a >= 5) System.out.println(a); a++; Output: 5 6 7 INFINITE LOOP int a = 4; while (a > 0) System.out.println(a * 2); a--; Output: 8 6 4 2 String word = "ILLUSION"; int x = 0; while (x < word.length()) System.out.print(word.charAt(x)); System.out.print(word.charAt(x)); x++; Output: IILLLLUUSSIIOONN

4. ( / 10) Using the coordinate system below representing the graphics area of an applet, draw the graphics displayed by the applet, identifying positions MID and TOP (in terms of their use in the applet), and the shapes RECTANGLE, OVAL, LINE. import javax.swing.japplet; public class Quiz4 extends JApplet //------------------------------------------------------ // Draws something... //------------------------------------------------------ public void paint (Graphics page) final int MID = 40; // MID final int TOP = 20; // TOP page.drawoval (-20, -20, 40,40 ); page.drawrect (MID, TOP, 20, 40); page.drawline (0, 40, 20, 0); // OVAL // RECTANGLE // LINE 0 20 40 60 80 100 120 140 0 80 20 40 60 80 100

5. ( / 10) Suppose you look up a class in the Java API and find the following info about the (alas, made-up!) SpringBreak class: java.exam Class SpringBreak java.lang.object java.exam.springbreak Constructor Summary SpringBreak() Creates a new SpringBreak object. Method Summary void destination(string x) static boolean shouldistayorshouldigo(double x) a) Write some code to declare variables for two objects of this class, named thing1 and thing2. SpringBreak thing1 = new SpringBreak (); SpringBreak thing2 = new SpringBreak (); b) Circle all of the following that are valid Java statements: SpringBreak.destination = Puerto Vallarta ; thing1.destination( Cancun ); thing2.destination(thing1); SpringBreak.destination( Miami ); System.out.print(thing2.destination( Cancun )); thing2.shouldistayorshouldigo(21.6) = true; System.out.print(SpringBreak.shouldIStayOrShouldIGo(32.4));

6. [ /10] a) Given the following declarations: int iresult, num1 = 7, num2 = 3; double fresult, val1 = 9.0; boolean status, part1 = false; What result is stored by each of the following assignment statements? Source code Result stored fresult = (num1 + 2)/ 2; 4.0 iresult = num1 % num2; 1 fresult = val1 / 2; 4.5 fresult = (double) num1 / 2; 3.5 status = part1 && (num1 > num2); false status = part1 (num1 > num2); true

7. [ /10] Short answer questions. a) Given a Random object named gen, what range of values are produced by the following expressions? gen.nextint(4) 0 to 3 gen.nextint(20) + 100 100 to 119 gen.nextint(4) 15-15 to -12 b) Write some code that uses the object gen, above, to generate pseudorandom numbers in the following specified ranges (including the endpoints): 0 to 9 gen.nextint(10) -2 to 5 gen.nextint(8) -2 c) Suppose the String variable message is already initialized. Write a Java code fragment that prints out message, one char per line. int count = 0; while (count < message.length) System.out.print(message.charAt(count)); count ++; d) Write some code that uses the Math class to compute and print out the value of sin(π/2). System.out.println(Math.sin( Math.PI /2) );

8. ( / 10) Complete the java code below so that it prints a table for the Investment problem: You put an initial amount (say, $10,000) into a bank account that earns 5% interest per year. Show the yearly returns on your investment until it doubles. year interest balance 0 $10,000.00 1 $500.00 $10,500.00 2 $525.00 $11,025.00 3 $551.25 $11576.25 import java.text.numberformat; import java.util.scanner; public class Investment public static void main (String[] args) Scanner scan = new Scanner(System.in); 4 $578.81 $12,155.06 (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));

9. [ / 10] Write a complete Java program that asks the user to input three integers and then prints the maximum value among the numbers entered. (For example, if the three numbers entered are 7, 43, 7, the program should print 43 as the maximum. Be sure to write a complete Java program, including class definition, variable and constant declarations, as appropriate, and proper indentation, to make it readable. Include one simple comment describing what the program does (no need need to write lots of comments). // Computes the max of three inputs import java.util.scanner; public class Max public static void main(string[] args) Scanner scan = new Scanner(System.in); System.out.println("Enter 3 numbers:"); int a = scan.nextint(); int b = scan.nextint(); int c = scan.nextint(); int max = a; if (b > max) max = b; if (c > max) max = c; System.out.print( "max = " + max);

10. [ / 10] Construct an algorithm that inputs some positive numbers from the user, terminated with a zero (sentinel value). The algorithm should print the numbers entered and compute and print their sum. After the sum is printed, print a goodbye message. Example: If the numbers 5-3 8 0 are entered as input, the algorithm should print: 5-3 8 Sum = 10 Goodbye [Note that the terminating zero should NOT be printed.] 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 sum num = 0 sum = 0 count = 0 num = num + 1 sum = sum + 1 count = count + 1 sum = sum + num sum = num + 1 if (sum == num) if (sum < num) if (num < sum) else while (num < sum) while (sum < num) while (count < sum) while (num!= 0) while (num!= sum) print Sum = sum print num print Goodbye input num sum = 0 while (num!= 0) sum = sum + num input num print Sum = sum print Goodbye

REFERENCE MATERIAL 20 45 Y 20 Y 10 Drawing a Line 150 Start% x%%%%%%%y% End% x%%%%%%%y% page.drawline (10, 20, 150, 45); page.drawline (150, 45, 10, 20); Drawing an Oval 175 80 50 page.drawoval (175, 20, 50, 80); X X Drawing a Rectangle 50 X 20 40 100 Y Start% x%%%%%%%y% Width%%%%Height% page.drawrect (50, 20, 100, 40); Drawing an Arc An arc is defined by an oval, a start angle, and an arc angle:

REFERENCE MATERIAL Random class double nextdouble() Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 int nextint() Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence. int nextint(int n) Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. Some methods and constants of the Math class static double abs(double a) Returns the absolute value of a double value. static double cos(double a) Returns the trigonometric cosine of an angle. static double pow(double a, double b) Returns the value of the first argument raised to the power of the second argument. static double random() Returns a double value greater than or equal to 0.0 and less than 1.0. static long round(double a) Returns the closest long to the argument. static double sin(double a) Returns the trigonometric sine of an angle. static double sqrt(double a) Returns the correctly rounded positive square root of a double value. static double PI static double E Value of π (constant) Value of e (constant) String class boolean isempty() Returns true if, and only if, length() is 0. char charat(int index) Returns the char value at the specified index. int length() Returns the length of this string. String touppercase() Returns a String with all of the characters in this String to upper case.

CSC 1051 Algorithms and Data Structures I Midterm Examination February 25, 2016 Name: KEY B 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. If you make a mistake or for some other reason need more space, please use the back of pages and clearly indicate where the answer can be found. Good luck!

1. [ /10] What output is produced by the following program? public class TraceThis public static void main (String[] args) int x = 10; int a = 20; int b = 30; x = a; a = b; b = 40; System.out.println("What gets printed? "); System.out.println("x = " + x + " a = " + a + " b = " + b); System.out.print ("Let\'s do this \none\n\"last time\" "); System.out.println (".\nplease count the \"\\\\\"s carefully!"); System.out.println ("Version 1 numbers: " + 4 + 6); System.out.println ("Version 2 numbers: " + "4 + 6"); System.out.println ("Version 3 numbers: " + (4 + 6)); Output: What gets printed? x = 20 a = 30 b = 40 Let's do this one "last time". Please count the "\\"s carefully! Version 1 numbers: 46 Version 2 numbers: 4 + 6 Version 3 numbers: 10

2. ( / 10) a) Which of the following code fragments correctly executes three assignments if the condition is true? Mark the correct one and explain your answer, briefly. i) if (x < 0) a = b * 2; y = x; z = a y; ii) if (x < 0) // This is the only one that works // because it uses the braces correctly a = b * 2; y = x; z = a y; iii) if (x < 0) a = b * 2; y = x; z = a y; iv) if (x < 0) a = b * 2; y = x; z = a y ; b) Which of the code fragments below will add 1 to x if x is positive and subtract 1 from x if x is negative, but leave x alone if x is 0? Explain your answer, briefly. i) if (x > 0) x++; else x--; ii) if (x == 0) x = 0; else x++; x--; iii) x++; x--; iv) if (x > 0) x++; else if (x < 0) // This is the only one that works because it x--; //leaves the x unchanged when it is zero.

3. ( / 10) What gets printed? Please show output as it will appear, or indicate NO OUTPUT, or show some of the output followed by INFINITE LOOP. int a = 0; while (a > 0) System.out.println(a); a = a + 3; Output: NO OUTPUT int a = 5; while (a >= 5) System.out.println(a); a++; Output: 5 6 7 INFINITE LOOP int a = 4; while (a > 0) System.out.println(a * 2); a--; Output: 8 6 4 2 String word = "ILLUSION"; int x = 0; while (x < word.length()) System.out.print(word.charAt(x)); System.out.print(word.charAt(x)); x++; Output: IILLLLUUSSIIOONN

4. ( / 10) Using the coordinate system below representing the graphics area of an applet, draw the graphics displayed by the applet, identifying positions MID and TOP (in terms of their use in the applet), and the shapes RECTANGLE, OVAL, LINE. import javax.swing.japplet; import java.awt.*; public class Quiz4 extends JApplet //------------------------------------------------------ // Draws something... //------------------------------------------------------ public void paint (Graphics page) final int MID = 40; // MID final int TOP = 20; // TOP page.drawoval (MID-10, TOP-20, 20,40); // OVAL page.drawrect (0, 0, 30, 40); // RECTANGLE page.drawline (0, 60, 100, 60); // LINE 0 20 40 60 80 100 120 140 0 80 20 40 60 80 100

5. ( / 10) Suppose you look up a class in the Java API and find the following info about the (alas, made-up!) SpringBreak class: java.exam Class SpringBreak java.lang.object java.exam.springbreak Constructor Summary SpringBreak() Creates a new SpringBreak object. Method Summary void destination(string x) static boolean shouldistayorshouldigo(double x) a) Write some code to declare variables for two objects of this class, named thing1 and thing2. SpringBreak thing1 = new SpringBreak (); SpringBreak thing2 = new SpringBreak (); b) Circle all of the following that are valid Java statements: SpringBreak.destination = Puerto Vallarta ; thing1.destination( Cancun ); thing2.destination(thing1); SpringBreak.destination( Miami ); System.out.print(thing2.destination( Cancun )); thing2.shouldistayorshouldigo(21.6) = true; System.out.print(SpringBreak.shouldIStayOrShouldIGo(32.4));

6. [ /10] a) Given the following declarations: int iresult, num1 = 7, num2 = 3; double fresult, val1 = 9.0; boolean status, part1 = false; What result is stored by each of the following assignment statements? Source code Result stored fresult = (num1 + 2)/ 2; 4.0 iresult = num1 % num2; 1 fresult = val1 / 2; 4.5 fresult = (double) num1 / 2; 3.5 status = part1 && (num1 > num2); false status = part1 (num1 > num2); true

7. [ /10] Short answer questions. a) Given a Random object named gen, what range of values are produced by the following expressions? gen.nextint(4) 0 to 3 gen.nextint(20) + 100 100 to 119 gen.nextint(4) 15-15 to -12 b) Write some code that uses the object gen, above, to generate pseudorandom numbers in the following specified ranges (including the endpoints): 0 to 9 gen.nextint(10) -2 to 5 gen.nextint(8) -2 c) Suppose the String variable message is already initialized. Write a Java code fragment that prints out message, one char per line. int count = 0; while (count < message.length) System.out.print(message.charAt(count)); count ++; d) Write some code that uses the Math class to compute and print out the value of sin(π/2). System.out.println(Math.sin( Math.PI /2) );

8. ( / 10) Complete the java code below so that it prints a table for the Investment problem: You put an initial amount (say, $10,000) into a bank account that earns 5% interest per year. Show the yearly returns on your investment until it doubles. year interest balance 0 $10,000.00 1 $500.00 $10,500.00 2 $525.00 $11,025.00 3 $551.25 $11576.25 import java.text.numberformat; public class Investment public static void main (String[] args) Scanner scan = new Scanner(System.in); 4 $578.81 $12,155.06 (keep going until balance >= $20000) System.out.print("Enter initial balance:"); double initial = scan.nextdouble(); int year = 0; double rate = 0.05; NumberFormat money = NumberFormat.getCurrencyInstance(); System.out.println("year\tinterest\tbalance"); System.out.println (year + "\t" + " " + "\t" + money.format(balance)); double interest; double balance = initial; 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));

9. [ / 10] Write a complete Java program that asks the user to input three integers and then prints the maximum value among the numbers entered. (For example, if the three numbers entered are 7, 43, 7, the program should print 43 as the maximum. Be sure to write a complete Java program, including class definition, variable and constant declarations, as appropriate, and proper indentation, to make it readable. Include one simple comment describing what the program does (no need need to write lots of comments). // Computes the max of three inputs import java.util.scanner; public class Max public static void main(string[] args) Scanner scan = new Scanner(System.in); System.out.println("Enter 3 numbers:"); int a = scan.nextint(); int b = scan.nextint(); int c = scan.nextint(); int max = a; if (b > max) max = b; if (c > max) max = c; System.out.print( "max = " + max);

10. [ / 10] Construct an algorithm that inputs some positive numbers from the user, terminated with a zero (sentinel value). The algorithm should print the numbers entered and compute and print their sum. After the sum is printed, print a goodbye message. Example: If the numbers 5-3 8 0 are entered as input, the algorithm should print: 5-3 8 Sum = 10 Goodbye [Note that the terminating zero should NOT be printed.] 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 sum num = 0 sum = 0 count = 0 num = num + 1 sum = sum + 1 count = count + 1 sum = sum + num sum = num + 1 if (sum == num) if (sum < num) if (num < sum) else while (num < sum) while (sum < num) while (count < sum) while (num!= 0) while (num!= sum) print Sum = sum print num print Goodbye input num sum = 0 while (num!= 0) sum = sum + num input num print Sum = sum print Goodbye

REFERENCE MATERIAL 20 45 Y 20 Y 10 Drawing a Line 150 Start% x%%%%%%%y% End% x%%%%%%%y% page.drawline (10, 20, 150, 45); page.drawline (150, 45, 10, 20); Drawing an Oval 175 80 50 page.drawoval (175, 20, 50, 80); X X Drawing a Rectangle 50 X 20 40 100 Y Start% x%%%%%%%y% Width%%%%Height% page.drawrect (50, 20, 100, 40); Drawing an Arc An arc is defined by an oval, a start angle, and an arc angle:

REFERENCE MATERIAL Random class double nextdouble() Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 int nextint() Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence. int nextint(int n) Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. Some methods and constants of the Math class static double abs(double a) Returns the absolute value of a double value. static double cos(double a) Returns the trigonometric cosine of an angle. static double pow(double a, double b) Returns the value of the first argument raised to the power of the second argument. static double random() Returns a double value greater than or equal to 0.0 and less than 1.0. static long round(double a) Returns the closest long to the argument. static double sin(double a) Returns the trigonometric sine of an angle. static double sqrt(double a) Returns the correctly rounded positive square root of a double value. static double PI static double E Value of π (constant) Value of e (constant) String class boolean isempty() Returns true if, and only if, length() is 0. char charat(int index) Returns the char value at the specified index. int length() Returns the length of this string. String touppercase() Returns a String with all of the characters in this String to upper case.

10. [ / 10] (Alternative version not used) Construct an algorithm that inputs 8 numbers from the user. The algorithm should print the numbers entered and compute and print their sum. After the sum is printed, print a goodbye message. Example: If the numbers 5-3 8 1 0-2 3 4 are entered as input, the algorithm should print: 5-3 8 1 0-2 3 4 Sum = 16 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 sum num = 0 sum = 0 count = 0 count = 1 num = num + 1 sum = sum + 1 count = count + 1 sum = sum + num if (sum == num) if (sum < num) if (num < sum) else while (num < sum) while (sum < num) while (count < sum) while count < 8) while count <= 8) while (num!= 0) while (num!= sum) print Sum = sum print num print Goodbye sum = 0 count = 0 while (count < 8) input num sum = sum + num count = count + 1 print Sum = sum print Goodbye

CSC1051 Data Structures and Algorithms I Dr. Papalaskari