Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

HELP IN JAVA: Need help write a code that will search for (and list) all the people who were born in a certain month Person.java:

HELP IN JAVA: Need help write a code that will search for (and list) all the people who were born in a certain month

Person.java:

public class Person {

private String firstName; private String lastName; private String birthDate; private String phoneNumber;

public Person(String firstName, String lastName, String birthDate, String phoneNumber) { this.firstName = firstName; this.lastName = lastName; this.birthDate = birthDate; this.phoneNumber = phoneNumber; }

public String getFirstName() { return firstName; }

public void setFirstName(String firstName) { this.firstName = firstName; }

public String getLastName() { return lastName; }

public void setLastName(String lastName) { this.lastName = lastName; }

public String getBirthDate() { return birthDate; }

public void setBirthDate(String birthDate) { this.birthDate = birthDate; }

public String getPhoneNumber() { return phoneNumber; }

public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public String toString() { return firstName + " " + lastName + " " + birthDate + " " + phoneNumber; } public int compareTo(Person another) { if (this.lastName.compareTo(another.getLastName())>0) return 1; else if (this.lastName.compareTo(another.getLastName())<0) return -1; else return 0; } }

BinaryTreeNode.java:

public class BinaryTreeNode { private T element; private BinaryTreeNode left, right;

/** * Creates a new tree node with the specified data. * * @param obj the element that will become a part of the new tree node */ BinaryTreeNode (T obj) { element = obj; left = null; right = null; }

public T getValue(){ return element; }

public T getElement() { return element; }

public void setElement(T element) { this.element = element; }

public BinaryTreeNode getLeft() { return left; }

public void setLeft(BinaryTreeNode left) { this.left = left; }

public BinaryTreeNode getRight() { return right; }

public void setRight(BinaryTreeNode right) { this.right = right; }

}

H9.java:

import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner;

public class H9 { public static void inOrder(BinaryTreeNode t) { if (t != null) { inOrder(t.getLeft()); System.out.println(t.getValue()); inOrder(t.getRight());

} } private static String lookup(BinaryTreeNode curr, String temp) { String foundperson = null; if (curr != null) { Person currValue; currValue = (Person)curr.getValue(); if (temp.equals(currValue.getLastName())){ foundperson = currValue.getFirstName() + " " + currValue.getLastName() + " " + currValue.getBirthDate() + " " + currValue.getPhoneNumber(); return foundperson; } else if (currValue.getLastName().compareTo(temp)==1) { return lookup(curr.getLeft(), temp); } else { return lookup(curr.getRight(), temp); } } return foundperson; } public static void TreeInsert(BinaryTreeNode curr, Person num){ BinaryTreeNode b=new BinaryTreeNode(num); while (curr != null) { Person currValue= (Person)curr.getValue(); if (num.compareTo(currValue) < 0){ if (curr.getLeft() == null) { curr.setLeft(b); break; } else { curr = curr.getLeft(); } } else { if (curr.getRight() == null) { curr.setRight(b); break; } else { curr = curr.getRight(); } } } } public static void main(String[] args) { File File1 = new File("H9Input.txt"); Scanner fileInput = null; Scanner s = new Scanner (System.in); try { fileInput = new Scanner(File1); } catch (FileNotFoundException e) { } Person rootPerson = new Person(fileInput.next(), fileInput.next(), fileInput.next(), fileInput.next()); BinaryTreeNode root=new BinaryTreeNode(rootPerson); String userInput = null; while(fileInput.hasNextLine()){ Person newPerson = new Person(fileInput.next(), fileInput.next(), fileInput.next(), fileInput.next()); if (newPerson.compareTo(rootPerson) != 0){ TreeInsert(root, newPerson); } else rootPerson = newPerson; } int menuInput; do{ System.out.println("Enter 1 to add another Person"); System.out.println("Enter 2 to modify a Person's phone number"); System.out.println("Enter 3 to search for specific Person's info"); System.out.println("Enter 4 to view all availble info"); System.out.println("Enter 5 to search according to birth month"); System.out.println("Enter 6 to exit"); System.out.println(); menuInput = s.nextInt(); switch (menuInput) { case 1: System.out.println("Enter new person's first name, last name, birthdate, and phone number:"); System.out.println(); Person temp = new Person(s.next(), s.next(), s.next(), s.next()); TreeInsert(root, temp); break; case 2: System.out.println("Enter the person's last name:"); userInput = s.next(); break; case 3: System.out.println("Enter the person's last name:"); userInput = s.next(); System.out.println("Found person: "+lookup(root, userInput)); break; case 4: System.out.println("All available data:"); inOrder(root); case 5: } } while (menuInput !=6); } }

Need help writing the case 5 code only that will get the info of all the people who were born in the same given month

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

Students also viewed these Databases questions