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

  Design a class that can be used to hold information

Implement and design a class that can be used to hold information about a musical note - Prepare a complete java program the default constructor should set a note to a middle C quarter note.

  Addressed with attributable risk

Which of the following questions is addressed with attributable risk?

  Define render a model scene in java

Render a Model Scene in Java - Imagine you are prototyping animation for a Jurassic Park Movie

  Design and implementation of a hangman game

Design and Implementation of a Hangman game

  Write a program to find solution tocryparithmetic puzzle

Write a program (Crypta.java) that finds a solution to the cryparithmetic puzzle: TOO + TOO + TOO+ TOO = GOOD

  Java program on eclipse

Using a while( true ) loop,and using the upper case alphabeth from Z to A,print the lower case alphabeth and its corresponding ascii values.You must terminate/exit/break this loop once you process the last letter (A).

  Explain how a borderlayout manages its display area

Write a Java statement to make the window associated with the object from question 1 visible. Explain how a BorderLayout manages its display area. Explain how a FlowLayout manages its display area

  Write an application to calculate the salespersons salary

Write an application that prompts the user to enter the sales for each employee, then it calculates the salesperson's salary. The process repeats until the user finishes entering all employees' information.

  Java application to declare and create a vector

Write a java application that perform the following task: declare and create a vector with initial size of 10, increment of 2

  Adt for a two color double stack adt consists of two stacks

design an adt for a two color double stack adt that consists of two stacks one red and one blue and has its operations color coded versions of the regular stack adt operations.

  Explain the concept of dma with cpu overhead

Explain the concept of DMA with CPU overhead

  Write a bag class with a generic type

Write a Bag class with a generic type. Write a class such as item that has a name and price. Add objects of item to the bag and find the average and the total of the price of all items in the bag.

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