Week 15
Project presentations, exam review.
Exam review
The exam is comprehensive – see also review for first exam, from week 7. You’ll have to use more methods/functions than last time. I’ll include a list of all methods you might need, but remember you can also bring 1 sheet of paper with notes.
Key concepts (in addition to concepts covered on midterm):
- Defining and using functions
- Using objects in Java, especially Strings
- Arrays: 1- and 2-dimensional
- Input and output. You won’t have to memorize specific classes to use, but you should be able to recognize what’s going on.
- Error handling. Recognize how exceptions are handled.
- Basic object-oriented programming. Know what an instance variable and instance method are, and how they’re different from static methods.
Example problems:
- Define a function called
rep
that takes a Strings
and an integern
as input and printss
repeatedn
times. Example:rep("x", 5)
prints xxxxx. - Define a function that takes an array of integers as input and returns the second largest value in the array.
- Define a function that takes an array of Strings as input and returns the longest.
- Define a function that takes two Strings as input and returns the number of characters where they match.
- Define a function that takes a 2-dimensional array of integers as input, and returns the average of all entries.
- Define a function that takes a 2-dimensional array of integers as input, and returns an array containing the average of each input row.
- Design a class that represents a triangle. What instance variables do you need? What should you check when creating a Triangle object to make sure the input is valid?
- Consider the following class:
public class Point { final int x; final int y; public Point(int x, int y) { this.x = x; this.y = y; } }
Define a method that takes integers
xOffset
andyOffset
and returns a newPoint
that is shifted from the original by the given values. - Design a class that represents a gun, including keeping track of how many
bullets the gun has left. Include a method called
shoot
that fires the gun. - What is the output of the following program?
int[] a = {0,1,2,3,4}; try { System.out.println(a[5]); } catch (IndexOutOfBoundsException e) { System.out.println("Bad array index"); } try { System.out.println(a[1]); } catch (IndexOutOfBoundsException e) { System.out.println("Bad array index"); }
- What is the output of the following program?
int[][] a = { {1, 2, 3}, {4, 5, 6} }; for (int[] row : a) { for (int entry: row) { if (entry % 2 == 0) System.out.print("even "); else System.out.print("odd "); } System.out.println(); }
- Write a program that uses a
Scanner
object to read in 10 integers and print them in reverse order of how they were read in (meaning printing the last integer read in first). - Write a program that uses a
Scanner
object to read in 10 integers and prints only the integers that are multiples of 5. - Write a program that uses a
Scanner
object to read in 10 integers and prints the product of the odd integers.
Reading
Java for Everyone, review chapters 1-8.
Homework
- Finish your project! Email to me before next class.
- Study for next week’s final exam.