Question

In: Computer Science

Design, plan, test, and write a computer program in Java that asks the user to enter...

  • Design, plan, test, and write a computer program in Java that asks the user to enter 1 number and a String. You will display the first n characters of the string where n is the number entered. For example, if the user enters 3 and java then you will print jav. If they enter 5 and Halloween then you print Hallo. If the user enters a number less than 0 then set the number to 0. Assume the user will not enter a number greater than the length of the string. Display the percentage of the word displayed. For 3 and Java, the percentage is 75%. For 5 and Halloween the percentage is 55.55555555555556%. No if, loops, arrays, classes or methods are needed. You will also need the String class to complete this problem.

Write the Java code for the program, the entire program can be written inside a main method.

Make sure the main method outputs your full name on screen.

Copy your code, with programmer comments, headers and place this into your MS Word document.

Follow Java Naming Conventions

Do not forget about importing java.util.Scanner, and using Scanner input = new Scanner(System.in); with input.nextLine(); and input.nextInt();

Update the test table with the actual outputs

inputs

Expected outputs

Actual outputs

Description

Sample Run

Enter a number:

3

Enter a String:

Java

Jav

You printed 75.0% of Java

Solutions

Expert Solution

import java.util.*;    
public class Main {    
     public static void main(String args[]){   
             Scanner input = new Scanner(System.in);  
             System.out.print("Enter a number : ");
             int n=input.nextInt();
             input.nextLine(); 
             System.out.print("Enter string\n");  
             String s = input.nextLine();  
        
             n=n<0?0:n;
                
                String s2=s.substring(0,n);
                System.out.print(s2);
                float per=((float)s2.length()/s.length())*100;
                System.out.println("\nYou printed "+per+"% of "+s);
        }
     }
          

Here is the code that asks the user to enter a number and then asks the user to enter a string and then rpitns the substring starting from index 0 to n value enter and the prints the percentage of the string printed compared to the original string.(I have used one extra input.nextLine() inorder to skip the new line and avoiding driectly termination of the string without entering.

SCREENSHOT OF THE OUTPUT :

YOU CAN WRITE YOU NAME IN System.out.println(" YOU NAME ") AT START OF THE PROGRAM.

************************************************PLEASE DO UPVOTE**************************************************


Related Solutions

Instructions Write a Java program that asks the user t enter five test scores. The program...
Instructions Write a Java program that asks the user t enter five test scores. The program should display a letter grade for each score and the average test score. Write the following methods in the program: * calcAverage -- This method should accept five test scores as arguments and return the average of the scores. * determineGrade -- This method should accept a test score as an argument and return a letter grade for the score, based on the following...
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
write a program in c++ that asks the user to enter their 5 test scores and...
write a program in c++ that asks the user to enter their 5 test scores and calculates the most appropriate mean. Have the results print to a text file and expected results to print to screen.
Java Program 1. Write a program that asks the user: “Please enter a number (0 to...
Java Program 1. Write a program that asks the user: “Please enter a number (0 to exit)”. Your program shall accept integers from the user (positive or negative), however, if the user enters 0 then your program shall terminate immediately. After the loop is terminated, return the total sum of all the previous numbers the user entered. a. What is considered to be the body of the loop? b. What is considered the control variable? c. What is considered to...
Write a java program which asks the user to enter name and age and calls the...
Write a java program which asks the user to enter name and age and calls the following methods: printName(): Takes name as parameter and prints it 20 times using a while loop. printAge(): Takes age as parameter and prints all the numbers from 1 up to age. Write a java program that will print first 10 multiples of 3 in a single line.
Write a program that asks the user to enter five test scores. The program should display...
Write a program that asks the user to enter five test scores. The program should display a letter grade for each score and the average test score. Write the following methods in the program: calcAverage: This method should accept five test scores as arguments and return the average of the scores. determineGrade: This method should accept a test score as an argument and return a letter grade for the score, based on the following grading scale: Score Letter Grade 90-100...
Write a java simple document retrieval program that first asks the user to enter a single...
Write a java simple document retrieval program that first asks the user to enter a single term query, then goes through two docuements named doc1.txt and doc2.txt (provided with assignment7) to find which document is more relevant by calculating the frequency of the query term in each document. Output the conclusion to a file named asmnt7output.txt in the following format (as an example for query “java” and “problem”). The percentages in parenthese are respective supporting frequencies. java: doc1(6.37%) is more...
IN JAVA Write a complete program that asks the user to enter two real numbers from...
IN JAVA Write a complete program that asks the user to enter two real numbers from the console. If both numbers are positive print the product, if both numbers are negative print the quotient, otherwise print INVALID INPUT. Use a nested if; output should be to a dialog and console; use printf to format the console output (for real numbers specify the width and number of digits after the decimal). The output must be labeled. Follow Java conventions, indent your...
Write a java program that asks user to enter a set of positive integer values. When...
Write a java program that asks user to enter a set of positive integer values. When the user stops (think of sentinel value to stop), the program display the maximum value entered by the user. Your program should recognize if no value is entered without using counter.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT