Question: help with java 1) Implement a public class called Node that represents a Node in a linked list. Node has two private instance variables: data

help with java

1) 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.

2) A LinkedList class has a Node head as one of its 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.

3)

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 "Cant divide by zero.

4)

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 ps spouse is q then qs 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 + "\t" + b + "\t" + c + "\t" + 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 + "\t" + b + "\t" + c + "\t" + 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

5)

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 its 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.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!