Question
need help making Junit tests for my code. he code i made makes a singly linked list, you can add to it, delete an item,
need help making Junit tests for my code. he code i made makes a singly linked list, you can add to it, delete an item, print out the items in the list, prints the amount of nodes in list, Return a reference to a specific node in the set , Determine the union (i.e. set of all distinct elements) and the intersection (i.e. set of all common distinct elements) of two sets.and you can look for a specific item of the list.
My code:
public class practice { // = a generic (this means the singly linked list can be created with any element i.e a string, int, etc)
//singly linked lists are composed of nodes
//implement a node
static class Node{
T element;
//Linked list has a node which contains the element itself and then it points to the next element
private Node next;//pointer to the next node
public Node(T e, Node n) { //creating a node with its own element and a pointer (Node) to another type of node (n)
element = e;
next = n;
}
//we want to retrieve the element back from the node above so we can display whatever data is in it (string,int,etc.)
public T getElement() {
return element;
}
//next we need to retrieve the pointer to return the next node
public Node getNext(){
return next;
}
//if we need to change what the pointer is pointing to:
public void setNext(Node n) {//this is going to take a node of the same parameter of a node of the same type
next = n;//setting next
}
}
//singly linked list implementation
//Initialization stage of the singly linked list so both head and tail will be empty
private Node head = null;
private Node tail = null;
private int size = 0; //keeping track of the size of linked list using (size)
public practice() {}; //(name of the file) empty initializer when I don't have anything going on
public void printList(){
Node n = head;
int num = 0;
while (n != null) {
System.out.println(n.element + " ,");
n = n.next;
}
}
public int getCount(){//getting the amount of node in the singly linked list
Node temp = head;
int count = 0;
while (temp != null)
{
count++;
temp = temp.next;
}
return count;
}
public int size() {//getting the size back from the singly linked list
return size;
}
public boolean isEmpty() {//return true if the list is empty
return size == 0;
}
public T first(){ //getting the first element in the list
if(isEmpty()) { //if size == o then return nothing
return null;
}//else
return head.getElement(); //if not empty then return the value of the head node
}
public T last(){ //getting the value of the tail node
if(isEmpty()) {
return null;
}//else
return tail.getElement();
}
//this function allows you to add an element to the list itself. You can either add it at the head (beginning) of the linked list and the end (tail) of the linked list
public void addFirst(T e) {//Remember the T = generic, e = element being added
head = new Node<>(e, head);//cr a new node which has our node (e) being added and it will point to the head.
if(size == 0) {//remember they (tail/head) have been originally initialized to NULL
tail = head;
}
size++;//size increased
System.out.println("Added head node with " + head.getElement() + " element.");
}
public void addLast(T e) {//adding element at the end (tail) of linked list
Node newNode = new Node<>(e, null);//tail is originally == NULL
if(isEmpty()) {//if we have no other elements within our list the head is going to be the new element added
head = newNode;
}else {
tail.setNext(newNode);
}
tail = newNode;
size++;
System.out.println("Added tail node with " + tail.getElement() + " element.");
}
public T removeElement(T e) {//e = element we are looking to remove
Node current = head;
Node previous = head;
int position = 0; //this helps keep track of how many steps have been made
while(current !=null && current.getElement() != e) {// while we are NOT at the end of the list and we have NOT found the element we are looking for:
previous = current; //moving the two references we have
current = current.getNext();
position++;
}
if(current == null) { //if the element isn't found
return null;
}else {
if(head == current) {//if the element we are looking for is at the head
head = current.getNext(); //shift the head over
}else if(tail == current) {//if the element we are looking for is at the tail
tail = previous;
tail.setNext(null);
}else {//else we will set the previous to be the next
previous.setNext(current.getNext());
}
System.out.println("found and removed node at position " + position);
size--; //decrement the size of the linked list
return current.getElement();
}
}
public T findElement(T e) {//e = element we are looking to remove
Node current = head;
Node previous = head;
int position = 0; //this helps keep track of how many steps have been made
while(current !=null && current.getElement() != e) {// while we are NOT at the end of the list and we have NOT found the element we are looking for:
previous = current; //moving the two references we have
current = current.getNext();
position++;
}
if(current == null) { //if the element isn't found
return null;
}else {
System.out.println("found at position " + position);
return current.getElement();
}
}
//returns the reference to the node with given value
public Node getReferenceTo(T item) {
//looping through each node
for (Node n = head; n != null; n = n.getNext()) {
//if the node's value is equal to the item, returning node
if (n.getElement().equals(item)) {
return n;
}
}
//null if not found
return null;
}
//returns a set/list that contains union of all distinct elements in this set and other set
public practice union(practice other) {
//creating a new set to store the union
practice result = new practice();
//looping through each node in this set
for (Node n = head; n != null; n = n.getNext()) {
//checking if n.element is in result set, if not, adding to it
if (result.getReferenceTo(n.getElement()) == null) {
result.addLast(n.getElement());
}
}
//looping through each node in other set
for (Node n = other.head; n != null; n = n.getNext()) {
//checking if n.element is in result set, if not, adding to it
if (result.getReferenceTo(n.getElement()) == null) {
result.addLast(n.getElement());
}
}
return result;
}
//returns a set/list that contains intersection of all distinct elements in this set and other set
public practice intersection(practice other) {
//creating a new set to store the intersection
practice result = new practice();
//looping through each node in this set
for (Node n = head; n != null; n = n.getNext()) {
//checking if current value exist on other set, and not already added to result set
if (other.getReferenceTo(n.getElement())!=null && result.getReferenceTo(n.getElement()) == null) {
//adding to result set
result.addLast(n.getElement());
}
}
return result;
}
HELP IN JAVA! I need this code that I provided to print out exactly what I am asking! (add a method for deleteEven(Node head) and add code for while( ) that is missing.
Create a circular (singly linked list) of ints using the file I/0 You are to use SINGLY LINKED NODES and NOT the java Linked List class. Append, do not prepend (just so we can compare your output is correct) Print the list. Pass this list to a method that deletes every node whose item is an EVEN number. Print the list. You should have only odd numbers remaining.
Node.java
public class Node { Object item; Node next;
Node(Object newItem) { item = newItem; next = null;
} Node(Object newItem, Node nextNode) { item = newItem; next = nextNode; } } L4.java
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner;
public class L4 {
public static void main(String[] args) { Node head = createInitialList(); printList(head); head=deleteEven(head); printList(head); } public static Node createInitialList() { File f = new File("H5a.txt"); // Text file try { } catch (FileNotFoundException e) { // Exit if bad things happen System.out.println("File not Found!"); System.exit(-1); }
while ( ) { // While we still have text in the file } return head; } //end createInitialList public static void printList(Node head) { System.out.println("printing list"); Node curr = head; while(curr.next!=head) { System.out.print(curr.item + " "); curr=curr.next; } System.out.println(curr.item); } public static Node deleteEven(Node head) { return head; } }
Problem Definiti an inheritance hierarchy containing base class Account and derived class Savings-Account. Base class Account should include one data member of typ double to represent the account balance. The class should provide a constructor that receives an initial baiance and uses it to initialize the data member. The class should provid three member functions. Member function credit should add an amount to the current balance. Member function debit should withdraw money from the Account and ensure tha the debit amount does not exceed the Account's balance. Member function getBalance should return the current balance Derived class SavingsAccount should inherit the functionality of an Account, but also include a data member of type double indicating the interest rate (percentage) assigned to the Account. SavingsAccount's constructor should receive the initial balance, as well as an initial value for the SavingsAccounts interest rate. SavingsAccount should provide a public member function calculatelnterest that retums a double indicating the amount of interest eamed by an account. Member function calculateinterest should determine this amount by multiplying the interest rate by the account balance. [Note: SavingsAccount should inherit member functions credit and debit as is without redefining them.] Part 1: Class Definition Implement the class definition for class Account and SavingsAccount in the files called Account h and SavingsAccount.h. The class definition should contain only the prototypes of the member functions and double members. Part 2.1: Member-function definition Implement the member-function definition of classes in the files calle Account.cpp and SavingsAccount.cpp. This cpp file contains the actual mplementation of the member functions of the two classes Part 2.2: Test program Wa program that tests your class Account and SavingAcounts. Call your file testAccount.cpp. This program should 1. Instantiate an object of class SavingAccounts and Accounts 2. Add some credit to both objects. 3. Add some credit to both objects 4.
kindly answer all
Step by Step Solution
3.43 Rating (150 Votes )
There are 3 Steps involved in it
Step: 1
implement the deleteEven method and read integers from a file to create a circular singly linked list you can follow these steps First lets implement ...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