7 Replies - 2291 Views - Last Post: 27 December 2011 - 01:20 PM Rate Topic: -----

#1 queenz68  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-May 11

car class and inheritance

Posted 25 December 2011 - 02:46 PM

I have an assignment I have to create a car with an id number and original dealer price. As an extension to this I have to create the same car but with the sales price, customer name and profit. Then I need a driver that will show these things and determine the profit after the car is sold. For now I'm stuck on the basic part. I have the car and was testing the driver just to see if the car part worked, but keep getting the follow error messages. "2 errors found:

Error: no suitable constructor found for Car(int)
constructor car.Car.Car() is not applicable
(actual and formal argument lists differ in length)
constructor car.Car.Car(int,float) is not applicable
(actual and formal argument lists differ in length)
I'm posting all three projects in case there's something wrong with one of the two classes. The first is the Car.
package car;
import java.text.*;
import java.io.*;
public class Car {

int idNumber;
float dealerCost;
//Direct constructor
public Car(int newIdNumber, float newDealerCost) 
{
idNumber = newIdNumber;
dealerCost = newDealerCost;
}
//Default constructor
public Car()
{
  idNumber = 0000000;
  dealerCost = 0;
}
//Observers

public int getIdNumber() 
{ 
  return idNumber; 
}
public float getDealerCost() 
{ 
  return dealerCost; 
}
public String toString()
  {
    return 
      "Vehicle ID Number: " + idNumber + "\n" 
      + "Dealer cost: $" + dealerCost + "\n"; 
  }
}



The second is the sold car:
package car;
public class SoldCar extends Car
{
  //New fields
  float soldPrice;
  String customerName;
  public SoldCar()
  {
    super();            //Superclass sets new fields
    soldPrice = 0;
    customerName = "";
  }
  public SoldCar(float newDealerCost, int newIdNumber, String customer, float price)
  {
    super(newIdNumber, newDealerCost);   //Superclass constructor sets inherited fields
    soldPrice = price;
    customerName = customer;
  }
  //Observer method to return new fields
  public String getCustomerName() 
  { 
  return customerName; 
  }
  public float getSoldPrice() 
  { 
    return soldPrice; 
  }
    public String toString()
  {
    String str = super.toString();
    return (str + customerName + soldPrice); //Appends new field
  }
}



And finally the driver, but I'm only testing it on the car at this point:

// Class TestBook is a driver to test class Book
package car;
import java.io.*;
import java.util.Scanner;
public class CarSales
{
  public static void main(String[] args)
  {
    Car idNumber;
    Car dealerCost;
    Scanner inData = new Scanner(System.in);
    
    idNumber = new Car(1245632);
    dealerCost = new Car(14000);
    
    
    System.out.println("Vehicle ID " + idNumber + "was purchased for " + dealerCost + " on October 11, 2001.");
    
  }
}



Is This A Good Question/Topic? 0
  • +

Replies To: car class and inheritance

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: car class and inheritance

Posted 25 December 2011 - 02:50 PM

In your CarSales class, you have two lines, 13 and 14, that try to create an instance of Car using a constructor with one parameter. If you check your Car class, there is no corresponding constructor.

Fix that and let us know how you're doing.
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: car class and inheritance

Posted 25 December 2011 - 02:51 PM

Your Car class has 2 contructors

public Car(int newIdNumber, float newDealerCost)
public Car()

but you try to buid a car that way

idNumber = new Car(1245632);

you simply don't have a constructor of Car that expects a single int as parameter
Was This Post Helpful? 1
  • +
  • -

#4 queenz68  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-May 11

Re: car class and inheritance

Posted 25 December 2011 - 04:37 PM

Ok, so I rearranged some things, but can't figure out why It wont print correctly. Here's what I did with the Car class.

package car;
import java.text.*;
import java.io.*;
public class Car {

int idNumber;
float dealerCost;
//Direct constructor
public Car(int newIdNumber)  
{
idNumber = newIdNumber;

}
public Car(float newDealerCost)
{
  dealerCost = newDealerCost;
}

//Observers

public int getIdNumber() 
{ 
  return idNumber; 
}
public float getDealerCost() 
{ 
  return dealerCost; 
}
public String toString()
  {
    return "Vehicle No: " + idNumber + "\n" 
      + " Dealer Cost:" + dealerCost + "\n";
   }
}


Then here's what I did with the driver:

// Class CarSales is a driver to test class Car
package car;
import java.io.*;
import java.util.Scanner;
public class CarSales
{
  public static void main(String[] args)
  {
    Car idNumber;
    Car dealerCost;
    Scanner inData = new Scanner(System.in);
    
    idNumber = new Car(1245632);
    dealerCost = new Car(14000);
       
    System.out.println(idNumber); 
    System.out.println(dealerCost);
  }
}
And here's what's printing
Vehicle No: 1245632
 Dealer Cost:0.0

Vehicle No: 14000
 Dealer Cost:0.0


Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: car class and inheritance

Posted 25 December 2011 - 04:46 PM

Everything's working as you've programmed it, but that's not what you wanted to hear.

You've created two instances of Car called idNumber and dealerCost using the constructor Car( int newIdNumber ). So idNumber and dealerCost are two cars, one with idNumber = 1245632 and the other with idNumber = 14000. You don't set the dealer cost for either car, so they are both set to 0.0.

Do you see how to fix it?
Was This Post Helpful? 1
  • +
  • -

#6 queenz68  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-May 11

Re: car class and inheritance

Posted 25 December 2011 - 05:12 PM

Not really. I mean I understand what you're saying, though, it's just the fixing it part that I'm stuck on.
Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: car class and inheritance

Posted 25 December 2011 - 05:24 PM

I don't think you want your car instances named idNumber and dealerCost. Something like usedFord or blueChevy would make more sense. Car color, make, and model might also be class (oops) instance fields, so there might be another naming convention for your car instances that fits your final design better.

Then, if your Car class had setters, or mutators for idNumber and dealerCost like,

public void setIdNumber( int idNumber )
{
   this.idNumber = idNumber;
}


you could then use them to set your idNumber and dealerCost values:
blueCar.setIdNumber( 123456 );

This post has been edited by GregBrannon: 25 December 2011 - 05:39 PM

Was This Post Helpful? 2
  • +
  • -

#8 Amatore  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 47
  • Joined: 27-December 11

Re: car class and inheritance

Posted 27 December 2011 - 01:20 PM

Using what GregBrannon says here:

View PostGregBrannon, on 25 December 2011 - 04:46 PM, said:

You've created two instances of Car called idNumber and dealerCost using the constructor Car( int newIdNumber ). So idNumber and dealerCost are two cars, one with idNumber = 1245632 and the other with idNumber = 14000. You don't set the dealer cost for either car, so they are both set to 0.0.


And what he says here :

GregBrannon said:

Then, if your Car class had setters, or mutators for idNumber and dealerCost like,


another way of fixing this is to do something like this

Car car1;
Car car2;

car1 = new Car(12345632);
car2 = new Car(Some other integer);

car1.setDealerPrice(some float value);
car2.set...




as you see you have 2 different cars, with 2 different id numbers, and you set each cars' dealer price 1 at a time.

where your method named setDealerPrice would look like :

public void setDealerPrice(float amount){
  this.dealerCost = amount;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1