Claremont McKenna College Computer Science

Similar documents
Lecture 8 " INPUT " Instructor: Craig Duckett

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

Algebra 1. Standard 11 Operations of Expressions. Categories Combining Expressions Multiply Expressions Multiple Operations Function Knowledge

CS111: PROGRAMMING LANGUAGE II

Chapter 3. Selections

Lecture 17. Instructor: Craig Duckett. Passing & Returning Arrays

Midterm Examination (MTA)

CSC 1051 Data Structures and Algorithms I

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

H212 Introduction to Software Systems Honors

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

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

Loops! Step- by- step. An Example while Loop. Flow of Control: Loops (Savitch, Chapter 4)

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

Controls Structure for Repetition

AP Computer Science Java Mr. Clausen Program 6A, 6B

CSC 1051 Data Structures and Algorithms I

COMP 202 Java in one week

STUDENT LESSON A12 Iterations

Loops. CSE 114, Computer Science 1 Stony Brook University

CS111: PROGRAMMING LANGUAGE II

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Top-Down Program Development

St. Edmund Preparatory High School Brooklyn, NY

Activity 4: Methods. Content Learning Objectives. Process Skill Goals

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

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Testing and Debugging

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

Introduction to Java Programming

4. Java Project Design, Input Methods

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

Last Class. While loops Infinite loops Loop counters Iterations

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points

CS 051 Homework Laboratory #2

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

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

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Fundamentals of Programming Data Types & Methods

CMPT 125: Lecture 4 Conditionals and Loops

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

AP Computer Science Unit 1. Writing Programs Using BlueJ

CSCE 145 Exam 1 Review. This exam totals to 100 points. Follow the instructions. Good luck!

Lecture 10. Instructor: Craig Duckett

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

There are several files including the start of a unit test and the method stubs in MindNumber.java. Here is a preview of what you will do:

Table of Laplace Transforms

Question: Total Points: Score:

APCS-AB: Java. Recursion in Java December 12, week14 1

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

2.2 - Making Decisions

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Object Oriented Programming. Java-Lecture 6 - Arrays

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

COMP 202 Java in one week

Repetition. Chapter 6

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014

Repetition. Chapter 6

Introduction to Computer Science Unit 2. Notes

Comp Assignment 2: Object-Oriented Scanning for Numbers, Words, and Quoted Strings

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

Introduction to Computer Science, Shimon Schocken, IDC Herzliya. Lectures Control Structures

n! = 1 * 2 * 3 * 4 * * (n-1) * n

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

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

Introduction to the Java Basics: Control Flow Statements

CS61BL. Lecture 1: Welcome to CS61BL! Intro to Java and OOP Testing Error-handling

Programming Exercise 7: Static Methods

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

Repetition CSC 121 Fall 2014 Howard Rosenthal

4. Java language basics: Function. Minhaeng Lee

LOOPS. Repetition using the while statement

Java Coding 3. Over & over again!

CS 112 Introduction to Programming

CS 112 Introduction to Programming

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Question: Total Points: Score:

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #06 Loops: Operators

Lesson 10: Quiz #1 and Getting User Input (W03D2)

CSE 1223: Exam II Autumn 2016

Full file at

Example Program. public class ComputeArea {

RECURSION, RECURSION, (TREE) RECURSION! 2

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

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

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

Chapter 17. Iteration The while Statement

Chapter 4 Lab. Loops and Files. Objectives. Introduction

CSC 1051 Data Structures and Algorithms I

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Nested Loops ***** ***** ***** ***** ***** We know we can print out one line of this square as follows: System.out.

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

Lesson 7 Part 2 Flags

CSC-140 Assignment 6

CS 231 Data Structures and Algorithms, Fall 2016

CMSC 201 Spring 2018 Project 2 Battleship

CS 177 Week 5 Recitation Slides. Loops

Worksheet 3: Predictive Text Entry

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Transcription:

Claremont McKenna College Computer Science CS 51 Handout 4: Problem Set 4 February 10, 2011 This problem set is due 11:50pm on Wednesday, February 16. As usual, you may hand in yours until I make my solutions available. To solve the problems below, you will be implementing classes. There is plenty of room for creativity in this course, but when I specify something, follow the specification exactly. It makes grading much easier for us. Each method that you write must be properly commented and attractively formatted. Be sure to include a comment at the top of each file submitted that gives your name, your student ID number, and your email address. Your submission should eventually include the following (do not hand in.class files or any other that I did not ask for.): Date.java Digits.java Pascal.java Slot.java Follow the instructions on how to submit your homework electronically which you can find on our class web page.

A Piece of Advice This is what I said in the FAQ for a problem set around this time of the semester the last time I taught this class. It might be helpful for you to read. I can see what you are doing. You are in a hurry to finish these programs. So, you try to write the whole program that would solve the entire problem quickly. Think again. Remember I have been telling you to solve them incrementally? Try a partial solution that works and little by little improve it toward the ultimate solution. I see these long programs that do not work that you are sending to me by email and ask me to take a look and tell you why they are not working. There are so many problems with these programs that you are sending to me that it s hard to know where to begin. Waiting until the last minute to solve these problems isn t a good strategy and problems in the future assignments will not get any easier. Those who are keeping up the pace with the class seem to be enjoying the class. We are just getting into the phase where we start experiencing some exciting programming. Try to take advantage and enjoy! I was going to add the diamond problem that we talked about in class, but decided not to. However, I still recommend that you complete the Tree.java program as an exercise. Start early - don t wait until two or three days before the due date. Problem 1 Implement a class named Date in a file named Date.java. Implement the following function in the class: public static String alphatonum (String m, int d, int y); where m is for month, d for day, and y for year. You may assume that you only get valid actual arguments. That is, you do not have to worry about handling error situations due to invalid input values. You may assume that values will be of correct type and in a valid range. Below is an example of a call to the method and its return value. alphatonum("february", 10, 2011) = "2/23/2011" Include at least three calls to alphatonum in your main to test your implementation. Here is an example of main calling it once. Note how I used print and println. public static void main(string[] args) { System.out.print("alphaToNum(\"February\", 10, 2011) should be 2/10/2011"); System.out.println(" : " + alphatonum("february", 10, 2011)); Hand in your Date.java. Hint: You will find a multi-branched if statement useful for this problem. Also consider introducing an auxiliary method with the following signature: // Examples: month("february") returns 2 // month("july") returns 7 public static String month (String m); 2

Problem 2 Implement a class named Digits in a file named Digits.java. Your program is to read a positive integer from keyboard and print the number of even digits in the number, the number of odd digits in the number, and the number of zeros in the number that you read in. As you design your program, implement the following functions in the class: public static boolean iseven (int n); public static int numevens (int n); public static int numodds (int n); public static int numzeros (int n); iseven returns true if n is even; false otherwise. numevens counts the number of even digits in n and returns the count. Similarly for the number of odd digits and zeros with the other two functions. Your main function should read a positive integer from keyboard and use these three functions to produce the desired output. For example, if the number that your program read in is 1073740804, your program should generate an output that looks like the following: 1073740804 contains 3 evens, 4 odds, and 3 zeros. Notes: As you design your program, you may feel that the whole thing could easily be done in one function, namely main without introducing any of the functions that are mentioned above. Well, that may be the case, but more importantly I would like you to learn how to use functions to manage complexity of your program. Hand in your Digits.java. Problem 3 In this problem you will solve the problem that you solved in Problem 2 once more but differently. Again your program is to read a positive integer from keyboard and print the number of even digits in the number, the number of odd digits in the number, and the number of zeros in the number that you read in. In this new design you will implement the following function: // numdigits will return the number of even digits in n if // which is 2, the number of zero digits in n if which is 0, // and the number of odd digits in n if which is 1. // public static int numdigits (int n, int which); You will add this function to Digits from Problem 2. As you implement numdigits, you may use iseven from Problem 2, but you may not use the other three functions from Problem 2. The main function from Problem 2 can now be extended to call numdigits as many times as you need to generate the desired output using the same number that you read in in Problem 2. Using the same input number 1073740804, your program should generate the same output that your program generated in Problem 2, namely: 1073740804 contains 3 evens, 4 odds, and 3 zeros. Hand in your Digits.java which now contain your solutions for both Problems 2 and 3. Problem 4 3

Implement a class named Pascal in a file named Pascal.java. Implement the following function in the class: public static void drawpascal (int n); where n is a positive integer. This function displays the Pascal triangle of size n. If you have to use any loop in this problem, you may only use while loops. A sample run of your program with n = 3 should produce the following. 1 1 1 1 2 1 1 3 3 1 Here is another run with n = 7 this time: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 The Pascal triangle can be obtained by arranging only the binomial coefficients in a triangle as we did in the sample output above. That is, we only include the binomial coefficients in the triangle while ignoring the a s and b s. Binomial coefficients can be computed using the Binomial Theorem: If n is a positive integer and if a and b are any two numbers, then ( ) ( ) ( (a + b) n n = a n n + a n 1 n b +... + 0 1 j ) a n j b j +... + ( n n ) b n where with n j 0. ( n j ) = n! (n j)!j! For example, if n = 0, you would get only one term, i.e., 1. If n = 1, you would get 1a + 1b thus 11 ignoring a and b. You can continue up to the given n for each call to drawpascal. Be sure that your program produces exactly the form of output shown above. Make sure that you use the same indentation and white spaces as shown in the sample output. Here is a sample main that you may use. public static void main (String[] args) { drawpascal(3); drawpascal(7); Hand in Pascal.java. 4

Hints: Try to define some auxiliary functions rather than trying to do everything in one function. For example you may define one that computes factorial, and another that computes the following: ( ) n n! = j (n j)!j! which would use the factorial function. Problem 5 Implement a class named Slot in a file named Slot.java. In this problem your program will simulate a simple slot machine in which three numbers between 0 and 9 (inclusive) are randomly selected and printed side by side separated by a reasonable number of blank spaces. It will also print an outcome telling the user whether it was a triple, double, or single. Continue playing until the user chooses to stop. When the user chooses to stop, print out some statistics much like my solution does (see below). Try to introduce a function whenever a specific task can be isolated as a separate function that can be called by another function. Hand in Slot.java. Note that you will need to use random numbers. Here is a sample interaction (generated by my solution): int-168-217:sol alee$ java Slot The numbers you drew are: 4, 9, 1 The numbers you drew are: 2, 3, 4 The numbers you drew are: 1, 6, 1 wow, double luck! The numbers you drew are: 7, 3, 2 The numbers you drew are: 3, 4, 4 wow, double luck! The numbers you drew are: 0, 6, 4 The numbers you drew are: 2, 6, 8 The numbers you drew are: 6, 5, 4 The numbers you drew are: 9, 5, 6 The numbers you drew are: 0, 5, 8 The numbers you drew are: 2, 2, 2 wow, triple luck! 5

The numbers you drew are: 8, 8, 0 wow, double luck! The numbers you drew are: 5, 8, 0 The numbers you drew are: 8, 7, 2 Continue? (enter 1 to continue 2 to stop): 2 Out of 14 hands, you had 1 triple jack pots (7%), 3 doubles (21%), and 10 singles (71%) today!!! Come again... I said Continue playing until the user chooses to stop. Well, we don t quite know how to do that. Here is some extra information for doing that. The idea is to use an infinite loop - we know how to create an infinite loop. The question is how to break out of an infinite loop when you need to. Well, you could have guessed. We use break to break out of a loop. Let me give you an example: import java.util.scanner; public class BreakTest { public static void main (String[] args) { while (true) { Scanner scan = new Scanner(System.in); System.out.print("Enter an integer: "); int n = scan.nextint(); System.out.println("You typed in: " + n); if (n == 22) { break; System.out.println("Done."); In the above code, if the number that was read in, namely n, is equal to 22, it will leave the while loop. Otherwise it will continue asking the user to type in a new integer. Remember that we can create an infinite loop using for right? Just in case you want to use a for loop instead of a while loop. If you don t remember, see the lecture notes. 6