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

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

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