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!
Related
thx for reading.
I need to create constructor, which in one case has default values example below.
this code is wrong of course, Is it possible to do that it means in case "dog" constructor will not ask for parameters: weight, age and will fill up class fields: weight=1, age=1.
In another case for example cat will ask for all parameters? How usually this problem is solved?
public Animal(String spacies, String name, double weight, byte age, boolean isALive){
if (spacies.equals("dog")) {
this.name = name;
this.weight= 1;
this.age = 1;
this.isALive = isALive;
} else {
this.spacies = spacies;
this.name = name;
this.weight = weight;
this.age = age;
this.isALive = isALive;
}
}
Best way is to use inheritance. Animal is abstract entity. You should derive specific animals from those.
There can be different approaches. Tried to provide one simple solution.
public abstract class Animal {
private final String species;
private final String name;
private final double weight;
private final byte age;
private final boolean isALive;
public Animal(String species, String name, double weight, byte age, boolean isAlive) {
this.species = species;
this.name = name;
this.weight = weight;
this.age = age;
this.isALive = isAlive;
}
}
class Dog extends Animal {
public Dog(String dogName, boolean isAlive) {
super("Dog", dogName, 1.0, (byte) 1,isAlive);
}
}
Try relating to real world when working on OOPS concepts.
Create a second constructor that sets these values
public Animal(String spacies, String name, boolean isALive){
this("dog", name, 1, 1, true)
}
I am working on my class assignment and I am stuck.
I have two classes: Part, which is abstract and has InHouse and Outsourced classes that extend Part. Then I have Product, which oddly has an observableArrayList of parts called associatedParts.
I am working on my AddProductController, trying to make a call to the method in the Product class addAssociatedPart(). My problem is the compiler doesn't find the method in Part. If I cast to an InHouse, it doesn't find the method in InHouse, and so on. I can't use a static method, because the method addAssociatedPart() is supposed to be non-static per the UML design. So, I can't tell it explicitly to find it in Product.addAssociatedPart(), because it tells me I can't reference a non-static etc.
Here's the code snippets starting with the Product class.
public class Product {
private ObservableList<Part> associatedParts = FXCollections.observableArrayList();
private int id;
private String name;
private double price;
private int stock;
private int min;
private int max;
public void addAssociatedPart(Part part) {
getAllAssociatedParts().add(part);
}
public ObservableList<Part> getAllAssociatedParts() {
return this.associatedParts;
}
And then the AddProductScreenController class:
public class AddProductScreenController implements Initializable {
#FXML
public void onAddProductAddPart(ActionEvent event) {
// this is triggered when the Add button is clicked
Part selectedItem = addProductTableViewAll.getSelectionModel().getSelectedItem();
selectedItem.addAssociatedPart(); // can't find method
Product.selectedItem.addAssociatedPart(); // can't find variable selectedItem (obviously bad formatting)
selectedItem.Product.addAssociatedPart(); // can't find variable Product (again bad formatting)
addAssociatedPart(selectedItem); // can't find method addAssociatedPart()
Product.addAssociatedPart(selectedItem); // non-static method, can't be referenced from a static context
InHouse newPart = new InHouse(1, "test", 1.99, 1, 1, 1, 101);
addAssociatedPart(newPart); // can't find method
Product.addAssociatedPart(newPart); // non-static method
newPart.addAssociatedPart(); // can't find method
addProductTableViewPartial.setItems(associatedParts);
}
}
The part code as requested:
public abstract class Part {
private int id;
private String name;
private double price;
private int stock;
private int min;
private int max;
public ObservableList<Part> allParts = FXCollections.observableArrayList();
public Part(int id, String name, double price, int stock, int min, int max) {
this.id = id;
this.name = name;
this.price = price;
this.stock = stock;
this.min = min;
this.max = max;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return this.id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setPrice(double price) {
this.price = price;
}
public double getPrice() {
return this.price;
}
public void setStock(int stock) {
this.stock = stock;
}
public int getStock() {
return this.stock;
}
public void setMin(int min) {
this.min = min;
}
public int getMin() {
return this.min;
}
public void setMax(int max) {
this.max = max;
}
public int getMax() {
return this.max;
}
}
This is InHouse
package model;
public class InHouse extends Part {
private int machineId;
public InHouse(int id, String name, double price, int stock, int min, int max, int machineId) {
super(id, name, price, stock, min, max);
this.machineId = machineId;
}
public void setMachineId(int machineId) {
this.machineId = machineId;
}
public int getMachineId() {
return this.machineId;
}
}
And then Outsourced:
package model;
public class Outsourced extends Part {
private String companyName;
public Outsourced(int id, String name, double price, int stock, int min, int max, String companyName) {
super(id, name, price, stock, min, max);
this.companyName = companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getCompanyName() {
return this.companyName;
}
}
If there is a particular part of Java you feel I need to brush up on to understand this, I am wide open to that. I want to understand the issue, not just get a fix. I'm not even looking for the answer, just a point in the direction of what the problem is.
Update
#Le and #Jackson pointed me in the right direction with their comments on the response he provided. I need to have a product first:
Product product = new Product(1, "test", 1.99, 1, 1, 1);
product.addAssociatedPart(selectedItem);
I was trying to explain you association of your various classes in comments but thought I would use visual help. I have simplified your scenario into a classic OOP problem.
public class Product {
public void addAssociatedPart(Part part) {
// some stuff
}
}
public abstract class Part {
}
public class InHouse extends Part {
}
public class Outsourced extends Part {
}
public class Assembly {
public static void main(String[] args) {
Product car = new Product();
Part seat = new InHouse();
Part engine = new Outsourced();
Part window = new InHouse();
car.addAssociatedPart(seat);
car.addAssociatedPart(engine);
car.addAssociatedPart(window);
}
}
I do not have any method in my Part or its sub-classes to add themselves to some Product. Was this you trying to achieve?
I am working on my AddProductController, trying to make a call to the
method in the Product class addAssociatedPart().
My problem is the compiler doesn't find the method in Part.
Why should it? Is Part a child of Product? Otherwise, you are calling a Product Method using a Part instance.
To use the methods of Inhouse and Oursourced for parts, you can do something like this
if (selectedItem instanceof InHouse){
Inhouse inhouse = (Inhouse)selectedItem;
//do what you need with inhouse methods
}else{
Outsourced outsourced = (Outsourced)selectedItem;
//do what you need with oursourced method
}
You are confused with static and non static method. You need a Product instance to access AddAssociatedPart(). Visualize your class in class diagram.
public void onAddProductAddPart(ActionEvent event) {
// this is triggered when the Add button is clicked
Part selectedItem = addProductTableViewAll.getSelectionModel().getSelectedItem();
selectedItem.addAssociatedPart(); // addAssociatedPart() is method of Product, not Part
Product.selectedItem.addAssociatedPart(); // Product class has no static member selectedItem
selectedItem.Product.addAssociatedPart(); // syntax error
addAssociatedPart(selectedItem); // addAssociatedPart() is not method of AddProcutController
Product.addAssociatedPart(selectedItem); // if you reference the method start with a class, the method is expected to be a static method. addAssociatedPart() is not a static method, call it with a product instance
InHouse newPart = new InHouse(1, "test", 1.99, 1, 1, 1, 101);
addAssociatedPart(newPart); // addAssociatedPart() is not part of AddProductController
Product.addAssociatedPart(newPart); // dont reference non-static method with a class name
newPart.addAssociatedPart(); // addAssociatedPart() is not part of Part
addProductTableViewPartial.setItems(associatedParts);
}
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
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 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);
}
}