Name: Checked: Preparation: Response time experiment accessing the system clock

Similar documents
Lab 6. Name: Checked:

Name: Checked: Preparation: Response time experiment accessing the system clock

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

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

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

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

Lab Activity Plan. John Dalbey CPE /30/2013

Selection and Repetition Revisited

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

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

Lab 5. Name: Checked: Part A. Focus on Strings

3/18/2015. Chapter 19 Sorting and Searching SELECTION SORT SORTING AN ARRAY OF INTEGERS SORTING AN ARRAY OF INTEGERS FILE SELECTIONSORTER.

Introduction to Computer Science Unit 2. Exercises

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

AP COMPUTER SCIENCE A

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

private static final char[] Alphabet = "abcdefghijklmnopqrstuvwxyz".tochararray();

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

St. Edmund Preparatory High School Brooklyn, NY

Loops. CSE 114, Computer Science 1 Stony Brook University

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Simple Control Flow: if-else statements

CSIS 10A Assignment 4 SOLUTIONS

while (/* array size less than 1*/){ System.out.print("Number of students is invalid. Enter" + "number of students: "); /* read array size again */

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

CSC 1051 Data Structures and Algorithms I

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

Anatomy of a Java Program: Comments

Selection and Repetition

Introduction to Computer Programming

2.2 - Making Decisions

Control flow, conditionals, boolean expressions, block statements, nested statements. Course website:

AP Computer Science Unit 1. Programs

Welcome to the Using Objects lab!

Web-CAT submission URL: CAT.woa/wa/assignments/eclipse

CCHS Math Recursion Worksheets M Heinen CS-A 12/5/2013. Recursion Worksheets Plus Page 1 of 6

CSC 1051 Data Structures and Algorithms I

Fundamentals of Programming Data Types & Methods

Mr. Monroe s Guide to Mastering Java Syntax

Oct Decision Structures cont d

CSC 1051 Data Structures and Algorithms I

Improved algorithm. Java code. Control flow and conditionals CSC 1051 Villanova University. Dr Papalaskari 1. Java Programè Algorithm

Java code. Updated program. Control flow and conditionals CSC 1051 Villanova University. Dr Papalaskari 1. Java Programè Algorithm. Improved algorithm

CAT.woa/wa/assignments/eclipse

Loops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department

Over and Over Again GEEN163

Selection and Repetition Revisited

Welcome to the Using Objects lab!

JAVA PROGRAMMING (340)

Midterm Examination (MTA)

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

Assignment 8B SOLUTIONS

Java I/O and Control Structures Algorithms in everyday life

Algorithms and Conditionals

Question: Total Points: Score:

Bjarne Stroustrup. creator of C++

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

CSE 1223: Introduction to Computer Programming in Java Chapter 1 Computer Basics

Java I/O and Control Structures

Maximization and Minimization Problems. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Lab1 Solution. Lab2 Solution. MathTrick.java. CoinFlip.java

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Topic 11 Scanner object, conditional execution

Building Java Programs

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

CS 112 Introduction to Programming

Arrays. Eng. Mohammed Abdualal

1. An operation in which an overall value is computed incrementally, often using a loop.

Welcome to the Primitives and Expressions Lab!

Building Java Programs

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

Multiple Choice Using Object Methods

ITERATION WEEK 4: EXMAPLES IN CLASS

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

Chapter 3: Classes and Objects. CSC2010 Lab 9. Java Programming. Copyright 2000 W. W. Norton & Company. All rights reserved. FROM THE BEGINNING

King Abdulaziz University Faculty of Computing and Information Technology Computer Science Department

COMP1406 Tutorial 1. Objectives: Getting Started:

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Building Java Programs

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

CSE142 Sample Midterm, Winter 2018

Section 003 Fall CS 170 Exam 1. Name (print): Instructions:

CS 231 Data Structures and Algorithms Fall Event Based Programming Lecture 06 - September 17, Prof. Zadia Codabux

Introduction to Computer Programming

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

Darrell Bethea May 10, MTWRF 9:45-11:15 AM Sitterson 011

COMP 110 Programming Exercise: Simulation of the Game of Craps

Selection and Repetition

Garfield AP CS. User Input, If/Else. Most slides from Building Java Programs. Thanks, Stuart Regesand Marty Stepp!

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Building Java Programs

Topic 11 Scanner object, conditional execution

Introduction to Computer Science Unit 2. Notes

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

Question: Total Points: Score:

CSE 142 Sample Midterm Exam #3

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

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

CS 101 Spring 2007 Midterm 2 Name: ID:

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

Repetition, Looping. While Loop

Transcription:

Lab 6 Name: Checked: Objectives: Practice using the conditional operator; switch statements; do and for loops. Learn how to implement a simple experimental setup to measure response times using System.currentTimeMillis() (current system time in milliseconds). Preparation: Response time experiment accessing the system clock 1. Download and run ResponseTimeExperiment.java. (shown on next page). 2. Run the program a few times to observe its function. Note the use of System.currentTimeMillis() to find the current time at two different points during the run, and thus obtain the amount of time elapsed between issuing of a question and the input (answer) from the user. 3. Refer to the code shown on the next page. Modify the code enclosed in the boxes, as follows: Using the conditional operator, compute the value of outcome in just one line of code (replace the if/else). Display the result ( Correct! or Incorrect. ) using a single System.out.println statement (again, use the conditional operator). 4. Test your program to ensure that it functions exactly as before. 5. Upload and submit ResponseTimeExperiment.java through blackboard under Lab 6 Prep.

// ResponseTimeExperiment.java // Measure response time for addition problems. // M A Papalaskari import java.util.scanner; import java.util.random; public class ResponseTimeExperiment { public static void main(string[] args) { Scanner in = new Scanner(System.in); Random rand = new Random(); System.out.print("Please enter your name: "); String name = in.nextline(); System.out.println("Hello " + name + ". Please answer as fast as you can." + "\n\nhit <ENTER> when ready for the question."); in.nextline(); // wait for user to hit <ENTER> int a = rand.nextint(100); int b = rand.nextint(100); long starttime = System.currentTimeMillis(); System.out.print(a + " + " + b + " = "); String response = in.nextline(); int number = Integer.parseInt(response); long endtime = System.currentTimeMillis(); int outcome; if (number == a + b) outcome = 1; else outcome = 0; long reactiontime = endtime - starttime; if (outcome == 1) System.out.println("Correct!"); else System.out.println("Incorrect."); } System.out.println("That took " + reactiontime + " milliseconds"); System.out.println("Thank you " + name + ", goodbye."); }

Part A: switch statement Write a program to input a number n that symbolizes a version of the Mac OS X 10.n software. Output the name of that version of the Mac OS X 10.n software. For example, if the user inputs 8, then the program should output Mountain Lion. [You will need to do a little googling to find the names of the different mac software. They run from 1-10 and most of them are cats, but more recently they are joined by mountains.] Part B. do loops Modify your code for Lab 4b to use a do loop (that is the version of the GPA program that asks each time whether to repeat ). Part C. for-loops Modify your code for Lab 4d to use a for-loop (that is the version of the GPA program with the exact count). Part D. Annotate the code of ResponseTimeExperiment.java 1. Examine the code of ResponseTimeExperiment.java printed on in Page 2. Annotate the code with the number to match each step in the algorithm (some of the numbers may correspond to more than one line of code). Algorithm: 1. input name 2. print personalized welcome message & instructions 3. a = random int 4. b = random int 5. starttime = current time 6. print question using a, b 7. input answer 8. endtime = current time 9. outcome = 1 or 0 (answer is correct or incorrect) 10. reactiontime = endtime starttime 11. print outcome as Correct or Incorrect 12. print goodbye

2. Now consider an algorithm that repeats the experiment four times using a for-loop., while also keeping track of the number of correct answers and computing the average response time of the user see the algorithm below. Some steps have been added (highlighted). Annotate the code for ResponseTimeExperiment.java in Page 2 again to add these new steps, as follows: o Draw a box around the code that needs to go inside the loop o Draw an arrow with a label in the appropriate positions for where A, B,..G need to be inserted (show an arrow and the label of the step where it should go in the code, eg, Aà should go ) to prepare to implement the algorithm. Be sure to mark clearly, with the appropriate letter, the exact position where code must be added. Algorithm: 1. input name 2. print personalized welcome message & instructions A. numcorrect = 0 B. totaltime = 0 C. repeat 4 times: 3. a = random int 4. b = random int 5. starttime = current time 6. print question using a, b 7. input answer 8. endtime = current time D. totaltime = totaltime + reactiontime 9. outcome = 1 or 0 (answer is correct or incorrect) 10. reactiontime = endtime starttime 11. print outcome as Correct or Incorrect E. numcorrect = numcorrect + outcome F. averagetime = totaltime/4 G. print averagetime H. print numcorrect 12. print goodbye 3. Add some code and restructure ResponseTimeExperiment.java to implement the new steps, using your notes from 2, above. Be sure to use a for-loop to implement the repetition of the experiment.

Lab 6 Comments Name: Checked: Comments on this lab, please: What was the most valuable thing you learned in this lab? What did you like best about this lab? Was there any particular problem? Do you have any suggestions for improving this lab as an effective learning experience?