Question: Please code in Java: 1. We have implemented the following College interface: interface College { public int getNumStudents(); } TASK: Create a class called WarrenCollege

Please code in Java:

1. We have implemented the following College interface:

interface College { public int getNumStudents(); }

TASK: Create a class called WarrenCollege that has the following properties:

  • It should have a private instance variable of type int called numStudents
  • It must have a constructor that has one int parameter, and it should set the numStudents instance variable to the value of the constructor's argument
  • It must have a no-parameter instance method called getNumStudents that returns the numStudents instance variable

Sample Input:

1000

Sample Output:

1000

2.

We have written the skeleton of a class called Node that has an int instance variable value and a Node instance variable next, where next can be null.

TASK: Override the toString method such that, for some Node node, it returns (node.value)->(node.next.value)->(node.next.next.value)->....

EXAMPLE: If we run the following code:

Node node = new Node(1); node.setNext(new Node(2)); node.getNext().setNext(new Node(3)); System.out.println(node);

We should print the following:

(1)->(2)->(3)

HINT: Just like any other method, you can define the toString method recursively!

Sample Input:

1 2 3

Sample Output:

(1)->(2)->(3)

class Node { // instance variables private int value; private Node next;

// constructor public Node(int value) { this.value = value; }

// get the 'next' variable public Node getNext() { return this.next; }

// set the 'next' variable public void setNext(Node next) { this.next = next; }

// return a representative String public String toString() { // YOUR CODE HERE

??? } }

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!