Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

Similar documents
COMP 110 Programming Exercise: Simulation of the Game of Craps

All answers will be posted on web site, and most will be reviewed in class.

Function Call Stack and Activation Records

Assignment 6. Methods Homework Spring 2017 P Question 1 all

AP CS Unit 3: Control Structures Notes

C Functions. 5.2 Program Modules in C

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

APCS Semester #1 Final Exam Practice Problems

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

(Not Quite) Minijava

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

The for Loop. Lesson 11

CS 139 Practice Midterm Questions #2

12/22/11. } Rolling a Six-Sided Die. } Fig 6.7: Rolling a Six-Sided Die 6,000,000 Times

2. Each element of an array is accessed by a number known as a(n) a. a. subscript b. size declarator c. address d. specifier

( &% class MyClass { }

Following is the general form of a typical decision making structure found in most of the programming languages:

Computer Programming: C++

BoredGames Language Reference Manual A Language for Board Games. Brandon Kessler (bpk2107) and Kristen Wise (kew2132)

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

LAB: WHILE LOOPS IN C++

Interfaces CSC 123 Fall 2018 Howard Rosenthal

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Introduction to the Java Basics: Control Flow Statements

VENTURE. Section 1. Lexical Elements. 1.1 Identifiers. 1.2 Keywords. 1.3 Literals

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

CHAPTER 7 OBJECTS AND CLASSES

Unit 2: Java in the small. Prepared by: Dr. Abdallah Mohamed, AOU-KW

Mat 2170 Week 9. Spring Mat 2170 Week 9. Objects and Classes. Week 9. Review. Random. Overloading. Craps. Clients. Packages. Randomness.

Last Time: Rolling a Weighted Die

Student Responsibilities. Mat 2170 Week 9. Notes About Using Methods. Recall: Writing Methods. Chapter Six: Objects and Classes

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

CHAPTER 7 OBJECTS AND CLASSES

Classwork 7: Craps. N. Duong & R. Rodriguez, Java Crash Course January 6, 2015

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Due Date: Two Program Demonstrations (Testing and Debugging): End of Lab

Java Bytecode (binary file)

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

What does this program print?

Unit 2: Java in the small

Decision Making in C

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

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Key Differences Between Python and Java

Object oriented programming C++

Introduction to Computer Science Unit 2. Notes

Methods. Contents Anatomy of a Method How to design a Method Static methods Additional Reading. Anatomy of a Method

Lecture 5: Methods CS2301

First Name: Last: ID# 1. Hexadecimal uses the symbols 1, 2, 3, 4, 5, 6, 7 8, 9, A, B, C, D, E, F,G.

Assignment Checklist. Prelab Activities. Lab Exercises. Labs Provided by Instructor. Postlab Activities. Section:

Values and Variables 1 / 30

GridLang: Grid Based Game Development Language Language Reference Manual. Programming Language and Translators - Spring 2017 Prof.

Chapter 4 Defining Classes I

Title. Java Just in Time. John Latham. October 25, October 25, 2017 Java Just in Time - John Latham Page 1(0/0)

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

M e t h o d s a n d P a r a m e t e r s

CSC 1300 Exam 4 Comprehensive-ish and Structs

1 Lexical Considerations

CS111: PROGRAMMING LANGUAGE II

CSE123. Program Design and Modular Programming Functions 1-1

CS-201 Introduction to Programming with Java

Lexical Considerations

CSC 307 DATA STRUCTURES AND ALGORITHM ANALYSIS IN C++ SPRING 2011

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.

Object Oriented Programming. Java-Lecture 6 - Arrays

1 Short Answer (15 Points Each)

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

Research Group. 2: More types, Methods, Conditionals

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

PieNum Language Reference Manual

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

1 Short Answer (2 Points Each)

Shut the Box. By the time you are done with this activity, you and your team should be able to:

Chapter 2: Functions and Control Structures

Functions and Recursion

CS 231 Data Structures and Algorithms, Fall 2016

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

York University AK/ITEC OBJECT-BASED PROGRAMMING. Midterm Test Sample. Examiner: S.Y. Chen Duration: One Hour and Fifteen Minutes

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Lecture Set 4: More About Methods and More About Operators

CS313D: ADVANCED PROGRAMMING LANGUAGE

6 Functions. 6.1 Focus on Software Engineering: Modular Programming TOPICS. CONCEPT: A program may be broken up into manageable functions.

Introduction to Computer Science Unit 4B. Programs: Classes and Objects

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Lesson 3: Accepting User Input and Using Different Methods for Output

COMP 110/L Lecture 13. Kyle Dewey

STUDENT LESSON A14 Boolean Algebra and Loop Boundaries

C++ How to Program, 9/e by Pearson Education, Inc. All Rights Reserved.

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

EECS168 Exam 3 Review

CS11 Java. Fall Lecture 1

2/6/2018. ECE 220: Computer Systems & Programming. Function Signature Needed to Call Function. Signature Include Name and Types for Inputs and Outputs

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

Mr. Monroe s Guide to Mastering Java Syntax

CSC 1351: Quiz 6: Sort and Search

Program #7: Let s Play Craps!

Flow Control. CSC215 Lecture

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

Transcription:

Assignment 3 Methods Review CSC 123 Fall 2018 Notes: All homework must be submitted via e-mail. All parts of assignment must be submitted in a single e-mail with multiple attachments when required. Notes: All homework must be submitted via e-mail. All parts of assignment must be submitted in a single e-mail with multiple attachments. E-mail address is: csc123csudh@gmail.com Each program is to be submitted in a separate file with the file name being the class name with extension.java as shown below. I only need the source file. GeneralAverage.java The other homework file should be submitted with your name using the following format: RosenthalH_Ax.doc, where x is the assignment number. Note: use your name, not mine All answers will be posted on web site, and most will be reviewed in class. Review Chapter 6 in Bravaco Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous 2. This appears at the beginning of a method definition. a. semicolon b. parentheses c. body d. header 3. The body of a method is enclosed in. a. curly braces { } b. square brackets [] c. parentheses () d. quotation marks "" 4. A method header can contain. a. method modifiers b. the method return type c. the method name d. a list of parameter declarations e. all of these f. none of these

5. A value that is passed into a method when it is called is known as a(n). a. parameter b. argument c. signal d. return value 6. A variable that receives a value that is passed into a method is known as a(n). a. parameter b. argument c. signal d. return value 7. This statement causes a method to end and sends a value back to the statement that called the method. a. end b. send c. exit d. return 8. True or False: You terminate a method header with a semicolon. 9. True or False: When passing an argument to a method, Java will automatically perform a widening conversion (convert the argument to a higher-ranking data type), if necessary. 10. True or False: When passing an argument to a method, Java will automatically perform a narrowing conversion (convert the argument to a lower-ranking data type), if necessary. 11. True or False: A parameter variable s scope is the entire program that contains the method in which the parameter is declared. 12. True or False: When code in a method changes the value of a formal parameter, it also changes the value of the actual parameter that was used to call the method. 13. True or False: When passing multiple arguments to a method, the order in which the arguments are passed is not important. 14. True or False: No two methods in the same program can have a local variable with the same name. 15. True or False: You must have a return statement in a value-returning method. 16. What is the output of this program public class ThisComputesSomeWeirdStuff { public static int method1(int a, int b) { if (a%2 == 0) return (a) else return (b) }

} public static int method2(int a, int b) { while (a!= 1) {b++; a = a / 2;} return (b); } public static void main(string[] args) { System.out.println(method2(method1(3, 10), method1(16, 57))); System.out.println(method2(method1(190, 10), method1(16, 57))); System.out.println(method2(method2(3, 10), method1(16, 57))); System.out.println(method1(method2(3, 10), method2(16, 57))); } 17. Overloaded Methods a. Method add(...) is overloaded as follows: static double add( int a, double b) { static double add(double a, int b) { return a + b; return a + b; } } Which, if any, of the following invocations fail to compile? Give reasons. i. add(1,2) ii. add(1.0,2.0) iii. add(1.0, 2) iv. add(2.0,2) b. Method sub( ) is overloaded as follows: int sub( int a, int b) { double sub(double a, double b) { return a - b; return a - b; } } Which, if any, of the following invocations fail to compile? Give reasons. i. sub(1,2) ii. sub(1.0,2.0) iii. sub(1.0, 2) iv. sub(2.0,2) c. What is the problem with the following overloaded method that returns a product as either an int or a long? int mul(int a, int b) { long mul(int a, int b) { return a * b; return a * b; } } Programming Exercises 1. HypotenuseAreaCalc Define a method hypotenuse that calculates the hypotenuse of a right triangle when the lengths of the two legs are given. The method accepts two type double arguments and returns the value of the hypotenuse as type double. Define a second method called areatri that calculates the area of the right triangle from the legs (remember Area =.5*b*h). The main method reads in the two values. It checks to make sure that both legs are greater than 0.0 or requests a reentry of both

values. It then calls each of the two methods and prints out a line that looks like this with values correct to two decimal places: The right triangle with legs leg1 and leg2 has a hypotenuse of length hypotonuselength and an area of trianglearea. 2. Multiples Write a method ismultiple that determines for a pair of integers, whether the second is a multiple of the first. The main method reads in the two integers and then calls ismultiple with the two integers as parameter, returning true if the second is a multiple of the first, otherwise false. The main program prints out the resulting answer of true or false. 3. EvenOddCounter Write a program with a method named iseven that accepts an int argument. The method should return true if the argument is even, or false otherwise. The program s main method should use a loop to generate 100 random integers between 1 and 100. It should use the iseven method to determine whether each random number is even, or odd. When the loop is finished, the program should display the number of even numbers that were generated, and the number of odd numbers. 4. ReverseDigits Write a method reverse that takes an integer value and return the number with its digits reversed. The main program reads in the integer and prints the reversed integer. For instance, if the input is 54378 the output integer would be 87345. 5. Craps Simulation When playing craps, a player rolls two dice repeatedly until she wins or loses. The first roll of the dice is called the come-out roll. If the player rolls a 7 or an 11 on the comeout roll, then she wins immediately. A 2, 3, or 12 on the come-out roll results in an immediate loss. If she rolls a 4, 5, 6, 8, 9, or 10 on the come-out roll, then that number becomes her point and she continues rolling until she rolls either her point or a 7. If she rolls her point, she wins, but if she rolls a 7 before rolling her point, she loses. Once a player has established her point, no other numbers (including 2, 3, 11, or 12) affect her winning or losing. Write a method boolean craps() that simulates one game of craps and returns true if and only if the player wins. Test your method by printing the values of each roll of the dice. When you are convinced that your simulation is correct, include this method in a program that executes craps() n times by requesting n from the user, and reports the percentage of wins correct to two decimal places. Note you need the roll of two six-sided die, not one 12-sided die in this simulation.

Here is a sample with 10 runs: Please enter the number of simulations you wish to run: 10 The come-out is 8 It is TRUE that you won craps game 1 The come-out is 3 It is FALSE that you won craps game 2 The come-out is 9 The next roll is 10 The next roll is 12 The next roll is 4 The next roll is 3 The next roll is 7 It is FALSE that you won craps game 3 The come-out is 9 The next roll is 10 The next roll is 7 It is FALSE that you won craps game 4 The come-out is 2 It is FALSE that you won craps game 5 The come-out is 4 The next roll is 3 The next roll is 9 The next roll is 9 The next roll is 3 The next roll is 4 It is TRUE that you won craps game 6 The come-out is 7 It is TRUE that you won craps game 7 The come-out is 5

The next roll is 7 It is FALSE that you won craps game 8 The come-out is 6 It is TRUE that you won craps game 9 The come-out is 8 The next roll is 9 The next roll is 12 It is TRUE that you won craps game 10 The player won 50.00% games out of the 10 games played