archive-edu.com » EDU » H » HWS.EDU

Total: 727

Choose link from "Titles, links and description words view":

Or switch to "Titles and links view".
  • Javanotes 7.0, Exercises for Chapter 10
    size of the table when it becomes too full You should also write a short program to test your solution See the Solution Exercise 10 4 A predicate is a boolean valued function with one parameter Some languages use predicates in generic programming Java 7 doesn t but this exercise looks at how predicates might work In Java we could implement predicate objects by defining a generic interface public interface Predicate T public boolean test T obj The idea is that an object that implements this interface knows how to test objects of type T in some way Define a class that contains the following generic static methods for working with predicate objects The name of the class should be Predicates in analogy with the standard class Collections that provides various static methods for working with collections public static T void remove Collection T coll Predicate T pred Remove every object obj from coll for which pred test obj is true public static T void retain Collection T coll Predicate T pred Remove every object obj from coll for which pred test obj is false That is retain the objects for which the predicate is true public static T List T collect Collection T coll Predicate T pred Return a List that contains all the objects obj from the collection coll such that pred test obj is true public static T int find ArrayList T list Predicate T pred Return the index of the first item in list for which the predicate is true if any If there is no such item return 1 In C methods similar to these are included as a standard part of the generic programming framework And Java 8 has a similar predicate interface and the Collections class has a removeIf method that uses a predicate See the Solution Exercise 10 5 An example in Subsection 10 4 2 concerns the problem of making an index for a book A related problem is making a concordance for a document A concordance lists every word that occurs in the document and for each word it gives the line number of every line in the document where the word occurs All the subroutines for creating an index that were presented in Subsection 10 4 2 can also be used to create a concordance The only real difference is that the integers in a concordance are line numbers rather than page numbers Write a program that can create a concordance The document should be read from an input file and the concordance data should be written to an output file You can use the indexing subroutines from Subsection 10 4 2 modified to write the data to TextIO instead of to System out You will need to make these subroutines static The input and output files should be selected by the user when the program is run The sample program WordCount java from Subsection 10 4 4 can be used as a model of how to use files

    Original URL path: http://math.hws.edu/javanotes/c10/exercises.html (2016-02-07)
    Open archived version from archive


  • Javanotes 7.0, Quiz on Chapter 10
    compute the sum of all the integer values in the collection Write a second code segment that does the same thing using a for each loop Question 5 Interfaces such as List Set and Map define abstract data types Explain what this means Question 6 What is the fundamental property that distinguishes Sets from other types of Collections Question 7 What is the essential difference in functionality between a TreeMap and a HashMap Question 8 Explain what is meant by a hash code Question 9 Modify the following Date class so that it implements the interface Comparable Date The ordering on objects of type Date should be the natural chronological ordering class Date int month Month number in range 1 to 12 int day Day number in range 1 to 31 int year Year number Date int m int d int y month m day d year y Question 10 Suppose that syllabus is a variable of type TreeMap Date String where Date is the class from the preceding exercise Write a code segment that will write out the value string for every key that is in the month of December 2014 Question 11 Write a generic class Stack T

    Original URL path: http://math.hws.edu/javanotes/c10/quiz.html (2016-02-07)
    Open archived version from archive

  • Javanotes 7.0, Exercises for Chapter 11
    network server program The program is a simple file server that makes a collection of files available for transmission to clients When the server starts up it needs to know the name of the directory that contains the collection of files This information can be provided as a command line argument You can assume that the directory contains only regular files that is it does not contain any sub directories You can also assume that all the files are text files When a client connects to the server the server first reads a one line command from the client The command can be the string INDEX In this case the server responds by sending a list of names of all the files that are available on the server Or the command can be of the form GET filename where filename is a file name The server checks whether the requested file actually exists If so it first sends the word OK as a message to the client Then it sends the contents of the file and closes the connection Otherwise it sends a line beginning with the word ERROR to the client and closes the connection The error response can include an error message on the rest of the line Write a subroutine to handle each request See the DirectoryList example in Subsection 11 2 2 for help with the problem of getting the list of files in the directory See the Solution Exercise 11 4 Write a client program for the server from Exercise 11 3 Design a user interface that will let the user do at least two things 1 Get a list of files that are available on the server and display the list on standard output and 2 Get a copy of a specified file from the server and save it to a local file on the computer where the client is running See the Solution Exercise 11 5 The sample program PhoneDirectoryFileDemo java from Subsection 11 3 2 stores name number pairs for a simple phone book in a text file in the user s home directory Modify that program so that it uses an XML format for the data The only significant changes that you will have to make are to the parts of the program that read and write the data file Use the DOM to read the data as discussed in Subsection 11 5 2 You can use the XML format illustrated in the following sample phone directory file xml version 1 0 phone directory entry name barney number 890 1203 entry name fred number 555 9923 phone directory This is just a short exercise in basic XML processing as before the program in this exercise is not meant to be a useful phone directory program See the Solution Exercise 11 6 The sample program Checkers java from Subsection 7 5 3 lets two players play checkers It would be nice if in the middle of a game the state of the

    Original URL path: http://math.hws.edu/javanotes/c11/exercises.html (2016-02-07)
    Open archived version from archive

  • Javanotes 7.0, Quiz on Chapter 11
    Streams are an abstraction Explain what this means and why it is important Question 2 Java has two types of streams character streams and byte streams Why What is the difference between the two types of streams Question 3 What is a file Why are files necessary Question 4 What is the point of the following statement out new PrintWriter new FileWriter data dat Why would you need a statement that involves two different stream classes PrintWriter and FileWriter Question 5 The package java io includes a class named URL What does an object of type URL represent and how is it used Question 6 What is the purpose of the JFileChooser class Question 7 Explain what is meant by the client server model of network communication Question 8 What is a socket Question 9 What is a ServerSocket and how is it used Question 10 What is meant by an element in an XML document Question 11 What is it about XML that makes it suitable for representing almost any type of data Question 12 Write a complete program that will display the first ten lines from a text file The lines should be written to standard output System

    Original URL path: http://math.hws.edu/javanotes/c11/quiz.html (2016-02-07)
    Open archived version from archive

  • Javanotes 7.0, Exercises for Chapter 12
    sockets from the main routine to the threads The sample program DateServerWithThreads java from Subsection 12 4 3 is an example of a multithreaded server that works in this way Your server program will work with the same client program as the original server You wrote the client program as the solution to Exercise 11 4 See the Solution Exercise 12 5 It is possible to get an estimate of the mathematical constant π by using a random process The idea is based on the fact that the area of a circle of radius 1 is equal to π and the area of a quarter of that circle is π 4 Here is a picture of a quarter of a circle of radius 1 inside a 1 by 1 square The area of the whole square is one while the area of the part inside the circle is π 4 If we choose a point in the square at random the probability that it is inside the circle is π 4 If we choose N points in the square at random and if C of them are inside the circle we expect the fraction C N of points that fall inside the circle to be about π 4 That is we expect 4 C N to be close to π If N is large we can expect 4 C N to be a good estimate for π and as N gets larger and larger the estimate is likely to improve We can pick a random point in the square by choosing numbers x and y in the range 0 to 1 using Math random Since the equation of the circle is x x y y 1 the point lies inside the circle if x x y y is less than 1 One trial consists of picking x and y and testing whether x x y y is less than 1 To get an estimate for π you have to do many trials count the trials and count the number of trials in which x x y y is less than 1 For this exercise you should write a GUI program that does this computation and displays the result The computation should be done in a separate thread and the results should be displayed periodically The program can use JLabels to the display the results It should set the text on the labels after running each batch of say one million trials Setting the text after each trial doesn t make sense since millions of trials can be done in one second and trying to change the display millions of times per second would be silly Your program should have a Run Pause button that controls the computation When the program starts clicking Run will start the computation and change the text on the button to Pause Clicking Pause will cause the computation to pause The thread that does the computation should be started at the beginning of the program but

    Original URL path: http://math.hws.edu/javanotes/c12/exercises.html (2016-02-07)
    Open archived version from archive

  • Javanotes 7.0, Quiz on Chapter 12
    that would create and start a thread belonging to that class Question 2 Suppose that thrd is an object of type Thread Explain the difference between calling thrd start and calling thrd run Question 3 What is a race condition Question 4 How does synchronization prevent race conditions and what does it mean to say that synchronization only provides mutual exclusion Question 5 Suppose that a program uses a single thread that takes 4 seconds to run Now suppose that the program creates two threads and divides the same work between the two threads What can be said about the expected execution time of the program that uses two threads Question 6 What is an ArrayBlockingQueue and how does it solve the producer consumer problem Question 7 What is a thread pool Question 8 Network server programs are often multithreaded Explain what this means and why it is true Question 9 Why does a multithreaded network server program often use many times more threads than the number of available processors Question 10 Consider the ThreadSafeCounter example from Subsection 12 1 3 public class ThreadSafeCounter private int count 0 The value of the counter synchronized public void increment count count 1

    Original URL path: http://math.hws.edu/javanotes/c12/quiz.html (2016-02-07)
    Open archived version from archive

  • Javanotes 7.0, Exercises for Chapter 13
    component from Subsection 13 4 5 displays the text Timing when the stop watch is running It would be nice if it displayed the elapsed time since the stop watch was started For that you need to create a Timer See Subsection 6 4 1 Add a Timer to the original source code StopWatchLabel java to drive the display of the elapsed time in seconds Create the timer in the mousePressed routine when the stop watch is started Stop the timer in the mousePressed routine when the stop watch is stopped The elapsed time won t be very accurate anyway so just show the integral number of seconds You only need to set the text a few times per second For my Timer method I use a delay of 200 milliseconds for the timer See the Solution Exercise 13 4 The custom component example MirrorText java from Subsection 13 4 5 uses an off screen canvas to show mirror reversed text in a JPanel An alternative approach would be to draw the text after applying a transform to the graphics context that is used for drawing See Subsection 13 2 5 With this approach the custom component can be defined as a subclass of JLabel in which the paintComponent method is overridden Write a version of the MirrorText component that takes this approach The solution is very short but tricky Note that the scale transform g2 scale 1 1 does a left right reflection through the left edge of the component See the Solution Exercise 13 5 The sample program PhoneDirectoryFileDemo java from Subsection 11 3 2 keeps data for a phone directory in a file in the user s home directory Exercise 11 5 asked you to revise that program to use an XML format for the data Both programs have a simple command line user interface For this exercise you should provide a GUI interface for the phone directory data You can base your program either on the original sample program or on the modified XML version from the exercise Use a JTable to hold the data The user should be able to edit all the entries in the table Also the user should be able to add and delete rows Include either buttons or menu commands that can be used to perform these actions The delete command should delete the selected row if any New rows should be added at the end of the table For this program you can use a standard DefaultTableModel Your program should load data from the file when it starts and save data to the file when it ends just as the two previous programs do For a GUI program you can t simply save the data at the end of the main routine since main terminates as soon as the window shows up on the screen You want to save the data when the user closes the window and ends the program There are several approaches One is to use a

    Original URL path: http://math.hws.edu/javanotes/c13/exercises.html (2016-02-07)
    Open archived version from archive

  • Javanotes 7.0, Quiz on Chapter 13
    for Question 4 If a Color c is created as c new Color 0 0 255 125 what is the effect of drawing with this color Question 5 What is antialiasing Question 6 How is the ButtonGroup class used Question 7 What does the acronym MVC stand for and how does it apply to the JTable class Question 8 Describe the picture that is produced by the following paintComponent method

    Original URL path: http://math.hws.edu/javanotes/c13/quiz.html (2016-02-07)
    Open archived version from archive