Ask Java Expert


Home >> Java

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
}
}

Java, Programming

  • Category:- Java
  • Reference No.:- M91421499
  • Price:- $25

Priced at Now at $25, Verified Solution

Have any Question?


Related Questions in Java

Chatbotscreate a small networked chat application that is

Chatbots Create a small, networked chat application that is populated by bots. Introduction On an old server park, filled with applications from the early days of the internet, a few servers still run one of the earliest ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

In relation to javaa what is constructor the purpose of

(In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C. What are static variables and instance variables? D. Compar ...

Project descriptionwrite a java program to traverse a

Project Description: Write a java program to traverse a directory structure (DirWalker.java) of csv files that contain csv files with customer info. A simple sample in provided in with the sample code but you MUST will r ...

Fundamentals of operating systems and java

Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

Assessment -java program using array of Assessment -JAVA Program using array of objects

Assessment -JAVA Program using array of objects Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed G ...

Applied software engineering assignment 1 -learning

Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As