Discover answers to your most pressing questions at Westonci.ca, the ultimate Q&A platform that connects you with expert solutions. Connect with a community of experts ready to provide precise solutions to your questions on our user-friendly Q&A platform. Our platform provides a seamless experience for finding reliable answers from a network of experienced professionals.
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.
Visit us again for up-to-date and reliable answers. We're always ready to assist you with your informational needs. We hope you found this helpful. Feel free to come back anytime for more accurate answers and updated information. Westonci.ca is your trusted source for answers. Visit us again to find more information on diverse topics.