Here is the starting file. So far from the starting file, I have written the fol
ID: 3861281 • Letter: H
Question
Here is the starting file.
So far from the starting file, I have written the following code. I am not sure how to do the last 3 methods. If anyone could help it would be greatly appreciated!
import java.io.*;
public class Recursion
{
public static void main(String[] args)
{
//TRISTARS
int rows = 5;
System.out.format("A triangle with %d rows contains %d stars ", rows, triStars(rows));
//SUMDIGITS
int number = 12345;
System.out.format("The sum of the digits in the number %d = %d ", number, sumDigits(number));
//COUNT7S
number = 713274772;
System.out.format("There are %d occurrences of the digit 7 in the number %d ", count7s(number), number);
//COUNT8S
number = 82338828;
System.out.format("There are %d occurrences of the digit 8 in the number %d ", count8s(number), number);
}
//count stars in a triangle using # of rows as input.
static int triStars(int rows)
{
if(rows == 1)
return 1;
return rows + triStars(rows-1);
}
//given a number return the sum of the digits.
static int sumDigits(int n)
{
if(n == 0)
return 0;
return n%10 + sumDigits(n/10);
}
//given a number compute the number of 7's in that number
static int count7s(int n)
{
if(n == 0)
return 0;
else if(n%10 == 7)
return 1+count7s(n/10);
else
return count7s(n/10);
}
//given a number count the number of8 but if an 8 has another 8 to its immediate left
//count it as 2.
//the number 8802388 will return a count of 6.
static int count8s(int n)
{
if(n == 0)
return 0;
else if(n%100 == 88)
return 3+count8s(n/100);
else if(n%10 == 8)
return 1+count8s(n/10);
else
return count8s(n/10);
}
//compute base to the power n
static int powerN(int base, int n)
{ return 0;
}
// return true only if the array is sorted
static boolean isSorted(int array[], int i, int count )
{ return false;
}
// return true if string is palindrome
static boolean isPalindrome(String s, int lo, int hi )
{ return false;
}
} // END CLASS Recursion
Explanation / Answer
import java.io.*;
import static test.Test.power;
public class Recursion
{
public static void main(String[] args)
{
//TRISTARS
int rows = 5;
System.out.format("A triangle with %d rows contains %d stars ", rows, triStars(rows));
//SUMDIGITS
int number = 12345;
System.out.format("The sum of the digits in the number %d = %d ", number, sumDigits(number));
//COUNT7S
number = 713274772;
System.out.format("There are %d occurrences of the digit 7 in the number %d ", count7s(number), number);
//COUNT8S
number = 82338828;
System.out.format("There are %d occurrences of the digit 8 in the number %d ", count8s(number), number);
//power
int base=2,n=3;
System.out.println("Power : "+powerN(base, n));
//issorted
int[] a={7,8,12,20,21,22,37,41,55,60,65,74,83,84,87};
int i=0;
for( i=0;i<a.length;i++)
{
//length is the property of array
System.out.print(" "+a[i]);
}
System.out.println(" is sorted "+isSorted(a, i));
//isPalindrome
String s="madam";
System.out.println(s+ " is palindrome "+ isPalindrome(s));
}
//count stars in a triangle using # of rows as input.
static int triStars(int rows)
{
if(rows == 1)
return 1;
return rows + triStars(rows-1);
}
//given a number return the sum of the digits.
static int sumDigits(int n)
{
if(n == 0)
return 0;
return n%10 + sumDigits(n/10);
}
//given a number compute the number of 7's in that number
static int count7s(int n)
{
if(n == 0)
return 0;
else if(n%10 == 7)
return 1+count7s(n/10);
else
return count7s(n/10);
}
//given a number count the number of8 but if an 8 has another 8 to its immediate left
//count it as 2.
//the number 8802388 will return a count of 6.
static int count8s(int n)
{
if(n == 0)
return 0;
else if(n%100 == 88)
return 3+count8s(n/100);
else if(n%10 == 8)
return 1+count8s(n/10);
else
return count8s(n/10);
}
//compute base to the power n
static int powerN(int base, int n)
{ if (n == 0)
return 1;
else
return base * power(base, n-1);
}
// return true only if the array is sorted
static boolean isSorted(int array[], int i )
{
int previous = array[0];
for( i = 1; i < array.length; i++)
{
if(previous > array[i])
//here it will check that previous no is greter than the next no or not
//if it is greater then false will return
//for ex. 2 3 4 array is der and
// 2>3 no then it will go to previous = array[1] i.e 3 value
//then i will increment by 1 now i=2;
//3>array[2] i.e 3>4 condition false then previous=array[2] i.e 4
//like wise if it is sorted will return true if not return false
return false;
previous = array[i];
}
return true;
}
// return true if string is palindrome
static boolean isPalindrome(String s)
{
// base case, if a string is empty or has just 1 character
// it is a palindrome itself
if (s.length() < 2) return true;
// else a string is a palindrome is first character matches the last
// one
if (s.charAt(0) == s.charAt(s.length() - 1)) {
return isPalindrome(s.substring(1, s.length() - 1)); // And the rest of the string is also a palindrome
}
else {
// Otherwise it is not a palindrome
return false;
}
}
} // END CLASS Recursion