Am trying to implement a system for a car dealer, but when I try to instantiate my Car class in a derived class, I get the error message
Multiple markers at this line
- The constructor Car(String, int, String, String, double, double) is
undefined
Here's the parent class Car:
package Number3;
public class Car {
private String plateNum;
private int year;
private String make;
private String model;
protected double costPrice;
protected double sellingPrice;
public Car()
{
plateNum = "";
year = 1990;
make = "";
model = "";
costPrice = 0.0;
sellingPrice = 0.0;
}
public Car(String plateNum,int year,String make,String model,double costPrice,double sellingPrice)
{
this.plateNum = plateNum;
this.year = year;
this.make = make;
this.model = model;
this.costPrice = costPrice;
this.sellingPrice = sellingPrice;
}
public double getCostPrice()
{
return costPrice;
}
public double computeSellPrice()
{
sellingPrice = costPrice;
return sellingPrice;
}
public void displayCarDetails()
{
System.out.println("Plate number: "+plateNum);
System.out.println("Year: "+year);
System.out.println("Make: "+make);
System.out.println("Cost price: "+costPrice);
System.out.println("Selling price: "+sellingPrice);
}
}
and the subclass newCar:
package Number3;
public class newCar extends Car{
private double tax;
public newCar(String plateNum,int year, String make, double costPrice, double sellingPrice, double tax)
{
super(plateNum,year,make,costPrice,sellingPrice); //where the error is found
this.tax = (25/100);
}
public double computeSellPrice()
{
sellingPrice = costPrice + (costPrice * tax);
return sellingPrice;
}
public void displayCarDetails()
{
super.displayCarDetails();
}
}
Any help would be appreciated.
The signature of your Car constructor does not match the signature in the derived class.
In your Car class, this is the constructor:
public Car(String plateNum,int year,
String make,String model,double costPrice,double sellingPrice) {
...
}
It is String, int, String, String, double, double)
While in the derived class:
you have:
super(plateNum,year,make,costPrice,sellingPrice)
Which is int, int, String, double, double
Change the arguments in call to Super in your newCar class to match the constructor of your Car class. That is, in your newCar class, the line
super(plateNum,year,make,costPrice,sellingPrice)
should be :
super(plateNum, year,
make, model, costPrice, sellingPrice)
The Car class does not have a constructor that takes 5 parameters.
It is defined as
public Car(String plateNum,int year,String make,String model,double costPrice,double sellingPrice)
{
...
}
and you're trying to call it without passing the model parameter.
super(plateNum,year,make,costPrice,sellingPrice); //where the error is found
Your super/parent class Car has a no-argument constructor public Car() { and the following 6-parameter constructor which is being called from the sub/child class constructors using the keyword super.
public Car(String plateNum,int year,String make,String model,double costPrice,double sellingPrice)
Notice, that it expects String model as its fourth parameter but your public newCar() constructor is passing it only five parameters. The parameter model is missing.
public newCar(String plateNum,int year, String make, double costPrice, double sellingPrice, double tax)
{
super(plateNum,year,make,costPrice,sellingPrice); // model MISSING!
So, to fix it, either modify the constructor to accept model as well (just like in your usedCar() constructor) or pass null to the super class constructor as
super(plateNum,year,make,null,costPrice,sellingPrice); // model = null
Related
public class ProductionWorker extends Employee
{
private int shift;
private double rateOfPay;
private double hoursWorked;
ProductionWorker(String name, int id, int shift, double rateOfPay, double hoursWorked)
{
super(name, id);
this.shift = shift;
this.rateOfPay = rateOfPay;
this.hoursWorked = hoursWorked;
}
public class TeamLeader extends ProductionWorker
{
private double monthlyBonus;
TeamLeader(String name,int id, int shift, double rateOfPay, double hoursWorked, double monthlyBonus)
{
super(name, id , shift, rateOfPay, hoursWorked);
this.monthlyBonus = monthlyBonus;
}
public double calcPay()
{
double pay = 0;
//night shift
if (shift == 2)
{
pay = ((hoursWorked + hoursWorked / 2) * rateOfPay) + monthlyBonus;
}
else
{
pay = (hoursWorked * rateOfPay) + monthlyBonus;
}
return pay;
}
}
I extend the class ProductionWorker to class TeamLeader then create a method calcPay() on the class TeamLeader my problem is I need to get the value of variable shift, hoursOfWorked and rateOfPay from the class ProductionWorker so i can use the method calcPay() to my main.Can anybody help me, thank you in advance.
To gain access to the variables of parent class (ProductionWorker), you need to change the variable scope from private to protected.
Private variables are visible only within the class.
Protected variables are visible in subclasses and to all classes within the package
You may refer to this link : http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html.
If you do not wish to set them as protected, you can create getters and setters for the variables too, but that is not the preferred way to go.
eg of getter and setter:
private int shift;
protected int getShift() {
return this.shift;
}
protected void setShift(int shift) {
this.shift = shift;
}
Because your fields are private that means that unless you create a method that returns the particular value you are looking for when the Object is instantiated, none of your other classes or their methods will be able to access that data, hence it being private.
What you need to do is create "getter" methods in your ProductionWorker class that will allow you to say something like this in your TeamLeader class:
shift = ProductionWorker.getShift();
These sorts of methods are specifically designed so that if you need to look at the fields of the Object from a different class, you can simply create the object and use its method to return the value. Here is what these methods should look like:
public int getShift()
{
return shift;
}
public double getHoursWorked()
{
return hoursWorked;
}
public double getRateOfPay()
{
return rateOfPay;
}
By putting these public methods in your superclass, you will be able to create a ProductionWorker in your TeamLeader subclass and use its methods to access the instance variables.
UPDATE
Here is some sample code that may work for your cause:
public class ProductionWorker extends Employee
{
private int shift;
private double rateOfPay;
private double hoursWorked;
ProductionWorker(String name, int id, int shift, double rateOfPay, double hoursWorked)
{
super(name, id);
this.shift = shift;
this.rateOfPay = rateOfPay;
this.hoursWorked = hoursWorked;
}
// Now we can add the "getters"
public int getShift()
{
return shift;
}
public double getHoursWorked()
{
return hoursWorked;
}
public double getRateOfPay()
{
return rateOfPay;
}
public class TeamLeader extends ProductionWorker
{
ProductionWorker prodWorker = new ProductionWorker(Jared, 10046, 2, 8.50, 12.6); // Creates a production worker you can reference
private int shift = prodWorker.getShift();
private double rateOfPay = prodWorker.getRateOfPay();
private double hoursWorked = prodWorker.getRateOfPay();
// These three statements demonstrate the getters and how they can be implemented
private double monthlyBonus;
TeamLeader(String name,int id, int shift, double rateOfPay, double hoursWorked, double monthlyBonus)
{
super(name, id , shift, rateOfPay, hoursWorked);
this.monthlyBonus = monthlyBonus;
}
public double calcPay()
{
double pay = 0;
//night shift
if (shift == 2)
{
pay = ((hoursWorked + hoursWorked / 2) * rateOfPay) + monthlyBonus;
}
else
{
pay = (hoursWorked * rateOfPay) + monthlyBonus;
}
return pay;
}
}
Keep in mind that this may not be the perfect solution for your entire project, as I cannot see the other superclass you are extending from or what your client should do with this code. This sample code is just meant to give you an idea of how getters work in classes that make Objects and demonstrate how they can be used. I would definitely keep the getters I set for you.
Also
Make sure that for every instance variable you have for an Object, you should have a getter for each one. You will find you might need to access that data from time to time to make calculations in a client.
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
Ok guys I've got an assignment that has a abstract class 'Order' and three other classes that extend it 'OverseasOrder', 'RegularOrder', and 'NonProfitOrder'
Here is my abstract class:
public abstract class Order {
protected String location;
protected double price;
public Order(double price, String location){
}
public abstract double calculateBill();
public String getLocation() {
return location;
}
public double getPrice() {
return price;
}
public abstract String printOrder(String format);
}
and here is my 'NonProfitOrder':
public class NonProfitOrder extends Order {
public NonProfitOrder(double price, String location) {
super(price, location);
}
public double calculateBill() {
double bill;
bill = price;
return bill;
}
public String printOrder(String format){
String Long = "Non-Profit Order" + "\nLocation: " + getLocation() + "\nTotal Price: " + getPrice();
return Long;
}
}
I'm taking it step by step to make sure everything is working so this is the only classes I have written so far. The problem I having is when I test something like
public class OrderTester {
public static void main(String[] args) {
Order o;
o = new NonProfitOrder(2000.0, "NY");
System.out.println(o.printOrder("Long"));
}
}
Non-Profit Order
Location: Null
Total Price: 0.0
I'm not sure if I'm calling the 'price' and location' wrong in my string, or if I'm doing something wrong in trying to implement those methods from the abstract Order class
Thanks for any and all help!
Your super constructor doesn't set the location
public Order(double price, String location){
}
so this constructor
public NonProfitOrder(double price, String location) {
super(price, location); // calls super class' constructor
}
doesn't actually set the price and location.
Change the constructor for Order to
public Order(double price, String location){
this.double = double;
this.location = location;
}
Un-initialized fields are given a default value. For reference types, that value is null. For numerical types, the value is 0. For boolean types, the value is false. That's what you were seeing.
You have just missing to assign your attributes in the abstract class.
public Order(double price, String location){
this.price = price;
this.location = location;
}
You have not initialized instance variables location and price.
So Java provides default values for those.
Here location is type String object so default value is null
The price is double type so default value is 0.0.
Those values are printing as output in your case.
So try to initialize them in Order class constructor
public Order(double price, String location){
this.double = double;
this.location = location;
}
I want to be able to give a specific value to discount depending on certain requirements like the following age: > 25 and profession = teacher / professor get 10% discount, age < 25 and gradepoint > 7 get 25% discount
this is my code so far I am using double OO paradigm:
public class customer {
//attribute definitions
private String name;
private String address;
private String profession;
private Integer age;
private Integer gradepoint;
private double discount;
//constructor
public customer(String newName, String newAddress, String newProfession, Integer newAge, Integer newGradepoint, double newDiscount)
{
setName(newName);
setAddress(newAddress);
setProfession(newProfession);
setAge(newAge);
setGradepoint(newGradepoint);
setDiscount (newDiscount);
}
//getters
public String getName()
{ return name;}
public String getAddress()
{ return address;}
public String getProfession()
{ return profession;}
public Integer getAge()
{ return age;}
public Integer getGradepoint()
{ return gradepoint;}
public double getDiscount()
{ return discount;}
//setters
public void setName (String newName)
{ name = newName;}
public void setAddress (String newAddress)
{ address = newAddress;}
public void setProfession (String newProfession)
{ profession = newProfession;}
public void setAge (Integer newAge)
{ age = newAge;}
public void setGradepoint (Integer newGradepoint)
{ gradepoint = newGradepoint;}
public void setDiscount (double newDiscount)
{ discount = newDiscount;}
//methods
}
Would I need to create a sub class called discount or each type of discount? or I can write a method directly into this customer class to control the discount?
write a method directly into this customer class to control the discount?
This. Make it a calculated field. Kill setDiscount function, kill discount variable, and make the getDiscount function into something like:
public double getDiscount() {
if (...) return ...;
if (....) return ...;
...
}
...unless you want to have this as the default discount, and still allow modification, in which case keep discount as a property, and move this whole logic into the constructor, having conditional setDiscount() calls.
Your getDiscount function would ideally do the calculation and return the appropriate discount for the current object. For example:
public double getDiscount()
{
if (getAge() < 25 && getGradepoint() > 7)
{
return .25;
}
else if // other logic...
}
Although not the simplest solution, I would abstract the discount calculation to a separate interface and class as well as having an override discount value in the customer object.
E.g.
public interface DiscountManager<T>
{
public double getDiscount(T discountObject);
}
public abstract class AbstractCustomerDiscountManager extends DiscountManager<Customer>
{
public double getDiscount(Customer customer)
{
if (customer.hasCustomDiscount()) { return customer.getDiscount(); }
else { return calculateDiscount(customer); }
}
public abstract double calculateDiscount(Customer customer);
}
public class DefaultDiscountManager extends AbstractCustomerDiscountManager
{
public double calculateDiscount(Customer customer)
{
double discount = 0;
if ((customer.getAge() != null) && (customer.getAge() < 25)) { discount += 25; }
...
return discount;
}
}
Probably over time different rules evolve. At the spot where the discounting takes place, in the order, the discount and and a reference to the rule applied should be stored together.
This kind of business logic could have its own class. A generic solution would even be to store the rule as scriptable code (BeanShell = Java, or JavaScript) and use java's scripting API. So that this kind of business logic resides more with the business managers, and the rules can be presented and edited.
Made correction to previous errors, this error now comes up.
C:\Documents and Settings\AdminUser\My Documents\InventoryPart3.java:93: invalid method declaration; return type required
public Television(int ItemNumber, String ProductName, int UnitsStock, double UnitPrice, String mfgName) {
^
1 error
Tool completed with exit code 1
class Television { //class name and attributes
private String ItemNumber; //item # of product
private String ProductName; //product name
private double UnitsStock; //# of units in stock
private double UnitPrice; //Price per unit
private String SerialNumber; //serial number of product
private double InventoryValue; //The dollar value of the inventory in stock
//constructor
public Television (String item, String product, double units, double price, String serial) {
ItemNumber = item;
ProductName = product;
UnitsStock = units;
UnitPrice = price;
SerialNumber = serial;
} //end constructor
///getter and setter methods for Television
public void setItemNumber (String item) { //setter for item number
this.ItemNumber = item;
} //end setter item number
public String getItemNumber() { //getter for item number
return ItemNumber;
} //end getter item number
public void setProductName (String product) { //setter for product name
this.ProductName = product;
} //end setter product name
public String getProductName() { //getter for product name
return ProductName;
} //end getter product name
public void setUnitsStock (double units) { //setter for units in stock
this.UnitsStock = units;
} //end setter units in stock
public double getUnitsStock() { //getter for units in stock
return UnitsStock;
} //end getter units in stock
public void setUnitPrice (double price) { //setter for unit price
this.UnitPrice = price;
} //end setter unit price
public double getUnitPrice() { //getter for unit price
return UnitPrice;
} //end getter for unit price
public void setSerialNumber (String serial) { //setter for serial number
this.SerialNumber = serial;
}//end setter for serial number
public String getSerialNumber() { //getter for serial number
return SerialNumber;
}//end getter for serial number
//calculate individual product inventory value
public double getInventoryValue(){
return UnitsStock * UnitPrice;
}//end calculate individual product inventory value
//calculate total inventory value
public double calculateInventory(){
return UnitPrice * UnitsStock;
}//end calculate total inventory value
///end getter and setter methods for Laptop
} //end class Television
class Manufacturer extends Television {
private String manufacturerName;
//constructor
public Television(int ItemNumber, String ProductName, int UnitsStock, double UnitPrice, String mfgName) {
super(ItemNumber, ProductName, UnitsStock, UnitPrice, ManufacturerName);
this.manufacturerName = mfgName;
}//end constructor
//getter and setter for class Manufacturer
public void setmanufacturerName(String mfgName) {
this.manufacturerName = mfgName;
}
public String getmanufacturerName() {
return manufacturerName;
}
//end getter and setter for class Manufacturer
//calculate total inventory value
public double calculateInventory(){
return ((UnitPrice * UnitsStock));
}//end calculate total inventory value
//calculate restocking fee method
public double getcalculateRestockFee() {
return getInventoryValue();
}//end calculate restocking fee method
}//end class Manufacturer
So there are two errors. The first one occurs on line 93 of the file InventoryPart3.java. On that line you will find the code:
super(ItemNumber, ProductName, UnitsStock, UnitPrice);
Which is actually a call to the constructor of the television class. However, the constructor of the television class accepts 5 parameters: String, String, double, double, String but you are passing only 4.
The second error occurs on line 120 of the same file. In this case you seem to be calling a method but you left out the parenthesis. That is, the code should be:
getInventoryValue()
The constructor of television which you are trying to call from the manufacturer class using super does not exist in television class. Please see the parameters in the super() call and the actual parameters in the Television constructor. The getInventoryValue function is not there in the Television class.Hence it is throwing an error for the super.getInventory call
The first error is related to your Television constructor. Your Television constructor takes 5 arguments of type String, String, double, double, String. In your Manufacturer class, you're calling the superclass constructor with an int, string, int, and double. In a subclass, your super() constructor call must match one of the constructors in the base class (Television).
For the second error, your call to getInventoryValue has a syntax error. To call a method in Java, you have to put parentheses after the name of the method:
return super.getInventoryValue() * 0.10;
Note that in this case, the super. part is optional. The getInventoryMethod() exists in the super class, and is not private, so you can call it from the subclass like this:
return getInventoryValue() * 0.10;