Go over the Unit testing video and answer these questions:
Write a GUI program that uses the Model-View-Controller paradigm similar to the program we did in class. The program will be used to convert a given temperature value from Fahrenheit to Celsius. Organize your classes according to
Make sure your MVC classes serve their individual purposes, neither more nor less.
We have already looked at the simple Item
class:
class Item {
int data;
Item nextItem;
public String toString() {
return "Item with data: " + data;
}
}
and its use in the main
method:
public static void main(String[] args) {
Item item = createList();
printList(item);
}
public static void printList(Item firstItem) {
Item currentItem = firstItem;
while (currentItem != null) {
System.out.println(currentItem);
currentItem = currentItem.nextItem;
}
}
public static Item createList() {
Item a = new Item();
a.data = 23;
Item b = new Item();
b.data = 29;
a.nextItem = b;
Item c = new Item();
c.data = 35;
b.nextItem = c;
Item d = new Item();
d.data = 43;
return a;
}
Now create the LinkedList
class so that you can do these things in a main
method:
LinkedList list = new LinkedList(); // creates an empty list
list.addItem(23); // adds a new item to the list with data 23
list.addItem(42); // ...
System.out.println(list);
Write a class Vehicle
that represents, of course, a vehicle. Give it all the properties (for e.g., weight, maximum speed)—modeled as variables—and behavior (drives, brakes, etc,)—modeled as methods—that you think are relevant. Subclass Vehicle
with classes representing cars, airplanes, and ships (I am taking a lot of liberty here with the regular definition of a ‘vehicle’). For each of these subclasses add two more subclasses each; for e.g., Car
may be subclassed by SUV
and Sedan
. While making subclasses, extend the properties and behavior of superclasses in ways that will seem pertinent. Also implement the toString()
method in each of them. Make extensive use of this
and super
in your classes.
Then in your test class create an array of 10 Vehicle
s and in the array store one object of each class that you have implemented. Then, in a loop, print out the description of each object in the array (using the toString()
method appropriately) and also call a method that’s defined in the Vehicle
class on it.
Write a program that will do the following:
String
“blip” in itread the contents of a directory (just like the commands ls
in Unix and dir
in Windows do) and find out if there is a file or directory with "bak"
in its name.
// count "blip" occurences
// a big assumption: every line contains no more than one "blip"
// for more sophisticated solution need to use StringTokenizer
try {
FileReader fr = new FileReader("input.txt");
BufferedReader reader = new BufferedReader(fr);
int blipCount = 0;
String singleLine;
while ((singleLine = reader.readLine()) != null) {
if (singleLine.indexOf("blip") != -1)
blipCount++;
}
System.out.println("Blip count is: " + blipCount);
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
// read the contents of current directory to find something with "bak" in it
try {
File dir = new File(".");
if (dir.exists() && dir.isDirectory()) {
String listing[] = dir.list();
int bakCount = 0;
for (int i = 0; i < listing.length; i++) {
if (listing[i].indexOf("bak") != -1)
bakCount++;
}
System.out.println("Number of files/directories with \"bak\" in them: " + bakCount);
}
} catch (Exception e) {
e.printStackTrace();
}