Can anyone tell me why the array size of the titles array in the data definition class doesn't equal the value the user inputted that was passed in from the implementation class and set as an instance variable in the data definition class?
This is the Data Definition Class.
public class Photograph {
private int maxTakes;
public Photograph() {
this.titles = new String[this.maxTakes];
numPhotosTaken = 0;
}
public void setMaxTakes(int maxTakes) {
this.maxTakes = maxTakes;
}
public boolean setTitle(String title) {
if (this.numPhotosTaken < this.titles.length) {
this.titles[numPhotosTaken] = title;
numPhotosTaken++;
return true;
}
else {
return false;
}
}
}
This is the implementation class.
import javax.swing.JOptionPane;
public class MakePhotographs {
public static void main (String[] args) {
Photograph photo;
do {
photo = create();
} while (JOptionPane.showConfirmDialog(null, "Enter another couple?") == JOptionPane.YES_OPTION);
}
private static Photograph create() {
Photograph photo = new Photograph();
photo.setMaxTakes(Integer.parseInt(JOptionPane.showInputDialog("Enter maximum number of photos to take")));
do {
String title = JOptionPane.showInputDialog("Enter title of photo");
if (!photo.setTitle(title)) {
JOptionPane.showMessageDialog(null, "No more photos allowed!");
}
} while (JOptionPane.showConfirmDialog(null, "Enter another photo?") == JOptionPane.YES_OPTION);
return photo;
}
}
maxTakeswhich, at the time the constructor is called, is 0.