In: Computer Science
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
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**************************************************