Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PLEASE HELP IN JAVA: Alter your program to make the code as efficient and modula

ID: 3751631 • Letter: P

Question

PLEASE HELP IN JAVA:

Alter your program to make the code as efficient and modular as possible. Use ONE method to construct your first list, and ONE method to add a node to a list. YOU DO NOT NEED TWO METHODS (one for the odd list and one for the even list!!!)

Also, prepend is the easiest way to add to a list if you are not told otherwise.

newNode.next = head;

head = newNode;

This is an exercise in using nodes, so no extra credit for using linkedList class, lists etc.

Rubric:

* One method to get the integers and create first list

* one method (used three times) to add a node to a list

Program:

public static void main(String[] args) {
//create a list called "list1" that conatains integers
Scanner keyboard = new Scanner(System.in);
List<Integer>list1 = new ArrayList<>();
  
int n;
System.out.println("Enter input numbers with space between each integer");
System.out.println("");
// add integers to list1
do{
n = keyboard.nextInt();
if(n==0){
break;

}
list1.add(n);
}
while (true);
//display inputed integers
System.out.print(" Inputed list of numbers are: ");
for(int i =0; i<list1.size(); i++){
System.out.print(list1.get(i)+ " ");
}
//create lists that will conatain only even and odd integers respectively
List<Integer>even = new ArrayList<>();
List<Integer>odd = new ArrayList<>();
//use for loop to determine whether an integer is even or odd
for (int i =0; i<list1.size(); i++){
if(list1.get(i)%2==0){
even.add(list1.get(i));
}
else{
odd.add(list1.get(i));
}
}
//display list with the removed integers
list1.clear();
System.out.println();
System.out.println(" List after removing elements: " +list1);
//display list with only even integers
System.out.println(" Even list of numbers are: ");
for (int i =0; i<even.size(); i++){
//print out the even list
System.out.print(even.get(i) + " ");
}
//display list with only odd integers
System.out.println(" Odd list of numbers are: ");
for (int i =0; i<odd.size(); i++){
//print out the odd
System.out.print(odd.get(i) + " ");
System.out.print("");
}
}
  
  
}

Explanation / Answer

import java.util.Scanner;

class Node {

int data;

Node next;

Node(int x) {

data = x;

next = null;

}

}

class LinkedistEx {

public static Node createList() {

System.out.println("Enter input numbers with space between each integer");

  

Scanner keyboard = new Scanner(System.in);

Node head = null;

do{

int n = keyboard.nextInt();

if(n==0)

break;

Node newNode = new Node(n);

if(head == null )

head = newNode;

else {

newNode.next = head;

head = newNode;

}

}while (true);

return head;

}

public static void addNodeToList( Node head) {

Node head_of_even_list = null;

Node head_of_odd_list = null;

while (head != null ) {

Node newNode = new Node(head.data);

if(head.data % 2 == 0) {

if(head_of_even_list == null)

head_of_even_list = newNode;

else {

newNode.next = head_of_even_list;

head_of_even_list = newNode;

}

}

else {

if(head_of_odd_list == null)

head_of_odd_list = newNode;

else {

newNode.next = head_of_odd_list;

head_of_odd_list = newNode;

}

}

head = head.next;

}

System.out.println(" Even list of numbers are: ");

while(head_of_even_list != null ){

System.out.print(head_of_even_list.data + " ");

head_of_even_list = head_of_even_list.next;

}

System.out.println(" Odd list of numbers are: ");

while(head_of_odd_list != null ){

System.out.print(head_of_odd_list.data + " ");

head_of_odd_list = head_of_odd_list.next;

}

}

public static void main(String args[]) {

Node list = createList();

addNodeToList(list);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote