Hello ,

I face problems dealing with the composition ,, I have this exercice to solve , I know how to write a class with constructors and setters & getters but still have a problem with linking the objects presents in different classes


You have to implement the CDPlayer class that has the following data fileds:
-mp3: a boolean value to set to true if the CD player can read mp3 songs
-bluetooth:a boolean value set to true if the CD player has Bluetooth
-manufacturer:String
-power:integer that represents the output peak power of the CD player
Add the following methods to the class:
- A default no-arg constructor.
- A constructor that accepts the mp3,bluetooth,manufacturer and power as parameters.
-Getters and Setters methods for each of the data fields.
- A display method that prints the CDPlayer information.


I write :

package testcar;

public class CDPlayer {

//Defining variables
boolean mp3;
boolean bluetooth;
String manufacturer;
int power;

//Default Constructor
CDPlayer(){
}

//Construct a CDPlayer object with specified data fields
CDPlayer(boolean Newmp3,boolean Newbluetooth,String Newmanufacturer,int Newpower){
mp3=Newmp3;
bluetooth=Newbluetooth;
manufacturer=Newmanufacturer;
power=Newpower;
}

public boolean Getmp3(){
return mp3;
}

public void Setmp3(boolean Newmp3){
mp3=Newmp3;
}

public boolean GetBluetooth(){
return bluetooth;
}

public void SetBluetooth(boolean Newbluetooth){
bluetooth=Newbluetooth;
}

public String GetManufacturer(){
return manufacturer;
}

public void SetManufacturer(String Newmanufacturer){
manufacturer=Newmanufacturer;
}

public int GetPower (){
return power;
}

public void SetPower(int Newpower){
power=Newpower;
}

public String toString(){
return mp3+" "+bluetooth+" "+manufacturer+" "+power;
}

}//end class


Then the second part of the assignment is :

Modify the Car class of assignment 1 to add the following data fields:
-CarCDPlayer of type CDPlayer that represents the features of the car CD player.
Add a get method to return this new data field and a set method to change its value. Update the display method to output this attribute as well.


This is the Car class of assignment 1 with the statements added colored in red :


package testcar;

//Define the car class with two constructors
public class Car {

String brand ;
String modelNumber ;
int year ;
double price ;
boolean used ;
CDPlayer carCDPlayer;
//Construct a car using a default constructor
public Car(){
}

//Construct a car with specified data fields
public Car(String newBrand ,String newmodelNumber, int newyear , double newprice, boolean newused,CDPlayer carCDPlayer){//Should I write the datafields of carCDPlayer here? and how?
brand = newBrand;
modelNumber = newmodelNumber;
year = newyear;
price = newprice;
used = newused;


}

public String getBrand() {
return brand;
}

public void SetBrand(String newBrand){
brand = newBrand;
}

public String getmodelNumber() {
return modelNumber;
}

public void setmodelNumber(String newmodelNumber) {
modelNumber = newmodelNumber;
}

public int getYear() {
return year;
}

public void setYear(int newyear){
year = newyear;
}

public double getPrice() {
return price;
}

public void setPrice(double newprice){
price = newprice;
}

public boolean isused(){
return true;
}

public void setUsed(boolean newused) {
used = newused;
}

I want to add a method to get the features but I don't know the type of this method ,, each data field has a different data type so how can I put them in one method?


public String toString(){
return carCDPlayer+brand+modelNumber+year+price+used;
}
}//end class car