PART A

Consider the LetterTemplate class. Compile and run the program. You will see that it writes a simple letter to the screen. The letter's message body is not filled in.

  • Extend the LetterTemplate class to a GasBoilerLetter class
  • By using method overriding, write your GasBoilerLetter class so that it will write a letter that says: "Your gas boiler is due for a service. We will be attending on 12 May 2015 between 8am and 1pm." All other parts of the letter should be the same as in the LetterTemplate class
  • Write a main method with statements to test your new class

PART B

Consider the program, InventoryItem.java, below:

public class InventoryItem {
private String item; private int count;
public InventoryItem(String item, int count) {
this.item = item; this.count = count;
}

public String getItem() {
return item;
}

public int getCount() {
return count;
}

public void setCount(int newCount) {
count = newCount;
}

public static void main (String args[]){

InventoryItem item1 = new InventoryItem("Scissors", 5);
InventoryItem item2 = new InventoryItem("Felt tip pens", 5);
InventoryItem item3 = new InventoryItem("Colouring books", 7);

//InventoryItem item4 = new InventoryItem("Chairs");
InventoryItem item5 = new InventoryItem("wobbly things", 10); System.out.println(item1.getCount()); item1.setCount(20); System.out.println(item1.getCount());
}
}

Note that the main method in the above program has been written to test the program. Normally a class such as InventoryItem would not have a main method, as it is intended to be used in other programs, for example in an Inventory program to make a list of items and store them in sorted order.

The fourth statement in main is a comment, ie it begins with //. If you remove the double slashes, "//", from in front of the statement and compile the program again what happens?

  • The fourth statement in the main method represents a case where a user wishes to note the existence of a certain item in the inventory, thus reminding themselves to come back later and add a count of the item. How would you change the class so that statement number four is legal, and the program will compile with it included? [HINT: A class can have more than one constructor provided their signatures are different.]
  • The user does not know the name of an item, and so has put the description "wobbly things" in item5. Currently the user has no way of returning to their InventoryItem object later and changing "wobbly things" to, for example, "gyroscopes". Amend the program so that the user can change an InventoryItem's String variable after they have made the object. For example after you have made your changes it should be possible to change item5's String variable from wobbly things to gyroscopes"
  • Write statements in the main method to test and demonstrate that the user can successfully change the String instance variable of the InventoryItem objects.
  • Write a toString() method for the InventoryItem class, and some code in main to check that it behaves as you expect it to

PART C

Using the Vector class, write a program called Dictionary to answer the questions below. Vector implements the Collection interface: http://docs.oracle.com/javase/7/docs/api/java/util/Collection.html

This means you can use the Collections class, which consists exclusively of static methods that operate on or return collections, on vectors. You may find it helpful to use some of these methods in your answer. http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html (Java 7) and http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html (Java 8)

See the file VectorEgOO.java for examples of how to use methods from the class Collections with vectors in the object-oriented style, and VectorEgStatic.java for examples of how to use the methods from the Collections class with static variables and methods.

  • Read the file user.dict into a vector, word by word
  • Sort the vector so that the words are in alphabetical order
  • Ask the user to enter a word. Read in the user's word. Check if the word is in the vector. If the word is not in the vector ask the user if they would like to add it (y/n). Add the word if the user responds 'y', 'Y', 'yes', or equivalent affirmative words starting with 'y' or 'Y'. After adding or discarding the word as appropriate, ask the user if they would like to enter another word (y/n).
  • If the user responds 'y', 'Y', 'yes', 'Yes', 'yeah' or 'Yeah' or equivalent then repeat the actions described in question 3. If the user enters 'n', 'N', 'no', 'No' or equivalent negative words beginning with 'n' or 'N' then save the words in the vector back to the file in sorted order and quit the program. The user should be able to repeatedly enter words until they decide to quit.
  • Write comments in your program. Imagine that you have put the program to one side for a year, and write comments that would help you to remember what it is that the program does and how it does it
  • Your program should be written with methods (static or instance). Each method should have one main task to perform, for example sorting the vector. The main method should only be used to call your methods in order to test your program. You may if you wish choose not to use methods, and instead write all of your statements in the main method, in which case the maximum mark you can get for part c will be 14 out of 22
  • Write your program in the object-oriented style, ie you should write a constructor, and use dot notation to call your class's instance methods. You may choose to write your program with static methods, in which case the maximum mark you can get for part c will be 18 out of 22.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.