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...

  • 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...

  • 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...

  • using java to write,show me the output. please write some common. You CAN NOT use inbuild...

    using java to write,show me the output. please write some common. You CAN NOT use inbuild functions for Tree ADT operations. using code below to finsih public class Main {    public static void main(String[] args) {        BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.left.left.left = new Node(8); tree.root.left.left .right= new Node(9);...

  • please help!!!! JAVA I done the project expect one part but I still give you all...

    please help!!!! JAVA I done the project expect one part but I still give you all the detail that you needed... and I will post my code please help me fix the CreateGrid() part in main and make GUI works    List Type Data Structures Overview : You will be implementing my version of a linked list. This is a linked list which has possible sublists descending from each node. These sublists are used to group together all nodes which...

  • Chapter 4 describes the ADT Sorted List using an array implementation with a maximum of 25...

    Chapter 4 describes the ADT Sorted List using an array implementation with a maximum of 25 items. The pseudocode for the ADT Sorted List Operations are provided on page 210. Use this information to create an ADT for handling a collection of Person objects, where each object will contain a Social Insurance Number (validate this), a first name, a last name, a gender and a data of birth. This implementation should prevent duplicate entries – that is, the Social Insurance...

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
ADVERTISEMENT