My name is Yuji Shimojo. I am from Okinawa, Japan and live there now. I currently work at an IT company. I am taking CMSC335 because it is required to obtain my B.S. in Computer Science. I used to study in Information Engineering at University of the Ryukyus in Okinawa until 3 yeas ago. I took CMIS242 the last term. I really hope to enjoy learning in this class. I am using Eclipse Indigo on Mac OS X Snow Leopard (10.6).
#ref(): File not found: "Homework1(YujiShimojo).docx" at page "UMUC Asia - CMSC335 - Object-Oriented and Concurrent Programming"
For this week, please consider a pair of concrete classes in the JDK in a parent-child relationship and discuss a few polymorphics and/or overloaded methods.
Polymorphism is a mechanism in object-oriented programming that enables to the programmer to send a message to objects but make them work differently as a function of each objects. The advantage of using polymorphism is that the programmer doesn't have to modify the caller method to change the program behaviors.
For example, if there are three classes named Shape, Rectangle, and Triangle, and Shape is the parent of other two classes, polymorphism is utilized. If you want to calculate the area of them, you can define a getArea method in Shape class as an abstract method. The function of this method is only to draw. Then, you can define another getArea methods, in which are implemented detailed functions, in Rectangle and Triangle class.
Method overloading is one of the most important functions to work polymorphism that is to define two or more methods of a same name with a different return value and number/type of arguments within the same class.
#ref(): File not found: "Project1_YujiShimojo(LibraryTest.java).docx" at page "UMUC Asia - CMSC335 - Object-Oriented and Concurrent Programming"
public class Author { //attributes of Author class String name; String phone; String street, city, state; Integer zipCode; // constructor Author(String _name, String _city, String _state, String _street, Integer _zipCode, String _phone) { name = _name; city = _city; state = _state; street = _street; zipCode = _zipCode; phone = _phone; } // override the toString method @Override public String toString() { String author = name + ":" + street + ":" + city + ":" + state + ":" + zipCode + ":" + phone + "\n"; return author; }
public class Book { //attributes of Book class String title, genre; String price; Integer authorIndex; // constructor Book(String _title, String _genre, String _price, Integer _authorIndex) { title = _title; genre = _genre; price = _price; authorIndex = _authorIndex; } // override the toString method @Override public String toString() { String book = title + ":" + genre + ":" + price + ":" + authorIndex + "\n"; return book; } }
import java.util.*; import java.util.Comparator; public class Library { // instantiate an Author and a Book objects public ArrayList<Author> authors; public ArrayList<Book> books; //constructor Library() { authors = new ArrayList<Author>(); books = new ArrayList<Book>(); } private int indexBook; private Book b; private ArrayList<Book> array = new ArrayList<Book>(); // search on book title // queryTitle is from user input. public Book searchTitle(String queryTitle) { Iterator<Book> it = books.iterator(); while (it.hasNext()) { b = it.next(); indexBook = b.title.indexOf(queryTitle); if (indexBook != -1){ return b; } } return null; } // search on book genre // queryGenre is from user input. public ArrayList<Book> searchGenre(String queryGenre) { array.clear(); Iterator<Book> it = books.iterator(); while (it.hasNext()) { b = it.next(); indexBook = b.genre.indexOf(queryGenre); if (indexBook != -1){ array.add(b); } } if (!array.isEmpty()) { return array; } else { return null; } } // search on author index // queryAuthorIndex is from user input. public ArrayList<Book> searchAuthorIndex(int queryAuthorIndex) { array.clear(); Iterator<Book> it = books.iterator(); while (it.hasNext()) { b = it.next(); if (queryAuthorIndex == b.authorIndex){ array.add(b); } } if (!array.isEmpty()) { return array; } else { return null; } } }
import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class LibraryTest implements ActionListener { //declare Swing components as instance variables static Library library; static JFrame outputFrame; static JPanel panel1, panel2, panel3; static Container container; static JLabel label1, label2, label3; static JTextField textField1, textField2, textField3; static JButton button1, button2, button3; static JTextArea textArea; public static void main(String args[]){ // instantiate a Library object library = new Library(); // create instances of authors library.authors.add(new Author("Dan Brown", "123 Street", "Concord", "New Hampshire", 3301, "603-123-456")); library.authors.add(new Author("Stephen R. Covey", "456 Street", "Salt Lake City", "Utah", 84101, "385-456-7890")); library.authors.add(new Author("Tina Seelig", "450 Serra Mall", "Stanford", "California", 94305, "650-725-1627")); library.authors.add(new Author("Y. Daniel Liang", "11935 Abercorn Street", "Savannah", "Georgia", 314191997, "912-344-3264")); library.authors.add(new Author("Ray Murphy", "University Road", "Galway", "County Galway, Ireland", 904018, "353-91-493081")); library.authors.add(new Author("Walter Isaacson", "789 Street", "New Orleans", "Louisiana", 70112, "504-789-0123")); // create instances of books library.books.add(new Book("The Lost Symbol", "Mystery & Thrillers", "$10", 0)); library.books.add(new Book("Angels & Demons", "Mystery & Thrillers", "$16", 0)); library.books.add(new Book("The Da Vinci Code", "Mystery & Thrillers", "$10", 0)); library.books.add(new Book("Deception Point", "Mystery & Thrillers", "$16", 0)); library.books.add(new Book("Digital Fortress", "Mystery & Thrillers", "$9", 0)); library.books.add(new Book("The 7 Habits of Highly Effective People", "Business & Investing", "$16", 1)); library.books.add(new Book("The 8th Habit: From Effectiveness to Greatness", "Business & Investing", "$16", 1)); library.books.add(new Book("The 3rd Alternative: Solving Life's Most Difficult Problems", "Business & Investing", "$16", 1)); library.books.add(new Book("What I Wish I Knew When I Was 20", "Business & Investing", "$23", 2)); library.books.add(new Book("Introduction to Java Programming, Comprehensive", "Computers & Technology", "$129", 3)); library.books.add(new Book("English Grammar In Use", "Education & Reference", "$36", 4)); library.books.add(new Book("Steve Jobs", "Biographies & Memoirs", "$30", 5)); // instantiate a LibraryTest object that invokes libraryGUI() method LibraryTest viewer = new LibraryTest(); viewer.libraryGUI(); } public void libraryGUI() { // instantiate a JFrame object outputFrame = new JFrame(); outputFrame.setSize(1100,300); outputFrame.setTitle("LibraryTest"); outputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); container = outputFrame.getContentPane(); container.setLayout( new FlowLayout() ); // instantiate a JPanel object panel1 = new JPanel(); // instantiate a JLabel object, add it to container and panel label1 = new JLabel( "Title :" ); container.add( label1 ); panel1.add(label1); textField1 = new JTextField( "", 10 ); container.add( textField1 ); panel1.add(textField1); //handler is the actionPerformed() below. textField1.addActionListener( this ); button1 = new JButton( "Search on Title" ); container.add( button1 ); panel1.add(button1); button1.addActionListener( this ); outputFrame.add(panel1); panel2 = new JPanel(); label2 = new JLabel( "Genre :" ); container.add( label2 ); panel2.add(label2); textField2 = new JTextField( "", 10 ); container.add( textField2 ); panel2.add(textField2); textField2.addActionListener( this ); button2 = new JButton( "Search on Genre" ); container.add( button2 ); panel2.add(button2); button2.addActionListener( this ); outputFrame.add(panel2); panel3 = new JPanel(); label3 = new JLabel( "Author Index :" ); container.add( label3 ); panel3.add(label3); textField3 = new JTextField( "", 2 ); container.add( textField3 ); panel3.add(textField3); textField3.addActionListener( this ); button3 = new JButton( "Search on author index" ); container.add( button3 ); panel3.add(button3); button3.addActionListener( this ); outputFrame.add(panel3); //create a textarea, attach a scrollbar, add to container. textArea = new JTextArea(10,75); container.add( new JScrollPane(textArea)); outputFrame.setVisible(true); } // event handler for JButton, JTextField events. public void actionPerformed( ActionEvent event ) { //access the text String queryTitle = textField1.getText(); String queryGenre = textField2.getText(); String queryAuthorIndex = textField3.getText(); if ( event.getSource() == button1 ) { textArea.setText(""); // change the text if (!queryTitle.equals("")) { Book resultTitle = this.library.searchTitle(queryTitle); if (resultTitle == null) { textArea.append("\"" + queryTitle + "\" is not found in the library.\n"); } else { textArea.append(resultTitle.toString() + "\n"); } } else { textArea.append("Enter a book title.\n"); } } else if ( event.getSource() == button2 ) { textArea.setText(""); // change the text if (!queryGenre.equals("")) { ArrayList<Book> resultGenre = this.library.searchGenre(queryGenre); if (resultGenre == null) { textArea.append("\"" + queryGenre + "\" is not found in the library.\n"); } else { textArea.append(resultGenre.toString() + "\n"); } } else { textArea.append("Enter a book genre.\n"); } } else if ( event.getSource() == button3 ) { textArea.setText(""); // change the text if (!queryAuthorIndex.equals("")) { int queryAuthorIndexInt = Integer.valueOf(queryAuthorIndex); ArrayList<Book> resultAuthorIndex = this.library.searchAuthorIndex(queryAuthorIndexInt); if (resultAuthorIndex == null) { textArea.append("\"" + queryAuthorIndex + "\" is not found in the library.\n"); } else { textArea.append(resultAuthorIndex.toString() + "\n"); } } else { textArea.append("Enter an author index.\n"); } } } }
For this week, please consider the taxonomy you defined in Week 1 and define an interface that could be used in your class hierarchy. Show the methods defined in the interface and describe how the interface fits in your design.
I created two interfaces for my MobilePhoneTaxonomy as below.
// MobilePhone.java public interface MobilePhone { // make a call. void actionCall(String name); // send an email. void sendMail(String email, String subject, String text); }
// Smartphone.java public interface Smartphone extends MobilePhone { // get a touch event. void onTouchEvent(); // get an accelerator sensor event. void accelSensor(); // get a geolocation. void getGeoLocation(); }
MobilePhone.java is a super interface, and Smartphone.java is its sub interface. Here is a sample implementation of the interfaces below. All the methods defined in the interfaces are overridden in Android.java. An instance of Android object is created in InterfaceTest.java.
// Android.java public class Android implements Smartphone { @Override public void actionCall(String name) { System.out.println("Calling " + name); } @Override public void sendMail(String email, String subject, String text) { System.out.println("Your message to " + email + " has been sent."); } @Override public void onTouchEvent() { System.out.println("Got your x and y coordinates."); } @Override public void accelSensor() { System.out.println("Got your x, y and z accelerations."); } @Override public void getGeoLocation() { System.out.println("Got your latitude and longitude."); } }
#ref(): File not found: "Homework2(YujiShimojo).docx" at page "UMUC Asia - CMSC335 - Object-Oriented and Concurrent Programming"
For this week, please pick one of the programming examples at the end of Chapters 16 or 17 in Liang and describe the JDK AWT and Swing classes needed by the program specified by that exercise.
I choose exercise 17.6 which is a simple speed unit converter. It converts miles to kilometers and vice versa quickly. The program is needed the following JDK AWT and Swing classes.
JFrame is a subclass of Frame class which creates a window. It is a basic class for Swing GUIs.
JPanel is a subclass of Container class. It allows the user to set a layout of some components at once.
JLabel allows the user to set character strings or images, fonts, and sizes.
JTextField creates a single-row input box from the user.
GridLayout is a layout manager. Components are divided into uniformly sized areas in a grid by setting the number of rows and columns.
BorderLayout is a layout manager. Components are divided into left, right, top, bottom, and center.
#ref(): File not found: "Project2_YujiShimojo(LibraryTestOfMap.java).docx" at page "UMUC Asia - CMSC335 - Object-Oriented and Concurrent Programming"
public class Book { //attributes of Book class String title, genre; Integer price, authorIndex; // constructor Book(String _title, String _genre, Integer _price, Integer _authorIndex) { title = _title; genre = _genre; price = _price; authorIndex = _authorIndex; } // override the toString method @Override public String toString() { String book = title + ":" + genre + ":$" + price + ":" + authorIndex + "\n"; return book; } }
import java.util.Comparator; public class BookAuthorIndexComparator implements Comparator<Book> { public int compare(Book book1, Book book2) { if (book1.authorIndex.compareTo(book2.authorIndex) < 0) { return -1; } else if (book1.authorIndex.compareTo(book2.authorIndex) == 0) { return 0; } else { return 1; } } }
import java.util.Comparator; public class BookGenreComparator implements Comparator<Book> { public int compare(Book book1, Book book2) { if (book1.genre.compareTo(book2.genre) < 0) { return -1; } else if (book1.genre.compareTo(book2.genre) == 0) { return 0; } else { return 1; } } }
import java.util.Comparator; public class BookPriceComparator implements Comparator<Book> { public int compare(Book book1, Book book2) { if (book1.price.compareTo(book2.price) < 0) { return -1; } else if (book1.price.compareTo(book2.price) == 0) { return 0; } else { return 1; } } }
import java.util.Comparator; public class BookTitleComparator implements Comparator<Book> { public int compare(Book book1, Book book2) { if (book1.title.compareTo(book2.title) < 0) { return -1; } else if (book1.title.compareTo(book2.title) == 0) { return 0; } else { return 1; } } }
import java.util.*; public class Library { // instantiate an Author and a Book objects public Map authors; public Map books; public ArrayList genreList; public ArrayList authorIndexList; //constructor Library() { authors = new HashMap<String,Author>(); books = new HashMap<String,Book>(); genreList = new ArrayList<Book>(); authorIndexList = new ArrayList<Book>(); } // search on book title // queryTitle is from user input. public Book searchTitle(String queryTitle) { return (Book) books.get(queryTitle); } private int indexBook; private ArrayList<Book> array = new ArrayList<Book>(); private Book b; // search on book genre // queryGenre is from user input. public ArrayList<Book> searchGenre(String queryGenre) { array.clear(); Iterator<Book> it = genreList.iterator(); while (it.hasNext()) { b = it.next(); indexBook = b.genre.indexOf(queryGenre); if (indexBook != -1){ array.add(b); } } if (!array.isEmpty()) { return array; } else { return null; } } // search on author index // queryAuthorIndex is from user input. public ArrayList<Book> searchAuthorIndex(int queryAuthorIndex) { array.clear(); Iterator<Book> it = authorIndexList.iterator(); while (it.hasNext()) { b = it.next(); if (queryAuthorIndex == b.authorIndex){ array.add(b); } } if (!array.isEmpty()) { return array; } else { return null; } } }
import java.awt.event.ActionListener; import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.table.DefaultTableModel; public class LibraryTestOfMap implements ActionListener, ItemListener { //declare Swing components as instance variables static Library library; static JFrame outputFrame; static JPanel panel1, panel2, panel3, panel4; static Container container; static JLabel label1, label2, label3, label4; static JTextField textField1, textField2, textField3; static JButton button1, button2, button3, button4; static JComboBox comboBox; static JTextArea textArea; static String [] sort = {"title", "genre", "price", "author index"}; static ArrayList<Book> list; // create an ArrayList that is used in the actionPerformed() below public static void main(String args[]){ // instantiate a Library object library = new Library(); // instantiate list list = new ArrayList<Book>(); // create instances of authors library.authors.put("Dan Brown", new Author("Dan Brown", "123 Street", "Concord", "New Hampshire", 3301, "603-123-456")); library.authors.put("Stephen R. Covey", new Author("Stephen R. Covey", "456 Street", "Salt Lake City", "Utah", 84101, "385-456-7890")); library.authors.put("Tina Seelig", new Author("Tina Seelig", "450 Serra Mall", "Stanford", "California", 94305, "650-725-1627")); library.authors.put("Y. Daniel Liang", new Author("Y. Daniel Liang", "11935 Abercorn Street", "Savannah", "Georgia", 314191997, "912-344-3264")); library.authors.put("Ray Murphy", new Author("Ray Murphy", "University Road", "Galway", "County Galway, Ireland", 904018, "353-91-493081")); library.authors.put("Walter Isaacson", new Author("Walter Isaacson", "789 Street", "New Orleans", "Louisiana", 70112, "504-789-0123")); // create instances of books // add key/value pairs to a HashMap object library.books.put("The Lost Symbol", new Book("The Lost Symbol", "Mystery & Thrillers", 10, 0)); library.books.put("Angels & Demons", new Book("Angels & Demons", "Mystery & Thrillers", 16, 0)); library.books.put("The Da Vinci Code", new Book("The Da Vinci Code", "Mystery & Thrillers", 10, 0)); library.books.put("Deception Point", new Book("Deception Point", "Mystery & Thrillers", 16, 0)); library.books.put("Digital Fortress", new Book("Digital Fortress", "Mystery & Thrillers", 9, 0)); library.books.put("The 7 Habits of Highly Effective People", new Book("The 7 Habits of Highly Effective People", "Business & Investing", 16, 1)); library.books.put("The 8th Habit: From Effectiveness to Greatness", new Book("The 8th Habit: From Effectiveness to Greatness", "Business & Investing", 16, 1)); library.books.put("The 3rd Alternative: Solving Life's Most Difficult Problems", new Book("The 3rd Alternative: Solving Life's Most Difficult Problems", "Business & Investing", 16, 1)); library.books.put("What I Wish I Knew When I Was 20", new Book("What I Wish I Knew When I Was 20", "Business & Investing", 23, 2)); library.books.put("Introduction to Java Programming, Comprehensive", new Book("Introduction to Java Programming, Comprehensive", "Computers & Technology", 129, 3)); library.books.put("English Grammar In Use", new Book("English Grammar In Use", "Education & Reference", 36, 4)); library.books.put("Steve Jobs", new Book("Steve Jobs", "Biographies & Memoirs", 30, 5)); library.books.put("Java Concurrency in Practice", new Book("Java Concurrency in Practice", "Computers & Technology", 60, 11)); library.genreList.add (new Book("The Lost Symbol", "Mystery & Thrillers", 10, 0)); library.genreList.add (new Book("Angels & Demons", "Mystery & Thrillers", 16, 0)); library.genreList.add (new Book("The Da Vinci Code", "Mystery & Thrillers", 10, 0)); library.genreList.add (new Book("Deception Point", "Mystery & Thrillers", 16, 0)); library.genreList.add (new Book("Digital Fortress", "Mystery & Thrillers", 9, 0)); library.books.put("Mystery & Thrillers", library.genreList); library.genreList.add (new Book("The 7 Habits of Highly Effective People", "Business & Investing", 16, 1)); library.genreList.add (new Book("The 8th Habit: From Effectiveness to Greatness", "Business & Investing", 16, 1)); library.genreList.add (new Book("The 3rd Alternative: Solving Life's Most Difficult Problems", "Business & Investing", 16, 1)); library.genreList.add (new Book("What I Wish I Knew When I Was 20", "Business & Investing", 23, 2)); library.books.put("Business & Investing", library.genreList); library.genreList.add (new Book("Introduction to Java Programming, Comprehensive", "Computers & Technology", 129, 3)); library.genreList.add (new Book("Java Concurrency in Practice", "Computers & Technology", 60, 11)); library.books.put("Computers & Technology", library.genreList); library.genreList.add (new Book("English Grammar In Use", "Education & Reference", 36, 4)); library.books.put("Education & Reference", library.genreList); library.genreList.add (new Book("Steve Jobs", "Biographies & Memoirs", 30, 5)); library.books.put("Biographies & Memoirs", library.genreList); library.authorIndexList.add (new Book("The Lost Symbol", "Mystery & Thrillers", 10, 0)); library.authorIndexList.add (new Book("Angels & Demons", "Mystery & Thrillers", 16, 0)); library.authorIndexList.add (new Book("The Da Vinci Code", "Mystery & Thrillers", 10, 0)); library.authorIndexList.add (new Book("Deception Point", "Mystery & Thrillers", 16, 0)); library.authorIndexList.add (new Book("Digital Fortress", "Mystery & Thrillers", 9, 0)); library.books.put((Integer)0, library.authorIndexList); library.authorIndexList.add (new Book("The 7 Habits of Highly Effective People", "Business & Investing", 16, 1)); library.authorIndexList.add (new Book("The 8th Habit: From Effectiveness to Greatness", "Business & Investing", 16, 1)); library.authorIndexList.add (new Book("The 3rd Alternative: Solving Life's Most Difficult Problems", "Business & Investing", 16, 1)); library.books.put((Integer)1, library.authorIndexList); library.authorIndexList.add (new Book("What I Wish I Knew When I Was 20", "Business & Investing", 23, 2)); library.books.put((Integer)2, library.authorIndexList); library.authorIndexList.add (new Book("Introduction to Java Programming, Comprehensive", "Computers & Technology", 129, 3)); library.books.put((Integer)3, library.authorIndexList); library.authorIndexList.add (new Book("English Grammar In Use", "Education & Reference", 36, 4)); library.books.put((Integer)4, library.authorIndexList); library.authorIndexList.add (new Book("Steve Jobs", "Biographies & Memoirs", 30, 5)); library.books.put((Integer)5, library.authorIndexList); library.authorIndexList.add (new Book("Java Concurrency in Practice", "Computers & Technology", 60, 11)); library.books.put((Integer)11, library.authorIndexList); // instantiate a LibraryTest object that invokes libraryGUI() method LibraryTestOfMap viewer = new LibraryTestOfMap(); viewer.libraryGUI(); } public void libraryGUI() { // instantiate a JFrame object outputFrame = new JFrame(); outputFrame.setSize(900,320); outputFrame.setTitle("LibraryTestOfMap"); outputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); container = outputFrame.getContentPane(); container.setLayout( new FlowLayout() ); // instantiate a JPanel object panel1 = new JPanel(); // instantiate a JLabel object, add it to container and panel label1 = new JLabel( "Title :" ); container.add( label1 ); panel1.add(label1); textField1 = new JTextField( "", 15 ); container.add( textField1 ); panel1.add(textField1); // handler is the actionPerformed() below textField1.addActionListener( this ); button1 = new JButton( "Search on Title" ); container.add( button1 ); panel1.add(button1); button1.addActionListener( this ); outputFrame.add(panel1); panel2 = new JPanel(); label2 = new JLabel( "Genre :" ); container.add( label2 ); panel2.add(label2); textField2 = new JTextField( "", 15 ); container.add( textField2 ); panel2.add(textField2); textField2.addActionListener( this ); button2 = new JButton( "Search on Genre" ); container.add( button2 ); panel2.add(button2); button2.addActionListener( this ); outputFrame.add(panel2); panel3 = new JPanel(); label3 = new JLabel( "Author Index :" ); container.add( label3 ); panel3.add(label3); textField3 = new JTextField( "", 3 ); container.add( textField3 ); panel3.add(textField3); textField3.addActionListener( this ); button3 = new JButton( "Search on author index" ); container.add( button3 ); panel3.add(button3); button3.addActionListener( this ); outputFrame.add(panel3); panel4 = new JPanel(); label4 = new JLabel( "Sort by :" ); container.add( label4 ); panel4.add(label4); comboBox = new JComboBox(sort); comboBox.setMaximumRowCount(4); container.add( comboBox ); panel4.add(comboBox); comboBox.addItemListener( this ); button4 = new JButton( "Sort" ); container.add( button4 ); panel4.add(button4); button4.addActionListener( this ); outputFrame.add(panel4); //create a textarea, attach a scrollbar, add to container. textArea = new JTextArea(10,65); container.add( new JScrollPane(textArea)); outputFrame.setVisible(true); } // event handler for JButton, JTextField events. public void actionPerformed( ActionEvent event ) { //access the text String queryTitle = textField1.getText(); String queryGenre = textField2.getText(); String queryAuthorIndex = textField3.getText(); if ( event.getSource() == button1 ) { if (!queryTitle.equals("")) { Book resultTitle = this.library.searchTitle(queryTitle); if (resultTitle == null) { textArea.append("\"" + queryTitle + "\" is not found in the library.\n"); } else { list.add(resultTitle); textArea.append(resultTitle.toString()); } } else { textArea.append("Enter a book title.\n"); } } else if ( event.getSource() == button2 ) { if (!queryGenre.equals("")) { ArrayList<Book> resultGenre = this.library.searchGenre(queryGenre); if (resultGenre == null) { textArea.append("\"" + queryGenre + "\" is not found in the library.\n"); } else { list.addAll(resultGenre); textArea.append(resultGenre.toString() + "\n"); } } else { textArea.append("Enter a book genre.\n"); } } else if ( event.getSource() == button3 ) { if (!queryAuthorIndex.equals("")) { int queryAuthorIndexInt = Integer.valueOf(queryAuthorIndex); ArrayList<Book> resultAuthorIndex = this.library.searchAuthorIndex(queryAuthorIndexInt); if (resultAuthorIndex == null) { textArea.append("\"" + queryAuthorIndex + "\" is not found in the library.\n"); } else { list.addAll(resultAuthorIndex); textArea.append(resultAuthorIndex.toString() + "\n"); } } else { textArea.append("Enter an author index.\n"); } } else if ( event.getSource() == button4 ) { if (comboBox.getSelectedItem() == "title") { // sort by title Collections.sort(list, new BookTitleComparator()); textArea.setText(""); textArea.append(list.toString() + "\n"); } else if (comboBox.getSelectedItem() == "genre") { // sort by genre Collections.sort(list, new BookGenreComparator()); textArea.setText(""); textArea.append(list.toString() + "\n"); } else if (comboBox.getSelectedItem() == "price") { // sort by price Collections.sort(list, new BookPriceComparator()); textArea.setText(""); textArea.append(list.toString() + "\n"); } else if (comboBox.getSelectedItem() == "author index") { // sort by author index Collections.sort(list, new BookAuthorIndexComparator()); textArea.setText(""); textArea.append(list.toString() + "\n"); } } } // event handler for JComboBox events. public void itemStateChanged( ItemEvent event ) { if ( event.getSource() == comboBox ) { if ( event.getStateChange() == ItemEvent.SELECTED ) { int i = comboBox.getSelectedIndex(); } } } }
For this week, please describe in your own words what multithreading is and how can multiple threads run simultaneously on a single-processor system? You don't need to provide code examples, just try to describe the principles in your own words.
A thread is an independent execution unit through a program. Normally, only a single thread can run on a single-processor at the same time. Multithreading in Java is the process which two or more threads run concurrently by dividing the CPU processing time into multiple small parts and executing one after another. The concept is also known as time sharing.
#ref(): File not found: "Homework3(YujiShimojo).docx" at page "UMUC Asia - CMSC335 - Object-Oriented and Concurrent Programming"
For this week, please pick one of the statements on pg 110 of Goetz and use your own words to explain why the statement is important for programmers and not just Java programmers.
4. Encapsulation makes it practical to manage the complexity. Encapsulation is the concept in Object-object programming, which is hiding fields or methods intentionally. It allows the program to decrease dependency on other modules, and increase its independency. Therefore, programmers will be easy to maintain programs efficiently. Also, abstraction of classes or operations is one of the essential elements of encapsulation.
#ref(): File not found: "Project3_YujiShimojo(LibraryTestOfJTableAndJTree.java).docx" at page "UMUC Asia - CMSC335 - Object-Oriented and Concurrent Programming"
public class Book { //attributes of Book class String title, genre; Integer price, authorIndex; // constructor Book(String _title, String _genre, Integer _price, Integer _authorIndex) { title = _title; genre = _genre; price = _price; authorIndex = _authorIndex; } // override the toString method @Override public String toString() { String book = title + ":" + genre + ":$" + price + ":" + authorIndex + "\n"; return book; } public String[] convertArray() { String[] book = {title, genre, String.valueOf(price), String.valueOf(authorIndex)}; return book; } }
import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.table.*; import javax.swing.tree.*; public class LibraryTestOfJTableAndJTree implements ActionListener, TreeSelectionListener { // declare Swing components as instance variables static Library library; static JFrame outputFrame; static JPanel panel1, panel2, panel3, panel4; static Container container; static JLabel label1, label2, label3, label4, label5, label6; static JTextField textField1, textField2, textField3; static JButton button1, button2, button3; static JScrollPane pane1, pane2; static JTable table; static DefaultTableModel tableModel; static TableRowSorter<TableModel> sorter; static JTree tree; static DefaultMutableTreeNode root, node1, node1_1, node1_2, node1_3, node1_4, node1_5, node2, node2_1, node2_2, node2_3, node2_4, node2_5, node2_6; public static void main(String args[]){ // instantiate a Library object library = new Library(); // create instances of authors library.authors.add(new Author("Dan Brown", "123 Street", "Concord", "New Hampshire", 3301, "603-123-456")); library.authors.add(new Author("Stephen R. Covey", "456 Street", "Salt Lake City", "Utah", 84101, "385-456-7890")); library.authors.add(new Author("Tina Seelig", "450 Serra Mall", "Stanford", "California", 94305, "650-725-1627")); library.authors.add(new Author("Y. Daniel Liang", "11935 Abercorn Street", "Savannah", "Georgia", 314191997, "912-344-3264")); library.authors.add(new Author("Brian Goetz", "777 Street", "Williston", "Vermont", 5495, "912-344-3264")); library.authors.add(new Author("Ray Murphy", "University Road", "Galway", "County Galway, Ireland", 904018, "802-388-8405")); library.authors.add(new Author("Walter Isaacson", "789 Street", "New Orleans", "Louisiana", 70112, "504-789-0123")); // create instances of books library.books.add(new Book("The Lost Symbol", "Mystery & Thrillers", 10, 0)); library.books.add(new Book("Angels & Demons", "Mystery & Thrillers", 16, 0)); library.books.add(new Book("The Da Vinci Code", "Mystery & Thrillers", 10, 0)); library.books.add(new Book("Deception Point", "Mystery & Thrillers", 16, 0)); library.books.add(new Book("Digital Fortress", "Mystery & Thrillers", 9, 0)); library.books.add(new Book("The 7 Habits of Highly Effective People", "Business & Investing", 16, 1)); library.books.add(new Book("The 8th Habit: From Effectiveness to Greatness", "Business & Investing", 16, 1)); library.books.add(new Book("The 3rd Alternative: Solving Life's Most Difficult Problems", "Business & Investing", 16, 1)); library.books.add(new Book("What I Wish I Knew When I Was 20", "Business & Investing", 23, 2)); library.books.add(new Book("inGenius: A Crash Course on Creativity", "Business & Investing", 26, 2)); library.books.add(new Book("Introduction to Java Programming, Comprehensive", "Computers & Technology", 129, 3)); library.books.add(new Book("Java Concurrency in Practice", "Computers & Technology", 60, 4)); library.books.add(new Book("English Grammar In Use", "Education & Reference", 36, 5)); library.books.add(new Book("Steve Jobs", "Biographies & Memoirs", 30, 6)); library.books.add(new Book("Einstein: His Life and Universe", "Biographies & Memoirs", 19, 6)); library.books.add(new Book("Benjamin Franklin: An American Life", "Biographies & Memoirs", 19, 6)); library.books.add(new Book("Kissinger: A Biography", "Biographies & Memoirs", 22, 6)); // instantiate a LibraryTest object that invokes libraryGUI() method LibraryTestOfJTableAndJTree viewer = new LibraryTestOfJTableAndJTree(); viewer.libraryGUI(); } public void libraryGUI() { outputFrame = new JFrame(); // instantiate a JFrame object outputFrame.setSize(850,500); outputFrame.setTitle("LibraryTestOfJTableAndJTree"); outputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); container = outputFrame.getContentPane(); container.setLayout(new FlowLayout()); panel1 = new JPanel(); // instantiate a JPanel object panel1.setLayout(new BorderLayout()); // set the panel1's layout // instantiate a JLabel object, add it to container and panel label1 = new JLabel("Title :"); container.add(label1); panel1.add(label1, BorderLayout.WEST); textField1 = new JTextField("", 15); container.add(textField1); panel1.add(textField1, BorderLayout.CENTER); //handler is the actionPerformed() below. textField1.addActionListener( this ); button1 = new JButton("Search on Title"); container.add(button1); panel1.add(button1, BorderLayout.EAST); button1.addActionListener( this ); label2 = new JLabel(""); // for exception error messages label2.setForeground(Color.red); container.add(label2); panel1.add(label2, BorderLayout.SOUTH); outputFrame.add(panel1); panel2 = new JPanel(); panel2.setLayout(new BorderLayout()); // set the panel2's layout label3 = new JLabel("Genre :"); container.add(label3); panel2.add(label3, BorderLayout.WEST); textField2 = new JTextField("", 15); container.add(textField2); panel2.add(textField2, BorderLayout.CENTER); textField2.addActionListener(this); button2 = new JButton("Search on Genre"); container.add(button2); panel2.add(button2, BorderLayout.EAST); button2.addActionListener(this); label4 = new JLabel(""); // for exception error messages label4.setForeground(Color.red); container.add(label4); panel2.add(label4, BorderLayout.SOUTH); outputFrame.add(panel2); panel3 = new JPanel(); panel3.setLayout(new FlowLayout(FlowLayout.LEFT)); // set the panel3's layout label5 = new JLabel("Author Index :"); container.add(label5); panel3.add(label5); textField3 = new JTextField("", 3); container.add( textField3 ); panel3.add(textField3); textField3.addActionListener(this); button3 = new JButton( "Search on author index" ); container.add( button3 ); panel3.add(button3); button3.addActionListener(this); label6 = new JLabel(""); // for exception error messages label6.setForeground(Color.red); container.add(label6); panel3.add(label6); outputFrame.add(panel3); panel4 = new JPanel(); panel4.setLayout(new BorderLayout()); // create a JTree object root = new DefaultMutableTreeNode("Root"); node1 = new DefaultMutableTreeNode("Genre"); node1_1 = new DefaultMutableTreeNode("Mystery & Thrillers"); node1_2 = new DefaultMutableTreeNode("Business & Investing"); node1_3 = new DefaultMutableTreeNode("Computers & Technology"); node1_4 = new DefaultMutableTreeNode("Education & Reference"); node1_5 = new DefaultMutableTreeNode("Biographies & Memoirs"); node2 = new DefaultMutableTreeNode("Author Index"); node2_1 = new DefaultMutableTreeNode(0); node2_2 = new DefaultMutableTreeNode(1); node2_3 = new DefaultMutableTreeNode(2); node2_4 = new DefaultMutableTreeNode(3); node2_5 = new DefaultMutableTreeNode(4); node2_6 = new DefaultMutableTreeNode(5); root.add(node1); node1.add(node1_1); node1.add(node1_2); node1.add(node1_3); node1.add(node1_4); node1.add(node1_5); root.add(node2); node2.add(node2_1); node2.add(node2_2); node2.add(node2_3); node2.add(node2_4); node2.add(node2_5); node2.add(node2_6); tree = new JTree(root); pane1 = new JScrollPane(); pane1.getViewport().setView(tree); pane1.setPreferredSize(new Dimension(500, 350)); panel4.add(tree, BorderLayout.WEST); tree.setRootVisible(false); // handler is the valueChanged() below. tree.addTreeSelectionListener(this); // create a JTable object String[] columnNames = {"Title", "Genre", "Price", "Author of Index"}; Object[][] data = null; tableModel = new DefaultTableModel(data, columnNames); table = new JTable(tableModel); sorter = new TableRowSorter<TableModel>(table.getModel()); table.setRowSorter(sorter); panel4.add(table, BorderLayout.EAST); pane2 = new JScrollPane(table); pane2.setPreferredSize(new Dimension(550, 350)); panel4.add(pane2); outputFrame.add(panel4); outputFrame.setVisible(true); } // event handler for JButton, JTextField events. public void actionPerformed( ActionEvent event ) { // reset error messages label2.setText(""); label4.setText(""); label6.setText(""); // reset table tableModel.setRowCount(0); // access the text String queryTitle = textField1.getText(); String queryGenre = textField2.getText(); String queryAuthorIndex = textField3.getText(); if ( event.getSource() == button1 ) { if (!queryTitle.equals("")) { Book resultTitle = this.library.searchTitle(queryTitle); if (resultTitle == null) { label2.setText("\"" + queryTitle + "\" is not found in the library.\n"); } else { tableModel.addRow(resultTitle.convertArray()); } } else { for(int i=0; i<library.books.size(); i++){ tableModel.addRow(library.books.get(i).convertArray()); } } } else if ( event.getSource() == button2 ) { if (!queryGenre.equals("")) { ArrayList<Book> resultGenre = this.library.searchGenre(queryGenre); if (resultGenre == null) { label4.setText("\"" + queryGenre + "\" is not found in the library.\n"); } else { for(int i=0; i<resultGenre.size(); i++){ tableModel.addRow(resultGenre.get(i).convertArray()); } } } else { for(int i=0; i<library.books.size(); i++){ tableModel.addRow(library.books.get(i).convertArray()); } } } else if ( event.getSource() == button3 ) { if (!queryAuthorIndex.equals("")) { int queryAuthorIndexInt = Integer.valueOf(queryAuthorIndex); ArrayList<Book> resultAuthorIndex = this.library.searchAuthorIndex(queryAuthorIndexInt); if (resultAuthorIndex == null) { label6.setText("\"" + queryAuthorIndex + "\" is not found in the library.\n"); } else { for(int i=0; i<resultAuthorIndex.size(); i++){ tableModel.addRow(resultAuthorIndex.get(i).convertArray()); } } } else { for(int i=0; i<library.books.size(); i++){ tableModel.addRow(library.books.get(i).convertArray()); } } } } // event handler for JTree events. public void valueChanged(TreeSelectionEvent e) { // get the last selected element DefaultMutableTreeNode current = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); DefaultMutableTreeNode parent = (DefaultMutableTreeNode) current.getParent(); String currentParent = (String) parent.getUserObject(); int count = current.getChildCount(); if (count == 0) { // if child nodes if (currentParent == "Genre") { String currentData = (String) current.getUserObject(); tableModel.setRowCount(0); //reset tree ArrayList<Book> resultGenre = this.library.searchGenre(currentData); for(int i=0; i<resultGenre.size(); i++){ tableModel.addRow(resultGenre.get(i).convertArray()); } } else if (currentParent == "Author Index") { int currentData = (Integer) current.getUserObject(); tableModel.setRowCount(0); ArrayList<Book> resultAuthorIndex = this.library.searchAuthorIndex(currentData); for(int i=0; i<resultAuthorIndex.size(); i++){ tableModel.addRow(resultAuthorIndex.get(i).convertArray()); } } } } }
For this week, please describe in your own words why some of the methods in the Thread class are deprecated.
Thread.stop, Thread.suspend, and Thread.resume are discouraged to use.
If you use Thread.stop, all the monitoring locks by the thread is released, so that the monitor objects become unable to preserve data consistency. Then, you lose thread safety, and that causes unexpected behavior of your application.
Thread.suspend and Thread.resume have the potential to cause deadlocks.
When you use Thread.suspend, and if the thread locks other monitors which have critical system resources, no other thread can access the resources until the thread resumes. If you try to release the suspended thread before you invoke Thread.resume, it causes a deadlock.
#ref(): File not found: "Homework4(YujiShimojo).docx" at page "UMUC Asia - CMSC335 - Object-Oriented and Concurrent Programming"
For this week, please respond to the following:
(a) Describe an application that can deadlock, or starve a thread.
(b) Describe a testing strategy that would detect such a problem efficiently.
A deadlock is multiple processes wait for releasing locks on resources each other, and finally stop their processing.
For avoiding deadlocks, it is important to conduct not only unit testing but also integration testing. Parallel testing, which several users access to application codes, modules, or database records concurrently, is also effective.
#ref(): File not found: "FinalProject_YujiShimojo(LibraryThreadManager.java).docx" at page "UMUC Asia - CMSC335 - Object-Oriented and Concurrent Programming"
public class Book { //attributes of Book class String title, genre; Integer price, authorIndex, maxNumber; // constructor Book(String _title, String _genre, Integer _price, Integer _authorIndex, Integer _maxNumber) { title = _title; genre = _genre; price = _price; authorIndex = _authorIndex; maxNumber = _maxNumber; } // override the toString method @Override public String toString() { String book = title + ":" + genre + ":$" + price + ":" + authorIndex + ":" + maxNumber + "\n"; return book; } public String[] convertArray() { String[] book = {title, genre, String.valueOf(price), String.valueOf(authorIndex), String.valueOf(maxNumber)}; return book; } }
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class LibraryClient1 implements Runnable { static List<Integer> borrowedBookIndices1 = Collections.synchronizedList(new ArrayList<Integer>()); private static int value, index; public int borrowBook() { synchronized (LibraryServer.lock) { LibraryServer.lock.lock(); // Acquire the lock try { if (LibraryServer.masterBookIndices.isEmpty()) { System.out.println("\t\t\tWait for notEmpty condition"); ((Thread) LibraryServer.notEmpty).interrupt(); } else { Collections.shuffle(LibraryServer.masterBookIndices); value = LibraryServer.masterBookIndices.get(0); borrowedBookIndices1.add(value); index = LibraryServer.masterBookIndices.indexOf(value); LibraryServer.masterBookIndices.remove(index); } } catch (SecurityException ex) { ex.printStackTrace(); } finally { LibraryServer.lock.unlock(); // Release the lock return value; } } } public int returnBook() { synchronized (LibraryServer.lock) { LibraryServer.lock.lock(); // Acquire the lock try { if (borrowedBookIndices1.isEmpty()) { System.out.println("Wait for notEmpty condition"); ((Thread) LibraryServer.notEmpty).interrupt(); } else if (LibraryServer.masterBookIndices.size() == LibraryServer.CAPACITY) { System.out.println("Wait for notFull condition"); ((Thread) LibraryServer.notFull).interrupt(); } else { Collections.shuffle(borrowedBookIndices1); value = borrowedBookIndices1.get(0); LibraryServer.masterBookIndices.add(value); index = borrowedBookIndices1.indexOf(value); borrowedBookIndices1.remove(index); } } catch (SecurityException ex) { ex.printStackTrace(); } finally { LibraryServer.lock.unlock(); // Release the lock return value; } } } public void run() { try { for (int i=0; i<40; i++) { int num = (int)(Math.random()*(2)); if (num == 0) { System.out.println("Client1 borrows book index " + borrowBook()); } else if (num == 1) { System.out.println("Client1 returns book index " + returnBook()); } Thread.sleep((int)(Math.random() * 1000)); } } catch (InterruptedException ex) { ex.printStackTrace(); } } // Method for showing books clients holds public static ArrayList<Book> showClient1Books() { ArrayList<Book> array = new ArrayList<Book>(); for (int i=0; i<borrowedBookIndices1.size(); i++) { array.add(LibraryServer.masterBooks.get(borrowedBookIndices1.get(i))); } return array; } }
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class LibraryClient2 implements Runnable { static List<Integer> borrowedBookIndices2 = Collections.synchronizedList(new ArrayList<Integer>()); private static int value, index; public int borrowBook() { synchronized (LibraryServer.lock) { LibraryServer.lock.lock(); // Acquire the lock try { if (LibraryServer.masterBookIndices.isEmpty()) { System.out.println("\t\t\t\tWait for notEmpty condition"); ((Thread) LibraryServer.notEmpty).interrupt(); } else { Collections.shuffle(LibraryServer.masterBookIndices); value = LibraryServer.masterBookIndices.get(0); borrowedBookIndices2.add(value); index = LibraryServer.masterBookIndices.indexOf(value); LibraryServer.masterBookIndices.remove(index); } } catch (SecurityException ex) { ex.printStackTrace(); } finally { LibraryServer.lock.unlock(); // Release the lock return value; } } } public int returnBook() { synchronized (LibraryServer.lock) { LibraryServer.lock.lock(); // Acquire the lock try { if (borrowedBookIndices2.isEmpty()) { System.out.println("\t\t\t\tWait for notEmpty condition"); ((Thread) LibraryServer.notEmpty).interrupt(); } else if (LibraryServer.masterBookIndices.size() == LibraryServer.CAPACITY) { System.out.println("Wait for notFull condition"); ((Thread) LibraryServer.notFull).interrupt(); } else { Collections.shuffle(borrowedBookIndices2); value = borrowedBookIndices2.get(0); LibraryServer.masterBookIndices.add(value); index = borrowedBookIndices2.indexOf(value); borrowedBookIndices2.remove(index); } } catch (SecurityException ex) { ex.printStackTrace(); } finally { LibraryServer.lock.unlock(); // Release the lock return value; } } } public void run() { try { for (int i=0; i<40; i++) { int num = (int)(Math.random()*(2)); if (num == 0) { System.out.println("\t\t\t\tClient2 borrows book index " + borrowBook()); } else if (num == 1) { System.out.println("\t\t\t\tClient2 returns book index " + returnBook()); } Thread.sleep((int)(Math.random() * 1000)); } } catch (InterruptedException ex) { ex.printStackTrace(); } } // Method for showing books client2 holds public static ArrayList<Book> showClient2Books() { ArrayList<Book> array = new ArrayList<Book>(); for (int i=0; i<borrowedBookIndices2.size(); i++) { array.add(LibraryServer.masterBooks.get(borrowedBookIndices2.get(i))); } return array; } }
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.concurrent.locks.*; public class LibraryServer { // Create a master book index list static List<Integer> masterBookIndices = Collections.synchronizedList(new ArrayList<Integer>()); // Maximum master size static int CAPACITY = 50; // Create a new lock static Lock lock = new ReentrantLock(); // Create two conditions static Condition notEmpty = lock.newCondition(); static Condition notFull = lock.newCondition(); // Create a master book list static List<Book> masterBooks = Collections.synchronizedList(new ArrayList<Book>()); // Constructor of LibraryServer public LibraryServer() { // create instances of books masterBooks.add(new Book("The Lost Symbol", "Mystery & Thrillers", 10, 0, 2)); masterBooks.add(new Book("Angels & Demons", "Mystery & Thrillers", 16, 0, 3)); masterBooks.add(new Book("The Da Vinci Code", "Mystery & Thrillers", 10, 0, 4)); masterBooks.add(new Book("Deception Point", "Mystery & Thrillers", 16, 0, 2)); masterBooks.add(new Book("Digital Fortress", "Mystery & Thrillers", 9, 0, 3)); masterBooks.add(new Book("The 7 Habits of Highly Effective People", "Business & Investing", 16, 1, 2)); masterBooks.add(new Book("The 8th Habit: From Effectiveness to Greatness", "Business & Investing", 16, 1, 3)); masterBooks.add(new Book("The 3rd Alternative: Solving Life's Most Difficult Problems", "Business & Investing", 16, 1, 2)); masterBooks.add(new Book("What I Wish I Knew When I Was 20", "Business & Investing", 23, 2, 3)); masterBooks.add(new Book("inGenius: A Crash Course on Creativity", "Business & Investing", 26, 2, 2)); masterBooks.add(new Book("Introduction to Java Programming, Comprehensive", "Computers & Technology", 129, 3, 2)); masterBooks.add(new Book("Java Concurrency in Practice", "Computers & Technology", 60, 4, 4)); masterBooks.add(new Book("English Grammar In Use", "Education & Reference", 36, 5, 3)); masterBooks.add(new Book("Steve Jobs", "Biographies & Memoirs", 30, 6, 3)); masterBooks.add(new Book("Einstein: His Life and Universe", "Biographies & Memoirs", 19, 6, 2)); masterBooks.add(new Book("Benjamin Franklin: An American Life", "Biographies & Memoirs", 19, 6, 2)); masterBooks.add(new Book("Kissinger: A Biography", "Biographies & Memoirs", 22, 6, 3)); // Add book indices to the master index list for (int i=0; i<masterBooks.size(); i++) { masterBookIndices.add(i); } // Copy each object indices specified times by the fifth field for (int i=0; i<masterBooks.size(); i++) { int n = masterBooks.get(i).maxNumber; for (int m=0; m<n-1; m++) { masterBookIndices.add(i); } } } // Method for showing books server holds public static ArrayList<Book> showLibraryServerBooks() { ArrayList<Book> array = new ArrayList<Book>(); for (int i=0; i<masterBookIndices.size(); i++) { array.add(masterBooks.get(masterBookIndices.get(i))); } return array; } }
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.util.concurrent.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class LibraryThreadManager implements ActionListener, ItemListener { // declare Swing components as instance variables static JFrame outputFrame; static JPanel panel1, panel2, panel3; static Container container; static JLabel label1, label2, label3; static JButton button1, button2; static JComboBox comboBox; static String [] show = {"server", "client1", "client2"}; static JScrollPane pane; static JTable table; static DefaultTableModel tableModel; static TableRowSorter<TableModel> sorter; public static void main(String[] args) { // Instantiate a LibraryThreadManager object LibraryThreadManager libraryThreadManager = new LibraryThreadManager(); // Instantiate a LibraryServer object LibraryServer libraryServer = new LibraryServer(); // Invoke a GUI method libraryThreadManager.LibraryGUI(); } // GUI method public void LibraryGUI() { outputFrame = new JFrame(); // Instantiate a JFrame object outputFrame.setSize(1000,800); outputFrame.setTitle("LibraryThreadManager"); outputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); container = outputFrame.getContentPane(); container.setLayout(new FlowLayout()); panel1 = new JPanel(); // Instantiate a JPanel object // Instantiate a JLabel object, add it to container and panel label1 = new JLabel("Run threads :"); container.add(label1); panel1.add(label1); button1 = new JButton("Run"); container.add(button1); panel1.add(button1); button1.addActionListener(this); label2 = new JLabel("Show books :"); container.add(label2); panel1.add(label2); comboBox = new JComboBox(show); comboBox.setMaximumRowCount(3); container.add( comboBox ); panel1.add(comboBox); comboBox.addItemListener(this); button2 = new JButton("Show"); container.add(button2); panel1.add(button2); button2.addActionListener(this); outputFrame.add(panel1); panel2 = new JPanel(); label3 = new JLabel(); container.add(label3); panel2.add(label3); outputFrame.add(panel2); panel3 = new JPanel(); // create a JTable object String[] columnNames = {"Title", "Genre", "Price", "Author of Index", "Maximum Number"}; Object[][] data = null; tableModel = new DefaultTableModel(data, columnNames); table = new JTable(tableModel); sorter = new TableRowSorter<TableModel>(table.getModel()); table.setRowSorter(sorter); panel3.add(table); pane = new JScrollPane(table); pane.setPreferredSize(new Dimension(900, 550)); panel3.add(pane); outputFrame.add(panel3); outputFrame.setVisible(true); } // Event handler for JButton events. public void actionPerformed(ActionEvent event) { tableModel.setRowCount(0); // reset table // Create a thread pool with two threads ExecutorService executor = Executors.newFixedThreadPool(2); if (event.getSource() == button1) { label3.setText("Borrow/return actions being run between a server and two clients." + " Each client accesses the server 40 times."); executor.execute(new LibraryClient1()); executor.execute(new LibraryClient2()); executor.shutdown(); } else if (event.getSource() == button2) { if (comboBox.getSelectedItem() == "server") { label3.setText(""); for(int i=0; i<LibraryServer.showLibraryServerBooks().size(); i++){ tableModel.addRow(LibraryServer.showLibraryServerBooks().get(i).convertArray()); } } else if (comboBox.getSelectedItem() == "client1") { for(int i=0; i<LibraryClient1.showClient1Books().size(); i++){ tableModel.addRow(LibraryClient1.showClient1Books().get(i).convertArray()); } } else if (comboBox.getSelectedItem() == "client2") { for(int i=0; i<LibraryClient2.showClient2Books().size(); i++){ tableModel.addRow(LibraryClient2.showClient2Books().get(i).convertArray()); } } } } // Event handler for JComboBox events. public void itemStateChanged(ItemEvent event) { if ( event.getSource() == comboBox) { if (event.getStateChange() == ItemEvent.SELECTED) {} } } }