association

 avatar
unknown
plain_text
2 years ago
2.0 kB
5
Indexable
**Mouse.java

public class Mouse {
    private String productName, serialNum, brand;

    public Mouse(String p, String s, String b){
        productName = p;
        serialNum = s;
        brand = b;
    }

    public String getProductName(){
        return productName;
    }

    public String getSerial(){
        return serialNum;
    }

    public String getBrand(){
        return brand;
    }
    
}

**PC.java
public class PC {
    private RAM r;
    private Mouse m;

    public PC(RAM r, Mouse m){
        this.r = r;
        this.m = m; //nama sama guna this
    }

    public void print(){
        System.out.println("Information about this PC's RAM: ");
        System.out.println("Brand: " + r.getBrand());
        System.out.println("Product Name: " + r.getProductName());
        System.out.println("Serial Number: " + r.getSerial());
        System.out.println("Speed: " + r.getSpeed());

        System.out.println("\n\nInformation about this PC's Mouse: ");
        System.out.println("Brand: " + m.getBrand());
        System.out.println("Product Name: " + m.getProductName());
        System.out.println("Serial Number: " + m.getSerial());
    }
}

**RAM.java

public class RAM {
    private String productName, serialNum, brand, speed;

    public RAM(String p, String s, String b, String sp){
        productName = p;
        serialNum = s;
        brand = b;
        speed = sp;
    }

    public String getProductName(){
        return productName;
    }

    public String getSerial(){
        return serialNum;
    }

    public String getBrand(){
        return brand;
    }

    public String getSpeed(){
        return speed;
    }
    
}


**TestPC.java

public class TestPc {
    public static void main(String[] args) throws Exception {
        RAM r1 = new RAM("Corsair ABC", "A12334", "Logitech", "2G");
        Mouse m1 = new Mouse("Logitech MX", "Y727", "Logitech");
        PC p1 = new PC(r1, m1);

        p1.print();
    }
}
Editor is loading...
Leave a Comment