Question

In: Computer Science

Write a Java program that takes in a string and a number and prints back the...

  1. Write a Java program that takes in a string and a number and prints back the string from the number repeatedly until the first character... for example Pasadena and 4 will print PasaPasPaP.
    1. Ask the user for the string and a number
    2. Print back the string from the number repeatedly until the first character
  2. For both programs please utilize:
    1. methods
    2. arrays
    3. loops
  3. Turn in screenshots

Solutions

Expert Solution

I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments(read them for better understanding).

Images of the Code:


Note: If the below code is missing indentation please refer code Images

Typed Code:

//importing required packages
import java.util.*;
import java.lang.*;
public class Main
{
public static void main(String[] args)
{
   //call a method
   inputs();
   }
//method called
static void inputs()
{
//scanner is used to get inputs from the user
       Scanner scnr = new Scanner(System.in);
       System.out.print("Enter String: ");
       //getting string input from the user
       String str = scnr.nextLine();
       System.out.print("Enter Number: ");
       //getting number input from the user
       int num = scnr.nextInt();
       //call a method
       string(str,num);
}
//method called
static void string(String s, int n)
{
//try block is used to check the errors in the block of code
try
{
//initializing empty string
String str = "";
//initializing character array with size of string
char[] c = new char[s.length()];
//for loop will iterate size of string times
for (int i = 0; i < s.length(); i++)
{
//for every iteration, storing a single character
//in character array from string
c[i] = s.charAt(i);
}
//storing n value in a
int a = n;
//for loop will iterate n times
for (int i = 0; i < n; i++)
{
//for loop will iterate a times
for(int j = 0; j<a; j++)
{
//for every iteration, storing a single character
//from character array to string
str += c[j];
}
//decreasing a
a--;
}
//printing string
System.out.print(str);
}
//catch block will handle the errors
catch(Exception e)
{
//printing that there is an error occured
System.out.print("An Error Occured");
}
}
}
//code ended here

Output:


If You Have Any Doubts. Please Ask Using Comments.

Have A Great Day!


Related Solutions

Write a Java program that prompts the user to input a string and prints whether it...
Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure. The program must include the following classes: The StackX class (or you can use the Java Stack class). The Palindrome class which must contain a method named palindrome()...
Text Wrap Problem Write a program in Python that takes an input string and prints it...
Text Wrap Problem Write a program in Python that takes an input string and prints it as multiple lines of text such that no line of text is greater than 13 characters and words are kept whole. For example, the first line of the Gettysburg address: Four score and seven years ago our fathers brought forth upon this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal Becomes: Four score and...
This function takes in a string as a parameter and prints the average number of characters...
This function takes in a string as a parameter and prints the average number of characters per word in each sentence in the string. Print the average character count per word for each sentence with 1 decimal precision (see test cases below). Assume a sentence always ends with a period (.) or when the string ends. Assume there is always a blank space character (" ") between each word. Do not count the blank spaces between words or the periods...
Write a program in C that takes as input an 8-bit binary number and prints the...
Write a program in C that takes as input an 8-bit binary number and prints the next 10 binary numbers. Define a binary number as int binNum[8]; Use binNum[0] to store the most significant (i.e., leftmost) bit and binNum[7] to store the least significant bit. Ask the user to input the first binary number with each bit separated by at least one space.
Write a program in C that takes as input a four-digit hexadecimal number and prints the...
Write a program in C that takes as input a four-digit hexadecimal number and prints the next 10 hexadecimal numbers. Define a hexadecimal number as int hexNum[4] Allow upper- or lowercase letters for input and use uppercase letters for the hexadecimal output. For example, 3C6f should be valid input and should produce output 3C6F, 3C70, 3C71, . . . .
Python Write a program that takes a text filename as command line argument, and prints number...
Python Write a program that takes a text filename as command line argument, and prints number of times each letter occurred in this file.
In Java, write a recursive function that accepts a string as its argument and prints the...
In Java, write a recursive function that accepts a string as its argument and prints the string in reverse order. Demonstrate the function in a driver program.
using Dr java Objective: Write a program that takes a phrase and then counts the number...
using Dr java Objective: Write a program that takes a phrase and then counts the number of vowels (case does not matter) in the phrase. It then should display all of the vowels in sorted ascending order according to their count. Only consider {AEIOU} as the vowels. Hint: It may be a good idea to keep track and sort two arrays: Has the vowels in alphabetic order Has the number of said vowels Whenever one would swap then it swaps...
Assume s is a string of numbers. Write a program that prints the longest substring of...
Assume s is a string of numbers. Write a program that prints the longest substring of s in which the numbers occur in ascending order and compute the average of the numbers found. For example, if s = '561984235272145785310', then your program should print Longest substring in numeric ascending order is: 14578 Average: 5 In the case of ties, print the first substring. For example, if s = '147279', then your program should print Longest substring in numeric ascending order...
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT