Skip to main content

more options

Assignment 1 (Computer Viruses)

Submit on the CMS two weeks into the course
Monitoring Computer Viruses


sickcomMarkus Hanhisalo, of Helsinki University of Technology, defines computer viruses in his website as: “a program, a block of executable code, which attaches itself to overwrite or otherwise replace another program in order to reproduce itself without a knowledge of a PC user.” Computer viruses are often designed to maliciously corrupt or steal data from individuals. Sometimes, they can even cause computers to stop working all together.

Computer viruses are different from computer worms. A virus attaches itself to other programs, while a worm is a self-contained program that is able to spread copies of itself to other computers without attaching itself to other programs.

Some viruses and worms, coming as attachments in an email, look like a jpg file —for example, MerryChristmas.jpg! But if your extensions/suffixes were showing, you would see that this is really a file named MerryChristmas.jpg.exe, and clicking on it would cause it to execute, firmly embedding itself on your computer. Fix your operating systems preferences so that extensions/suffixes always show!virus

Did you know that the first computer worm of any consequence was set loose by a Cornell computer science grad student? In November 1988, Robert Morris wrote a worm —not to cause damage but to get an estimate of the size of the internet. He made a big mistake. When the worm reached another computer, it did not check to see whether it was already on that computer but just invaded the computer and sent itself on to other computers. So it spread much much faster than he thought it would. Anywhere from 6,000 to 60,000 computers were infected, and the internet was brought to its knees. Damage was estimated at $10M to $100M. Morris was convicted of violating the 1986 computer fraud and abuse act. He ended up on probation, a hefty fine, and community service. You can read more on Wikipedia. David Gries and Juris Hartmanis, of the Cornell CS Department, were on a Cornell commission that investigated the Morris worm. You can read about it in this article.

Because viruses and worms are so dangerous, people often wish to monitor them in order to help curtail their growth and spreading. Your task in this assignment is to develop a Java class ComputerVirus, which will maintain information about a virus, and a JUnit class ComputerVirusTester to maintain a suite of testcases for ComputerVirus. This assignment will help illustrate how Java’s classes and objects can be used to maintain data about a collection of things –like computer viruses.

Read the whole assignment before starting. Follow the instructions given below in order to complete the assignment as quickly and as efficiently as possible.


If you don't know where to start, if you don't understand testing, if you are lost, SEE SOMEONE IMMEDIATELY —the course instructor, a TA, a consultant. Do not wait. A little one-on-one help can do wonders.

Class ComputerVirus

An instance of class ComputerVirus represents a single computer virus. It has several fields that one might use to describe a computer virus, as well as methods that operate on these fields.  Here are the fields, all of which should be private (you can choose the names of these fields).

  • name (a String)
  • type (a char'W' for windows, 'M' for Mac, and 'L' for the rare Linux virus)
  • month of discovery (an int)
  • year of discovery (an int)
  • predecessor (a ComputerVirus —the name on the tab of folder of a ComputerVirus object from which this one “evolved”)
  • number of variations (an int: the number of known viruses that directly evolved from this one)
  • id number (an int: a number used to reference this ComputerVirus in a database under development)

Here are some details about these fields.

  • The name is a string of characters. It is okay for two viruses to have the same name because some strange happenstance with the media and people who cannot tell the difference between two viruses could lead to two viruses having the same name.
  • The month of discovery is in the range 1..12, representing a month of the year. The year of discovery is something like 1997 or 2005. Do not worry about invalid dates; do not write code that checks whether dates are valid: assume they are valid.
  • The id number is an index into a database. Assume all tags given are unique; do not write code to check whether a virus with a given id number already exists. If a virus hasn’t been assigned an id number yet, this field should contain -1.
  • The predecessor is the name of a ComputerVirus object (manilla folder) on which this virus was based or from which it evolved (computer viruses can evolve too).  It is not a String. The predecessor is null if it is either not known or if this is an original virus.
wurmark virus image

The Wurmark-D worm (Jan 05) travels as an email attachment. It pretends to be amusing, but it installs itself on your computer and forwards itself to others.

Accompanying the declarations of these fields should be comments that describe what each field means –what it contains.  For example, on the declaration of the field type, state in a comment that the field represents what kinds of computers the virus infects, 'W' for Windows, etc.  The collection of the comments on these fields is called the class invariant. The class describes all legal values for the fields together.

Whenever you write a method (see below), look through the class invariant and convince yourself that class invariant is correct when the method terminates.

ComputerVirus Methods

Class ComputerVirus has the following methods. Pay close attention to the parameters and return values of each method. The descriptions, while informal, are complete.

Constructor Description
ComputerVirus(String name, int month, int year, chartype) Constructor: a new ComputerVirus. Parameters are, in order, the name of the virus, the month and year of discovery, and what type of processor it infects. The virus has no id number, its predecessor is not known, and it has no known variations. Precondition: the month is in the range 1..12, and the type of processor is 'W' (windows), 'M' (Mac), or 'L' (Linux).
ComputerVirus(String name, char type, ComputerVirus pred, int month, int year, int id number) Constructor: a new ComputerVirus. Parameters are, in order, the name of the virus, its type, its predecessor, the month and year of birth, and the id number. Precondition: the month is in the range 1..12, the type of processor is 'W' (windows),'M' (Mac), or 'L' (Linux), the predecessor is not null, and the id number is ≥ 0.

When you write a constructor body, be sure to set all the fields to appropriate values.

Getter Method Description
getName() = the name of this virus (a String)
getType() = the type of operating system this virus attacks ('W' for windows, 'M' for Mac, or 'L' for Linux) (a char)
getMOD() = the month this virus was discovered, in the range 1..12 (an int)
getYOD() = the year this virus was discovered, >= 1970 (an int)
getPredecessor() = the name (of the folder containing) this virus’s predecessor (a ComputerVirus)
getId() = this virus’s id number (-1 if it has not been assigned one) (an int)
getVariations() = the number of viruses that have this one as a predecessor (an int)
toString() = a String representation of this ComputerVirus.  Its precise specification is discussed below.

Setter Method Description
setName(String s) Set the name of this virus to s.
setType(char t) Set the type of this virus to t. Precondition: t is one of 'W', 'M', and 'L'.
setMOD(int m) Set the month this virus was discovered to m. Precondition: m is in the range 1..12.
setYOD(int y) Set the year this virus was discovered to y. Precondition: y>=1970.
setPredecessor(ComputerVirus v) Set the predecessor of this ComputerVirus to v.
Precondition: This virus’s predecessor is null, and v is not null.
setId(int i) Set this ComputerVirus’s id number to i.
Precondition: i >= 0, and the id number is currently -1.

Comparison Method Description
isOlder(ComputerVirus v) = "v is not null, and this ComputerVirus is older than v —i.e. this virus was discovered before v". (a boolean)
isOlder(ComputerVirus v1, ComputerVirus v2) Static method. = "v1 and v2 are not null, and v1 is older than v2—i.e. v1 was discovered before v2". (a boolean)

Make sure that the names of your methods match those listed above exactly, including capitalization. The number of parameters and their order must also match. The best way to ensure this is to copy and paste our names. Our testing will expect those method names, so any mismatch will fail during our testing. Parameter names will not be tested —you can change the parameter names if you want.

Each method MUST be preceded by an appropriate specification, or "spec", as a Javadoc comment. The best way to ensure this is to copy and paste. After you have pasted, be sure to do any necessary editing. For example, the spec of a function does not have to say that the function yields a boolean or int or anything else, because that is known from the header of the method.

A precondition should not be tested by the method; it is the responsibility of the caller to ensure that the precondition is met. For example, it is a mistake to call method setPredecessor with null for the predecessor argument. However, in function isOlder, the tests for v1 and v2 not null MUST be made, because that is part of the specification.

It is possible for person v1 to be v2's predecessor and visa versa, at the same time. We do not check for such strange occurrences.

In writing methods setPredecessor, be careful! If P is becoming the predecessor of this ComputerVirus, then P has one more variation, and its field that contains its number of variations has to be updated accordingly.

Do not use if-statements, and use conditional statement (...? ... : ...) only in function toString. Boolean expressions, using the operators && (AND), || (OR), and ! (NOT), are sufficient to implement the comparison methods.

Function toStringworm

We illustrate the required format of the output of toString with an example and then make some comments. Here is an example of output from function toString:

"W computer virus Superfun. Id 34. Discovered 6/2005. Has 2 variations."

Here are some points about this output.

  1. Exactly one blank separates each piece of information (including the word “computer” and the word “virus”), and the periods are necessary.
  2. The 'W' at the beginning means it is a windows virus. Use 'M' for Mac and'L'for Linux.
  3. The name of the virus follows "computer virus".
  4. If the id number is unknown, omit it entirely (in the example above, omit " Id 34.").
  5. The predecessor is not described in the output of this function.
  6. If the virus has exactly 1 variation, the word "variation" should appear instead of "variations".
  7. In writing the body of function toString, do not use if-statements or switches. Instead, use the conditional expression (condition ? expression-1: expression-2). This is the only place you can use the conditional expression.

Class ComputerVirusTester

We require you to build a suite of test cases as you develop class ComputerVirus in a JUnit class ComputerVirusTester. Make sure that your test suite adheres to the following principles:

  • Every method in your ComputerVirus needs at least one test case in the test suite.
  • The more interesting or complex a method is, the more test cases you should have for it. What makes a method 'interesting' or complex can be the number of interesting combinations of inputs that method can have, the number of different results that the method can have when run several times, the different results that can arise when other methods are called before and after this method, and so on.
  • Here is one important point. If an argument of a method can be null, there should be a test case that has null for that argument.
  • Test each method (or group of methods) carefully before moving on to the rest of the assignment.

How to do this assignment

First, remember that if-statement are not allowed. If your assignments has if-statements, you will be asked to revise the assignment and resubmit.

Second, you should develop and test class in a methodologically sound way, which we outline below. If we detect that you did not develop it this, we may ask you to start from scratch and write one of the other alternatives for this assignment.

  • First, start a new folder on your hard drive that will contain the files for this project. Start every new project in its own folder.
  • Second, write a class ComputerVirus using DrJava. In it, declare the fields in class ComputerVirus, compiling often as you proceed. Write comments that specify what these fields mean.
  • Third, start a new JUnit class, calling it ComputerVirusTester.
  • Fourth, this assignment should properly be done in several parts. For each part, write the methods, write a test procedure in class ComputerVirusTester and add test cases to it for all the methods you wrote, and then test the methods thoroughly. Do not go on to the next group of methods until the ones you are working on are correct. If we determine that you did not follow this way of doing things —for example, you wrote all methods and then tried to test, we may ask you to set this assignment aside and do another instead. Here are the groups of methods.
    • (1) The first constructor and all the getter methods of class ComputerVirus.
    • (2) The second constructor.
    • (3) Function toString.
    • (4) The setter methods.
    • (5) The two comparison methods.

At each step, make sure all methods are correct before proceeding to the next step. When adding a new method, cut and paste the comment and the header from the assignment handout and then edit the comment. It must have suitable javadoc specifications as well as suitable comments on the field declarations. The assignment will not be accepted for testing until it does.

Other hints and directions

  • Do not use if statements.
  • Remember that a string literal is enclosed in double quotation marks and a char literal is enclosed in single quotation marks.
  • Be sure to create the javadoc specification and look at it carefully, to be sure that the methods are specified thoroughly and clearly.

Procedure for submission

You may submit the assignment whenever you wish. We will look at it in several steps.

1. If the field specifications and the javadoc specifications are not appropriate, we will ask you to fix them and resubmit.

2. If the field and javadoc specs are ok, we will look at your test cases. If they are inadequate, we will ask you to fix them and resubmit.

3. If the test cases are adequate, we will test your program. If there are errors, you will be asked to correct them and resubmit.

The assignment will be considered completed when it passes all three steps.

Submitting the assignment

First, at the top of file, put a comment that says that you looked carefully at the specifications produced by clicking the javadoc button and checked that the specifications of methods and the class specification were OK (put this comment after doing what the comments says).

Second, upload files and on the CMS. You will be asked to upload and Don't worry about this; just upload files and

Make sure you submit .java files. do not submit a file with the extension/suffix .java~. It will help to set the preferences in your operating system so that extensions always appear.