Translate To Preferred Language

Search Obioku's Thoughts

Please Read Today's Featured Post

Things I Would Like To Happen Soon

For all those who believe in speaking ideas into existence and prayer in the universe will return to a person.  I simply want to write a lis...

Template for Bear-Fish Simulation in Java (some bugs may occur)

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bear_fish;

import java.awt.*;
import static java.awt.Frame.MAXIMIZED_BOTH;
import java.io.*;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;

/**
 *
 * @author obobotette0
 */
public class Bear_Fish {

    /**
     * @param args the command line arguments
     */
    //class for object animal
    public static class animal {

        //constructor to determine type
        animal(){
            form = "blank";
            reproduce = 0;
            try{
            pic = ImageIO.read(new File("pics\\null.jpg"));
            } catch (IOException e) {
                                System.out.println("error");
                            }
        }
       
        //variable for array index and type
        String form = new String();
        int arrayloc;
        int reproduce;
        boolean youth = false;
        public Image pic = null;

        //method to return type
        String typeCheck() {
            return form;
        }

    }
   
    public static class bear extends animal{
   
        bear(){
        form = "bear";
        reproduce = 1;
        try{
        pic = ImageIO.read(new File("pics\\adultbear.jpg"));
        } catch (IOException e) {
                                System.out.println("error");
                            }
        }

    }

    public static class fish extends animal{
        fish(){       
        form = "fish";
        reproduce = 2;
        try{
        pic = ImageIO.read(new File("pics\\adultfish.jpg"));
        } catch (IOException e) {
                                System.out.println("error");
                            }
        }
    }
    public static void main(String[] args) throws IOException, FileNotFoundException {
        // TODO code application logic here

        //Variables for number of array elements and first round of populating array
        int contents = 100;
        int first_loop = (int) (contents * 0.3);

        //create variables
        animal[] array = new animal[contents];
        int index;
        int count1 = 0;
        int count2 = 0;
        int bear = 0;
        int fish = 0;
        Random num = new Random();
        Random type = new Random();

        //GUI variables
        JLabel[] animals1 = new JLabel[contents];
        JLabel[] animals2 = new JLabel[contents];
        JPanel arraypic1 = new JPanel();
        JPanel arraypic2 = new JPanel();
        JScrollPane use1 = new JScrollPane(arraypic1);
        JScrollPane use2 = new JScrollPane(arraypic2);
        JFrame fullpic1 = new JFrame();
        JFrame fullpic2 = new JFrame();

        //File variables
        String firstArray = new String("FirstArray.doc");
        String secondArray = new String("SecondArray.doc");
        FileWriter writer = new FileWriter(firstArray);
        PrintWriter output = new PrintWriter(writer);

        //create empty array to avoid null pointer error
        for (int i = 0; i < array.length; i++) {
            array[i] = new animal();
        }

        //loop to create array as suggested
        for (int i = 0; i <= first_loop; i++) {
            index = num.nextInt(array.length);
            int m = type.nextInt(3);
            if(m==1){
                array[index] = new bear();
            }else if (m==2){
                array[index] = new fish();
            }else{
               
            }
           
            array[index].arrayloc = index;
           
        }

        //Print each animal at its array location as a check
        System.out.println("First count");
        for (int i = 0; i < array.length; i++) {
            if (array[i].typeCheck() != "blank") {
                System.out.println(array[i].typeCheck() + " at " + array[i].arrayloc);
                count1++;
                output.append(array[i].typeCheck() + " at " + array[i].arrayloc);
                output.println();

                //Count each type
                if (array[i].typeCheck() == "bear") {
                    bear++;
                } else if (array[i].typeCheck() == "fish") {
                    fish++;
                }
               
            }
        }

        //Print count for each type
        System.out.println("There are " + fish + " fish and " + bear + " bears");
        output.append("There are " + fish + " fish and " + bear + " bears");
        output.println();
        output.flush();

        //Add first count to first panel
        for (int i = 0; i < array.length; i++) {
            if (array[i].pic != null) {
                animals1[i] = new JLabel(new ImageIcon(array[i].pic));
                animals1[i].setSize(10, 10);
                arraypic1.add(animals1[i]);
                arraypic1.setSize(10, 10);
            }
        }
        //loop to do movements
        for (int i = 0; i < array.length; i++) {
            index = num.nextInt(array.length);

            //only perform for defined types
            if (array[index].typeCheck() != "blank") {
                move(array[index], array);
            }
        }

        //reset types
        bear = 0;
        fish = 0;
        writer = new FileWriter(secondArray);
        output = new PrintWriter(writer);

        //Print each animal at its array location as a check
        System.out.println("\nSecond count");
        for (int i = 0; i < array.length; i++) {
            if (array[i].typeCheck() != "blank") {
                System.out.println(array[i].typeCheck() + " at " + array[i].arrayloc);
                count2++;
               output.append(array[i].typeCheck() + " at " + array[i].arrayloc);
                output.println();

            }
            //Count each type
            if (array[i].typeCheck() == "bear") {
                bear++;
            } else if (array[i].typeCheck() == "fish") {
                fish++;
            }
        }

        //Print count for each type
        System.out.println("There are now " + fish + " fish and " + bear + " bears");
        output.append("There are " + fish + " fish and " + bear + " bears");
        output.println();

        //Add second count to first panel
        for (int i = 0; i < array.length; i++) {
            if (array[i].pic != null) {
                animals2[i] = new JLabel(new ImageIcon(array[i].pic));
                animals2[i].setSize(10, 10);
                arraypic2.add(animals2[i]);
                arraypic2.setSize(10, 10);
            }
        }
        //Print final difference
        System.out.println("\nStarted with " + count1 + " animals and now there are " + count2 + " animals");
        output.append("\nStarted with " + count1 + " animals and now there are " + count2 + " animals");
        output.println();
        output.flush();
        //Show results
        arraypic1.setVisible(true);
        arraypic2.setVisible(true);
        fullpic1.add(use1);
        fullpic1.setTitle("First Array Results");
        fullpic2.add(use2);
        fullpic2.setTitle("Second Array Results");
        fullpic1.setExtendedState(MAXIMIZED_BOTH);
        fullpic1.setLocationByPlatform(true);
        fullpic1.setVisible(true);
        fullpic1.setDefaultCloseOperation(EXIT_ON_CLOSE);
        fullpic2.setExtendedState(MAXIMIZED_BOTH);
        fullpic2.setLocationByPlatform(true);
        fullpic2.setVisible(true);
        fullpic2.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    //move function to define interaction
    public static void move(animal next, animal[] array){
        //if at the end of array
        //move to the front
        if (next.arrayloc == array.length - 1) {
            //if types are the same
            if (next.typeCheck() == array[0].typeCheck()) {
                //search for first blank return to place new type
                for (int i = 0; i < array.length; i++) {
                    if (array[i].typeCheck() == "blank") {
                        if(next instanceof fish){
                        array[i] = new fish();
                        array[i].arrayloc = i;
                        array[i].youth = true;
                        try {
                                array[i].pic = ImageIO.read(new File("pics\\kidfish.jpg"));
                            } catch (IOException e) {
                                System.out.println("error");
                            }
                    }else{
                            array[i] = new bear();
                        array[i].arrayloc = i;
                        array[i].youth = true;
                        try {
                                array[i].pic = ImageIO.read(new File("pics\\cubbear.jpg"));
                            } catch (IOException e) {
                                System.out.println("error");
                            }
                        }
                       
                        break;
                    }
                }

            }//else if values ar not the same
            else if (next.typeCheck() != array[0].typeCheck()) {
                //bear replace both blank or fish
                if (next.typeCheck() == "bear" || next.typeCheck() == "fish" && array[0].typeCheck() == "blank") {
                    array[next.arrayloc] = new animal();
                    next.arrayloc = 0;
                    array[0] = next;

                }//if fish move to bear cell
                else if (next.typeCheck() == "fish" && array[0].typeCheck() == "bear") {
                    //delete fish object                   
                    array[next.arrayloc] = new animal();

                }//else error
                else if (next.typeCheck() == "blank") {
                    array[next.arrayloc] = new animal();
                    System.out.println("Mistake");
                }
            }

        }//in all other interaction other than moving from end of array
        else {
            //if type is same as one in next index
            if (next.typeCheck() == array[next.arrayloc + 1].typeCheck()) {
                //search for first blank or null return to place new type
                for (int i = next.arrayloc; i < array.length; i++) {
                    if (array[i].typeCheck() == "blank") {
                        if(next instanceof fish){
                        array[i] = new fish();
                        array[i].arrayloc = i;
                        array[i].youth = true;
                        try {
                                array[i].pic = ImageIO.read(new File("pics\\kidfish.jpg"));
                            } catch (IOException e) {
                                System.out.println("error");
                            }
                    }else{
                            array[i] = new bear();
                        array[i].arrayloc = i;
                        array[i].youth = true;
                        try {
                                array[i].pic = ImageIO.read(new File("pics\\cubbear.jpg"));
                            } catch (IOException e) {
                                System.out.println("error");
                            }
                        }
                        break;
                    }
                }

            }//else if values ar not the same
            else if (next.typeCheck() != array[next.arrayloc + 1].typeCheck()) {
                //bear replace both blank or fish
                if (next.typeCheck() == "bear" || next.typeCheck() == "fish" && array[next.arrayloc + 1].typeCheck() == "blank") {
                    array[next.arrayloc] = new animal();
                    next.arrayloc += 1;
                    array[next.arrayloc] = next;

                }//if fish move to bear cell
                else if (next.typeCheck() == "fish" && array[next.arrayloc + 1].typeCheck() == "bear") {
                    //delete fish object
                    array[next.arrayloc] = new animal();

                }//else error
                else if (next.typeCheck() == "blank") {
                    array[next.arrayloc] = new animal();
                    System.out.println("Mistake");
                }
            }

        }

    }


}

No comments:

Post a Comment

Thank you very much for viewing this entry and I hope you are able to return soon to continue to enjoy more of the site.
Please share your thoughts in the comment section.
Be blessed and enjoy life!