Practice Problem 1
File problem1.java contains an incomplete program. The goal of the program is to go through an array of integers, and print out all integers that are greater than or equal to 50.
Complete that program, by defining a print_large function, that satisfies the following specs:
- Function print_large takes one argument, called array, which is an array of integers.
- For each integer in the array, the function prints out that integer if it is greater than or equal to 50.
If you implement your function correctly, the program should print:
105
82
60
Practice Problem 2
File problem2.java contains an incomplete program. The goal of the program is similar to that of Problem 1: we want to go through an array of integers, and print out all integers that are greater than or equal to 50. However, here we do it a little bit differently: we do not hardcode the value 50 in the print_large function, but we pass it in as an argument.
Complete that program, by defining a print_large function, that satisfies the following specs:
- Function print_large takes two arguments, called array, threshold.
- Argument array is an array of integers.
- Argument threshold is an integer.
- For each integer in the array, the function prints out that integer if it is greater than or equal to threshold.
If you implement your function correctly, the program should print:
105
82
60
Practice Problem 3
File problem3.java contains an incomplete program. The goal of the program is similar to that of Problems 1 and 2: we want to go through an array of integers, and print out all integers that are greater than or equal to 50. However, here we do it in a different way, by writing two functions: keep_large and print_array_list
Define a keep_large function, that satisfies the following specs:
- Function keep_large takes two arguments, called array, threshold.
- Argument array is an array of integers.
- Argument threshold is an integer.
- The function returns an array list of integers. For each integer in the array, the function adds that integer to the result if the integer is greater than or equal to threshold.
Define a print_array_list function, that satisfies the following specs:
- Function print_array_list takes one argument, called list, which is an array list of integers.
- The function prints out every integer in the list, one integer per line.
.
If you implement your two functions correctly, the program should print:
105
82
60
Practice Problem 4
File problem4.java contains an incomplete program. The goal of the program is a little different from that of Problems 1, 2, 3. Here the data is stored in an array list, instead of an array. We still want to go through the data in that array list, and print out all integers that are greater than or equal to 50.
You should reuse the print_array_list function from the previous problem.
You should define a new keep_large function, different from your solution in the previous problems, that satisfies the following specs:
- Function keep_large takes two arguments, called list, threshold.
- Argument list is an array list of integers.
- Argument threshold is an integer.
- The function returns an array list of integers. For each integer in the array, the function adds that integer to the result if the integer is greater than or equal to threshold.
If you implement your function correctly, the program should print:
105
82
60
Practice Problem 5
File problem5.java contains an incomplete program. The goal of the program is to read integers stored in a file, and print out all integers that are greater than or equal to 50.
You should reuse the print_array_list and keep_large functions from previous problems.
You should define a new read_numbers function, that satisfies the following specs:
- Function read_numbers takes one argument, a string that specifies the name of a file.
- The function returns an array list of integers stored in that file. You can assume that the file contains only integers, one integer per line.
If you implement your function correctly, using file numbers1.txt, the program should print:
105
82
60
Practice Problem 6
Write a function reverse_lines(filename)
that:
- Takes as argument a string
filename
, specifying the name of the file.
- Reverses the order of lines in the file.
For example, suppose that, right before calling the function, file file1.txt
has these contents:
hello
today is
Tuesday
Then, after calling reverse_lines("file1.txt")
, file file1.txt
should have these contents:
Tuesday
today is
hello