I am making a little text based game for fun and i'm messing around with generics and have run into an issue.
I have a class Equippable:
public class Equippable extends Item
{
double mDurability;
public Equippable(String name, double weight, double durability)
{
super(name, weight);
mDurability = durability;
}//Equippable
//Getters
public double getDurability(){return mDurability;}
//Setters
public void setDurability(double Durability){mDurability = Durability;}
}//Equippable
I have a constructor for a class called BodyPart that I want to accept any subclass of Equippable as a parameter:
public BodyPart(String name, Class<? extends Equippable> equipClass)
{
mName = name;
mEquipClass = equipClass;
}//BodyPart
Equippable has a subclass Armor.
public class Armor extends Equippable
{
double mArmorRating;
public Armor(String name, double weight, double durability, double armorrating)
{
super(name, weight, durability);
mArmorRating = armorrating;
}//Armor
}//Armor
Finally I have a couple classes that extend Armor.class. I won't post them all as they are identical at this point except for the name of the class.
public class HumanoidBodyArmor extends Armor
{
public HumanoidBodyArmor(String name, double weight, double durability, double armorrating)
{
super(name, weight, durability, armorrating);
}//HumanoidBodyArmor
}//HumanHeadArmor
When I do:
BodyPart bp = new BodyPart("Head" , Armor.class));
everything is fine, and the object is created. However when I do
BodyPart bp = new BodyPart("Head" , HumanHeadArmor.class));
Eclipse throws a fit and says
The constructor BodyPart(String, Class<HumanoidHeadArmor>) is undefined
I was so sure this would work but apparently I am missing something. Can somebody shed some light on the subject?
Just for sanity I did
Equippable.class.isAssignableFrom(HumanoidHeadArmor.class)
and it did indeed evaluate to true.
EDIT
As requested in the comments, here is the HumanoidBodyArmor class
public class HumanoidBodyArmor extends Armor
{
public HumanoidBodyArmor(String name, double weight, double durability, double armorrating)
{
super(name, weight, durability, armorrating);
}//HumanoidBodyArmor
}//HumanoidBodyArmor
There is no difference between headarmor/bodyarmor/footarmor. Thy are all identical besides the name. Just empty classes with identical constructors that just call their parent's (Armor) constructor.
This all works fine for me - I suspect you have some naming issue.
class Item {
public Item(String name, double weight) {
}
}
public class Equippable extends Item {
double mDurability;
public Equippable(String name, double weight, double durability) {
super(name, weight);
mDurability = durability;
}
//Getters
public double getDurability() {
return mDurability;
}
//Setters
public void setDurability(double Durability) {
mDurability = Durability;
}
}
class BodyPart {
String name;
Class<? extends Equippable> equipClass;
public BodyPart(String name, Class<? extends Equippable> equipClass) {
this.name = name;
this.equipClass = equipClass;
}
}
public class Armor extends Equippable {
double mArmorRating;
public Armor(String name, double weight, double durability, double armorrating) {
super(name, weight, durability);
mArmorRating = armorrating;
}
}
public class HumanoidBodyArmor extends Armor {
public HumanoidBodyArmor(String name, double weight, double durability, double armorrating) {
super(name, weight, durability, armorrating);
}
}
public void test() {
BodyPart head = new BodyPart("Head", Armor.class);
BodyPart body = new BodyPart("Body", HumanoidBodyArmor.class);
}
You are not using generics correctly
To start with, Equippable should probably be an interface rather than a class, and Armor can also be an interface that extends Equippable
Then you can have
public class BodyPart {
public BodyPart(String name, Equipable equip)
{
mName = name;
mEquip = equip;
}
}
and
public class Head extends BodyPart {
public Head(String name, HumanoidHeadArmor equippable) {
super(name, equippable);
}
}
Related
My assignment for uni has clear instructions and they want me to create a dish by utilizing an interface. But I can't even create a proper test-dish without an error, what am I doing wrong?
DishTypes as enum:
(This can't be changed at all)
public enum DishType {
STARTER, MAIN_DISH, DESSERT, OTHER;
}
Interface Dish:
(For the interface the classes and names can't be changed/deleted as per instructions, only their body and return-statements)
public interface Dish {
String getName();
double getBasePrice();
DishType getDishType();
static Dish createDish(String name, double basePrice, DishType type) {
return createDish(name,basePrice,type);
}
}
And here is the class I created on my own to be able to implement a dish:
public class DishImplementation implements Dish {
public String name;
private double basePrice;
private DishType type;
public DishImplementation(String name, double basePrice, DishType type) {
this.name = name;
this.basePrice = basePrice;
this.type = type;
}
#Override
public String getName() {
return name;
}
#Override
public double getBasePrice() {
return basePrice;
}
#Override
public DishType getDishType() {
return type;
}
Now if I try to create a test-dish using my Test class I always get an error:
public class Test {
public static void main(String[] args) {
Dish d1 = Dish.createDish("a",1.0, DishType.MAIN_DISH);
System.out.println(d1.getName());
}
}
How do I get the console to actually show me the name of my test-dish?
Your createDish(...) method should return an object instead of recursively invoking itself.
Here is how you'd do it.
public interface Dish {
String getName();
double getBasePrice();
DishType getDishType();
static Dish createDish(String name, double basePrice, DishType type) {
return new DishImplementation(name,basePrice,type);
}
}
I tried searching around for an answer to this question but I am either
1. not asking it correctly
2. not thinking straight
Basically, I have an assignment that is a mock grocery store. I have an Item class that I need to use for subclasses that will inherit the traits from the Item class. (inheritance)
Below is what I have so far for this class and its working to my expectations
public class Baguette extends Item {
int price;
String name;
public Baguette(String name, int price){
this.name = name;
this.price = price;
}
#Override
public String getName(){
return name;
}
public int getCost(){
return price;
}
}
Now here is the subclass of this Baguette class
public class FlavoredBaguette extends Baguette {
String name;
int price;
String flavor;
int costFlav;
public FlavoredBaguette(String name, int price, String flavor, int costFlav)
{
this.name = name;
this.price = price;
this.flavor = flavor;
this.costFlav = costFlav;
}
}
Upon doing this I get this error in the line
constructor Baguette in class baguette cannot be applied to given types
required: string, int
found: no arguments
reason: actual and formal argument lists differ in length
I know it has something to do with the number of arguments being different but I'm clueless here. Thanks for the help!
You need to call the Baguette's constructor first, super(name, price);, this means that FlavoredBaguette doesn't need name or price as it will inherit these from Baguette
public class FlavoredBaguette extends Baguette {
//String name;
//int price;
String flavor;
int costFlav;
public FlavoredBaguette(String name, int price, String flavor, int costFlav)
{
super(name, price);
//this.name = name;
//this.price = price;
this.flavor = flavor;
this.costFlav = costFlav;
}
}
For extending, you don't need to declare the fields of the super class. also in the constructor, simply call super() with the parameters name and price.
public class FlavoredBaguette extends Baguette {
String flavor;
int costFlav;
public FlavoredBaguette(String name, int price, String flavor, int costFlav){
super(name, price);
this.flavor = flavor;
this.costFlav = costFlav;
}
edit: looks like MadProgrammer beat me to it :c
public class Monitor extends Peripheral {
public Monitor(){
super();
}
public Monitor (String name,String maker, int age, int price,String type,int size,String res,String ports){
super(name,maker,age,price);
this.type = typel
this.size = size;
this.res = res;
this.ports = ports;
}
}
This is the the child class.I want to make it so that I can create a monitor object without giving it any parameters. These are the classes of its parents:
public class Product {
protected String name, maker;
protected int age,price;
public Product(){}
public Product(String name,String maker, int age, int price){
this.name = name;
this.maker = maker;
this.age = age;
this.price = price;
}
}
public class Peripheral extends Product {
//basically nothing here
private static double discount = 0;
public static void setDiscount(double disc){
discount = disc;
}
public static double getDiscount(){
return discount;
}
}
The compiler says:error: constructor Peripheral in class Peripheral cannot be aplied to given types;
super();
required: String,String,int,int
found: no arguments
There is no constructor with Peripheral parametars!
Peripheral(String name, String maker, int age, int price)
Peripheral does not inherit constructor from Product, you need to declare it explictly:
public class Peripheral extends Product {
public Peripheral(String name,String maker, int age, int price) {
super(name,maker,age,price);
}
//....
}
first of all, you have a typo in here:
public Monitor (String name,String maker, int age, int price,String type,int
size,String res,String ports){
super(name,maker,age,price);
this.type = typel <--- here(nasty semi-colons)
I think you will forcefully have to create a product object before your monitor object as you will be missing those super parameters which your monitor is trying to get.
A work around is, you might want to create another constructor that doesn't have the super(params); so that way it will get rid of that error you are getting.
so instead of your constructor, you should do something like this:
public Monitor (String type,int size,String res,String ports){
this.type = type;
this.size = size;
this.res = res;
this.ports = ports;
}
hope this solves your issue!
when i trying to do this i got the problem said
Constructor Product in class Product cannot be applied to given types;
required: java.lang.String,int,double; found: java.lang.String;
reason: actual and formal arguments lists differ in length
And i have 2 classes:
import java.text.*
public class Product {
private String name;
private int stock;
private double price;
public Product(String name, int stock, double price) {
this.name = name;
this.stock = stock;
this.price = price;
}
public double sell(int n) {
stock = n;
return stock;
}
public void restock(int n) {
}
#Override
public String toString() {
return stock + name + "at $"+price;
}
}
public class Store {
public static void main(String[] args) {
new Store().use();
}
private Product product;
private Product cashRegister;
public Store() {
product = new Product("Sticky tape");
cashRegister = new Product("Cash register");
}
public void use() {
}
private void sell() {
}
private void restock() {
}
private void viewStock() {
}
private void viewCash() {
}
private void help() {
System.out.println("Menu options");
System.out.println("s = sell");
System.out.println("r = restock");
System.out.println("v = view stock");
System.out.println("c = view cash");
System.out.println("x = exit");
}
}
I understand that i have to declare for Product constructor. But i think i have done it. If anyone know where i got wrong please explain. Thank you!
you do not have constructor with one param, so you can not using this form
product = new Product("Sticky tape");
decare one more constructor with one param or fill all param
product = new Product("Sticky tape", 10, 20.0);
You need to:
overload the constructor
public Product(String name){...}
or create instances of Product using the right and only one constructor uor have:
public Product(String name, int stock, double price)
if you overload then something like this should happen
public Product(String name){
this(name, 0, 0.0);
}
so you call a constructor from the other constructor
This is the time to learn constructor overloading. Overloading comes from OOP.
You can use Overloading to methods and constructors. Overloading means for a same method name you can implement that method
several time with different parameters(number of parameters)
. Actualy not only that,
you can use different data types for parameter.
also can change order of parameter.
keep remember method name must be same.
For the constructor also same thing. If you use for constructor you can add parameters like:
//constructor with one parameter
public Product(String name) {
this.name = name;
this.stock = 0;//or whatever your default value
this.price = 0;//or whatever your default value
}
//constructor with two parameter
public Product(String name, , int stock) {
this.name = name;
this.stock = stock;
this.price = 0;//or whatever your default value
}
public Product(String name, int stock, double price) {
this.name = name;
this.stock = stock;
this.price = price;
}
Like that you can add as many as you want.
Or you can use one constructor and pass argument to match with the implementation of the constructor when creating object. Like below:
product = new Product("Sticky tape", 0, 0);
this is not complete description you can read this to learn more
You have no constructor In Product class that takes single String argument. Create it like so:
public Product(String name) {
this.name = name;
}
In import statement you forgot semicolon:
import java.text.*;
your program is having 3 coding error which include
you forgot the " ; " after " import java.text.* " actually it is not required in your code, you can remove it, nothing will change.
you cannot make class Product as public , because you've made "Store" as your Primary class having main method.So remove public keyword from the Product class.
You didn't create a parameterized constructor
which should be like
public Product(String name){ this.name = name;}
in your product class.
your code will be like after correcting
class Product {
private String name;
private int stock;
private double price;
public Product(String name, int stock, double price) {
this.name = name;
this.stock = stock;
this.price = price;
}
public Product(String name) {
this.name = name;
}
public double sell(int n) {
stock = n;
return stock;
}
public void restock(int n) {
}
#Override
public String toString() {
return stock + name + "at $"+price;
}
}
public class Store {
public static void main(String[] args) {
Store s = new Store();
System.out.println(s.product);
System.out.println(s.cashRegister);
}
private Product product;
private Product cashRegister;
public Store() {
product = new Product("Sticky tape");
cashRegister = new Product("Cash register");
}
}
The errors are in these lines of code:
product = new Product("Sticky tape");
cashRegister = new Product("Cash register");
The Product constructor defined expects:
public Product(String name, int stock, double price)
I am currently working on a project for java and the teacher isn't very great at explaining himself.
Currently I'm stuck trying to reference an extended Class in java to get the balance. (i'm trying to edit the balance so that it can be displayed and changed in the array.
my main account looks like this
public class Account {
private String name;
private double quantity, price;
private double rate;
private Asset[] Asset;
}
The account that is extended is
abstract public class Asset {
String symbol;
public String getSymbol() {
return symbol;
}
protected Asset(String symbol) {
this.symbol = symbol;
}
}
The extensions look like so;
public class Cash extends Asset {
private double Quantity;
public Cash(double Quantity, String symbol) {
super(symbol);
this.Quantity = Quantity;
}
#Override
public String getSymbol() {
return super.getSymbol(); //To change body of generated methods, choose Tools | Templates.
}
public double getQuantity() {
return Quantity;
}
public void setQuantity(double Quantity) {
this.Quantity = Quantity;
}
}
Now lets say I have a test instance created of class Account.
How would I be able to edit the Cash value from the Class account? (Main arg)
Here are the other extensions I have too just for reference.
public class Stock extends Asset {
private String name;
private double quantity, rate;
public Stock(String name, double quantity, double rate, String symbol) {
super(symbol);
this.name = name;
this.quantity = quantity;
this.rate = rate;
}
#Override
public String getSymbol() {
return super.getSymbol(); //To change body of generated methods, choose Tools | Templates.
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getQuantity() {
return quantity;
}
public void setQuantity(double quantity) {
this.quantity = quantity;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
}
If it helps, how would I be able to change the values in the extended classes?
I've tried
Asset[0] = new Cash(25000.00,string);
but that's only helpful in putting the value manually.
Apologies for any mistakes, I'm pretty new to coding and java in general.
The way you named your Asset array is confusing, java is case sensitive so name it for example:
private Asset[] asset;
You need to get a reference of the right type through casting before you can access it's own methods. You can do for example:
if (asset[0] instanceof Cash) {
Cash cash = (Cash)asset[0];
// Then you can access the setQuantity method
cash.setQuantity(25000.00);
}