Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please help, my code so far is class List{ private Node head; public List(Node loopy){ loopy = this.head; } public int sum(){ } } class

image text in transcribed

please help, my code so far is

class List{ private Node head; public List(Node loopy){ loopy = this.head; } public int sum(){ } }

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; // get the 'value' variable public int getValue() { return this.value; // set the next' variable public void setNext(Node next) { this.next = next; TASK: Create a class called List that has the following properties: It should have one private instance variable of type Node called head . It should have a constructor with one parameter of type Node, which sets the head instance variable to the argument It should have a non-parametric public instance method called sum that returns the sum of head.value, head.next.value, head.next.next.value, etc. EXAMPLE: If we run the following code: Node node = new Node(1) : node.setNext(new Node (2)); node.getNext().setNext(new Node (3)); List list = new List(node); System.out.println(list.sum()); We should print 1 + 2 + 3 = 6

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions