Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Lab 5 Lab 5: More Functions and Conditionals in Java In this lab, you will practice more using functions and conditional statements in Java. Feel free to work in pairs. Exercise 1: More Functions Practice This part is continuing to add to the Practice.java file from Lab 4. You can add to your prior code, or start from the posted solution to last lab. 1. Define a function named add4 which takes 4 integers and returns the sum of the four integers. For example, the line: int sum = add4(1, 2, 3, 4); should assign 10 to sum. Note that this function add4 must use add2 defined earlier above. Verify the result of your function calls by printing the value of sum using System.out.println in the caller, again perhaps main. 2. Define a function named add4addStrings which takes 4 integers and three strings, and returns the 'sum' of the seven values. For example, the line: String sum7 = add4addStrings(1, 2, 3, 4, "Apple", "Orange", "Kiwi"); should assign "10AppleOrangeKiwi" to sum7. Note that this function add4addStrings must use add4 and addStrings defined earlier above. Verify the result of your function calls by printing the value of sum7 using System.out.println in the caller, again the same main. 3. Define a function named addStringsAdd4 which takes 3 strings and 4 integers, and returns the 'sum' of the seven values. For example, the line: String sum7_2 = addStringsAdd4("Apple", "Orange", "Kiwi", 1, 2, 3, 4); should assign "AppleOrangeKiwi1234" to sum7_2. Note that this function addStringsAdd4 can use addStrings defined earlier above, but cannot use add4 defined earlier above. Also think about why I used the variable name sum7_2 instead of just using sum7 again assuming that both sum7 and sum7_2 are declared in the same function, i.e., main. I could've used sum7 again without declaring a new variable, in which case the previous value of sum7 would have been lost, which would have been okay since we are not using that value again any more. I am writing this to make you think about how variables are used and what declaring a variable means in this context. Verify the result of your function calls by printing the value of sum7_2 using System.out.println in the caller, again the same main. Exercise 2: The Collatz Problem In 1937, Luthar Collatz proposed the following algorithm: Given a positive integer input n, if n is odd, the next Collatz number is obtained by multiplying it by three and add one. If n is even, the next Collatz number is obtained by dividing it by two. He noticed that no matter what number he started with, repeating this procedure eventually produced the number 1. To this day, every number checked eventually reaches 1 although people are yet to prove why. For this exercise, implement a class named Collatz. For today, you will start by writing a main() that uses a Scanner to read an integer from the console. You will also write a method nextCollatz that takes an integer argument and returns an integer. The main() method will pass the integer read to it. The method will compute the next Collatz number according to the algorithm above and then return it to the caller. main will then print the result of the nextCollatz() call to the console. It will help to build your code incrementally here: Define isEven and make sure it is working correctly by calling it from main a few times with different input parameters, making sure that you are calling it at least twice each producing a different result. Define nextCollatz and make sure it is working correctly by calling it from main a few times: once with an even integer and once more with an odd integer. Now write a method listCollatz which takes one integer input and displays the sequence of Collatz numbers, starting with the input and ending with 1. This method should use nextCollatz to produce an entire Collatz sequence. For example, listCollatz(13) should produce: 13 40 20 10 5 16 8 4 2 1