подходящий конструктор не найден (Java) - PullRequest
0 голосов
/ 05 февраля 2020

Попытка создать объект с использованием агрегации, но ошибка не показывает подходящего конструктора при использовании массива для создания объектов. Я пытаюсь понять, почему массив не идентифицирует информацию, реализованную как я, когда я не предоставляю информацию об объекте, он может создать объект с информацией по умолчанию из конструктора обоих классов 'no-arg

public class ComputerTest {
public static void main(String[] args){
    Computer c[] = new Computer[5];

    c[0] = new Computer("OEM", "Basic");
    c[1] = new Computer("Dell", "Optiplex", "discrete", "PCIEx16", 375, "8-pin",16);
    c[2] = new Computer("Lenova","IdeaCentre", "integrated", "N/A", 120, "N/A", 4);
    c[3] = new Computer("HP", "Omen Obelisk", "discrete", "PCIEx8", 150,"6-pin", 8);
    c[4] = new Computer("Lenovo" , "IdeaCentre", "integrated", "N/A", -110, "N/A", 2);
    printComputers(c);


}
static void printComputers(Computer c[]){
    for (int i=0; i<c.length; i++){
        System.out.println(c[i].getInfo());
    }

}

Использование информации из этого класса

public class Computer {
private String brand;
private String model;
private VideoCard VideoCard;

public Computer(){
    this.setBrand("OEM");
    this.setModel("Basic");
    this.VideoCard = new VideoCard();

}

public Computer(String brand, String model, VideoCard VideoCard){
    this.setBrand(brand);
    this.setModel(model);
    this.setVideoCard(VideoCard);
}

public void setBrand(String brand){
    this.brand = brand;
}

public void setModel(String model){
    this.model = model;
}

public void setVideoCard(VideoCard VideoCard){
    this.VideoCard = VideoCard;
}

public String getBrand(){
    return brand;
}

public String getModel(){
    return model;
}

public VideoCard getVideoCard(){
    return this.VideoCard;
}

public String getInfo(){
    String info = String.format("%s " + "%s\n" + "Video Card Info:\n" + "%s\n", getBrand(), getModel(), VideoCard.getInfo());
    return info;
}

, которая агрегируется из этого класса

public class VideoCard {
private String type;
private String connection;
private int power;
private String extPower;
private int memory;

public VideoCard(){
    this.setType("integrated");
    this.setConnection("N/A");
    this.setPower(100);
    this.setExtPower("N/A");
    this.setMemory(1);
}

public VideoCard(String type, String connection, int power, String extPower, int memory){
    this.setType(type);
    this.setConnection(connection);
    this.setPower(power);
    this.setExtPower(extPower);
    this.setMemory(memory);

}

public void setType(String type){
    this.type = type;
}

public void setConnection(String connection){
    this.connection = connection;
}

public void setPower(int power){
    if(power>= 1 && power<=300){
    this.power = power;
    } if(power<1) {
        this.power = 75;
    } if(power>300){
        this.power = 200;
    }
}

public void setExtPower(String extPower){
    this.extPower = extPower;
}

public void setMemory(int memory){
    if(memory>=1 && memory<=12){
    this.memory = memory;
    } if(memory<1){
        this.memory = 1;
    } if(memory>12){
        this.memory = 12;
    }
}

public String getType(){
    setType(this.type);
    return type;
}

public String getConnection(){
    setConnection(this.connection);
    return connection;
}

public int getPower(){
    setPower(this.power);
    return power;
}

public String getExtPower(){
    setExtPower(this.extPower);
    return extPower;
}

public int getMemory(){
    setMemory(this.memory);
    return memory;
}

public String getInfo(){
    String info = String.format("Type:\t\t%s\n " + "Connection:\t%s\n " + "Power:\t\t%sW \n " + "Ext Power:\t%s\n " + "Memory:\t%dGB \n ",
            getType(), getConnection(), getPower(), getExtPower(), getMemory());
    return info;
}
...