KaiquanMah commited on
Commit
05fe4a9
·
verified ·
1 Parent(s): d789391

ClassName combined = new ClassName(param1, param2)

Browse files
Week 4: Writing classes/13B. Calculate products together ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ The program defines the class Product.
2
+
3
+ Write the method
4
+ public static Product calculate(Product product1, Product product2)
5
+
6
+ which takes two products as parameters.
7
+
8
+ The method creates and returns a new product in which the names of the products received as parameters
9
+ are combined with the 'and' word
10
+ and the quantities are added together.
11
+
12
+
13
+
14
+ Example of calling the method:
15
+ public static void main(String[] args) {
16
+ Product carrot = new Product("Carrot", 5);
17
+ Product potato = new Product("Potato", 7);
18
+
19
+ Product combined = calculate(carrot, potato);
20
+ System.out.println(combined.getName());
21
+ System.out.println(combined.getAmount());
22
+ }
23
+
24
+
25
+
26
+ Program outputs:
27
+ Carrot and Potato
28
+ 12
29
+
30
+
31
+
32
+
33
+
34
+
35
+
36
+
37
+
38
+ ============================
39
+
40
+
41
+ import java.util.Random;
42
+
43
+ public class Test{
44
+ public static void main(String[] args){
45
+ final Random r = new Random();
46
+
47
+
48
+ String[] products = {"Blueberry", "Strawberry", "Raspberry", "Grape",
49
+ "Lingonberry", "Cranberry", "Currant"};
50
+
51
+ for (int test=0; test<3; test++) {
52
+ String t1 = products[r.nextInt(products.length)];
53
+ String t2 = t1;
54
+ while (t1.equals(t2)) {
55
+ t2 = products[r.nextInt(products.length)];
56
+ }
57
+
58
+ Product product1 = new Product(t1, r.nextInt(20) + 1);
59
+ Product product2 = new Product(t2, r.nextInt(20) + 1);
60
+
61
+ System.out.println("Product 1: " + product1);
62
+ System.out.println("Product 2: " + product2);
63
+
64
+ Product product3 = calculate(product1, product2);
65
+ System.out.println("Names of the berries: " + product3.getName());
66
+ System.out.println("Combined quantity: " + product3.getAmount());
67
+ System.out.println("");
68
+
69
+ }
70
+ }
71
+
72
+
73
+ //ADD HERE
74
+ // inside 'Test' class, outside 'main' method
75
+ public static Product calculate(Product product1, Product product2) {
76
+ Product combined = new Product(product1.getName() + " and " + product2.getName(),
77
+ product1.getAmount() + product2.getAmount());
78
+ return combined;
79
+ }
80
+
81
+
82
+ }
83
+
84
+
85
+
86
+
87
+ class Product {
88
+ private String name;
89
+ private int amount;
90
+
91
+ public Product(String name, int amount) {
92
+ this.name = name;
93
+ this.amount = amount;
94
+ }
95
+
96
+ public String getName() {
97
+ return name;
98
+ }
99
+
100
+ public int getAmount() {
101
+ return amount;
102
+ }
103
+
104
+ @Override
105
+ public String toString() {
106
+ return "Product (name=" + name + ", amount=" + amount + ")";
107
+ }
108
+ }
109
+
110
+
111
+
112
+
113
+
114
+ Product 1: Product (name=Raspberry, amount=7)
115
+ Product 2: Product (name=Grape, amount=19)
116
+ Names of the berries: Raspberry and Grape
117
+ Combined quantity: 26
118
+
119
+ Product 1: Product (name=Blueberry, amount=20)
120
+ Product 2: Product (name=Grape, amount=5)
121
+ Names of the berries: Blueberry and Grape
122
+ Combined quantity: 25
123
+
124
+ Product 1: Product (name=Blueberry, amount=20)
125
+ Product 2: Product (name=Strawberry, amount=12)
126
+ Names of the berries: Blueberry and Strawberry
127
+ Combined quantity: 32
128
+
129
+