Question: 1. Consider the following code which is supposed to subtract a value passed to the method from an instance variable; however, there is a problem.

1. Consider the following code which is supposed to subtract a value passed to the method from an instance variable; however, there is a problem. Re-write this code, without renaming any variable names, to resolve the problem.

private int number = 542; public void subtractNumbers (int number) { number = number - number; System.out.println("The local variable is: " + number); System.out.println("The instance variable is: " + number); }

2. Consider the following segment of code.

SomeClass someObject1 = new SomeClass(23); SomeClass someObject2 = new SomeClass(98.6); SomeClass someObject3 = new SomeClass(23, 98.6);

Write complete constructors for each of these statements including the assignment of variables.

3. Write a class method called findRemainder that will accept two integer values and calculate and print their remainder after division. (6 points)

4. Consider the following statement.

double triangleArea = someObject.calcTriangleArea(14)

Write an instance method that calculates the area of a triangle using a class constant of 2 feet for the base of a triangle. The value passed to the method represents the height of the triangle in feet. (6 points)

5. A waiter or waitress serving an individual table at a restaurant is usually tipped between 15 and 20% of the cost of the meal. However, some restarurants don't leave the amount of the tip up to the customer and charge a fixed percentage of the meal. First, complete the calcTip() method shown below for a party of two. Then, Write a complete overloaded method called calcTip which calculates the tip at the rate of 17.5%. (Note: You will submit two methods for this question.)

//post condiditon: returns the amount of the tip public double calcTip(double percent, double mealCost) { //complete the code for the first method here } //second method to be written here

(6 points)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!