HW lap in Computer science

Anonymous
timer Asked: Nov 17th, 2016

Question description

More Collections Practice: Can u please see the attachment and answer it.

Homework 9: More Collections Practice CS116: Fundamentals Of Software Development I King’s College, Fall 2016 In this assignment we are going to implement a model of a computer network. 1 Getting Started Just like you do in other BlueJ-based assignments, you need to download the archive file 2016fall-cs116-hw09.zip from Moodle and save it to your desktop. Unzip the archive file and open it up in BlueJ. If you don’t remember how to do this, go back to the “Getting Started” section of the Lab 2 that contains explicit instructions. You should see that the project is empty. Before you can start working, I need to explain a little about networking. 2 Background: Network Packets Computers are tremendously useful tools by themselves, but become even more useful when they are connected together into a network. (Especially when they are connected to the largest network of all, the Internet.) When two computers are connected to each other, they can send messages back and forth. These messages could be webpages, emails, songs, telephone calls, videos, or any other kind of data. In order to know who they are communicating with, computers need to have addresses. Real computer addresses come in two forms: IP numbers such as 209.50.140.42 or hostnames such as www.kings.edu. But for our model we will do something simpler and say that a computer’s address is just a single integer, like 5 or 12. It is hard to design a network that works well with messages of different sizes, and some messages (like videos) are naturally much larger than others (like emails). And when you are watching a video streamed over the Internet you do not want to wait for the entire message to be transferred to your computer before you can start watching it. So long messages are divided into smaller parts called packets by the sender, sent one at a time, and then reassembled into the complete message by the recipient. To help the recipient to do this, each packet contains its own number and the total number of packets in the message. For example, suppose that computer 7 wanted to send the following message to computer 3: The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. CS116 Homework 9 Page 2 If packets were limited to 40 characters, this would be broken up into the following packets: Sender Recipient Number Total Fragment 7 3 0 10 The best programs are written so that co 7 3 1 10 mputing machines can perform them quickl 7 3 2 10 y and so that human beings can understan 7 3 3 10 d them clearly. A programmer is ideally 7 3 4 10 an essayist who works with traditional a 7 3 5 10 esthetic and literary forms as well as m 7 3 6 10 athematical concepts, to communicate the 7 3 7 10 way that an algorithm works and to conv 7 3 8 10 ince a reader that the results will be c 7 3 9 10 orrect. Of course, this explanation is a simplification of the way that network packets really work, but it is sufficient for our model. 3 Assignment: Implementing A Packet Class 1. (1 point) Create a Packet class. Remove all of the auto-generated code and write a Javadoc comment listing yourself as the author. 2. (5 points) Create fields to store the sender’s address, recipient’s address, number within a message, total number of packets for the message, and message fragment. 3. (3 points) Create a constructor with parameters to match each of the fields. 4. (5 points) Create accessors for each of the fields. 5. (10 points) Write a method named splitMessage that receives a sender address, a recipient address, and a message. It should return an ArrayList containing the packets that you get when dividing the message up into 40-character chunks. Note 1: This method and the next are strange, because they do not use the fields of the class. We will soon learn a better way to write methods like these. Note 2: We want you to practice writing while loops. Thus, to get full credit you should not be using for loops anywhere on this assignment. 6. (10 points) Write a method named joinMessages that receives an ArrayList. This contains packets from one or more complete messages, and you may assume that all of the packets from the first message (in order) are first, then all of the packets from the second message (in order), and so forth. The method returns an ArrayList containing all of the complete messages. 7. (4 points) Make sure that all parts of the Packet class have complete, correct Javadoc comments. CS116 Homework 9 Page 3 8. (0 points) Test the Packet class thoroughly and fix any errors that you find. To test the joinMessages method, first call the splitMessage method and when you get the return value window click “Get” to save that ArrayList object to the workbench. Then when calling the joinMessages method you can type in the name of that object as the parameter. 4 Background: Switching It is not usually the case that two computers are connected directly to each other. Instead, computers are connected through other intermediate devices such as hubs, swiches, and routers. We are interested specifically in switches, which work like the picture in Figure 1. Figure 1: A diagram of a network switch and router. When a switch receives a packet from a computer, it temporarily stores it. Then, when the appropriate link becomes available, it forwards the packet in the right direction. When only one computer is trying to send data the switch has an easy job, but when every computer is trying to send information at the same time, it can get very congested. There are many different ways that a switch could decide which packet to forward next when it has several stored. We are going to choose a strategy that might not be fair, but will be interesting to implement based on our current knowledge. We will assume that the switch has three computers connected to it, just like in the picture. We will give them addresses 1, 2, and 3. And let’s assume that they never want to talk to each other – only to other computers out on the Internet (which will have addresses different from 1, 2, or 3). And for simplicity we will assume that computers on the Internet never send messages back to our computers. CS116 Homework 9 Page 4 So the switch must be able to receive packets from computers 1, 2, and 3, and to send them out to the Internet. Whenever the link to the Internet is available, our switch will want to send a packet from whichever computer has the most packets waiting. If multiple computers have the same number of packets waiting, we give priority to computer 1 over computer 2, and computer 2 over computer 3. Once we have decided which computer’s packet to forward, we will choose the one from that computer that has been waiting the longest. 5 Assignment: Implementing A Switch Class 1. (1 point) Create a Switch class. Remove all of the auto-generated code and write a Javadoc comment listing yourself as the author. 2. (5 points) Create fields to store all packets that have been received by the switch but not yet forwarded to the Internet. It is possible to solve this problem with either one field or three; one choice makes some methods easy while the other makes other methods easy. 3. (3 points) Create a default constructor. New switches should have no packets waiting. 4. (5 points) Create a method named receivePacket that receives a Packet object and stores it. 5. (5 points) Create a method named receivePacket that receives a sender address, recipient address, number within a message, total number of packets for the message, and message fragment. It should use those to construct a Packet object and store it. Note 1: Your class will now have two different methods, both named receivePacket, but with different types of parameters. This is called method overloading, and is perfectly OK. Note 2: It seems like much of the code that you wrote for the first version of receivePacket will be needed again in the second version. You could write it again, but we are lazy programmers who do not like to do that. Instead, the second version of receivePacket can call the first version of receivePacket. When you are inside one method for an object and you want to call another method on that same object, you can just write the method name without an object or a dot. This is an internal method call. 6. (10 points) Create a method named sendPacket that chooses a Packet by the rules described above, removes it from storage, and returns it. If there are no packets waiting, this method should return null. 7. (5 points) Create a method named packetCount that does not receive any parameters and that returns the total number of packets that are stored. 8. (10 points) Create a method named anyPacketsForMe that receives a recipient address and returns whether or not the switch has any stored packets that are going to that address. Note: For full credit, this method must use the early-exit strategy that we will learn on Friday to avoid doing unnecessary work. CS116 Homework 9 Page 5 9. (10 points) Create a method named sendAllPackets that returns an ArrayList containing all stored packets in the order that they should be sent (and removes all of them from storage). Note: This is another excellent opportunity to use an internal method call to save yourself duplicate work. 10. (4 points) Make sure that all parts of the Switch class have complete, correct Javadoc comments. 11. (0 points) Test the Switch class thoroughly and fix any errors that you find. 12. (4 points) Check that both the Packet and Switch classes follow the style guidelines below and are not using for loops. 6 Using Good Style Make sure that you are following style guidelines: • All class names should start with an uppercase letter and have an uppercase letter wherever a new word starts within the name. • All variable (instance, local, or parameter) names should start with a lowercase letter and have an uppercase letter wherever a new word starts within the name. • All variable names should be meaningful. • All variables should have a type that makes sense. • All opening curly braces should be on the same line as the code directly before them. • All closing curly braces should be on a line by themselves. • All code that is inside a set of curly braces should be indented by four spaces per set of curly braces it is inside. Use BlueJ’s convenient “Auto-layout” function by selecting Edit...Autolayout or pressing Ctrl+Shift+I. • There should be a space after (but not before) every comma that separates parameters. • The order of things inside a class should be first fields, then constructors, then methods. • All classes should be public, all fields should be private, all methods should be public, and all constructors should be public. • All classes should have Javadoc comments with the @author and @version tags. All methods, constructors, and fields (including the main method) should have Javadoc comments, with @param and @return tags where they are appropriate. CS116 Homework 9 7 Page 6 Finishing Up Make sure that your class compiles, behaves as expected, and has complete documentation. You can close all of the BlueJ windows. Then create an archive file of your CS116-HW09 directory and upload the archive file to Moodle. To create the archive, right-click on the directory and choose “7-Zip”, then “Add to ... .zip”. If you are not sure that you are uploading the correct file, ask us to look at it.

Tutor Answer

(Top Tutor) Studypool Tutor
School: New York University
Studypool has helped 1,244,100 students
flag Report DMCA
Similar Questions
Hot Questions
Related Tags
Study Guides

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors