I have a class called "Card"
public class Card
{
private int cardNumber;
private String cardName;
public Card(int cardNumber, String cardName)
{
this.cardNumber = cardNumber;
this.cardName = cardName;
}
public String toString()
{
return cardName;
}
public int getNumber()
{
return cardNumber;
}
}
I'm trying to make a subclass of the Card class called "Ace", but I keep getting the following error whenever I try to compile Ace:
Ace.java:5: error: constructor Card in class Card cannot be applied to given types;
Here's what I have for Ace:
public class Ace extends Card
{
public String isAce;
public Ace()
{
}
public Ace(int cardNumber, String cardName, String isAce)
{
this.cardNumber = cardNumber;
this.cardName = cardName;
this.isAce = "yes";
}
}
I don't understand why I am getting the error when I try to compile Ace. What am I doing wrong?
Card has a constructor (so it doesn't get a default empty constructor). You need to explicitly invoke it in the Ace constructor. Like,
public Ace(int cardNumber, String cardName, String isAce)
{
super(cardNumber, cardName);
this.isAce = "yes";
}
Without an explicit invocation of super() (or this()) the compiler implicitly adds super() (with no arguments). That doesn't work here. Because (as I mentioned) Card already has a non-empty, non-default constructor.
Related
Error
public Cat (String nm, int legs, String sd, String col)
For this constructor I got following compiler error:
constructor Animal in class Animal cannot be applied to given types;
required: String, int
found: no arguments
reason: actual and formal arguments lists differ in length
Code
The parent class is right below the child class.
public class Cat extends Animal {
private String sound;
private String colour;
public Cat (String nm, int legs, String sd, String col) {
nm = super.getName();
legs = super.getNumOfLegs();
sound = sd;
colour = col;
}
public abstract class Animal {
protected String name;
protected int numOfLegs;
public Animal() {
}
public Animal(String nm, int legs) {
name = nm;
numOfLegs = legs;
}
public String getName() {
return name;
}
public int getNumOfLegs() {
return numOfLegs;
}
public abstract String display();
}
}
Should the parent abstract class be placed in a separate file instead?
I've tried that initially but it returned way more errors than it did now, especially from the abstract method display().
What is causing the error?
There are a couple of things you should change.
First of all, it is the best way to put the super class into a separate file. If you want to keep in one file you need drag it out of the Cat class and remove the scope (not public or private). But this is not a good coding style for a super class.
The next thing is, with the name/nm and legs/numOfLegs. Either you call the super constructor and provide the two variables (see my example) or you use name = nm; and numOfLegs = legs;
You should also reconsider if the name and numOfLegs varialbes need to be protected or if is fine to provide the access only through the getter.
If the number of legs, the name, sound and color will not change you could also make them immutable (with the key word final, e.g. private final String sound). If not you can make them accessible with a setter.
Finally you need to implement the abstract method in the Cat class...
public class Cat extends Animal {
private String sound;
private String colour;
public Cat(String nm, int legs, String sd, String col) {
super(nm, legs);
sound = sd;
colour = col;
}
#Override
public String display() {
return null;
}
}
abstract class Animal {
protected String name;
protected int numOfLegs;
public Animal(String nm, int legs) {
name = nm;
numOfLegs = legs;
}
public String getName() {
return name;
}
public int getNumOfLegs() {
return numOfLegs;
}
public abstract String display();
}
I have 2 subclasses(MultiCard and BasicCard) and I want to call a method(addCurrency) belonging to MultiCard from the class Bank. The method is used to add more currency inside a MultiCard which has an ArrayList of currencies. However, because MultiCard is the sub-class of class Card, I can't access the MultiCard. All cards are stored in a List<Card>cards;
I do not want to use a casting based solution (checking with instanceof and then casting the Card instance to a MultiCard)
The Super Class:
public abstract class Card implements Comparable<Card>, Cloneable
{
protected String id;
protected String name;
protected List<Purchase> purchases;
static int counter = 1000;
public Card(String name)
{
counter++;
this.id = Integer.toString(counter);
this.name = name;
this.purchases = new ArrayList<Purchase>();
}
The method I want to use in class MultiCard extends Card
public class MultiCard extends Card implements Cloneable
{
protected static List<String> currencies;
protected double[] balance;
public static final int currencyCount = 5;
public MultiCard (String name)
{
super (name);
currencies = new ArrayList<String>();
balance = new double[currencyCount];
currencies.add ("AUD");
for (int i = 0; i < balance.length; i++)
balance[i] = 0;
}
public boolean addCurrency (String currency)
{
if (currencies.size () == currencyCount ||containsCurrency (currency)) {
return false;
}
currencies.add (currency);
return true;
}
The method addCurrency() that I need to modify in class Bank
private static final String[] currencyLabel = { "AUD", "NZD", "USD", "CND", "YEN", "BPD" };
private static final double[] currencyRate = { 1.0, 1.2, 0.75, 0.85, 80, 0.7 };
private String adminUsername;
private String adminPassword;
Map<String, User> users = new HashMap<>();// username
public Bank(String admName, String admPassword){
this.adminUsername = admName;
this.adminPassword = admPassword;
public boolean addCurrency(String cardID, String username, String password, String currency) { ...
}
The method addCurrency is used to add a currency inside a List which is in MultiCard. It calls the method in the MultiCard
This is what I tried:
public boolean addCurrency(String cardID, String username, String password, String currency)
{
User user = users.get(username);
user.getACard(cardID, username, password); // here is my problem - a Card instance is returned, which must be cast to a MultiCard
}
Since the question is a bit confusing, I assume that when you are calling user.getACard(cardID, username, password) method, you are getting an object of Card . Ideal fix would be that To invoke addCurency() method,
You can add an abstract method public boolean addCurrency (String currency) in the Card class (if at all you are allowed to modify the Card class).
Otherwise you will have to cast the Card Object to MultiCard Object.
I need help fixing my code with the basic concepts listed above. To save from clutter, I took a screen shot of the directions here: https://imgur.com/SdiotUi
However, when I run my code it isn't working. I know there are a lot of errors but I'm having trouble fixing them even though I've spent the past few hours googling the correct way to do this.
When I create the first constructors I am not sure if I am assigning the name and legs correctly, I am having trouble returning "true", I get an error calling the parent class taking one argument, and I don't think I am overriding the abstract class correctly.
My code:
public class Animal1 {
private String animalName;
public int numberOfLegs;
public Animal1(String name){
name = animalName;
name = "John";
}
public Animal1(String name, int legs){
name = animalName;
legs = numberOfLegs;
name = "Jack";
legs = 4;
}
public String getName(){
return animalName;
}
public int getLegs(){
return numberOfLegs;
}
public void isAMammal(){
return true;
}
public void isCarnivorous(){
return true;
}
public abstract class getHello{
}
}
public class Cat1 extends Animal1{
public Cat1(String name){
Animal1.name;
}
public abstract class getHello{
return "Meow";
}
}
public class Dog1 extends Animal1{
public Dog1(String name){
Animal1.name;
}
public abstract class getHello{
return "Woof";
}
}
public abstract class Animal1 { // If you want to have an abstract method, declare the class as abstract
private final String animalName;
private final int numberOfLegs; // better of using private and make it final since it's not going to change.
public Animal1(final String name, final int legs){ //better making the input parameters final since they are not supposed to be changed
//name = animalName;
//legs = numberOfLegs;//it assigned the field to an input parameter. that will take no effect on the object created.
animalName = name;
numberOfLegs = legs;
}
public String getName(){
return animalName;
}
public int getLegs(){
return numberOfLegs;
}
public boolean isAnimal(){ //boolean function needs a return type too!!
return true;
}
public boolean isCarnivorous(){
return true;
}
public abstract String getHello(); // an abstract method has same requirement as a normal method besides the abstract modifier. it will need a return type. And it ends with a semicolon
}
public class Cat1 extends Animal1{
public Cat1(final String name){
super(name, 4); //use super to call parent constructor
}
#Override
public String getHello(){
return "Meow";
}
}
public class Dog1 extends Animal1{
public Dog1(final String name){
super(name, 4);
}
#Override
public String getHello(){
return "Woof";
}
}
First, it looks like a few of your methods are declared as classes. I assume you wanted to make them abstract methods. They need to be changed to:
public abstract String getHello();
Note that abstract methods can only be declared in an abstract class. So, you need to redefine Animal1 as abstract.
public abstract class Animal1
Next, when you implement the abstract method, you define it as
public String getHello()
If you are using an IDE like Eclipse it will automatically offer to generate this method.
Finally, when using your constructor in your child classes like Cat1, you are trying to set "name" as if it was a static variable and bypassing the constructor you already had set for Animal1. The best way to correct this is to change the constructor in Cat1 and Dog1 to call the super constructor.
public Cat1(String name){
super(name);
}
Here is my field.java class. Ive got the public Field(String name, int number) defined here.
public class Field
{
String name;
int number;
public Field(String name, int number){
this.name = name;
this.number = number;
}
public String getName() {
return name;
}
public int getNumber() {
return number;
}
#Override
public String toString() {
return "Field{" + "name=" + name + ", number=" + number + '}';
}
}
Here is my Player.java class, Im getting an error on my Field currentField = new Field(); - it says that my Field is not defined as a constructor in my Field.java class
public class Player
{
private String name;
private int pos;
Field currentField = new Field();
public Player()
{
}
}
Anyone got a suggestion on why Im throwing errors?
You have provided a parameterized constructor in your class
public Field(String name, int number){
this.name = name;
this.number = number;
}
And hence no default (no-arg) constructor is provided when you define a parametrized constructor.
So when you are trying to create an instance using Field currentField = new Field();, it cannot compile since there is no matching constructor.
Solutions you can try:
1.
Add a no-arg constructor to your class :
public Field()
{
}
Or
2.
While creating an instance, pass values to constrcutor :
Field currentField = new Field("abc", 123);
Yes because your class receiving two arguments name and number and you are trying to create instance of it without passing them.
Either you can pass them
Field currentField = new Field("test", 1); // for ex :
or create a default no arg constructor to your Field class.
/** default no arg constructor **/
public Field(){
// TODO : when there is no param
}
Field currentField = new Field();
You are not passing any arguments to the constructor. You will either need to provide a name and number as parameters, or define a default constructor :
public Field(String name, int number){
this.name = name;
this.number = number;
}
Field currentField = new Field("fieldName", 1);
or
public Field(){
this.name = "";
this.number = 0;
}
Field currentField = new Field();
In every Java class there is a default constructor, if you add any other constructor it will override the default constructor. So to make your code work you have to add no argument constructor.
public Field(){
}
This question already has answers here:
Java error: Implicit super constructor is undefined for default constructor
(12 answers)
Closed 8 years ago.
I'm trying to extend my Vehicle class to a HumanPowered class -- Has a field for calories per hour. This is my first time trying to extend a class so I'm a bit confused here.
class Vehicle
{
String description;
String idNumber;
int numWheels;
public Vehicle(String aDescription, String aIdNumber, int aNumWheels)
{
description = aDescription;
idNumber = aIdNumber;
numWheels = aNumWheels;
}
void setDescription (String aDescription)
{
description = aDescription;
}
void setIdNumber (String aIdNumber)
{
idNumber = aIdNumber;
}
void setNumWheels (int aNumWheels)
{
numWheels = aNumWheels;
}
public String getDescription()
{
return description;
}
public String getIdNumber()
{
return idNumber;
}
public int getNumWheels()
{
return numWheels;
}
public String toString()
{
String result= String.format("ID: %s Description: %s Wheels: %d",idNumber,description,numWheels);
return result;
}
}
class humanPowered extends Vehicle
{
int calories;
public humanPowered(String aDescription, String aIdNumber, int aNumWheels, int aCalories) //Error here
{
description = aDescription;
idNumber = aIdNumber;
numWheels = aNumWheels;
calories = aCalories;
}
void setCalories (int aCalories)
{
calories = aCalories;
}
public int getCalories()
{
return calories;
}
public String toString()
{
String result= String.format("ID: %s Description: %s Wheels: %d Calories per Hour: %d",idNumber,description,numWheels, calories);
return result;
}
}
I'm getting an error marked above on my constructor for my humanPowered class saying "Implicit super constructor Vehicle() is undefined. Must explicitly invoke another constructor." I can't figure out where I'm going wrong here. Thanks for any and all help!
Vehicle don't have default constructor hence you have to call its constructor form humanPowered class passing required arguments at the first line of its constructor.
public humanPowered(String aDescription, String aIdNumber, int aNumWheels, int aCalories)
{
super(aDescription,aIdNumber,aNumWheels);
...//other code
}
Points to remember:
Every class have default constructor that is no-argument constructor
If class creates a constructor passing arguments then by default constructor is not created
Each constructor by default calls default constructor of its super-class