File size: 3,113 Bytes
05fe4a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
The program defines the class Product.

Write the method
public static Product calculate(Product product1, Product product2)

which takes two products as parameters.

The method creates and returns a new product in which the names of the products received as parameters 
are combined with the 'and' word 
and the quantities are added together.



Example of calling the method:
public static void main(String[] args) {
    Product carrot = new Product("Carrot", 5);
    Product potato = new Product("Potato", 7);
    
    Product combined = calculate(carrot, potato);
    System.out.println(combined.getName());
    System.out.println(combined.getAmount());
}



Program outputs:
Carrot and Potato
12









============================


import java.util.Random;

public class Test{
    public static void main(String[] args){
        final Random r = new Random();
        
        
         String[] products = {"Blueberry", "Strawberry", "Raspberry", "Grape",
                             "Lingonberry", "Cranberry", "Currant"};
        
        for (int test=0; test<3; test++) {
            String t1 = products[r.nextInt(products.length)];
            String t2 = t1;
            while (t1.equals(t2)) {
                t2 = products[r.nextInt(products.length)];
            }
            
            Product product1 = new Product(t1, r.nextInt(20) + 1);
            Product product2 = new Product(t2, r.nextInt(20) + 1);
            
            System.out.println("Product 1: " + product1);
            System.out.println("Product 2: " + product2);
            
            Product product3 = calculate(product1, product2);
            System.out.println("Names of the berries: " + product3.getName());
            System.out.println("Combined quantity: " + product3.getAmount());
            System.out.println("");
            
        }
    }


    //ADD HERE
    // inside 'Test' class, outside 'main' method
    public static Product calculate(Product product1, Product product2) {
        Product combined = new Product(product1.getName() + " and " + product2.getName(),
                                       product1.getAmount() + product2.getAmount());
        return combined;
    }


}




class Product {
    private String name;
    private int amount;
    
    public Product(String name, int amount) {
        this.name = name;
        this.amount = amount;
    }

    public String getName() {
        return name;
    }

    public int getAmount() {
        return amount;
    }

    @Override
    public String toString() {
        return "Product (name=" + name + ", amount=" + amount + ")";
    }  
}





Product 1: Product (name=Raspberry, amount=7)
Product 2: Product (name=Grape, amount=19)
Names of the berries: Raspberry and Grape
Combined quantity: 26

Product 1: Product (name=Blueberry, amount=20)
Product 2: Product (name=Grape, amount=5)
Names of the berries: Blueberry and Grape
Combined quantity: 25

Product 1: Product (name=Blueberry, amount=20)
Product 2: Product (name=Strawberry, amount=12)
Names of the berries: Blueberry and Strawberry
Combined quantity: 32