Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement a public class called Node that represents a Node in a linked list. No

ID: 3726488 • Letter: I

Question

Implement a public class called Node that represents a Node in a linked list. Node has two private instance variables: data of type int and next of type Node. Implement default constructor that leaves next and data null, and a two-parameter constructor with parameters data of type int and next of type Node.   Include appropriate getters and setters.

A LinkedList class has a Node head as one of it’s instance variables. As ususal an empty list has head == null. Implement a method public void insertFirst(int data) that inserts a value at the front of the linked list.

Write a method int countDifferentChars(String s, String t) that counts the number of corresponding characters of s and t that don't match (are different).   For example countDifferentChars("abcxa", "abcdax") would return 2   and countDifferentChars("xabc", "abcx") would return 4. Notice that if one string is longer than another, all the additional characters count as non-matches.

             

Which is the correct order from least visible to most visible? (Note: by default we me no explicit access modifier is provided, also called package access.)

private, protected, default, public

protected, private, default, public

private, default, public, protected

private, default, protected, public

default, private protected, public

Implement a method divide that takes parameters of x and y of type double and returns the double x / y, except if y equals 0 the method should throw an

ArithmeticException with the message "Can’t divide by zero.”

       

The statements below are executed from main. Based on the comments and sample output, deduce the intended behavior of Person.marry() and Person.divorce(). Then fill in the missing code. A Person is considered to be unmarried just when their spouse is null. (Hence to test whether a Person is single or married we can test whether or not the value of spouse is null.) Also, marriage is a symmetric relation, so if p’s spouse is q then q’s spouse is p.

                Person a= new Person("Ann"), b= new Person("Bob"), c= new Person("Cat"), d= new Person("Don");

                a.marry(a).marry(b);

                System.out.println(a + " " + b + " " + c + " " + d);

                c.divorce();

                b.marry(c);

                c.marry(b);

                a.divorce().marry(d).divorce().marry(c);

                d.marry(b).marry(b);

                System.out.println(a + " " + b + " " + c + " " + d);

                OUTPUT Silly Ann. You can't marry yourself!

Congratulations Ann and Bob, you are now now married!

             

Cat, you're single, so there's nobody to divorce.

Sorry Bob, you'll need to divorce Ann before marrying Cat.

Sorry Cat, you'll need to convince Bob to divorce Ann first.

Divorce complete: Ann and Bob are no longer married.

Congratulations Ann and Don, you are now now married!

Divorce complete: Ann and Don are no longer married.

Congratulations Ann and Cat, you are now now married!

Congratulations Don and Bob, you are now now married!

Don, you don't need to marry Bob since you guys are already married.

              

class Person {

private Person spouse;    // current Person's spouse or null if the Person isn't married

public final String name; // Note: it's possible that two diff. people have same name.

      

public Person(String name) {this.name = name;}

Person marry(Person p) // p is the prospective spouse of the the current Person

{     

       if (_______________________________)    // current object and p are the same Person

System.out.println("Silly "+ name +". You can't marry yourself!");

       else if (_____________________________) // current Person is already married

                    

              if (____________________________) // current Person and p are already married

                          

                     System.out.println(name + ", you don't need to marry " + p.name

                                                + " since you guys are already married.");     

              else

                     System.out.println("Sorry "+ name + ", you'll need to divorce "

                                         + spouse.name + " before marrying " + p.name + ".");

             

       else if (_____________________________) // p is already married

              System.out.println("Sorry " + name + ", you'll need to convince "

                                  + p.name + " to divorce " + p.spouse.name + " first.");

       else {

              _______________________________________

              _______________________________________

              System.out.println( "Congratulations " + name + " and "

                                  + p.name + ", you are now now married!");

       }     

       return _________________________; // return the current object

}

Person divorce() {

             

       if (_______________________)

System.out.println(name +", you're single, so there's nobody to divorce.");

       else {

             

              String nom = spouse.name; // Save the spouse's name in a temp variable

                    

              ________________________________________; // Performing the divorce

      

              ________________________________________; // requires two statements.

                    

              System.out.println("Divorce complete: " + name + " and "

                                         + nom + " are no longer married." );

       }

       return __________________________; // return the current object     

}

      

Person getSpouse() {return spouse;}

      

public String toString() {

       return "<" + name + "," + (spouse == null ? "null" : spouse.name) + ">";

}

} // end of Person class

Here is a recursive method that returns the smallest character in a string:

       // return smallest character in s, e.g. getMinChar("difference") would return 'c'

       static char getMinChar(String s) {

              if (s.length() == 1) return s.charAt(0);

             

              if (getMinChar(s.substring(1)) > s.charAt(0))

                     return s.charAt(0);

              else

                     return getMinChar(s.substring(1));      

       }

The method returns the correct value but it can be extremely slow when s is a long string. Recode getMinChar so that it’s more efficient by reducing the number of recursive calls. Your code should still be recursive. There is no need to make major changes to the algorithm.

Explanation / Answer

Please find my implementation of Q1.

Please repost others in separate post.

public class Node {

  

   private int data;

   private Node next;

  

   public Node(int data, Node next) {

       this.data = data;

       this.next = next;

   }

   public int getData() {

       return data;

   }

   public Node getNext() {

       return next;

   }

   public void setData(int data) {

       this.data = data;

   }

   public void setNext(Node next) {

       this.next = next;

   }

  

}

########

public class LinkedList {

  

   private Node head;

  

   public LinkedList() {

       head = null;

   }

  

   public void insertFirst(int data) {

       Node newNode = new Node(data, head);

       head = newNode;

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote