I would like somebody to please explain how this program works and why it prints
ID: 3721634 • Letter: I
Question
I would like somebody to please explain how this program works and why it prints what it prints. I know I can just copy and paste code but I dont know how it prints that output.
Can someone please be kind enough and explain in detials how this code works? Thank you very much!
6. The following Java program compiles and runs 1. class SomeClass 2 String str; SomeClass (String s) f str = s ; 4 void aMethod (int i) 7 8 if (i !- str.length)-1) aMethod(i+1); System.out.print(str.charAt (i)) 10 public static void main (String args []) I 12 13 14 15 16 17 SomeClass some -new SomeClass("A test"; some.aMethod(0); a) When the main method above runs, what output does it produce? (5 points)Explanation / Answer
here i have commented out the code as per the requirement..This code print the string in reverse order.
================================================================
Program:
===============================================================
//this program revesre a given string
//this is the public class
public class SomeClass
{
//String declration
String str;
//class constrcutor which takes one arrgumant a string
SomeClass(String s)
{
//initalize the string
str = s;
}
//aMethod is a method print the string char in reverse order
void aMethod(int i)
{
//if the value of i is not equal to string length-1 the call the amethod
//this will the a method recursively
if(i != str.length()-1)
{
aMethod(i+1);
}
System.out.print(str.charAt(i));
}
//this is the main method
public static void main(String args[])
{
//here we have created a class object name as some and constructor is initalize //string as A test
SomeClass some = new SomeClass("A test");
//call the amethod
some.aMethod(0);
}
}
//end of the program
================================================================
Output:
tset A
===============================================================
Kindly Check and Verify Thanks..!!!