Westonci.ca is the Q&A platform that connects you with experts who provide accurate and detailed answers. Discover comprehensive solutions to your questions from a wide network of experts on our user-friendly platform. Connect with a community of professionals ready to provide precise solutions to your questions quickly and accurately.
Sagot :
Answer:
Following are the code to this question:
import java.util.*;//import package
public class DriverMain//defining a class DriverMain
{
public static void main(String args[])//main method
{
int num,width;
Scanner input = new Scanner(System.in);//creating Scanner class fore user input
num = Integer.parseInt(input.nextLine().trim());//input value
width = Integer.parseInt(input.nextLine().trim());//input value
GW6_P5 gw6P5 = new GW6_P5();//creating base class object
System.out.print(gw6P5.format(num,width));//print method that calls base class format method
}
}
class GW6_P5 //defining base class GW6_P5
{
String format(int number, int width)//defining method format that takes two integer variable in its parameter
{
String s = ""+number;//defining s String variable that holdes integer value
int digitCount = 0;//defining integer variable
while(number!=0) //defining while loop to check value is not equal to 0
{
digitCount++;//incrementing the integer variable value
number/=10;//holding quotient value
}
if(width>digitCount)//defining if block that checks width value greather then digitCount value
{
for(int i=0;i<width-digitCount;i++)//defining for loop to add 0 in String variable
{
s = "0"+s;//add value
}
}
return s;//return String value
}
}
Output:
34
5
00034
Explanation:
In the given code, a class "DriverMain" is defined, and inside the class main method is defined that defines two integer variable "num and width", that uses the scanner class to input the value, and in the next step the "GW6_P5" class object is created that calls the format method and print its values.
In the class "GW6_P5", the format method is defined, that declared the string and integer variables, in the while loop it checks number value not equal to 0, and increments the number values, and in the for loop it adds the 0 in string value and return its value.
Thank you for visiting our platform. We hope you found the answers you were looking for. Come back anytime you need more information. Thank you for your visit. We're dedicated to helping you find the information you need, whenever you need it. Thank you for trusting Westonci.ca. Don't forget to revisit us for more accurate and insightful answers.