Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write method named isDivisible that takes two integers, n and m, and returns true if n is divisible by m, and false otherwise. 2. If

Write method named isDivisible that takes two integers, n and m, and returns true if n is divisible by m, and false otherwise.


2. If you are given three sticks, you may or may not be able to arrange them in a triangle. For example, if one of the sticks is 12 inches long and the other two are one inch long, you will not be able to get the short sticks to meet in the middle. For any three lengths, there is a simple test to see if it is possible to form a triangle: If any of the three lengths is greater than the sum of the other two, you cannot form a triangle.


Write method named isTriangle that takes three integers as arguments and returns either true or false, depending on whether you can or cannot form a triangle from sticks with the given lengths. The point of this exercise is to use conditional statements to write a value method.


3. What is the output of the following program?


public static void main(String[] args) {
boolean flag1 = isHoopy(202);
boolean flag2 = isFrabjuous(202);
System.out.println(flag1);
System.out.println(flag2);
if (flag1 && flag2) {
System.out.println("ping!");
}
if (flag1 || flag2) {
System.out.println("pong!");
}
}
public static boolean isHoopy(int x) {
boolean hoopyFlag;
if (x % 2 == 0) {
hoopyFlag = true;
} else {
hoopyFlag = false;
}
return hoopyFlag;
}
public static boolean isFrabjuous(int x) {
boolean frabjuousFlag;
if (x > 0) {
frabjuousFlag = true;

 

 

} else {
frabjuousFlag = false;
}
return frabjuousFlag;
}


4. Write recursive method named oddSum that takes a positive odd integer n and returns the sum of odd integers from 1 to n. Start with base case and use temporary variables to debug your solution. You 

might find it helpful to print the value of n each time oddSum is
invoked.} else {
frabjuousFlag = false;
}
return frabjuousFlag;
}


4. Write recursive method named oddSum that takes a positive odd integer n and returns the sum of odd integers from 1 to n. Start with a base case and use temporary variables to debug your solution. You might find it helpful to print the value of n each time oddSum is?

Step by Step Solution

3.53 Rating (146 Votes )

There are 3 Steps involved in it

Step: 1

1 Method isDivisible java public static boolean isDivisibleint n int m return n m 0 2 Method isTrian... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advanced Accounting

Authors: Gail Fayerman

1st Canadian Edition

9781118774113, 1118774116, 111803791X, 978-1118037911

More Books

Students also viewed these Programming questions