Consider the following class definitions (virtually identical to those used in c
ID: 3935383 • Letter: C
Question
Consider the following class definitions (virtually identical to those used in class): class IntNode {int datum;//datum to be stored IntNode next;//next item in list public int getDatum() {return datun;} public void setDatum(int which) {datum = which;} public IntNode getNext() {return next;} public IntNode setNext(IntNode which) {next = which;}} class LinkedList {IntNode head;//first item in the list; public LinkedList() {head = null;}} Each of the following methods is a member of the LinkedList class. All the methods compile correctly: however, they do not function as described in their headers. What is wrong? How would you fix them?//This method counts the number of items in the list. public int length () {int count = 1; IntNode current = head; while (current.getNext() ! = null) {current = current.getNext(); count++;} return count;}//This method places "target" at the front of the list public void prepend (int target) {IntNode newNode = new IntNode(); newNode.setDatum(target); head = newNode; newNode.setNext(head);}//This method searches a sorted list for "target"//Assumption: all numbers in the list are sorted//from small to large public boolean search (int target) {if (head == null) return false; IntNode current = head; while (current.getDatum()Explanation / Answer
5) Answer
import java.util.Scanner;
class palindrome
{
//Recursive method to check palindrome
public static boolean isPalin(String str)
{
// If length is 0 or 1 then String is palindrome
if(str.length() == 0 || str.length() == 1)
return true;
if(str.charAt(0) == str.charAt(str.length()-1))
// Check for first and last char of String. If they are same then do the same thing for a substring with first and last char removed.
// Continue this until our string completes or condition fails.
return isPalin(str.substring(1, str.length()-1));
return false;
}
public static void main(String[]args)
{
//Scanner class object is created
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the String for check:");
//Accept a string
String string = scanner.nextLine();
// If function returns true then the string is palindrome else not palindrome
if(isPalin(string))
System.out.println(string + " Palindrome");
else
System.out.println(string + " Not Palindrome");
}
}
Output 1:
Enter the String for check:
thisis
thisis Not Palindrome
Output 2:
Enter the String for check:
madam
madam Palindrome
4) Answer:
public int average()
{
int sum = 0;
int cou = 0;
IntNode current = head;
//Checks for empty
if (head == null)
return 0;
//Loops till end of the linked list
while(current.getNext() != null)
{
//Calculates total
sum = sum + current.getDatum();
//Counter for number of items
cou++;
}
System.out.println("Average of the elements in the list = " + (sum / cou));
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.