4.1 "Thinking Ahead" Pond OOP Program
MainPondDatabase.java
/Users/johnr/Desktop/Pond Database/src/pond/database/MainPondDatabase.java |
1
2
3
4
5
6 package pond.database;
7
8 import java.util.Scanner;
9
10
11
12 @author
13
14 public class MainPondDatabase {
15
16
17 @param args
18
19 static Carp[] carpArray = new Carp[500];
20 static Catfish[] catfishArray = new Catfish[500];
21
22 public static void main(String[] args) {
23
24
25
26
27
28
29
30
31 inputCarpStuff();
32 inputCatfishStuff();
33 calculateHeaviest();
34
35 }
36
37 static Scanner snr = new Scanner(System.in);
38 static int counterForCarp = 0;
39 static int counterForCatfish = 0;
40
41 public static void inputCarpStuff() {
42 for(int i = 0; i < carpArray.length; i++){
43 System.out.println("What is the id of this carp?");
44 int id = snr.nextInt();
45 System.out.println("What is the weight of this carp?");
46 double weight = snr.nextDouble();
47 carpArray[counterForCarp] = new Carp(id, weight);
48 counterForCarp++;
49 }
50
51 }
52
53 public static void inputCatfishStuff(){
54 for(int i = 0; i < catfishArray.length; i++){
55 System.out.println("What is the id of this catfish?");
56 int id = snr.nextInt();
57 System.out.println("What is the weight of this catfish?");
58 double weight = snr.nextDouble();
59 catfishArray[counterForCatfish] = new Catfish(id, weight);
60 counterForCatfish++;
61 }
62 }
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public static void calculateHeaviest() {
78
79 double heaviestFishWeight = 0;
80 int heaviestCarpOrCatfishNumber = 0;
81 boolean heaviestIsCarp = true;
82
83
84 for (int i = 0; i < carpArray.length; i++) {
85 if (carpArray[i].getWeight() > heaviestFishWeight) {
86 heaviestFishWeight = carpArray[i].getWeight();
87 heaviestCarpOrCatfishNumber = i;
88 heaviestIsCarp = true;
89 }
90 if (catfishArray[i].getWeight() > heaviestFishWeight) {
91 heaviestFishWeight = catfishArray[i].getWeight();
92 heaviestCarpOrCatfishNumber = i;
93 heaviestIsCarp = false;
94 }
95 }
96 if (heaviestIsCarp) {
97 System.out.println("The heaviest carp or catfish is a carp with id # " + carpArray[heaviestCarpOrCatfishNumber].getID());
98 } else {
99 System.out.println("The heaviest carp or catfish is a catfish with id # " + catfishArray[heaviestCarpOrCatfishNumber].getID());
100 }
101
102 }
103 }
104
/Users/johnr/Desktop/Pond Database/src/pond/database/Catfish.java |
1
2
3
4
5
6
7 package pond.database;
8
9
10
11 @author
12
13 public class Catfish {
14 private int id = -999;
15 private double weight = -999.999;
16
17
18 public Catfish(){
19
20 }
21
22
23 public Catfish(int id, double weight){
24 this.id = id;
25 this.weight = weight;
26 }
27
28
29 public int getID(){
30 return id;
31 }
32
33 public double getWeight(){
34 return weight;
35 }
36
37 }
38
1
2
3
4
5
6
7 package pond.database;
8
9
10
11 @author
12
13 public class Carp {
14
15 private int id = -999;
16 private double weight = -999.999;
17
18 public Carp(){
19
20 }
21
22 public Carp(int id, double weight){
23 this.id = id;
24 this.weight = weight;
25 }
26
27 public int getID(){
28 return id;
29 }
30
31 public double getWeight(){
32 return weight;
33 }
34
35 }
36