Question

In: Computer Science

In Java please: 1) Part 1: Edit the getTileList method (add length method too) Dear Developer,...

In Java please:

1) Part 1: Edit the getTileList method (add length method too)


Dear Developer, It seems an overzealous programmer tried to create a Fibonacci slider puzzle from our old code. This brought up the fact there is a data integrity issue in our SlidingSquarePuzzle class. It makes sense because the class’s data only consists of an int[]. Since, you are new this is a good opportunity to get your feet wet. I want you to change the offending code and help shore up our security hole. I’ve attached their Driver and the GUI and SlidingSquarePuzzle files. I’ve also commented out the offending code. Sincerely, Your Boss P.S. You might as well include some documentation while you’re at it. It’s been bugging me for the longest time.

Part 1: Edit the getTileList method

1. Why does this method create a security issue? a. Because you have direct access to the array. As seen in the driver, you have the reference to the array.

2. Change the method to getTile(int index) and return an int at the specific index of tileList.

3. This will break the GUI code. You will need to replace instances of getTileList with getTile. a. Even though you have changed everything, your code will not work. b. This is due to the return data being primitive. You cannot assign data to a primitive data type.

4. Add a length method that returns the tileList.length. a. Replace where necessary.

=================

Driver.java:

public class Driver {

        public static void main(String[] args) {
                //change this number to how many tiles you want
                int numberOfTiles = 9;
                
                SlidingSquarePuzzle fibonaciSlider = new SlidingSquarePuzzle(numberOfTiles);
                
//              fibonaciSlider.getTileList()[0] = 1;
//              fibonaciSlider.getTileList()[1] = 1;
//              fibonaciSlider.getTileList()[2] = 2;
//              fibonaciSlider.getTileList()[3] = 3;
//              fibonaciSlider.getTileList()[4] = 5;
//              fibonaciSlider.getTileList()[5] = 8;
//              fibonaciSlider.getTileList()[6] = 13;
//              fibonaciSlider.getTileList()[7] = 21;
                
                GUI.init(fibonaciSlider);
        }

}

=====================

GUI.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
@SuppressWarnings("serial")
public class GUI extends JPanel {
 
    private SlidingSquarePuzzle ssp;
    private final int sideLength;
    private final int tileSize;
    private int blankPos;
    private final int margin;
    private final int gridSize;
    private boolean gameOver;
 
    private GUI(SlidingSquarePuzzle ssp) {
        this.ssp = ssp;
                sideLength = (int) Math.floor(Math.sqrt(ssp.getTileList().length));
        final int resolution = 640;
        margin = 40;
        tileSize = (resolution - 2 * margin) / sideLength;
        gridSize = tileSize * sideLength;
 
        setPreferredSize(new Dimension(resolution, resolution + margin));
        setBackground(Color.WHITE);
        setForeground(new Color(0x006940)); 
        setFont(new Font("SansSerif", Font.BOLD, 60));
 
        gameOver = true;
        
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (gameOver) {
                        newGame();
                } else {
 
                    int ex = e.getX() - margin;
                    int ey = e.getY() - margin;
                    if (ex < 0 || ex > gridSize || ey < 0 || ey > gridSize) return;
 
                    int clickX = ex / tileSize;
                    int clickY = ey / tileSize;
                    int blankX = blankPos % sideLength;
                    int blankY = blankPos / sideLength;
 
                    int clickPos = clickY * sideLength + clickX;
 
                    int direction = 0;
                    if (clickX == blankX && Math.abs(clickY - blankY) > 0) {
                        direction = (clickY - blankY) > 0 ? sideLength : -sideLength;
 
                    } else if (clickY == blankY && Math.abs(clickX - blankX) > 0) {
                        direction = (clickX - blankX) > 0 ? 1 : -1;
                    }
 
                    if (direction != 0) {
                        do {
                            int newBlankPos = blankPos + direction;
                            ssp.getTileList()[blankPos] = ssp.getTileList()[newBlankPos];
                            blankPos = newBlankPos;
                        } while (blankPos != clickPos);
                        ssp.getTileList()[blankPos] = 0;
                    }
 
                    gameOver = ssp.isSolved();
                }
                repaint();
            }
        });
        newGame();
    }
    
    private void newGame() {
        ssp.shuffle();
        gameOver = false;
        for (int i = 0; i < ssp.getTileList().length; i++) {
                if(ssp.getTileList()[i] != 0) continue;
                blankPos = i;
                break;
        }
    }
 
    private void drawGrid(Graphics2D g) {
        for (int i = 0; i < ssp.getTileList().length; i++) {
            int x = margin + (i % sideLength) * tileSize;
            int y = margin + (i / sideLength) * tileSize;
 
            if (ssp.getTileList()[i] == 0) {
                if (gameOver) {
                    g.setColor(Color.GREEN);
                    drawCenteredString(g, "\u2713", x, y);
                }
                continue;
            }
 
            g.setColor(getForeground());
            g.fillRoundRect(x, y, tileSize, tileSize, 25, 25);
            g.setColor(Color.green.darker());
            g.drawRoundRect(x, y, tileSize, tileSize, 25, 25);
            g.setColor(Color.WHITE);
 
            drawCenteredString(g, String.valueOf(ssp.getTileList()[i]), x, y);
        }
    }
 
    private void drawStartMessage(Graphics2D g) {
        if (gameOver) {
            g.setFont(getFont().deriveFont(Font.BOLD, 18));
            g.setColor(getForeground());
            String s = "click to start a new game";
            int x = (getWidth() - g.getFontMetrics().stringWidth(s)) / 2;
            int y = getHeight() - margin;
            g.drawString(s, x, y);
        }
    }
 
    private void drawCenteredString(Graphics2D g, String s, int x, int y) {
        FontMetrics fm = g.getFontMetrics();
        int asc = fm.getAscent();
        int des = fm.getDescent();
 
        x = x + (tileSize - fm.stringWidth(s)) / 2;
        y = y + (asc + (tileSize - (asc + des)) / 2);
 
        g.drawString(s, x, y);
    }
 
    @Override
    public void paintComponent(Graphics gg) {
        super.paintComponent(gg);
        Graphics2D g = (Graphics2D) gg;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
 
        drawGrid(g);
        drawStartMessage(g);
    }
 
    public static void init(SlidingSquarePuzzle ssp) {
        SwingUtilities.invokeLater(() -> {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setTitle("Slidng Square Puzzle");
            f.setResizable(false);
            f.add(new GUI(ssp), BorderLayout.CENTER);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        });
    }
}

======================

SlidingSquarePuzzle.java

import java.util.Arrays;
import java.util.Random;

public class SlidingSquarePuzzle {
        private int [] tileList;
        
        public SlidingSquarePuzzle() {
                this(16);
        }
        public SlidingSquarePuzzle(int size) {
                tileList = size < 4 
                                ? new int[4]
                                : new int[(int) Math.pow(Math.floor(Math.sqrt(size)), 2)];
                solve();
        }
        public int[] getTileList() {
                return tileList;
        }
        public void solve() {
                for(int i = 0; i < tileList.length - 1; i++) {
                        tileList[i] = i + 1;
                }
        }
        public boolean isSolved() {
                for(int i = 0; i < tileList.length - 1; i++)
                        if (tileList[i] != i + 1) return false;
                return true;
        }
        /*
         * This looks at the puzzle and checks if it is solvable
         */
        private boolean isSolvable()  {
                int inversions = 0;
                int m = (int) Math.floor(Math.sqrt(tileList.length));
                for(int i = 0; i < tileList.length; i++)
                        for(int j = i + 1; j < tileList.length; j++)
                        {
                                if(tileList[i] == 0 || tileList[j] == 0) continue;
                if (tileList[j] < tileList[i])
                    inversions++;
                        }
                if(m % 2 == 1)
                        return inversions % 2 == 0;
                else {
                        int blankIndex = 0; 
                        for(int i = 0; i < tileList.length; i++)
                                if(tileList[i] == 0 ) {
                                        blankIndex = i;
                                        break;
                                }
                        if((blankIndex / m) % 2 == 0)
                                return inversions % 2 == 1;
                        else
                                return inversions % 2 == 0;
                }
        }
        public void shuffle() {
                Random rand = new Random();
                do {
                        for(int i = tileList.length - 1; i > 0; i--) {
                                int j = rand.nextInt(i + 1);
                                int temp = tileList[j];
                                tileList[j] = tileList[i];
                                tileList[i] = temp;
                        }
                }while(!this.isSolvable());
        }

}

Solutions

Expert Solution

---------------------------------------------------------------------------------------------------------------------------

Below are the three method required in SlidingSquarePuzzle.java file. Apart from given documentation , I also added settile() method to set the value in array.

-----------------------------------------------------------------------------------------------------------------------------

public int getTile(int index)
{
           return tileList[index];
  }
      
       public void setTile(int index,int value)
       {
           tileList[index]=value;
       }
      
       public int length()
       {
           return tileList.length;
       }

========================================================================

Replace some code in GUI.java file

1. ssp.setTile(blankPos, ssp.getTile(newBlankPos)) in place of

ssp.getTileList()[blankPos] = ssp.getTileList()[newBlankPos];

2. replace ssp.getTileList().length to ssp.length()

3. replace ssp.getTileList()[index] to ssp.getTile(index)

========================================================================

-------------------------------------------------------------------------------------------------------------------------------

Below is required editable code.

------------------------------------------------SlidingSquarePuzzle.java----------------------------------------------

import java.util.Arrays;
import java.util.Random;

public class SlidingSquarePuzzle {
private int [] tileList;
  
public SlidingSquarePuzzle() {
this(16);
}
public SlidingSquarePuzzle(int size) {
tileList = size < 4
? new int[4]
: new int[(int) Math.pow(Math.floor(Math.sqrt(size)), 2)];
solve();
}
      
       public int getTile(int index)
       {
           return tileList[index];
       }
      
       public void setTile(int index,int value)
       {
           tileList[index]=value;
       }
      
       public int length()
       {
           return tileList.length;
       }
      
      
public void solve() {
for(int i = 0; i < tileList.length - 1; i++) {
tileList[i] = i + 1;
}
}
public boolean isSolved() {
for(int i = 0; i < tileList.length - 1; i++)
if (tileList[i] != i + 1) return false;
return true;
}
/*
* This looks at the puzzle and checks if it is solvable
*/
private boolean isSolvable() {
int inversions = 0;
int m = (int) Math.floor(Math.sqrt(tileList.length));
for(int i = 0; i < tileList.length; i++)
for(int j = i + 1; j < tileList.length; j++)
{
if(tileList[i] == 0 || tileList[j] == 0) continue;
if (tileList[j] < tileList[i])
inversions++;
}
if(m % 2 == 1)
return inversions % 2 == 0;
else {
int blankIndex = 0;
for(int i = 0; i < tileList.length; i++)
if(tileList[i] == 0 ) {
blankIndex = i;
break;
}
if((blankIndex / m) % 2 == 0)
return inversions % 2 == 1;
else
return inversions % 2 == 0;
}
}
public void shuffle() {
Random rand = new Random();
do {
for(int i = tileList.length - 1; i > 0; i--) {
int j = rand.nextInt(i + 1);
int temp = tileList[j];
tileList[j] = tileList[i];
tileList[i] = temp;
}
}while(!this.isSolvable());
}

}

--------------------------------------------------GUI.java---------------------------------------------------------

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class GUI extends JPanel {

private SlidingSquarePuzzle ssp;
private final int sideLength;
private final int tileSize;
private int blankPos;
private final int margin;
private final int gridSize;
private boolean gameOver;

private GUI(SlidingSquarePuzzle ssp) {
this.ssp = ssp;
sideLength = (int) Math.floor(Math.sqrt(ssp.length()));
final int resolution = 640;
margin = 40;
tileSize = (resolution - 2 * margin) / sideLength;
gridSize = tileSize * sideLength;

setPreferredSize(new Dimension(resolution, resolution + margin));
setBackground(Color.WHITE);
setForeground(new Color(0x006940));
setFont(new Font("SansSerif", Font.BOLD, 60));

gameOver = true;
  
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (gameOver) {
newGame();
} else {

int ex = e.getX() - margin;
int ey = e.getY() - margin;
if (ex < 0 || ex > gridSize || ey < 0 || ey > gridSize) return;

int clickX = ex / tileSize;
int clickY = ey / tileSize;
int blankX = blankPos % sideLength;
int blankY = blankPos / sideLength;

int clickPos = clickY * sideLength + clickX;

int direction = 0;
if (clickX == blankX && Math.abs(clickY - blankY) > 0) {
direction = (clickY - blankY) > 0 ? sideLength : -sideLength;

} else if (clickY == blankY && Math.abs(clickX - blankX) > 0) {
direction = (clickX - blankX) > 0 ? 1 : -1;
}

if (direction != 0) {
do {
int newBlankPos = blankPos + direction;
ssp.setTile(blankPos, ssp.getTile(newBlankPos));
blankPos = newBlankPos;
} while (blankPos != clickPos);
ssp.setTile(blankPos,0);
}

gameOver = ssp.isSolved();
}
repaint();
}
});
newGame();
}
  
private void newGame() {
ssp.shuffle();
gameOver = false;
for (int i = 0; i < ssp.length(); i++) {
if(ssp.getTile(i) != 0) continue;
blankPos = i;
break;
}
}

private void drawGrid(Graphics2D g) {
for (int i = 0; i < ssp.length(); i++) {
int x = margin + (i % sideLength) * tileSize;
int y = margin + (i / sideLength) * tileSize;

if (ssp.getTile(i) == 0) {
if (gameOver) {
g.setColor(Color.GREEN);
drawCenteredString(g, "\u2713", x, y);
}
continue;
}

g.setColor(getForeground());
g.fillRoundRect(x, y, tileSize, tileSize, 25, 25);
g.setColor(Color.green.darker());
g.drawRoundRect(x, y, tileSize, tileSize, 25, 25);
g.setColor(Color.WHITE);

drawCenteredString(g, String.valueOf(ssp.getTile(i)), x, y);
}
}

private void drawStartMessage(Graphics2D g) {
if (gameOver) {
g.setFont(getFont().deriveFont(Font.BOLD, 18));
g.setColor(getForeground());
String s = "click to start a new game";
int x = (getWidth() - g.getFontMetrics().stringWidth(s)) / 2;
int y = getHeight() - margin;
g.drawString(s, x, y);
}
}

private void drawCenteredString(Graphics2D g, String s, int x, int y) {
FontMetrics fm = g.getFontMetrics();
int asc = fm.getAscent();
int des = fm.getDescent();

x = x + (tileSize - fm.stringWidth(s)) / 2;
y = y + (asc + (tileSize - (asc + des)) / 2);

g.drawString(s, x, y);
}

@Override
public void paintComponent(Graphics gg) {
super.paintComponent(gg);
Graphics2D g = (Graphics2D) gg;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

drawGrid(g);
drawStartMessage(g);
}

public static void init(SlidingSquarePuzzle ssp) {
SwingUtilities.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("Slidng Square Puzzle");
f.setResizable(false);
f.add(new GUI(ssp), BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}

-------------------------------------------------Driver.java-------------------------------------------------

public class Driver {

public static void main(String[] args) {
//change this number to how many tiles you want
int numberOfTiles = 9;
  
SlidingSquarePuzzle fibonaciSlider = new SlidingSquarePuzzle(numberOfTiles);
  
// fibonaciSlider.getTileList()[0] = 1;
// fibonaciSlider.getTileList()[1] = 1;
// fibonaciSlider.getTileList()[2] = 2;
// fibonaciSlider.getTileList()[3] = 3;
// fibonaciSlider.getTileList()[4] = 5;
// fibonaciSlider.getTileList()[5] = 8;
// fibonaciSlider.getTileList()[6] = 13;
// fibonaciSlider.getTileList()[7] = 21;
  
GUI.init(fibonaciSlider);
}

}


Related Solutions

In Java please: 2) Part 2: Add a blankPos field Dear Developer, It seems an overzealous...
In Java please: 2) Part 2: Add a blankPos field Dear Developer, It seems an overzealous programmer tried to create a Fibonacci slider puzzle from our old code. This brought up the fact there is a data integrity issue in our SlidingSquarePuzzle class. It makes sense because the class’s data only consists of an int[]. Since, you are new this is a good opportunity to get your feet wet. I want you to change the offending code and help shore...
JAVA- How do I edit the following code as minimally as possible to add this method...
JAVA- How do I edit the following code as minimally as possible to add this method for calculating BMI? BMI Method: public static double calculateBMI(int height, int weight) { double BMI = (((double) weight) * 0.453592d) / ((((double) height) * 0.0254) * (((double) height) * 0.0254)); Format f = new DecimalFormat("##.######"); return (f.format(BMI)); } Code: import java.text.DecimalFormat; import java.util.Scanner; public class test2 { public static void main(String[] args) { DecimalFormat f = new DecimalFormat("##.0"); Scanner reader = new Scanner(System.in); System.out.printf("%10s...
Please edit, rephrase or add something to the following part of my report. Question:Whether debt funding...
Please edit, rephrase or add something to the following part of my report. Question:Whether debt funding would be desirable, or even possible for the new equipment required should the company decide to pursue aggressive expansion – noting that the equipment used by the business is quite specialised and cannot easily be sold and taken to another company. Answer:Debt funding is not adviseable as these assets are specialised and resale would not be easy .An Operating lease option would be preferred...
Please add the following method as a part of the UnorderedList class definition: •print_list: the method...
Please add the following method as a part of the UnorderedList class definition: •print_list: the method prints the elements of the list using the same format as a Python list (square brackets and commas as separators). In the main function of the modified UnorderedList.py, please test the new method to demonstrate that it works as expected. Please leave comments in the code to show what is done UnorderList.py file # Implementation of an Unordered List ADT as a linked list....
Please write code in java ASAP and add comments too, will be really appreciated. Thanks CSCI203/CSCI803...
Please write code in java ASAP and add comments too, will be really appreciated. Thanks CSCI203/CSCI803 This assignment involves extension to the single source-single destination shortest path problem. The Program Your program should: 1. Read the name of a text file from the console. (Not the command line) 2. Read an undirected graph from the file. 3. Find the shortest path between the start and goal vertices specified in the file. 4. Print out the vertices on the path, in...
Please edit and add more details to the event report. In the current era of digital...
Please edit and add more details to the event report. In the current era of digital revolution, where all the organizations are moving towards digitalization, it has become imperative to understand the mechanism of data analysis. The objective of this event is to make the people aware of the importance and application of data analysis and to give them a hands on experience of data analysis. Thus corporates can make use of the same in the growth of their business....
java beginner level how do i add messages that say win, too high or too low...
java beginner level how do i add messages that say win, too high or too low for a lottery number generator guessing game? //generate lottery numbers int lottery = (int) (Math.random() * 500); int guess = 0; boolean correct = false; // prompt user to enter a guess Scanner input = new Scanner(System.in); System.out.print ("Enter your lottery pick (three digits): "); int number = input.nextInt(); // keep asking user until user enters a 3-digit number while (guess < 100 ||...
JAVA Please: Lab9B: MicroDB. In Lab9A, each method returned an integer. In this part of the...
JAVA Please: Lab9B: MicroDB. In Lab9A, each method returned an integer. In this part of the lab, all methods will have a void return type and take in an array of integers as a parameter. You’re going to write a program that creates a mini database of numbers that allows the user to reset the database, print the database, add a number to the database, find the sum of the elements in the database, or quit. In main, you will...
use repil.it edit my code please i already did all part but need to edit more...
use repil.it edit my code please i already did all part but need to edit more its run for some not shwing all intro to C-programin be sure to edit on my code very basic and add good comments thank you 1-Write a program that requests 5 integers from the user and stores them in an array. You may do this with either a for loop OR by getting a string from stdin and using sscanf to store formatted input...
Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with...
Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with the higher value data. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head; while (tempPtr != null) { tempPtr = tempPtr.nextNode; result =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT