1. Consider the following code which is supposed to subtract a value passed to t
ID: 3843039 • Letter: 1
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. 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)
Explanation / Answer
1) Problem1.java:
public class Problem1 {
private int number = 542;
public void subtractNumbers (int number)
{
this.number = this.number - number;
System.out.println("The local variable is: " + number);
System.out.println("The instance variable is: " +this.number);
}
public static void main(String[] args) {
Problem1 pb = new Problem1();
pb.subtractNumbers(235);
}
}
Sample Input and Output:
The local variable is: 235
The instance variable is: 307
2) SomeClass.java:
public class SomeClass {
public SomeClass(int value){
System.out.println("The given int value is: "+value);
}
public SomeClass(double val){
System.out.println("The given float value is: "+val);
}
public SomeClass(int v1,double v2){
System.out.println("The int value: "+v1+" and double value: "+v2);
}
public static void main(String[] args) {
SomeClass someObject1 = new SomeClass(23);
SomeClass someObject2 = new SomeClass(98.6);
SomeClass someObject3 = new SomeClass(23,98.6);
}
}
Sample Input and Output:
The given int value is: 23
The given float value is: 98.6
The int value: 23 and double value: 98.6
3) RemainderCalculation.java:
import java.util.Scanner;
public class RemainderCalculation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter two integer values:");
int num1 = sc.nextInt();
int num2 = sc.nextInt();
if(num2>num1){
int temp = num1;
num1 = num2;
num2 = temp;
}
RemainderCalculation rc = new RemainderCalculation();
rc.findRemainder(num1,num2);
}
public void findRemainder(int a,int b){
int rem = a % b;
System.out.println("The remainder is: "+rem);
}
}
Sample Input and Output:
Enter two integer values:
234 38
The remainder is: 6
4) CalculateArea.java:
public class CalculateArea {
final double base = 2;
public double calcTriangleArea(double height){
double areaTriangle = (base * height)/2;
return areaTriangle;
}
public static void main(String[] args) {
CalculateArea someObject = new CalculateArea();
double triangleArea = someObject.calcTriangleArea(14);
System.out.println("Area of a triangle is: "+triangleArea);
}
}
Sample Input and Output:
Area of a triangle is: 14.0
5) TipFinding.java:
import java.util.Scanner;
public class TipFinding {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
TipFinding t = new TipFinding();
System.out.println("Enter the tip percentage between 15 and 20:");
double percent = sc.nextDouble();
System.out.println("Enter mealCost: ");
double mealCost = sc.nextDouble();
double tip_amount = t.calcTip(percent,mealCost);
System.out.println("Tip amount @ "+percent+"% cost on meal is: "+tip_amount);
System.out.println("Enter mealCost: ");
double mealCos = sc.nextDouble();
double tip_amt = t.calcTip(mealCos);
System.out.println("Tip amount @ "+17.5+"% cost on meal is: "+tip_amt);
}
public double calcTip(double percent, double mealCost) {
double tip_amount = (percent * mealCost)/100;
return tip_amount;
}
public double calcTip(double mealCost) {
double percent = 17.5;
double tip_amount = (percent * mealCost)/100;
return tip_amount;
}
}
Sample Input and Output:
Enter the tip percentage between 15 and 20:
16
Enter mealCost:
450
Tip amount @ 16.0% cost on meal is: 72.0
Enter mealCost:
580
Tip amount @ 17.5% cost on meal is: 101.5
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.