Wednesday, October 7, 2020

#344 Create class Recursive Methods that has

Create class Recursive Methods that has - Computer Science

ChemistryExplain daily providing Q&A content “#344 Create class Recursive Methods that has" in Computer science, Ba computer science, Berkeley computer science, Computer science associate degree jobs

ChemistryExplain “#344 Create class Recursive Methods that has in Computer science, Ba computer science, Berkeley computer science, Computer science

Get the Free Online Chemistry Q&A Questions And Answers with explain. To crack any examinations and Interview tests these Chemistry Questions And Answers are very useful. Here we have uploaded the Free Online Chemistry Questions. Here we are also given the all chemistry topic.

 ChemistryExplain team has covered all Topics related to inorganic, organic, physical chemistry, and others So, Prepare these Chemistry Questions and Answers with Explanation Pdf.

For More Chegg Questions

Free Chegg Question

Ex 1 Create class Recursive Methods that has the following recursive methods

a- Method void printDecending(int n): that prints the numbers from n to 0.

b- Method void printAcending(int n): that prints the numbers from 0 to n.

C- Method int total(int n) that returns the total of 1+2+3+..+n

d- Method int totalDigists(int n): that returns the total of digits in a number n, for example if n=452, it returns 11.

e- Method void reverseArray(int a[],int i,int that reverse the order of the elements in the array a.

f- Method void reverseArrayList (ArrayList a) that reverses the elments in the array list.

g- Method void reverseSinglyList(SinglyLinkedList myList) which reverses the order of the nodes in the singly linked list myList.

Test each one of the method above in the main

Ex 2 Palindrome string is the one that its reverse is the same as the string like “radar”, “aabbaa”. Wirte a recursive method is Palindrome that receives a string (only one parameter) and returns true if it's a palindrome, false if it is not.

Hint: you can use string method called substring (1,4) that extracts a string starting from character at position x to character at position y-1, and a method charAt(int i) that returns the char at position i.

Test your method in the main.

Assignment: Write a recursive method removeMiddle that receives an array list which has odd number of elements, then it deletes the element in the middle only. Note: The method should return the middle element that will be removed. The method should receive only one parameter (the array list that is of generic type).

For More Chemistry Notes and Helpful Content Subscribe Our YouTube Chanel - Chemistry Explain  

Free Chegg Answer

Ex1

class RecursiveMethods {
  static void PrintDecenting(int N)

{
if (N <= 0)
{
return;
}
else
{
System.out.print(N + " ");
  
// Recursive call of the function
PrintReverseOrder(N - 1);
}

}

private static void printAcending(int n)

{
if (n > 1)

{
display(n - 1);
}
System.out.println(n);
}

public static int total(int n) {

int sum = 0;
sum += n;
if (n == 0)
return sum;

return total(n - 1);

}

static int totaldigits(int n)
{
if (n == 0)
return 0;
return (n % 10 + totaldigits(n / 10));
}

static void rvereseArray(int arr[], int start, int end)
{
int temp;
if (start >= end)
return;
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
rvereseArray(arr, start+1, end-1);
}

/* Utility that prints out an array on a line */
static void printArray(int arr[], int size)
{
for (int i=0; i < size; i++)
System.out.print(arr[i] + " ");
System.out.println("");
}

public static void main(String[] args)
{
int N = 5;
  
printDescenting(N);

printAceding(N);

int res=total(N);

system.out.println("sum of digits is"+res);

int result = totaldigits(num);
System.out.println("Sum of digits in " +
num + " is " + result);

int arr[] = {1, 2, 3, 4, 5, 6};

rvereseArray(arr, 0, 5);
System.out.println("Reversed array is ");
printArray(arr, 6);
}
}

EX2:

answer:

public class Palindrome
{
public static void main(String args[])
{
String a, b = "";
Scanner s = new Scanner(System.in);
System.out.print("Enter the string you want to check:");
a = s.nextLine();
int n = a.length();
for(int i = n - 1; i >= 0; i--)
{
b = b + a.charAt(i);
}
if(a.equalsIgnoreCase(b))
{
System.out.println("TRUE.");
}
else
{
System.out.println("FALSE.");
}
}
}

 

Labels: , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home