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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started