Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program in JAVA. some example code is given and needs to be assed to. Database w

ID: 3599005 • Letter: P

Question

Program in JAVA. some example code is given and needs to be assed to.
Database with Sorted Arrays as Indexes Practice (not as long as it looks)

What to do: Create a simple database with three fields : id, lastName, firstName all as Strings. You should define a class called DatabaseRecord with three private String varibles (the three fields), along with the appropriate getters and setters.
Here's what mine looked like(teacher example):
public class DatabaseRecord {

private String id; private String first; private String last;

public DatabaseRecord(String id, String first, String last) {

this.id = id; this.first = first;
this.last = last;
}

public String toString() { return id + " " + first + " " + last; } }

You should also declare an object called DatabaseArray which is an array of DatabaseRecords. Records of type DatabaseRecord should be added at the end of the DatabaseArray. Create an IndexRecord class:
public class IndexRecord {
private String key;
private int where;
//other stuff here
}
Now create an IndexIterator. This is an array of IndexRecord and is to be implemented as an OrderedArray class. That is, insertions must maintain the order in the array, where order is maintained by the key value. Hint: String class in Java comes with builtin compareTo method that you might find useful here.

Iterators

Your IndexIterator must implement an iterator. An iterator is simply a variable that maintains a current reference into the data structure. In this instance, since IndexIterator is a static array, the iterator is just an integer; a pointer into the array. You should implement the following methods:
void iteratorInitFront - set the iterator to zero
void iteratorInitBack - set the iterator to the last element in the array
boolean hasNext - returns true if iterator<= current last index in the array, false otherwise.
boolean hasPrevious - returns true if iterator>0 , false otherwise
int getNext - returns the where component of the IndexRecord referenced by iterator and then increments the iterator
int getPrevious - returns the where component of the IndexRecord referenced by iterator and then decrements the iterator
Finally, create a class called Database. Here's what mine looked like(teacher example):
public class Database {

private DatabaseArray myDbArr; private IndexIterator idIt, firstIt, lastIt;

public Database(int size) { myDbArr = new DatabaseArray(size); idIt = new IndexIterator(size); firstIt = new IndexIterator(size); lastIt = new IndexIterator(size); }

/// other stuff goes here…

}
To get an idea of how the iterators work, here's the code I have for printing out the database in ascending order by first name:
public void listByFirst() { firstIt.iteratorInitFront();

while (firstIt.hasNext()) {

System.out.println(myDbArr.retrieve(firstIt.getNext())); }

System.out.println(myDbArr.retrieve(firstIt.getCurrent())); }
Here's the output from the following for my sample data set:
1234 alice jones
9988 dave bing
2244 ed smiley
6633 ellen nance
3234 mac edwards
6655 mary rogers 9999 mike adams
4234 roger morris
2233 sue charles
1235 zelda smith
Here is your Driver program:
public class Driver { public static void main(String[] args) {

Database d = new Database(10); d.insert("1234", "alice", "jones"); d.insert("1235", "zelda", "smith"); d.insert("9999", "mike", "adams"); d.insert("9988", "dave", "bing"); d.insert("2233", "sue", "charles"); d.insert("2244", "ed", "smiley"); d.insert("6655", "mary", "rogers"); d.insert("6633", "ellen", "nance"); d.insert("4234", "roger", "morris"); d.insert("3234", "mac", "edwards");

System.out.println("by id:"); d.listById(); System.out.println("****"); System.out.println("by first:"); d.listByFirst(); System.out.println("*************"); System.out.println("by last:"); d.listByLast(); }

Grading (Practice) Correct implementation of the below classes will earn you points: • DatabaseArray (25 pts) • OrderedArray (25 pts) • IndexIterator (25 pts) • Database (25 pts)

• What will lose points: You should follow directions here closely. You will lose 50% of your grade for each of the following - your IndexIterator is not implemented as an OrderedArray. - if you maintain order of your IndexIterator by sorting the array - if you implement the DatabaseArray as three arrays of String - if you implement the IndexIterator as two arrays: one of type String and the other as type int. - if you do not use the driver program exactly as it is given • If your code does not run the assignment automatically receives 0. Program in JAVA. some example code is given and needs to be assed to.
Database with Sorted Arrays as Indexes Practice (not as long as it looks)

What to do: Create a simple database with three fields : id, lastName, firstName all as Strings. You should define a class called DatabaseRecord with three private String varibles (the three fields), along with the appropriate getters and setters.
Here's what mine looked like(teacher example):
public class DatabaseRecord {

private String id; private String first; private String last;

public DatabaseRecord(String id, String first, String last) {

this.id = id; this.first = first;
this.last = last;
}

public String toString() { return id + " " + first + " " + last; } }

You should also declare an object called DatabaseArray which is an array of DatabaseRecords. Records of type DatabaseRecord should be added at the end of the DatabaseArray. Create an IndexRecord class:
public class IndexRecord {
private String key;
private int where;
//other stuff here
}
Now create an IndexIterator. This is an array of IndexRecord and is to be implemented as an OrderedArray class. That is, insertions must maintain the order in the array, where order is maintained by the key value. Hint: String class in Java comes with builtin compareTo method that you might find useful here.

Iterators

Your IndexIterator must implement an iterator. An iterator is simply a variable that maintains a current reference into the data structure. In this instance, since IndexIterator is a static array, the iterator is just an integer; a pointer into the array. You should implement the following methods:
void iteratorInitFront - set the iterator to zero
void iteratorInitBack - set the iterator to the last element in the array
boolean hasNext - returns true if iterator<= current last index in the array, false otherwise.
boolean hasPrevious - returns true if iterator>0 , false otherwise
int getNext - returns the where component of the IndexRecord referenced by iterator and then increments the iterator
int getPrevious - returns the where component of the IndexRecord referenced by iterator and then decrements the iterator
Finally, create a class called Database. Here's what mine looked like(teacher example):
public class Database {

private DatabaseArray myDbArr; private IndexIterator idIt, firstIt, lastIt;

public Database(int size) { myDbArr = new DatabaseArray(size); idIt = new IndexIterator(size); firstIt = new IndexIterator(size); lastIt = new IndexIterator(size); }

/// other stuff goes here…

}
To get an idea of how the iterators work, here's the code I have for printing out the database in ascending order by first name:
public void listByFirst() { firstIt.iteratorInitFront();

while (firstIt.hasNext()) {

System.out.println(myDbArr.retrieve(firstIt.getNext())); }

System.out.println(myDbArr.retrieve(firstIt.getCurrent())); }
Here's the output from the following for my sample data set:
1234 alice jones
9988 dave bing
2244 ed smiley
6633 ellen nance
3234 mac edwards
6655 mary rogers 9999 mike adams
4234 roger morris
2233 sue charles
1235 zelda smith
Here is your Driver program:
public class Driver { public static void main(String[] args) {

Database d = new Database(10); d.insert("1234", "alice", "jones"); d.insert("1235", "zelda", "smith"); d.insert("9999", "mike", "adams"); d.insert("9988", "dave", "bing"); d.insert("2233", "sue", "charles"); d.insert("2244", "ed", "smiley"); d.insert("6655", "mary", "rogers"); d.insert("6633", "ellen", "nance"); d.insert("4234", "roger", "morris"); d.insert("3234", "mac", "edwards");

System.out.println("by id:"); d.listById(); System.out.println("****"); System.out.println("by first:"); d.listByFirst(); System.out.println("*************"); System.out.println("by last:"); d.listByLast(); }

Grading (Practice) Correct implementation of the below classes will earn you points: • DatabaseArray (25 pts) • OrderedArray (25 pts) • IndexIterator (25 pts) • Database (25 pts)

• What will lose points: You should follow directions here closely. You will lose 50% of your grade for each of the following - your IndexIterator is not implemented as an OrderedArray. - if you maintain order of your IndexIterator by sorting the array - if you implement the DatabaseArray as three arrays of String - if you implement the IndexIterator as two arrays: one of type String and the other as type int. - if you do not use the driver program exactly as it is given • If your code does not run the assignment automatically receives 0. Program in JAVA. some example code is given and needs to be assed to.
Database with Sorted Arrays as Indexes Practice (not as long as it looks)

What to do: Create a simple database with three fields : id, lastName, firstName all as Strings. You should define a class called DatabaseRecord with three private String varibles (the three fields), along with the appropriate getters and setters.
Here's what mine looked like(teacher example):
public class DatabaseRecord {

private String id; private String first; private String last;

public DatabaseRecord(String id, String first, String last) {

this.id = id; this.first = first;
this.last = last;
}

public String toString() { return id + " " + first + " " + last; } }

You should also declare an object called DatabaseArray which is an array of DatabaseRecords. Records of type DatabaseRecord should be added at the end of the DatabaseArray. Create an IndexRecord class:
public class IndexRecord {
private String key;
private int where;
//other stuff here
}
Now create an IndexIterator. This is an array of IndexRecord and is to be implemented as an OrderedArray class. That is, insertions must maintain the order in the array, where order is maintained by the key value. Hint: String class in Java comes with builtin compareTo method that you might find useful here.

Iterators

Your IndexIterator must implement an iterator. An iterator is simply a variable that maintains a current reference into the data structure. In this instance, since IndexIterator is a static array, the iterator is just an integer; a pointer into the array. You should implement the following methods:
void iteratorInitFront - set the iterator to zero
void iteratorInitBack - set the iterator to the last element in the array
boolean hasNext - returns true if iterator<= current last index in the array, false otherwise.
boolean hasPrevious - returns true if iterator>0 , false otherwise
int getNext - returns the where component of the IndexRecord referenced by iterator and then increments the iterator
int getPrevious - returns the where component of the IndexRecord referenced by iterator and then decrements the iterator
Finally, create a class called Database. Here's what mine looked like(teacher example):
public class Database {

private DatabaseArray myDbArr; private IndexIterator idIt, firstIt, lastIt;

public Database(int size) { myDbArr = new DatabaseArray(size); idIt = new IndexIterator(size); firstIt = new IndexIterator(size); lastIt = new IndexIterator(size); }

/// other stuff goes here…

}
To get an idea of how the iterators work, here's the code I have for printing out the database in ascending order by first name:
public void listByFirst() { firstIt.iteratorInitFront();

while (firstIt.hasNext()) {

System.out.println(myDbArr.retrieve(firstIt.getNext())); }

System.out.println(myDbArr.retrieve(firstIt.getCurrent())); }
Here's the output from the following for my sample data set:
1234 alice jones
9988 dave bing
2244 ed smiley
6633 ellen nance
3234 mac edwards
6655 mary rogers 9999 mike adams
4234 roger morris
2233 sue charles
1235 zelda smith
Here is your Driver program:
public class Driver { public static void main(String[] args) {

Database d = new Database(10); d.insert("1234", "alice", "jones"); d.insert("1235", "zelda", "smith"); d.insert("9999", "mike", "adams"); d.insert("9988", "dave", "bing"); d.insert("2233", "sue", "charles"); d.insert("2244", "ed", "smiley"); d.insert("6655", "mary", "rogers"); d.insert("6633", "ellen", "nance"); d.insert("4234", "roger", "morris"); d.insert("3234", "mac", "edwards");

System.out.println("by id:"); d.listById(); System.out.println("****"); System.out.println("by first:"); d.listByFirst(); System.out.println("*************"); System.out.println("by last:"); d.listByLast(); }

Grading (Practice) Correct implementation of the below classes will earn you points: • DatabaseArray (25 pts) • OrderedArray (25 pts) • IndexIterator (25 pts) • Database (25 pts)

• What will lose points: You should follow directions here closely. You will lose 50% of your grade for each of the following - your IndexIterator is not implemented as an OrderedArray. - if you maintain order of your IndexIterator by sorting the array - if you implement the DatabaseArray as three arrays of String - if you implement the IndexIterator as two arrays: one of type String and the other as type int. - if you do not use the driver program exactly as it is given • If your code does not run the assignment automatically receives 0.

Explanation / Answer

package com;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Graphics;

import javax.swing.JButton;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.event.ItemEvent;

import java.awt.event.ItemListener;

import java.awt.event.MouseEvent;

import java.util.ArrayList;

import java.util.Calendar;

import java.util.Random;

import javax.swing.JPanel;

import javax.swing.JToggleButton;

import javax.swing.JToolBar;

import javax.swing.JFrame;

import java.util.Timer;

import java.util.TimerTask;

import javax.swing.border.EtchedBorder;

class GlobalVariables {

public ArrayList<Fish> mFish;

public FishTank mFishTank;

private GlobalVariables() {

mFish = new ArrayList<Fish>();

mFishTank = new FishTank();

}

private static GlobalVariables instance;

public static GlobalVariables getInstance() {

if (instance == null){

instance = new GlobalVariables();

}

return instance;

}

}

class Fish implements Comparable<Fish>{

  

int mX;

int mY;

int mId;

Color mColor;

  

public Fish(int id, int x, int y, Color color){

  

mId = id;

mX = x;

mY = y;

mColor = color;

}

  

public void paint(Graphics g){

  

// Implement this function

}

  

public void move(){

// Implement this function

  

}

@Override

public int compareTo(Fish o) {

return mId;

  

// Implement this function

}

}

class FishTick extends TimerTask{

@Override

public void run() {

if (FishTank.mSimulateStatus){

  

for (int x=0;x<GlobalVariables.getInstance().mFish.size();x++){

  

Fish f = GlobalVariables.getInstance().mFish.get(x);

f.move();

GlobalVariables.getInstance().mFish.set(x, f);

}

  

GlobalVariables.getInstance().mFishTank.mDrawPanel.paint();

}

}

}

public class FishTank extends javax.swing.JFrame implements java.awt.event.MouseListener, java.awt.event.MouseMotionListener{

  

private final int mNumRows = 20;

private final int mNumCols = 20;

private final int mGridSz = 30;

  

private int mSelectedFishIndex = -1;

private boolean mDragged = false;

  

private int mTopHeight;

JToolBar mToolbar;

JToggleButton mSimulationButton;

DrawPanel mDrawPanel;

  

private int mFishIndex = 0;

  

static public boolean mSimulateStatus = false;

  

public static void main(String[] args) {

GlobalVariables global = GlobalVariables.getInstance();

  

if (global == null){

System.out.println("Cannot initialize, exiting ....");

return;

}

  

}

private JToggleButton addButton(String title){

  

JToggleButton button = new JToggleButton(title);

button.addItemListener(new ItemListener() {

public void itemStateChanged(ItemEvent ev) {

mSimulateStatus = !mSimulateStatus;

}

});

  

this.mToolbar.add(button);

  

return (button);

}

  

public FishTank()

{

JFrame guiFrame = new JFrame();

guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

guiFrame.setTitle("MY FISH TANK");

  

// Create a toolbar and give it an etched border.

this.mToolbar = new JToolBar();

this.mToolbar.setBorder(new EtchedBorder());

  

mSimulationButton = addButton("Simulate");

this.mToolbar.add(mSimulationButton);

//This will center the JFrame in the middle of the screen

guiFrame.setLocationRelativeTo(null);

  

this.mDrawPanel = new DrawPanel(mNumRows, mNumCols, mGridSz);

  

this.mDrawPanel.setBackground(Color.cyan);

this.mDrawPanel.paint();

  

guiFrame.add(mDrawPanel);

guiFrame.add(this.mToolbar, BorderLayout.NORTH);

  

// Add the Exit Action

JButton button = new JButton("Quit");

button.setToolTipText("Quit the program");

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

System.exit(0);

}

});

mToolbar.add(button);

  

guiFrame.addMouseListener(this);

guiFrame.addMouseMotionListener(this);

  

//make sure the JFrame is visible

guiFrame.setVisible(true);

  

mTopHeight = guiFrame.getInsets().top + mToolbar.getHeight();

guiFrame.setSize(mNumRows * mGridSz, mNumCols * mGridSz + mTopHeight);

  

Timer timer = new Timer("tick", true);

timer.scheduleAtFixedRate(new FishTick(), Calendar.getInstance().get(Calendar.MILLISECOND), 500);

}

@Override

public void mouseClicked(MouseEvent e) {

  

// Implement this function

}

@Override

public void mousePressed(MouseEvent e) {

}

@Override

public void mouseReleased(MouseEvent e) {

  

// Implement this function

}

@Override

public void mouseEntered(MouseEvent e) {

}

@Override

public void mouseExited(MouseEvent e) {

}

@Override

public void mouseDragged(MouseEvent e) {

  

// Implement this function

}

@Override

public void mouseMoved(MouseEvent e) {

}

}

class DrawPanel extends JPanel{

int mRows;

int mCols;

int mGridSz;

int maxGridSz;

  

ArrayList<Fish> mFish;

  

public DrawPanel(int numberOfRows, int numberOfCols, int gridSz){

  

mGridSz = gridSz;

mRows = numberOfRows;

mCols = numberOfCols;

maxGridSz = mGridSz * mRows;

}

  

private void paintBackground(Graphics g){

  

for (int i = 1; i < mRows; i++) {

g.drawLine(i * mGridSz, 0, i * mGridSz, maxGridSz);

}

  

for (int mAnimateStatus = 1; mAnimateStatus < mCols; mAnimateStatus++) {

g.drawLine(0, mAnimateStatus * mGridSz, maxGridSz, mAnimateStatus * mGridSz);

}

}

  

@Override

public void paintComponent(Graphics g){

  

super.paintComponent(g);

  

paintBackground(g);

  

for (Fish f:GlobalVariables.getInstance().mFish){

f.paint(g);

}

  

}

public void paint(){

repaint();

}

}