Question

Using java Part A Make a list of random items in your nodes. (Simple nodes with...

Using java

Part A

Make a list of random items in your nodes.

(Simple nodes with item an int)

Print out this list.

Write a method to put this list in order, either smallest

to largest or largest to smallest (item)

Print of the list

Part B.

Make a list of the modified Bicycle nodes (added age and gender to the Bicycle class) // did it

Print out the list with the name, age and gender for each node. //did it

Write a method to find the oldest owner and print out the name, age and gender of this owner.//(idk how to do)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Part A)

CODE

========================

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

import java.util.Random;

public class Node implements Comparator<Node>{

   int item;

   public Node(int item) {

       this.item = item;

   }

  

   public Node() {}

   @Override

   public int compare(Node o1, Node o2) {

       if(o1.item == o2.item)

           return 0;

       else if(o1.item > o2.item)

           return 1;

       return -1;

   }

  

   public static void main(String args[]) {

       List<Node> nodes = new ArrayList<>();

       Random rand = new Random();

       for(int i=0; i<10; i++) {

           int n = rand.nextInt(100);

           nodes.add(new Node(n));

       }

       System.out.println("The original list is: ");

       for(int i=0; i<10;i++) {

           System.out.print(nodes.get(i).item + " ");

       }

      

       Collections.sort(nodes, new Node());

       System.out.println("\nThe sorted list is: ");

       for(int i=0; i<10;i++) {

           System.out.print(nodes.get(i).item + " ");

       }

   }

}

OUTPUT

========================

terminated> Node [Java Application] /Library/Java/JavaVi The original list is: 75 79 59 60 57 23 33 67 25 89 The sorted list

Part B)

CODE

==========================

public class BicycleNode{

   int age;

   String gender, name;

   public BicycleNode(int age, String gender, String name) {

       super();

       this.age = age;

       this.gender = gender;

       this.name = name;

   }

   /* (non-Javadoc)

   * @see java.lang.Object#toString()

   */

   @Override

   public String toString() {

       return "BicycleNode [age=" + age + ", gender=" + gender + ", name=" + name + "]";

   }

   public static void findOldestOwner(BicycleNode nodes[]) {

       if(nodes.length > 0) {

           int maxAge = nodes[0].age;

           BicycleNode node = nodes[0];

           for(int i=1; i<5; i++) {

               if(maxAge < nodes[i].age) {

                   maxAge = nodes[i].age;

                   node = nodes[i];

               }

           }

           System.out.println("\n\nThe oldest owner details are: ");

           System.out.println(node);

       }

   }

   public static void main(String args[]) {

       BicycleNode[] nodes = new BicycleNode[5];

       nodes[0] = new BicycleNode(45, "Name1", "Male");

       nodes[1] = new BicycleNode(56, "Name2", "Female");

       nodes[2] = new BicycleNode(42, "Name3", "Male");

       nodes[3] = new BicycleNode(31, "Name4", "Female");

       nodes[4] = new BicycleNode(78, "Name5", "Female");

       System.out.println("The bicyles are: ");

       for(int i=0; i<5; i++) {

           System.out.println(nodes[i]);

       }

      

       findOldestOwner(nodes);

   }

}

OUTPUT

==========================

The bicyles are: BicycleNode [age-45, gender-Name1, name-Male] BicycleNode [age-56, gender-Name2, name-Female] BicycleNode [a

Add a comment
Know the answer?
Add Answer to:
Using java Part A Make a list of random items in your nodes. (Simple nodes with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This exam is closed book. Write your name on every page. Write your solutions in the...

    This exam is closed book. Write your name on every page. Write your solutions in the space provided. If you need more space, attach a sheet and staple to your exam. Do not spend too much time on any problem. Show your work, as partial credit will be given. You will be graded not only on he correctness and efficiency of your answers, but also on your clarity. Be neat. public class Node public int icem,: publie Node next: Node...

  • Using the provided Linked List template, add the following recursive functions and demonstrate them in a...

    Using the provided Linked List template, add the following recursive functions and demonstrate them in a separate cpp file. Write a recursive function to print the list in order. Write a recursive function to print the list in reverse order. Write a recursive function to print every other node in the list in order. Write a recursive function to return the number of nodes in the list. Write a Boolean function that implements the recursive version of sequential search. THIS...

  • This class implements a doubly linked list in which the nodes are of the class DLNode....

    This class implements a doubly linked list in which the nodes are of the class DLNode. This class must implement the interface DLListADT.java that specifies the public methods in this class. The header for this class will then be public class DLList implements DLListADT This class will have three private instance variables: • private DLNode front. This is a reference to the first node of the doubly linked list. • private DLNode rear. This is a reference to the last...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a class NameCard.java which has •Data...

    PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a class NameCard.java which has •Data fields: name (String), age(int), company(String) •Methods: •Public String getName(); •Public int getAge(); •Public String getCom(); •Public void setName(String n); •Public void setAge(int a); •Public void setCom(String c); •toString(): \\ can be used to output information on this name card 2, write a class DoublyNameCardList.java, which is a doubly linked list and each node of the list a name card. In the DoublyNameCardList.java: •Data field:...

  • n JAVA, students will create a linked list structure that will be used to support a...

    n JAVA, students will create a linked list structure that will be used to support a role playing game. The linked list will represent the main character inventory. The setting is a main character archeologist that is traveling around the jungle in search of an ancient tomb. The user can add items in inventory by priority as they travel around (pickup, buy, find), drop items when their bag is full, and use items (eat, spend, use), view their inventory as...

  • you’ll write a program to illustrate how Linked list and structure are used in practice, we’ll...

    you’ll write a program to illustrate how Linked list and structure are used in practice, we’ll develop a program that maintains a database of information about parts stored in a warehouse. The program is built around the database stored in linked list and structures, with each structure containing information - part number, name, and quantity – about one part. • The program tracks parts stored in a warehouse. • Contents of each structure: – Part number – Name – Quantity...

  • CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to...

    CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to implement its inventory of computing machines as a linked list, called ComputerList. Write a Computer node class, called ComputerNode, to hold the following information about a Computer: • code (as a String) • brand (as a String) • model (as a String) • price (as double) • quantity (as int) ComputerNode should have constructors and methods (getters, setters, and toString()) to manage the above...

  • JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList...

    JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList Class (some methods included). Remember, you will use the LinkedList Class that we developed in class not Java’s LinkedList Class. You will add the following method to the LinkedList Class: printEvenNodes – this is a void method that prints Nodes that have even indices (e.g., 0, 2, 4, etc). Create a LinkedListDemo class. Use a Scanner Class to read in city names and store...

  • In Java: Develop a simple class for a Student. Include class variables; StudentID (int), FirstName, LastName,...

    In Java: Develop a simple class for a Student. Include class variables; StudentID (int), FirstName, LastName, GPA (double), and Major. Extend your class for a Student to include classes for Graduate and Undergraduate. o Include a default constructor, a constructor that accepts the above information, and a method that prints out the contents FOR EACH LEVEL of the object, and a method that prints out the contents of the object. o Write a program that uses an array of Students...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT