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