-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjectLoop.java
More file actions
42 lines (36 loc) · 1.1 KB
/
Copy pathobjectLoop.java
File metadata and controls
42 lines (36 loc) · 1.1 KB
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
import java.util.Scanner;
public class objectLoop {
public static void main(String[] args) {
Plant[] plants = new Plant[7];
int sum = 0;
int counter = 0;
do {
System.out.println("Enter the name of the plant: " + (counter + 1));
String name = new Scanner(System.in).nextLine();
System.out.println("Enter the number of how many " + name + ":");
int number = new Scanner(System.in).nextInt();
plants[counter] = new Plant(name, number);
sum += number;
counter++;
} while (counter < 7);
System.out.println("There are: " + sum + " new plants");
for(Plant plant : plants) {
System.out.println( plant.getValue() + ":" );
System.out.println(plant.getName());
}
}
}
class Plant {
private String name;
private int value;
public Plant(String name, int value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public int getValue() {
return value;
}
}