Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

further below is the actual problem. *For the entirety of this question you are not allowed to use any data structure * other than a

further below is the actual problem.

*For the entirety of this question you are not allowed to use any data structure * other than a custom built linked list. This should consist ONLY of a node * class that will be given. You should not be creating a secondary class to handle the data * structure. * * Failure to do so will result in no credit.

the node class

public class Node { private E item; private Node next;

public Node(E item, Node next) { //0, null this.item = item; this.next = next; }

public E getItem() { return item; }

public void setItem(E item) { this.item = item; }

public Node getNext() { return next; }

public void setNext(Node next) { this.next = next; }

}

the problem: THIS IS WHERE I AM STUCK, PLEASE READ THE PROBLEM. * Problem: Given a Linked List of Integers you print from problem A (PPROBLEM A IS BELOW THIS CODE), count the number of numbers that contain 4 in the list. A number can have multiple 4s, but it still only counts as 1 number. i.e. The list [3, 54, 4, 444, 49] has 4 numbers with the number 4. Return your final count. DO NOT MAKE ANY OTHER METHODS OR CLASSES AND DO NOT CHANGE ANY OF THE METHOD HEADERS OR CLASSES */

public int countNumbersWith4(Node linkedList) { int count =0; Node p = linkedList; Node start = linkedList; while(p != null) { if (p.getItem() > 0) { while (p.getItem() % 10 == 4) { count++; p = p.getNext(); } } break; } System.out.println(count); return count; // You do not need to print anything here. }

//this code does not work and returns zero, if I enter numbers with 4.

PROBLEM A:

public Node addInts() { Scanner userInput = new Scanner(System.in);

Node start, tail = new Node<>(0, null); start = null; System.out.println("Enter numbers and enter STOP to finish"); while(true) {

if(userInput.hasNextInt()) { int numInput = userInput.nextInt(); if (start == null) { start = new Node<>(numInput, null); tail = start; } else { Node next = new Node<>(numInput, null); tail.setNext(next); tail = tail.getNext(); } } else if(userInput.hasNext("STOP") || userInput.hasNext("stop")) { break; } else { userInput.next(); System.out.println("Not a valid input, please enter another integer or type STOP to print off linked list."); } } Node p = start; while (p != null) { System.out.print(p.getItem()); if (p.getNext() != null) System.out.print(", "); p = p.getNext(); } System.out.println(); return start; }

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

Recommended Textbook for

Microeconomics An Intuitive Approach with Calculus

Authors: Thomas Nechyba

1st edition

538453257, 978-0538453257

More Books

Students also viewed these Programming questions