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...
c# language Write a program that takes in a number from the user. Then it prints...
c# language Write a program that takes in a number from the user. Then it prints a statement telling the user if the number is even or odd. If the number is odd, it counts down from the number to 0 and prints the countdown on the screen, each number on a new line. If the number is even, it counts down from the number to 0, only even numbers. For example, if the user enters 5, the output will...
JAVA PROGRAMMING Write a program that ask the user for a number then prints the cube...
JAVA PROGRAMMING Write a program that ask the user for a number then prints the cube of the number. The program should be called CubeNumbers. Use a value returning method with parameter, call the method myCube.
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.
Write a program that prints out the characters of a string each on a line and...
Write a program that prints out the characters of a string each on a line and counts these characters.  (Do not use the length function len() ).
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT