Write a static method named showtwos

Assignment Help JAVA Programming
Reference no: EM13851725

While Loop Mystery
1. Consider the following method. For each call below, indicate what output is produced.

public static void mystery1(int x) {

    int y = 1;

    int z = 0;

    while (2 * y <= x) {

        y *= 2;

        z++;

    }

    System.out.println(y + " " + z);

}

Method Call             Output Produced

 

mystery1(1);            _______________

 

mystery1(6);            _______________

 

mystery1(19);           _______________

 

mystery1(39);           _______________

 

mystery1(74);           _______________

2. Consider the following method. For each call below, indicate what output is produced.

public static void mystery2(int x) {

    int y = 0;

    while (x % 2 == 0) {

        y++;

        x /= 2;

    }

    System.out.println(x + " " + y);

}

Method Call             Output Produced

 

mystery2(19);           _______________

 

mystery2(42);           _______________

 

mystery2(48);           _______________

 

mystery2(40);           _______________

 

mystery2(64);           _______________

3.   Consider the following method.  For each call below, indicate what value is returned.

public static int mystery3(int x, int y) {

    while (x != 0 && y != 0) {

        if (x < y) {

            y -= x;

        } else {

            x -= y;

        }

    }

    return x + y;

}

Method Call             Value Returned

 

mystery3(3, 3);         _______________

 

mystery3(5, 3);         _______________

 

mystery3(2, 6);         _______________

 

mystery3(12, 18);       _______________

 

mystery3(30, 75);       _______________

While Loop Programming

4. Write a static method named showTwos that shows the factors of 2 in a given integer. For example, the following calls produce the following output:
Call Output
showTwos(7); 7 = 7
showTwos(18); 18 = 2 * 9
showTwos(68); 68 = 2 * 2 * 17
showTwos(120); 120 = 2 * 2 * 2 * 15

The idea is to express the number as a product of factors of 2 and an odd number. The number 120, for example, has 3 factors of 2 multiplied by the odd number 15. For odd numbers (as in the first example of 7), there are no factors of 2, so you just show the number itself. Assume that your method is passed a number greater than 0.
Test your code with the following code file:
public class TestShowTwos {
public static void main(String[] args) {
showTwos(7); // 7 = 7
showTwos(18); // 18 = 2 * 9
showTwos(68); // 68 = 2 * 2 * 17
showTwos(120); // 120 = 2 * 2 * 2 * 15
}

// your code goes here
}

5. Write a static method named gcd that accepts two integers as parameters and returns the greatest common divisor (GCD) of the two numbers. The greatest common divisor of two integers a and b is the largest integer that is a factor of both a and b. The GCD of any number and 1 is 1, and the GCD of any number and 0 is that number. One efficient way to compute the GCD is to use Euclid's algorithm, which states the following:
GCD(a, b) = GCD(b, a % b)
GCD(a, 0) = Absolute value of a
For example, gcd(24, 84) returns 12, gcd(105, 45) returns 15, and gcd(0, 8) returns 8.
Test your code with the following code file:
public class TestGCD {
public static void main(String[] args) {
System.out.println("GCD of 27 and 6 is " + gcd(27, 6)); // 3
System.out.println("GCD of 24 and 84 is " + gcd(24, 84)); // 12
System.out.println("GCD of 38 and 7 is " + gcd(38, 7)); // 1
System.out.println("GCD of 45 and 105 is " + gcd(45, 105)); // 15
System.out.println("GCD of 1 and 25 is " + gcd(1, 25)); // 1
System.out.println("GCD of 25 and 1 is " + gcd(25, 1)); // 1
System.out.println("GCD of 0 and 14 is " + gcd(0, 14)); // 14
System.out.println("GCD of 14 and 0 is " + gcd(14, 0)); // 14
}

// your code goes here

}


Boolean Logic
6. Write a method named season that takes two integers as parameters representing a month and day, and that returns a String indicating the season for that month and day. Assume that months are specified as an integer between 1 and 12 (1 for January, 2 for February, and so on) and that the day of the month is a number between 1 and 31.
For this problem, winter occurs between 12/16 and 3/15. Spring is between 3/16 and 6/15. Summer is between 6/16 and 9/15. Fall is between 9/16 and 12/15.

Random Numbers
7. Write a method randomWalk that performs a random one-dimensional walk, reporting each position reached and the maximum position reached during the walk. The random walk should begin at position 0. On each step, you should either increase or decrease the position by 1 (with equal probability). The walk stops when 3 or -3 is hit. The output should look like this:
position = 0
position = 1
position = 0
position = -1
position = -2
position = -1
position = -2
position = -3
max position = 1


8. Write code that prints a random number of lines between 2 and 10 lines inclusive, where each line contains a random number of 'x' characters between 5 and 20 inclusive. For example:
xxxxxxx
xxxxxxxxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxx
xxxxxxxxxxx
xxxxxxxxxxxxxxxx

9. Write an interactive program that prompts for a desired sum, then repeatedly rolls two six-sided dice until their sum is the desired sum. Here is the expected dialogue with the user:

Desired dice sum: 9
4 and 3 = 7
3 and 5 = 8
5 and 6 = 11
5 and 6 = 11
1 and 5 = 6
6 and 3 = 9

Finish the following code file:

import java.util.*;

public class TestDiceRoll {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.print("Desired dice sum: ");
int sum = console.nextInt();

// your code here
}
}

Reference no: EM13851725

Questions Cloud

Consideration when seeking an efficient allocation : On the three issues that economics suggests are worth discussing or consideration when seeking an efficient allocation. Discuss these against the backdrop of the ensuing principles or themes that emanate from them as well as how these relate to the s..
Solve equilibrium interest rate-equilibrium for real output : Derive the IS relation. Solve the equilibrium for real output. Solve the equilibrium interest rate. Now suppose that the money supply increases to M/P = 1840. Solve for Y, i, c and T, and describe in words the effects of an expansionary monetary poli..
Analyze the benefits of using neural networks : Assignment: Prepare a response to the following discussion topic: Analyze the benefits of using neural networks, fuzzy logic, and intelligent agents in a business setting
Economists descriptive statements-propositions-predictions : Should one distinguish between economist’s descriptive statements, propositions, and predictions about the world, and their statements about what policies should be adopted?
Write a static method named showtwos : Write a static method named showTwos
What is the total amount of interest anthony : Anthony is considering the purchase of used car. The price, including the title and taxes, is $9,530. Anthony is able to make a $2,530 down payment. The balance, $7,000, will be borrowed from the Credit Union at an interest rate of 9.25% compounded d..
Consider the game of the battle of sexes : Consider the game of the battle of sexes. How would you modify the payoffs to (F,O) and (O,F) to reflect the following: Namely, the husband is unhappiest when he is at the opera by himself, he is a little happier when he is with his wife at the opera..
Nash equilibrium in mixed strategy and equilibrium payoff : Reduce the game by IEDS as far as you can. For the original game, find all Nash equilibrium in pure strategy and the equilibrium payoff. For the original game, find all Nash equilibrium in mixed strategy and the equilibrium payoff.
How would the photography affect day-to-day store operations : If done in-house, what equipment would be required and what costs would be incurred? How would the photography affect the day-to-day store operations under each model

Reviews

Write a Review

JAVA Programming Questions & Answers

  Describe how an eavesdropped can gain access

Describe how an eavesdropped can gain access to the remote server with a relatively modest number of guesses ( Hint:  The eavesdropped starts guessing after the original user has typed all but one character of the password.

  Explain where the following method invocations

Explain where the following method invocations are most likely to be found in a program for dealing with census data organized using the Model, View, Controller (MVC) design pattern. Choices are zero or more of Model, View, and Controller. Be sure to..

  Develop a java program

Develop a Java program which, given the width, length and depth (in metres), of a swimming pool, determines and outputs.

  Create a class safestack that implements a stack of strings

Create a class SafeStack that implements a stack of strings

  Write a simple java program that will read an array of info

write a simple java program that will read an array of information containing the information (Name, offence [between arson, assault or theft] and year of offence

  Discuss the use of a binary tree

Discuss the use of a binary tree when searching for keys in an array. Discuss the use of a binary tree when searching for keys in a linked list

  Write the definition of the class inventory

Write the definition of the class Inventory such that object of this class can store an item's id, name, number of pieces in stock, manufacturer's price, and selling price. The class should include constructors, setters, getters, and toString meth..

  What ways can vulnerable inmates be protected

What ways can vulnerable inmates be protected? Why are classification and housing assignments so critical to these populations?

  Creat a class that saves the grades

Creat a class that saves the grades of students in an array, and contains a few methods that do some statistics on these grades. Your task is to create this class, and create client testing class that does all the necessary tests of its methods.

  Java program to decide whether integer is perfect number

For example, 6 is a perfect number because 6 = 1 + 2 + 3. Write a Java program that decides whether integer is a perfect number.

  A program that reads a four-digit number from the keyboard

Write a program that reads a four-digit number from the keyboard as a string and then converts it into decimal. For example, if the input is 1100, the output should be 12. Hint: Break the string into characters and then convert each character to a va..

  Write a statement that prints the number of characters

Write a statement that prints the number of characters in a String object called myString. Write statements to prompt for and read user's age using Scanner variable

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd