METHODS EXERCISES GuessNumber and Sample run SumAll Sample Run

Similar documents
Condition Controlled Loops. Introduction to Programming - Python

CONDITION CONTROLLED LOOPS. Introduction to Programming - Python

Module Certification and Testing

Lab ACN : C++ Programming Exercises

i) Natural numbers: Counting numbers, i.e, 1, 2, 3, 4,. are called natural numbers.

Microsoft Excel 2016 LEVEL 3

Objectives/Outcomes. Introduction: If we have a set "collection" of fruits : Banana, Apple and Grapes.

LOOPS. 1- Write a program that prompts user to enter an integer N and determines and prints the sum of cubes from 5 to N (i.e. sum of 5 3 to N 3 ).

Roberto Clemente Middle School

Types, lists & functions

What two elements are usually present for calculating a total of a series of numbers?

Final Exam Review (Revised 3/16) Math MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

DOING MORE WITH EXCEL: MICROSOFT OFFICE 2013

Condition Controlled Loops. Introduction to Programming - Python

Repetition Structures

CSE 131 Introduction to Computer Science Fall 2016 Exam I. Print clearly the following information:

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

lyndaclassroom Getting Started Guide

Text Input and Conditionals

Other operators. Some times a simple comparison is not enough to determine if our criteria has been met.

COMP 202 Java in one week

MAT 142 College Mathematics. Module ST. Statistics. Terri Miller revised July 14, 2015

Student Outcomes. Lesson Notes. Classwork. Discussion (4 minutes)

3.2-Measures of Center

//If target was found, then //found == true and a[index] == target.

Vocabulary: Bits and Pieces III

7 th Pre-AP REVIEW for TEST1 1 st Six Weeks

Microsoft Excel 2010 Handout

1. Managing Information in Table

review for 3rd 9 weeks test

Exponents. Although exponents can be negative as well as positive numbers, this chapter will only address the use of positive exponents.

DEFECT TESTING. Since the goal of each test run is to uncover another error, a successful test run is one that causes your program to fail.

ACM Pacific NW Region Programming Contest 13 November 2004 Problem A: Mersenne Composite Numbers

Stratford upon Avon School Mathematics Homework Booklet

MATH 099 HOMEWORK TWO

HSBC Security Device: Troubleshooting guide

Crude Video Game Simulator Algorithm

Dept. of CSE, IIT KGP

1. Managing Information in Table

CSE 131 Introduction to Computer Science Fall Exam I

Excel Functions & Tables

Worlframalpha.com Facebook Report

Topic 1. Mrs. Daniel Algebra 1

SUMMER REVIEW PACKET 2 FOR STUDENTS ENTERING ALGEBRA 1

Assignment 2.4: Loops

Other Loop Options EXAMPLE

Chapter 5 : Repetition (pp )

Integrated Algebra Regents Exam 0109 Page 1

G.CO.C.9: Compound Statements

CS3 Midterm 1 Fall 2007 Standards and solutions

Geometric Probabiltiy

Section 3.2 Measures of Central Tendency MDM4U Jensen

Grade 5 Mathematics MCA-III Item Sampler Teacher Guide

AP Computer Science Homework Set 1 Fundamentals

Today in CS161. Lecture #7. Learn about. Rewrite our First Program. Create new Graphics Demos. If and else statements. Using if and else statements

4. Use a loop to print the first 25 Fibonacci numbers. Do you need to store these values in a data structure such as an array or list?

Algebra I Semester 1 Study Guide Create a sequence that has a common difference of 3

Control Statements: Part 1

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

Subject: Computer Science

Outline. Announcements. Homework 2. Boolean expressions 10/12/2007. Announcements Homework 2 questions. Boolean expression

2-2 Adding Integers. Warm Up Problem of the Day Lesson Presentation Lesson Quizzes

CS 303E Fall 2011 Exam 2 Solutions and Criteria November 2, Good Luck!

VARIABLE, OPERATOR AND EXPRESSION [SET 1]

[Page 177 (continued)] a. if ( age >= 65 ); cout << "Age is greater than or equal to 65" << endl; else cout << "Age is less than 65 << endl";

Properties of Operations

82 2 Loops and Lists. Exercise 2.6: Compute energy levels in an atom The n-th energy level for an electron in a Hydrogen atom is given by

Final Examination Semester 2 / Year 2010

4. Assignment statements Give an assignment statement that sets the value of a variable called total to 20: Answer: total = 20;

Computer Science Foundation Exam

Homework Set 1- Fundamentals

CSI Lab 02. Tuesday, January 21st

Chapter 3 Problem Solving and the Computer

Questions Frequently Asked by Consumers PartyLite Preferred Program Consumer Questions

B.2 Measures of Central Tendency and Dispersion

Page 1 CCM6+ Unit 10 Graphing UNIT 10 COORDINATE PLANE. CCM Name: Math Teacher: Projected Test Date:

mirosoft.

PLEXUS PAY PORTAL YOUR HOW-TO GUIDE

EXAM Computer Science 1 Part 1

Reviewing all Topics this term

Q1: Functions / 33 Q2: Arrays / 47 Q3: Multiple choice / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10

Math 20 Practice Exam #2 Problems and Their Solutions!

COURSE: Introduction to JAVA Programming GRADE(S): UNIT 1: Introduction to Applets NATIONAL STANDARDS: ALL STUDENTS

Identifying Slope and y-intercept slope y = mx + b

Task 1: Print a series of random integers between 0 and 99 until the value 77 is printed. Use the Random class to generate random numbers.

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Lesson 9: An Application of Linear Equations

Python lab session 1

Honors Computer Science Python Mr. Clausen Programs 4A, 4B, 4C, 4D, 4E, 4F

How to request, find and cancel room bookings in Resource Booker

Student Learning Center

Kharagpur Site Online Problems 2013

Absolute C++ 6th Edition Savitch TEST BANK Full download at:

Introduction to Object Oriented Systems Development. Practical Session (Week 2)

Create your first workbook

MICROSOFT DYNAMICS GP GENERAL LEDGER YEAR-END CLOSING PROCEDURES

District 5910 Website Quick Start Manual Let s Roll Rotarians!

Problem Solving for Intro to Computer Science

Midterm Exam Solutions March 7, 2001 CS162 Operating Systems

/ / / x means sum of scores and n =/ f is the number of scores. J 14. Data. Knowing More. Mean, Median, Mode

Transcription:

METHODS EXERCISES Write a method called GuessNumber that receives nothing and returns back nothing. The method first picks a random number from 1-100. The user then keeps guessing as long as their guess is wrong, and they've guessed less than 7 times. If their guess is higher than the number, say "Too high." If their guess is lower than the number, say "Too low." When they get it right, the game stops. Or, if they hit seven guesses, the game stops even if they never got it right and display a message for that. Sample run I'm thinking of a number between 1-100. You have 7 guesses. First guess: 50 Sorry, you are too low. Guess # 2: 75 Sorry, you are too low. Guess # 3: 87 Sorry, that guess is too high. Guess # 4: 82 Sorry, you are too low. Guess # 5: 84 You guessed it! What are the odds?!? Write a method called SumAll that receives one parameter and it returns back an integer value. The method asks the user to input several integer numbers limited by the parameter value or when the user input 0-. The method has to sum up all the integers they give you. The method returns back the total at the end. Sample Run I will add up the numbers you give me. Number: 6 The total so far is 6 Number: 9 The total so far is 15 Number: -3 The total so far is 12 Number: 2

The total so far is 14 Number: 0 The total is 14. Write a method called BankATM that mimic the Bank s ATM machine login process. The method takes no parameters and it returns back a string. You have to ask the user to input his PIN account, the user has a maximum of 3 tries to guess a PIN. If he failed to guess the PIN then you have to return a message that telling him that his bank account is locked, otherwise, return a message that welcome the user to the main menu. Write a method called Total that receives one int parameter and it returns back a long value. The method calculates the sum of 1, 2, 3,..., to an upper bound (the passed parameter) and returns it back. Write a method called Average that receives one integer parameter that represents number of items. The method returns back a double value that represents the average total over items-. The total can be calculated using a call to the method Total above. Sample output The sum is 5050 The average is 50.5 A positive integer is a prime if it is divisible by 1 and itself only. Write a method called isprime that takes a positive integer and returns true if the number is a prime or false otherwise. Write a method called PrimeList that takes no parameters and it returns back a double. The method prompts the user for an upper bound (a positive integer), and lists-outputs- all the primes less than or equal to it. Also the

method calculates the percentage of prime numbers among the whole numbers (up to 2 decimal places). Sample output Please enter the upper bound: 10000 1 2 3...... 9967 9973 [1230 primes found (12.30%)] Write a method isproductofprimefactors that takes a positive integer, and it returns true if the product of all its prime factors (excluding 1 and the number itself) is equal to its value. For example, the method returns true for 30 (30=2 3 5) and false for 20 (20 2 5). You may need to use the isprime method in the previous exercise. Write a program called PerfectPrimeFactorList that receives an integer parameter which represents an upper bound and it returns nothing. The method shall display all the numbers (less than or equal to the upper bound) that meets the above criteria - isproductofprimefactors-. The method also finds out how many prime numbers that satisfy the criteria and their percentage. Sample output Enter the upper bound: 100 These numbers are equal to the product of prime factors: 1 6 10 14 15 21 22 26 30 33 34 35 38 39 42 46 51 55 57 58 62 65 66 69 70 74 77 78 82 85 86 87 91 93 94 95 [36 numbers found (36.00%)

(Conversions between Celsius and Fahrenheit) Write a project that has a class which contains the following two methods: /** Convert from Celsius to Fahrenheit */ public static double celsiustofahrenheit (double celsius) /** Convert from Fahrenheit to Celsius */ public static double fahrenheittocelsius (double fahrenheit) The formula for the conversion is: fahrenheit = (9.0 / 5) * celsius + 32 celsius = (5.0 / 9) * (fahrenheit 32)

Source: Check the following website.

Monthly Budget Write a method called MonthlyBudget that receives one double parameter which represent the user budgeted for a month and it returns back a boolean value. Inside the method, write a loop that prompts-asks- the user to enter each of his or her expenses for the month Think about what could break this loop?-. During the reading of the expenses calculate the expenses total. When the loop finishes, the method returns back a true if the user over the budget, or false if his expenses is under budget. Book Club Reward Serendipity Booksellers has a book club that awards points to its customers based on the number of books purchased each month. The points are awarded as follows: If a customer purchases 0 books, he or she earns 0 points. If a customer purchases 1 book, he or she earns 5 points. If a customer purchases 2 books, he or she earns 15 points. If a customer purchases 3 books, he or she earns 30 points. If a customer purchases 4 or more books, he or she earns 60 points. Write a method called BookClubReward that takes no parameters and it returns back an integer. Inside the method, asks the user to enter the number of books that he or she has purchased from January to December. The method then returns back the number of points awarded during the whole year. Bank Fees A bank account charges $10 per month plus the following check fees for a commercial checking account: $.10 each for fewer than 20 checks $.08 each for 20-39 checks $.06 each for 40-59 checks $.04 each for 60 or more checks Write a method called BankFees that takes no parameter and it returns back a double. The method asks the client for the number of checks written for the month. The method should then calculate and returns back the bank's services fees for the month. Sales Write a program that asks the user to input how many weeks first. After that the program iterates over these weeks week by week- and asks the user to input the dollar amount of sales of every week. The program should display the following:

The average daily sales for each week The total sales for all of the weeks The average weekly sales The week number that had the highest amount of sales The week number that had the lowest amount of sales Sample output: Enter how many weeks: 3 Enter Week #1 sales: $12092.75 Average daily sales for week #1: $1727.5357142857142 Enter Week #2 sales: $27461.0 Average daily sales for week #2: $3923.0 Week #3 sales: $12058.34 Average daily sales for week #3: $1722.6200000000001 Total sales for all weeks: $51612.09 Average weekly sales: $17204.03 The highest sales was: $27461.0 The lowest sales were made during 12058.34 Source: https://www.leveluplunch.com/java/exercises/

Method with Strings (Not covered yet) Exercise WordGuess: Write a method called WordGuess that receives one parameter of data type string. The method try guess the word by trying to guess the individual characters. Your program shall look like: Key in one character or your guess word: t Trail 1: t t Key in one character or your guess word: g

Trail 2: t t g Key in one character or your guess word: e Trail 3: te_t g Key in one character or your guess word: testing Trail 4: Congratulation! You got in 4 trials Method with Arrays (Not covered yet) The "key" array is an array containing the correct answers to an exam, like {"a", "a", "b", "b"}. the "answers" array contains a student's answers, with "?" representing a question left blank. The two arrays are not empty and are the same length. Return the score for this array of answers, giving +4 for each correct answer, -1 for each incorrect answer, and +0 for each blank answer. scoreup(["a", "a", "b", "b"], ["a", "c", "b", "c"]) 6 scoreup(["a", "a", "b", "b"], ["a", "a", "b", "c"]) 11 scoreup(["a", "a", "b", "b"], ["a", "a", "b", "b"]) 16 You can try that using eclipse or the click this link Given an array of strings, return a new List (e.g. an ArrayList) where all the strings of the given length are omitted. See wordswithout() below which is more difficult because it uses arrays. wordswithoutlist(["a", "bb", "b", "ccc"], 1) ["bb", "ccc"] wordswithoutlist(["a", "bb", "b", "ccc"], 3) ["a", "bb", "b"] wordswithoutlist(["a", "bb", "b", "ccc"], 4) ["a", "bb", "b", "ccc"] You can try that using eclipse or the click this link Given an array of positive ints, return a new array of length "count" containing the first even numbers from the original array. The original array will contain at least "count" even numbers. copyevens([3, 2, 4, 5, 8], 2) [2, 4] copyevens([3, 2, 4, 5, 8], 3) [2, 4, 8] copyevens([6, 1, 2, 4, 5, 8], 3) [6, 2, 4]

You can try that using eclipse or the click this link