Logout

4.1 "Thinking Ahead" Pond OOP Program

 

MainPondDatabase.java
/Users/johnr/Desktop/Pond Database/src/pond/database/MainPondDatabase.java
  1 /*
  2  * To change this license header, choose License Headers in Project Properties.
  3  * To change this template file, choose Tools | Templates
  4  * and open the template in the editor.
  5  */
  6 package pond.database;
  7 
  8 import java.util.Scanner;
  9 
 10 /**
 11  *
 12  * @author johnrayworth
 13  */
 14 public class MainPondDatabase {
 15 
 16     /**
 17      * @param args the command line arguments
 18      */
 19     static Carp[] carpArray = new Carp[500];
 20     static Catfish[] catfishArray = new Catfish[500];
 21 
 22     public static void main(String[] args) {
 23         //Inputs - Carp and Catfish data...
 24         //Processing - Seeing which carp or catfish is the heaviest
 25         //Outputs - The id number of the largest carp or catfish and its weight
 26 
 27         //Proposed Methods:
 28         //Input carps and add to carp array
 29         //Input catfish and add to catfish array
 30         //All the determination of heaviest part (see below for method "stubs")
 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      Pre-conditions
 67      A carp array and a catfish array BOTH THE SAME SIZE!!@#$!@$!@$!@#$
 68      And all of the info for each put in.
 69     
 70      Post-conditions
 71      This method will loop through both arrays and find the 
 72      heaviest carp or catfish   
 73      It will output wether the heaviest is a carp or catfish, along
 74      with its id number and its weight.
 75     
 76      */
 77     public static void calculateHeaviest() {
 78 
 79             double heaviestFishWeight = 0;
 80             int heaviestCarpOrCatfishNumber = 0;
 81             boolean heaviestIsCarp = true;
 82 
 83             //A for loop for looping through the carpArray (i.e. repeating 500 times.)
 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  * To change this license header, choose License Headers in Project Properties.
 3  * To change this template file, choose Tools | Templates
 4  * and open the template in the editor.
 5  */
 6 
 7 package pond.database;
 8 
 9 /**
10  *
11  * @author johnrayworth
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     //Main Constructor
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  * To change this license header, choose License Headers in Project Properties.
 3  * To change this template file, choose Tools | Templates
 4  * and open the template in the editor.
 5  */
 6 
 7 package pond.database;
 8 
 9 /**
10  *
11  * @author johnrayworth
12  */
13 public class Carp {
14     
15     private int id = -999;//Months 
16     private double weight = -999.999;
17     
18     public Carp(){
19         
20     }
21     //Constuctor Method
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