This is probably an elementary question. However, I have completed reading the 5th Chapter of Java Programming for the Absolute Beginner and have approached the Challenges section. I cannot quite understand the question.
The question asks:
"Create a Holder class so that it maintains a list of objects, possibly represented by Strings, which you can add to or take from by using its methods. Hint: try overriding the Vector class."
I have looked at the int [] [] convert --to--> Vector<Vector<Double>> page and saw an example of a simple Vector at the last response, but I am still having difficulty understanding the question.
I think that my main problem is understanding how to override the Holder class.
An answer to this question can potentially aid many new Java programmers in understanding Vectors and overriding.
For Your Information:
The second question states:
"Create a subclass of the Holder class. Use your imagination. Consider a Refrigerator class that holds food and keeps it cold, or a Wallet that holds money, perhaps. Try overriding methods defined in the Holder class. Try creating some overloaded methods as well.
This is the complete question, I will add some instances of examples in the chapter to allow you to better understand where my problem is. I DO NOT want a coding answer (unless necessary), just this question explained in more basic terms.
These are the examples provided within the chapter: are these the techniques I have to incorporate into my answer?
Simple Class Definition:
public class SimpleCardDeck {
String[] cards = {“2C”, “3C”, “4C”, “5C”, “6C”, “7C”, “8C”,
“9C”, “10C”, “JC”, “QC”, “KC”, “AC”,
“2D”, “3D”, “4D”, “5D”, “6D”, “7D”, “8D”,
“9D”, “10D”, “JD”, “QD”, “KD”, “AD”,
“2H”, “3H”, “4H”, “5H”, “6H”, “7H”, “8H”,
“9H”, “10H”, “JH”, “QH”, “KH”, “AH”,
“2S”, “3S”, “4S”, “5S”, “6S”, “7S”, “8S”,
“9S”, “10S”, “JS”, “QS”, “KS”, “AS”};
public void list() {
for (int c=0; c < cards.length; c++) {
System.out.print(cards[c] + “ “);
}
}
}
Creating and Testing the Object:
public class SimpleCardDeckTest {
public static void main(String args[]) {
SimpleCardDeck deck = new SimpleCardDeck();
System.out.println(deck.cards[0]);
System.out.println(deck.cards[10]);
System.out.println(deck.cards[51]);
deck.list();
}
}
A Class Example:
public class Automobile {
public static final String DEFAULT_COLOR = “white”;
public String name;
public boolean running;
public String color;
public int numMiles;
public Automobile() {
this(false, DEFAULT_COLOR, 0);
}
public Automobile(boolean running, String color, int numMiles) {
this.running = running;
this.color = color;
this.numMiles = numMiles;
name = null;
}
public void start() {
if (running) {
System.out.println(“Can’t start, already running.”);
}
else {
running = true;
System.out.println(“The automobile has been started.”);
}
}
public void shutOff() {
if (!running) {
System.out.println(“Can’t shut off, not running.”);
}
else {
running = false;
System.out.println(“The automobile has been shut off.”);
}
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public void drive() {
if (running) {
numMiles += 10;
System.out.println(“You have driven 10 miles”);
}
else {
System.out.println(“You need to start the automobile first.”);
}
}
public int getNumMiles() {
return numMiles;
}
public String toString() {
String str;
str = “name = “ + name
+ “, running = “ + running
+ “, color = “ + color
+ “, numMiles = “ + numMiles;
return str;
}
}
Class Test Example:
public class AutomobileTest {
public static void main(String args[]) {
Automobile auto1 = new Automobile();
System.out.println(“Auto 1: “ + auto1.toString());
Automobile auto2 = new Automobile(true, “green”, 37000);
auto2.name = “INGRID”;
System.out.println(“Auto 2: “ + auto2.toString());
System.out.println(“Driving Auto 1...”);
auto1.drive();
System.out.println(“Driving Auto 2...”);
auto2.drive();
System.out.println(“Starting Auto 1...”);
auto1.start();
System.out.println(“Starting Auto 2...”);
auto2.start();
System.out.println(“Giving Auto 1 a paint job...”);
auto1.setColor(“red”);
System.out.println(“Auto 1 is now “ + auto1.getColor());
System.out.println(“Renaming Auto 1...”);
auto1.name = “CHRISTINE”;
System.out.println(“Auto 1 is named “ + auto1.name);
System.out.println(“Shutting off Auto 2...”);
auto2.shutOff();
System.out.println(“Shutting off Auto 2 AGAIN...”);
auto2.shutOff();
System.out.println(“Auto 1: “ + auto1.toString());
System.out.println(“Auto 2: “ + auto2.toString());
}
}
Subclass of Automobile:
public class BigTruck extends Automobile {
protected boolean trailer;
public BigTruck() {
this(false, DEFAULT_COLOR, 0);
}
public BigTruck(boolean running, String color, int numMiles) {
super(running, color, numMiles);
trailer = false;
}
public void attachTrailer() {
if (trailer) {
System.out.println(“There is already a trailer attached.”);
}
else {
trailer = true;
System.out.println(“Attached a trailer.”);
}
}
public void detachTrailer() {
if (trailer) {
trailer = false;
System.out.println(“Detached the trailer.”);
}
else {
System.out.println(“There is no trailer attached.”);
}
}
public void haul() {
if (trailer) {
drive();
}
else {
System.out.println(“There is nothing to haul.”);
}
}
public String toString() {
String str = super.toString();
str += “, trailer = “ + trailer;
return str;
}
}
Subclass Test:
public class BigTruckTest {
public static void main(String args[]) {
BigTruck truck = new BigTruck();
System.out.println(truck);
System.out.println(“Starting...”);
truck.start();
System.out.println(“Driving...”);
truck.drive();
System.out.println(“Attaching Trailer...”);
truck.attachTrailer();
System.out.println(“Hauling...”);
truck.haul();
System.out.println(“Detaching trailer...”);
truck.detachTrailer();
System.out.println(“Shutting off...”);
truck.shutOff();
System.out.println(“Painting...”);
truck.setColor(“black”);
System.out.println(truck);
}
}
Vector Victor:
import java.util.Vector;
public class VectorVictor {
public static void main(String args[]) {
Vector v = new Vector(5, 5);
System.out.println(“size, capacity”);
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“Fuzzy”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“Wuzzy”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“was”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“a”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“bear”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“Fuzzy”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“Wuzzy”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“had”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“no”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“hair”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“Fuzzy”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“Wuzzy”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“wasn’t”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“fuzzy”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“was”));
System.out.println(v.size() + “, “ + v.capacity());
v.add(new String(“he”));
System.out.println(v.size() + “, “ + v.capacity());
v.trimToSize();
System.out.println(v.size() + “, “ + v.capacity());
String[] str = new String[v.size()];
v.copyInto(str);
for (int s=0; s < str.length; s++) {
System.out.print(str[s] + “ “);
}
}
}