Question

(JAVA NetBeans)

Write programs in java

2. Modify the examples 9.13-9.15 to process the students data. Student.java /l/name, department, reg no School.java. Student

Example 9.13~9.15

//Book.Java

public class Book {

private String title;
private String author;
private double price;

/**
* default constructor
*/
public Book() {
title = "";
author = "";
price = 0.0;
}

/**
* overloaded constructor
*
* @param newTitle the value to assign to title
* @param newAuthor the value to assign to author
* @param newPrice the value to assign to price
*/
public Book(String newTitle, String newAuthor, double newPrice) {
title = newTitle;
author = newAuthor;
price = newPrice;
}

/**
* getTitle method
*
* @return the title
*/
public String getTitle() {
return title;
}

/**
* getAuthor method
*
* @return the author
*/
public String getAuthor() {
return author;
}

/**
* getPrice method
*
* @return the price
*/
public double getPrice() {
return price;
}

/**
* toString
*
* @return title, author, and price
*/
public String toString() {
return ("title: " + title + "\t"
+ "author: " + author + "\t"
+ "price: " + price);
}
}

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

//BookSearchEngine.java

import java.util.ArrayList;
import javax.swing.JOptionPane;

public class BookSearchEngine {

public static void main(String[] args) {
BookStore bs = new BookStore();
String keyword = JOptionPane.showInputDialog(null,
"Enter a keyword");
System.out.println("Our book collection is:");
System.out.println(bs.toString());

ArrayList results = bs.searchForTitle(keyword);

System.out.println("The search results for " + keyword
+ " are:");
for (Book tempBook : results) {
System.out.println(tempBook.toString());
}
}
}

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

//BookStore.java

import java.util.ArrayList;

public class BookStore {

private ArrayList library;

/**
* default constructor instantiates ArrayList of Books
*/
public BookStore() {
library = new ArrayList();
library.add(new Book("Intro to Java", "James", 56.99));
library.add(new Book("Advanced Java", "Green", 65.99));
library.add(new Book("Java Servlets", "Brown", 75.99));
library.add(new Book("Intro to HTML", "James", 29.49));
library.add(new Book("Intro to Flash", "James", 34.99));
library.add(new Book("Advanced HTML", "Green", 56.99));
library.trimToSize();
}

/**
* toString
*
* @return each book in library, one per line
*/
public String toString() {
String result = "";
for (Book tempBook : library) {
result += tempBook.toString() + "\n";
}
return result;
}

/**
* Generates list of books containing searchString
*
* @param searchString the keyword to search for
* @return the ArrayList of books containing the keyword
*/
public ArrayList searchForTitle(String searchString) {
ArrayList searchResult = new ArrayList();
for (Book currentBook : library) {
if ((currentBook.getTitle()).indexOf(searchString) != -1) {
searchResult.add(currentBook);
}
}
searchResult.trimToSize();
return searchResult;
}
}

DBookSearch Engine.java X Bookjava 1eimport java.util.ArrayList; 2 import javax.swing.Joption Pane ; BookStore.java SECOND ED

2. Modify the examples 9.13-9.15 to process the students' data. Student.java /l/name, department, reg no School.java. StudentSearchEngine.java /lone can search student by name or department or reg no
DBookSearch Engine.java X Bookjava 1eimport java.util.ArrayList; 2 import javax.swing.Joption Pane ; BookStore.java SECOND EDITION 3 ava 4 public class BookSearchEngine 5 { 6e public static void main String args ) 輸入 7 BookStore bs new BookStore (); String keyword JoptionPane. showInputDialog ( null, 8 Enter a keyword ? Java 10 "Enter a keyword" ); 確定 取消 11 System.out.println( "Our book collection is:"); System.out.println( bs.toString ); 12 13 ArrayList results bs.searchForTitle keyword ); 14 System.out.println "The search results for " keyword are:"); 16 17 + for Book tempBook: results 18 System.out. println ( tempBook.tostring (); 19 20 bur book collection is: title: Intro to Java title: Advanced Java title: Java Servlets title: Intro to HTML title: Intro to Flash title: Advanced HTML 21 author: James price: 56.99 price: 65.99 price: 75.99 price: 29.49 price: 34.99 price: 56.99 22 author: Green author Brown author: James author: James author: Green The search results for Java are title: Intro to Java title: Advanced Java title: Java Servlets price: 56.99 price: 65.99 price: 75.99 author: James author: Green author: Brown 61 ett Publishers, Inc. (www.jbpub.com)
0 0
Add a comment Improve this question Transcribed image text
Answer #2
Thanks for the question.

Here is the completed code for this problem.  Let me know if you have any doubts or if you need anything to change.

Thank You !!

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

public class Student {

    private String studentName;
    private String departmentName;
    private int registrationNumber;


    public Student() {
        studentName=departmentName="";
        registrationNumber=0;
    }

    public Student(String studentName, String departmentName, int registrationNumber) {
        this.studentName = studentName;
        this.departmentName = departmentName;
        this.registrationNumber = registrationNumber;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }

    public int getRegistrationNumber() {
        return registrationNumber;
    }

    public void setRegistrationNumber(int registrationNumber) {
        this.registrationNumber = registrationNumber;
    }

    @Override
    public String toString() {
        return 
                "Student Name: " + studentName + 
                ", Department Name: " + departmentName + 
                ", Registration No: " + registrationNumber ;
                
    }
    
    
}

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

import java.util.ArrayList;

public class School {

    private ArrayList<Student> students;

    public School() {
        students = new ArrayList<Student>();
    }

    public void addStudent(Student aStudent) {
        students.add(aStudent);
    }

    
@Override
public String toString() {
    StringBuffer studentInformation = new StringBuffer();
    for(Student student:students){
        studentInformation.append(student).append("\n");
    }
    return studentInformation.toString();
}


}

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

import java.util.ArrayList;

public class StudentSearchEngine {

    // method that takes in a name and returns
    // an array list of all students having the same name
    public static ArrayList<Student> searchByName(ArrayList<Student> students, String studentName) {
        ArrayList<Student> sameNameList = new ArrayList<Student>();
        for (Student student : students) {
            if (student.getStudentName().equalsIgnoreCase(studentName)) {
                sameNameList.add(student);
            }
        }
        return sameNameList;
    }

    // method that takes in a department name and returns
    // an array list of all students having the same department name
    public static  ArrayList<Student> searchByDepartment(ArrayList<Student> students, String department) {
        ArrayList<Student> sameDepartmentList = new ArrayList<Student>();
        for (Student student : students) {
            if (student.getDepartmentName().equalsIgnoreCase(department)) {
                sameDepartmentList.add(student);
            }
        }
        return sameDepartmentList;
    }

    // method that takes in a registration number and returns
    // an array list of all students having the same registration number
    public static ArrayList<Student> searchByRegistration(ArrayList<Student> students, int registration) {
        ArrayList<Student> sameRegistrationList = new ArrayList<Student>();
        for (Student student : students) {
            if (student.getRegistrationNumber() == registration) {
                sameRegistrationList.add(student);
            }
        }
        return sameRegistrationList;
    }
}

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

Add a comment
Answer #1

Solution :

Java classes are given below :

1) Student.java :

// Student.java
public class Student{
private String name, department, reg_no;
Student(){
name = "";
department = "";
reg_no = "";
}

Student(String n, String d, String r){
name = n;
department = d;
reg_no = r;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDepartment() {
return department;
}

public void setDepartment(String department) {
this.department = department;
}

public String getReg_no() {
return reg_no;
}

public void setReg_no(String reg_no) {
this.reg_no = reg_no;
}

public String toString(){
String result = "";
result = "Name : " + name + ", Department : "+ department + ", Registration number : "+ reg_no;
return result;
}
  
}

2) School.java :

import java.util.ArrayList;

public class School{
private ArrayList<Student> students;

public School(){
students = new ArrayList<Student>();
students.add(new Student("student1", "computer science", "111AAA"));
students.add(new Student("student2", "environment science", "222AAA"));
students.add(new Student("student3", "mechanical engineering", "333AAA"));
students.add(new Student("student4", "psychology", "444AAA"));
students.add(new Student("student5", "computer science", "555AAA"));
students.add(new Student("student6", "computer science", "666AAA"));
students.add(new Student("student7", "psychocoly", "777AAA"));
students.add(new Student("student8", "environment science", "888AAA"));
students.add(new Student("student9", "mechanical engineering", "999AAA"));
students.add(new Student("student10", "computer science", "111BBB"));
}

public String toString(){
String result = "";
for(int i=0; i<students.size(); i++){
Student student = students.get(i);
result = result + student.toString() + "\n";
}
return result;
}

public ArrayList searchForName(String searchString){
ArrayList searchResult = new ArrayList<>();
for(int i=0; i<students.size(); i++){
Student student = students.get(i);
if((student.getName()).toLowerCase().indexOf(searchString.toLowerCase()) != -1){
searchResult.add(student);
}
}
return searchResult;
}

public ArrayList searchForDepartment(String searchString){
ArrayList searchResult = new ArrayList<>();
for(int i=0; i<students.size(); i++){
Student student = students.get(i);
if((student.getDepartment()).toLowerCase().indexOf(searchString.toLowerCase()) != -1){
searchResult.add(student);
}
}
return searchResult;
}

public ArrayList searchForRegNo(String searchString){
ArrayList searchResult = new ArrayList<>();
for(int i=0; i<students.size(); i++){
Student student = students.get(i);
if((student.getReg_no()).toLowerCase().indexOf(searchString.toLowerCase()) != -1){
searchResult.add(student);
}
}
return searchResult;
}
}

3) StudentSearchEngine.java :

import java.util.ArrayList;
import javax.swing.JOptionPane;
public class StudentSearchEngine {
public static void main(String[] args) {
School school = new School();
int flag = 0;
int choice;
while(flag == 0){
choice = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter your search criteria(1 for name, 2 for department, 3 for registration number, 4 for exit) : "));
if(choice == 1){
String keyword = JOptionPane.showInputDialog(null,"Enter a keyword");
System.out.println("Students in school :");
System.out.println(school.toString());
ArrayList<Student> results = school.searchForName(keyword);
System.out.println("The search results for name for keyword " + keyword+ " are:");
for (Student student : results) {
System.out.println(student.toString());
}
System.out.println();
}
else if(choice == 2){
String keyword = JOptionPane.showInputDialog(null,"Enter a keyword");
System.out.println("Students in school :");
System.out.println(school.toString());
ArrayList<Student> results = school.searchForDepartment(keyword);
System.out.println("The search results for Department for keyword " + keyword+ " are:");
for (Student student : results) {
System.out.println(student.toString());
}
System.out.println();
}
else if(choice == 3){
String keyword = JOptionPane.showInputDialog(null,"Enter a keyword");
System.out.println("Students in school :");
System.out.println(school.toString());
ArrayList<Student> results = school.searchForRegNo(keyword);
System.out.println("The search results for registration number for keyword " + keyword+ " are:");
for (Student student : results) {
System.out.println(student.toString());
}
System.out.println();
}
else if(choice == 4){
flag = 1;
}
else{
System.out.println("Invalid input. Please try again. ");
}
}
  
}
}

Sample outputs :

Input X Enter your search criteria(1 for name, 2 for department, 3 for registration number, 4 for exit) OK Cancel

Input X Enter a keyword ? BBE OK Cancel

Students in school student1, Department computer science, Registration number 111AAA environment science, Registration number

if you have any doubts then you can ask in comment section if you find the solution helpful then upvote the answer. Thank you.

Add a comment
Know the answer?
Add Answer to:
(JAVA NetBeans) Write programs in java Example 9.13~9.15 //Book.Java public class Book { private String title; private...
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
  • Language is Java! Write a class named Book that has two public String variables named title,...

    Language is Java! Write a class named Book that has two public String variables named title, and author. Do not add or change any other code.

  • public class Song { private String title; private String artist; private int duration; public Song() {...

    public class Song { private String title; private String artist; private int duration; public Song() { this("", "", 0, 0); } public Song(String t, String a, int m, int s) { title = t; artist = a; duration = m * 60 + s; } public String getTitle() { return title; } public String getArtist() { return artist; } public int getDuration() { return duration; } public int getMinutes() { return duration / 60; } public int getSeconds() { return...

  • JAVA /** * This class stores information about an instructor. */ public class Instructor { private...

    JAVA /** * This class stores information about an instructor. */ public class Instructor { private String lastName, // Last name firstName, // First name officeNumber; // Office number /** * This constructor accepts arguments for the * last name, first name, and office number. */ public Instructor(String lname, String fname, String office) { lastName = lname; firstName = fname; officeNumber = office; } /** * The set method sets each field. */ public void set(String lname, String fname, String...

  • JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet {    ...

    JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet {     /**      * Searches through the ArrayList arr, from the first index to the last, returning an ArrayList      * containing all the indexes of Strings in arr that match String s. For this method, two Strings,      * p and q, match when p.equals(q) returns true or if both of the compared references are null.      *      * @param s The string...

  • How to build Java test class? I am supposed to create both a recipe class, and...

    How to build Java test class? I am supposed to create both a recipe class, and then a class tester to test the recipe class. Below is what I have for the recipe class, but I have no idea what/or how I am supposed to go about creating the test class. Am I supposed to somehow call the recipe class within the test class? if so, how? Thanks in advance! This is my recipe class: package steppingstone5_recipe; /** * *...

  • JAVA help Create a class Individual, which has: Instance variables for name and phone number of...

    JAVA help Create a class Individual, which has: Instance variables for name and phone number of individual. Add required constructors, accessor, mutator, toString() , and equals method. Use array to implement a simple phone book , by making an array of objects that stores the name and corresponding phone number. The program should allow searching in phone book for a record based on name, and displaying the record if the record is found. An appropriate message should be displayed if...

  • (JAVA NetBeans) Write programs in java Modify the textbook example textbook example: //BankAccount.java import java.tex...

    (JAVA NetBeans) Write programs in java Modify the textbook example textbook example: //BankAccount.java import java.text.DecimalFormat; public class BankAccount { public final DecimalFormat MONEY = new DecimalFormat( "$#,##0.00" ); private double balance; /** Default constructor * sets balance to 0.0 */ public BankAccount( ) { balance = 0.0; System.out.println( "In BankAccount default constructor" ); } /** Overloaded constructor * @param startBalance beginning balance */ public BankAccount( double startBalance ) { if ( balance >= 0.0 ) balance = startBalance; else balance...

  • Need Help Using Java programming only import java.util.ArrayList; //Import anything you need here public class Dictionary...

    Need Help Using Java programming only import java.util.ArrayList; //Import anything you need here public class Dictionary {     // Declare any variables you feel necessary here     public Dictionary(/*DO NOT PUT ANY PARAMETERS HERE!*/) {         // Initialise those variables     }     /***      * @return number of terms currently in the dictionary hint: starts at 0 when      *         there are no terms      */     public int numberOfTerms() {         return 0;     }     /***     ...

  • Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args)...

    Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); final int maxSize = 128; String[] titles = new String[maxSize]; int[] lengths = new int[maxSize]; int numDVDs = 0; String op; op = menu(stdIn); System.out.println(); while (!op.equalsIgnoreCase("q")) { if (op.equalsIgnoreCase("a")) { if (numDVDs < maxSize) numDVDs = addDVD(titles, lengths, numDVDs, stdIn); } else if (op.equalsIgnoreCase("t")) searchByTitle(titles, lengths, numDVDs, stdIn);    else if (op.equalsIgnoreCase("l")) searchByLength(titles, lengths, numDVDs, stdIn); System.out.println('\n');...

  • Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a...

    Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a string s as a parameter and returns true if the title of the string is equal to s. Create the same method for your library material copies. Note that it will need to be abstract in the LibraryMaterialCopy class MY CODE **************************************************************** LibraryCard: import java.util.List; import java.util.ArrayList; import java.time.LocalDate; import    java.time.temporal.ChronoUnit; public class LibraryCard {    private String id;    private String cardholderName;   ...

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