I’m trying out java and I seem to be running into some problems. The only problem I seem to be having is when I add an extension of the Stars class, the constructor seems to be called without me declaring one like Stars test = new Star();
Knight.java
import javax.swing.JOptionPane;
public class Knight extends Stars {
private String name;
private int health, battles, age, gold;
public Knight() {
name = JOptionPane.showInputDialog("What is the knight's name?");
String message = String.format("How much health does %s have?", name);
health = Integer.parseInt(JOptionPane.showInputDialog(message));
message = String.format("How many battles has %s been in?", name);
battles = Integer.parseInt(JOptionPane.showInputDialog(message));
message = String.format("How old is %s?", name);
age = Integer.parseInt(JOptionPane.showInputDialog(message));
message = String.format("How much gold does %s have?", name);
gold = Integer.parseInt(JOptionPane.showInputDialog(message));
}
public String getStats() {
// String message =
return String.format("\nKnight Name: %s\nKnight Health: %d\nKnight Battles: %d\nKnight Age: %d\nKnight Gold: $%d\n\n", name, health, battles, age, gold);
}
}
Stars.java
import javax.swing.JOptionPane;
public class Stars {
private int rows, cols;
private String skyScape = new String();
public Stars() {
rows = Integer.parseInt(JOptionPane.showInputDialog("How many rows of stars are there?"));
cols = Integer.parseInt(JOptionPane.showInputDialog("How many columns of stars are there?"));
for (int count = 0; count < rows; ++count) {
if ((count % 2) == 1) {
skyScape += " *";
} else {
skyScape += "*";
}
for (int colCount = 1; colCount < cols; ++colCount) {
skyScape += " *";
if (colCount == cols - 1) {
skyScape += "\n";
}
}
}
}
public int getRows() {
return rows;
}
public int getCols() {
return cols;
}
public String getSky() {
return skyScape;
}
}
Any help would be appreciated!
Java requires that every constructor (except Object’s) call some superclass constructor, to ensure that the data contained in the superclass is initialized. If you don’t call a superclass constructor explicitly, the compiler will insert an implicit call to the default (zero-argument) constructor of the superclass.