Question

In: Computer Science

Question 1 Consider the method displayRowOfCharacters that displays any given character the specified number of times...

Question 1

Consider the method

displayRowOfCharacters

that displays any

given character the specified number of times on one line. For example, the call:

displayRowOfCharacters('*', 5);

produces the line: *****
Implement this method in Java by using recursion.

Question 2

Write a method that asks the user for integer input that is between 1 and 10, inclusive. If the input is out of range, the method should recursively ask the user to enter a new input value.

HELP ME PLEASE.

Solutions

Expert Solution

Question 1

Raw code:

//class name

class Main {

//main

public static void main(String[] args) {

//calling static method

displayRowOfCharacters('*', 5);

}

//method to display characters s of count k

public static void displayRowOfCharacters(char s,int k) {

//codition to reutrn nothing when k is 0

if (k ==0) {

return ;

}

//if k is greater than 0

else {

//print the characters

System.out.print(s);

//call the function recursively

displayRowOfCharacters(s, k-1);

}

}

}

Editor:

output:

Question 2

Raw code:

import java.util.Scanner;

//class name

class Main {

//main

public static void main(String[] args) {

//calling static method

ask_input();

}

//method to ask input between 1 and 10 inclusinve

public static int ask_input(){

//object to Scanner

Scanner sc = new Scanner(System.in);

//prompt

System.out.println("Entr value between 1 and 10 : ");

//get input from user

int input=sc.nextInt();

//checking input condition

if(input>=1 &&input<=10){

//if satisfied return input

return input;

}

//else recursively ask for the input

else{

System.out.print("Please enter between 1 and 10 ");

return ask_input();

}

}

}

Editor:

Hope this helps you! If you still have any doubts or queries please feel free to comment in the comment section.

"Please refer to the screenshot of the code to understand the indentation of the code".

Thank you! Do upvote.


Related Solutions

Q(1): An object moving vertically is at the given heights at the specified times. Find the...
Q(1): An object moving vertically is at the given heights at the specified times. Find the position equation s = 1/2at2 + v0t + s0 for the object At t = 1 second, s = 141 feet t = 2 seconds, s = 93 feet t = 3 seconds, s = 13 feet Q(2): Find the equation y = ax2 + bx + c of the parabola that passes through the points. To verify your result, use a graphing utility...
An object moving vertically is at the given heights at the specified times. Find the position...
An object moving vertically is at the given heights at the specified times. Find the position equation for the object. s=(1/2)at2 + v0t + s0 At t = 1 second, s = 151 feet At t = 2 seconds, s = 103 feet At t = 3 seconds, s = 23 feet
Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase.
# PYTHONWrite a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase.Ex: If the input is:n Mondaythe output is:1Ex: If the input is:z Today is Mondaythe output is:0Ex: If the input is:n It's a sunny daythe output is:2Case matters.Ex: If the input is:n Nobodythe output is:0n is different than N.
QUESTION: Add code so that if no parameter values are specified for method “main”, then the...
QUESTION: Add code so that if no parameter values are specified for method “main”, then the error message “file name expected” is printed out instead of the "Opening file " message. Hint: If no parameter values are specified for method “main”, then the length of the array “args” will be zero. If that error message “file name expected” is printed out, the program should stop. You can make any Java program stop using this line … System.exit(0); skeleton code: /**...
Question 1- A graphing calculator is recommended. Are the mean number of times a month a...
Question 1- A graphing calculator is recommended. Are the mean number of times a month a person eats out the same for whites, blacks, Hispanics and Asians? Suppose that the table below shows the results of a study. White Black Hispanic Asian 5 3 8 7 8 1 3 3 2 5 5 5 4 2 4 1 6 6 7 Assume that all distributions are normal, the four population standard deviations are approximately the same, and the data were...
. Consider the character array as follows: char mySentence[] = "Hello World!"; The number of characters...
. Consider the character array as follows: char mySentence[] = "Hello World!"; The number of characters in the array can be determined by application of the strlen() function. Therefore, the end of the array can be determined by pointer arithmetic as follows: char * endArray = mySentence + strlen(mySentence); Now that the end of the array has been determined, utilise pointers in C++ to output each character in array in reverse and display it to the console window. What other...
Question 3: Create a method for the Binary Search Tree (deleteNode) that deletes a specified node...
Question 3: Create a method for the Binary Search Tree (deleteNode) that deletes a specified node identified by its value, and rearranges the descendants of the deleted node to ensure the resulting Tree meets the requirements of a Binary Search Tree. a) Discuss and justify your approach to address each possible case. b) Is the new tree (with the deleted node removed) unique? Discuss your answer. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or...
1.Write a Java program that inputs a binary number and displays the same number in decimal....
1.Write a Java program that inputs a binary number and displays the same number in decimal. 2.Write Java program that inputs a decimal number and displays the same number in binary.
In Java: Write a complete main method that does the following: 1. Takes any number, but...
In Java: Write a complete main method that does the following: 1. Takes any number, but at least two, command line arguments which are words (represented as strings) and will print to the console the number of words of that end with a digit. (Hint: loop through the args array) 2. If there are not at least two command line arguments, throw an IllegalArgumentException with an appropriate message.
Hello! I need to add a method that will display the total number of times the...
Hello! I need to add a method that will display the total number of times the user answered the question correctly, how many time he was right or wrong and the percentage of time they were correct in answering. import java.util.Random; import java.util.Scanner; public class Test { private static int getUserInput() { int n; Scanner scanner = new Scanner(System.in); n = scanner.nextInt(); return n; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String choice = "Yes";...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT