Need help with these easy programing questions, thank you! This is everything i
ID: 3860867 • Letter: N
Question
Need help with these easy programing questions, thank you! This is everything i was given the first problem doesn't have to be a whole program just a method that does what it is asking, which i dont get what it is asking which is why i am asking chegg. This is all the question says though.
For this exercise, please write two methods using while loops.
1. The first method should be named problem1 and take in an int start as a parameter.
Problem1 should find the first number below zero in the sequence: start -1, start -2, start -4, start -8 ... start - iteration.
An example: problem1(5) should generate "The first number below 0 in this sequence starting at 5 is: -3"
2. Write a method problem2 that will use a random object and a scanner. The while loop for this part should run until the user enters the same number as is chosen that iteration of the while loop. Every time the loop executes a different number should be chosen from the random object. It is good to make the range for the numbers small for testing. Make sure to include proper prompts and information prints for the user.
Explanation / Answer
These are the most common object oriented approach for both of your problems!
void problem1 (int start) {
int result;
int n = 1;
do {
result = start - n;
n = n*2;
}while (result >= 0);
System.out.println("Result is: " + result);
}
void problem2() {
Random random = new Random();
Scanner in = new Scanner(System.in);
boolean loop = false;
while (!loop) {
int rand = random.nextInt();
int guess = in.nextInt();
loop = rand == guess;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.