Skip to main content

more options

Assignment 1 (Business/Management)

Submit on the CMS two weeks into the course
Company Network Database


Many engineers and scientists are in their profession because they enjoy doing research, investigating new ideas, discovering new things, or designing processes and goods that help people. Some scientists and engineers hit the jackpot and change the world for the better, or win a MacArthur Fellowship, a Nobel prize, or an award given by the professional society of their field.

But many scientists and engineers find out that they enjoy the management/business as well as the technical side of their professions. In any case, some knowledge of businesses and how they work will help all new engineers, and it is probably a good idea to take a course or two in business/management/entrepreneurship while you are at Cornell.

Cornell's Johnson Graduate School of Management is one of the best business schools in the nation. Diverse programs and degrees on business and management are offered. Take a look at its website.

Because there are infinitely many companies in this world, even in this country, we might want a database that contains brief information of many companies, for example, maintaining growth trends over the years. Your task in this assignment is to develop a Java class Company, which will maintain information about a company, and a JUnit class CompanyTester to maintain a suite of testcases for class Company. This assignment will help illustrate how Java’s classes and objects can be used to maintain data about a collection of things —like companies.

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

Nobel medalHELP

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 Company

An instance of class Company represents a single company. It has several fields that one might use to describe a company, 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: 'D' for domestic, 'O' for overseas, or 'I' for international. (a char)
  • year of foundation (an int)
  • number of employees. (an int)
  • parent: —the name on the tab of folder of a company object that owns this company. (a Company)
  • the number of companies that this company owns. (an int)
  • id number: a number used to reference this Company in a database under development. (an int)

Here are some details about these fields.

  • The name is a String of characters. It is okay for two companies to have the same name. This happens a lot.
  • The year of foundation 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.
  • Number of employes is always positive. It does not make sense to have < 1 employee. Do not write code that checks whether it is positive: assume it is valid.
  • The id number is just a number, >= 0. Assume all ids given are unique; do not write code to check whether a company with a given id number already exists. If a company hasn’t been assigned an id number yet, this field should contain -1.
  • The parent is the name of a Company object (manila folder) that owns this company. It is not a String. The parent is null if no company owns this company.

The Johnson SchoolAccompanying 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 this company is, ‘D’ for domestic, 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.

Company Methods

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

Constructor Description
Company(String name, int year, int employee, char type) Constructor: a new company. Parameters are, in order, the name of the company, the year of foundation, number of the employees, and what type of company it is. The company has no id number, it has no parent company, and it owns no companies. Precondition: All parameters are valid as described.
Company(String name, char type, Company parent, int year, int employees, int id) Constructor: a new company. Parameters are, in order, the name of the company, its type, its parent, year of foundation, number of employees, and the id number. Precondition: All parameters are valid as described, the parent 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 company. (a String)
getType() = the type of this company ('D' for domestic, 'O' for overseas, or 'I' for international). (a char)
getYOF() = the year this company was founded. (an int)
getEmployees() = number of employees in this company. (an int)
getParent() = the name (of the folder containing) of this company’s parent. (a Company)
getId() = this company’s id number (-1 if it has not been assigned one). (an int)
getCompanies() = the number of companies that have this one as a parent. (an int)
toString() = a String representation of this Company. Its precise specification is discussed below.
Setter Method Description
setName(String s) Set the name of this company to s.
setType(char t) Set the type of this company to t. Precondition: t is one of 'D', 'O', and 'I'.
setYOF(int y) Set the year this company was founded to y.
setEmployees(int e) Set the number of employees of this company to e. Precondition: e > 0.
setParent(Company p) Set the Parent of this company to p.
Precondition: This company’s parent is null, and p is not null.
setId(int i) Set this company’s id number to i.
Precondition: i >= 0, and the id number is currently -1.


Comparison Method Description
isBigger(Company c) = "c is not null, and this company has more employees than c has". (boolean)
isBigger(Company c1, Company c2) Static method. = "c1 and c2 are not null, and c1 has more employees than c2 has". (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.

Hand shakeA 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 setParent with null for the parent argument. However, in function hasMoreEmployees, the tests for c, c1, and c2 not null MUST be made, because that is part of the specification.

It is possible for Company c1 to be c2's parent company and visa versa, at the same time. We do not check for such strange occurrences.

In writing methods setParent, be careful! If c is becoming the parent of this company, then c owns one more company, and its field that contains its number of companies 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 toString

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:

"D company SuperMaster. Id 34. Founded 2005. Has 1000 employees. Owns 2 companies."

Here are some points about this output.

  1. Exactly one blank separates each piece of information, and the periods are necessary.
  2. The 'D' at the beginning means it is a domestic company. Use 'O' for overseas and 'I' for international.
  3. The name of the company follows "company".
  4. If the id number is unknown, omit it entirely (in the example above, omit " Id 34.").
  5. The parent is not described in the output of this function.
  6. If the company own exactly 1 company, the word "company" should appear instead of "companies".
  7. In writing the body of function toString, do not use if-statements or switches. Instead, use the conditional expression (condition ? expression1: expression2). This is the only place you can use the conditional expression.

Class CompanyTester

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

  • Every method in your Company 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 Company using DrJava. In it, declare the fields in class Company, compiling often as you proceed. Write comments that specify what these fields mean.
  • Third, start a new JUnit class, calling it CompanyTester.
  • Fourth, this assignment should properly be done in several parts. For each part, write the methods, write a test procedure in class CompanyTester 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 Company.
    • (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 askyou 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.