Question

I am going to show you my code and for some reason when I addDomino to...

I am going to show you my code and for some reason when I addDomino to front, my program gives me a null value, I need help figuring out why.  

public void addDominoFront(String d){
        String temp1 = Domino[0];
        String temp2;
        Domino[0] = d;
        for(int i = 1; i< numDominoes; i++){
            temp2 = Domino[i];
            Domino[i] = temp1;
            temp1 = temp2;
        }
        numDominoes = numDominoes +1;
    }

The I have the following code and it will not work addtoFront adds the value to front, but a null value to back

package assign1;
import javax.swing.JOptionPane;

public class Assign1 {

    public static void main(String[] args) {
        DominoGroup OriginalSet;
        DominoGroup PlayerSet;
        DominoGroup ComputerSet;
        DominoGroup GameSet;
        Domino CompareDomino1;
        Domino CompareDomino2;
        String RemovedDomino;
        int PlayerNumber;
        int ComputerNumber;
        int GameNumber;
        String HoldDominoes;
        String DominoOptions;
        String Game;
        int Counter;
        String StringCounter;
        int PlayerDomino;
        String Side;
        String Swap;
        char SideChar;
        char DominoNum1;
        char DominoNum2;
        int Num1;
        int Num2;
        OriginalSet = new DominoGroup();
        PlayerSet = new DominoGroup();
        ComputerSet = new DominoGroup();
        GameSet = new DominoGroup();
        CompareDomino1 = new Domino();
        CompareDomino2 = new Domino();
        OriginalSet.makeDominoSet();
        OriginalSet.mix();
        for ( int i = 0; i<7; i++){
            RemovedDomino = OriginalSet.removeDomino(0);
            PlayerSet.addDominoBack(RemovedDomino);
            RemovedDomino = OriginalSet.removeDomino(0);
            ComputerSet.addDominoBack(RemovedDomino);
        }
        JOptionPane.showMessageDialog(null, "Welcome to Dominoes."+ "\n"
                                       + "You will lie the first domino.");
        PlayerNumber = PlayerSet.getNumDominoes();
        ComputerNumber = ComputerSet.getNumDominoes();
        while ((PlayerNumber !=0) && (ComputerNumber != 0)){
            Counter = 0;
            DominoOptions = "";
            for ( int i = 0; i< PlayerNumber; i++){
                Counter++;
                StringCounter = String.valueOf(Counter)+ ". ";
                HoldDominoes = PlayerSet.getDomino(i);
                DominoOptions = DominoOptions +StringCounter + HoldDominoes
                                + "\n";
            }
        Counter++;
        StringCounter = String.valueOf(Counter)+ ". Pass";
        DominoOptions = DominoOptions + StringCounter;
        Game = GameSet.toString();
        System.out.println(Game);
        PlayerDomino = Integer.parseInt(JOptionPane.showInputDialog(" Please "
                + "pick the domino to be laid"+ "\n" + Game + "\n"
                +DominoOptions));
        RemovedDomino = PlayerSet.removeDomino(PlayerDomino-1);
        GameNumber = GameSet.getNumDominoes();
        if (GameNumber == 0){
            GameSet.addDominoBack(RemovedDomino);
        }
        else GameSet.addDominoFront(RemovedDomino);
        PlayerNumber = PlayerSet.getNumDominoes();
    }
    }
    }

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public void addDominoFront(String d){
    // lets suppose array was of size 4 and containing
    // 1, 2, 3, 4
    // now you are trying to insert d=0 at front

    String temp1 = Domino[0];
    // temp1 = 1

    String temp2;
    Domino[0] = d;
    // array becomes 0,2,3,4

    // now you loop from i=1 to i=3 (i < 4)..
    // and you shift the elements one by one..
    // after i=1
    // 0,1,3,4, temp1 = 2
    // after i=2
    // 0,1,2,4, temp1 = 3
    // after i=3
    // 0,1,2,3, temp1 = 4
    for(int i = 1; i< numDominoes; i++){
        temp2 = Domino[i];
        Domino[i] = temp1;
        temp1 = temp2;
    }

    // now you increment the number of dominoes to 5
    // which gives you an idea that the 5 elements have been put
    // while in reality, your loop just kept 4 elements only..
    // and at 5th position, the null value remained
    numDominoes = numDominoes +1;
}



// the correct code would be:
public void addDominoFront(String d){
    String temp1 = Domino[0];
    String temp2;
    Domino[0] = d;
    numDominoes = numDominoes +1;
    for(int i = 1; i< numDominoes; i++){
        temp2 = Domino[i];
        Domino[i] = temp1;
        temp1 = temp2;
    }
}

Hi. please find the code.. i have given code comments so that it is very easy for you to understand the flow. In case of any doubts, please ask in comments. If the answer helps you, please upvote. Thanks!

Add a comment
Know the answer?
Add Answer to:
I am going to show you my code and for some reason when I addDomino to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is the source code: Please incorporate the Exception Handling import javax.swing.JOptionPane; import java.awt.*; public class...

    This is the source code: Please incorporate the Exception Handling import javax.swing.JOptionPane; import java.awt.*; public class GuessingGame { public static void main(String[] args) { int computerNumber = (int) (Math.random() * 10 + 1); System.out.println("The correct guess would be " + computerNumber); int userAnswer = 0; int count = 0; while (computerNumber != userAnswer) { count++; String response = JOptionPane.showInputDialog(null, "Enter a guess between 1 and 10"); userAnswer = Integer.parseInt(response); String result = null; if (userAnswer == computerNumber) { result =...

  • Please help, Array does not print properly. I need to print out the 8 class numbers...

    Please help, Array does not print properly. I need to print out the 8 class numbers entered by the user ! Java only using J option pane, you cannot use ulti or anything else only J option pane Program compiles but does not print the array correctly! import javax.swing.JOptionPane; public class programtrial {    public static void main(String[] args) {    int newclass = 0;    int countclass = 0;    final int class_Max = 8;    int[] classarray =...

  • Hello can someone help me in my code I left it as comment  task #0, task#1, task#2a...

    Hello can someone help me in my code I left it as comment  task #0, task#1, task#2a and task#2b the things that I am missing I'm using java domain class public class Pizza { private String pizzaCustomerName; private int pizzaSize; // 10, 12, 14, or 16 inches in diameter private char handThinDeep; // 'H' or 'T' or 'D' for hand tossed, thin crust, or deep dish, respecitively private boolean cheeseTopping; private boolean pepperoniTopping; private boolean sausageTopping; private boolean onionTopping; private boolean...

  • I need help fixing my java code for some reason it will not let me run...

    I need help fixing my java code for some reason it will not let me run it can someone pls help me fix it. class LinearProbingHashTable1 { private int keyname; private int valuename; LinearProbingHashTable1(int keyname, int valuename) { this.keyname = keyname; this.valuename = valuename; } public int getKey() { return keyname; } public int getValue() { return valuename; } } class LinearProbingHashTable2 { private final static int SIZE = 128; LinearProbingHashTable2[] table; LinearProbingHashTable2() { table = new LinearProbingHashTable2[SIZE]; for (int...

  • Write a program in Java that prompts a user for Name and id number. and then...

    Write a program in Java that prompts a user for Name and id number. and then the program outputs students GPA MAIN import java.util.StringTokenizer; import javax.swing.JOptionPane; public class Main {    public static void main(String[] args) {                       String thedata = JOptionPane.showInputDialog(null, "Please type in Student Name. ", "Student OOP Program", JOptionPane.INFORMATION_MESSAGE);        String name = thedata;        Student pupil = new Student(name);                   //add code here       ...

  • So I am working on an assignment where we make a rudimentary browser history with c#....

    So I am working on an assignment where we make a rudimentary browser history with c#. The requirement is that we need to use a linked list to do this. The issue that I am having is that when I want to go backwards in the history and print it takes from the beginning of the list and not the front. (Example is if I had a list with 1, 2, 3, 4 in it and went back It would...

  • This is my code for my game called Reversi, I need to you to make the...

    This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...

  • Hi, for my Java class I have built a number guessing game. I need to separate...

    Hi, for my Java class I have built a number guessing game. I need to separate my code into two classes and incorporate a try-catch statement. Any help would be appreciated! Here is my code. import javax.swing.JOptionPane; import javax.swing.UIManager; import java.awt.Color; import java.awt.color.*; import java.util.Random; public class game { public static void main (String [] args) { UIManager.put("OptionPane.backround", Color.white); UIManager.put("Pandelbackround", Color.white); UIManager.put("Button.background", Color.white); Random nextRandom = new Random(); int randomNum = nextRandom.nextInt(1000); boolean playerCorrect = false; String keyboardInput; int playerGuess...

  • JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList...

    JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList Class (some methods included). Remember, you will use the LinkedList Class that we developed in class not Java’s LinkedList Class. You will add the following method to the LinkedList Class: printEvenNodes – this is a void method that prints Nodes that have even indices (e.g., 0, 2, 4, etc). Create a LinkedListDemo class. Use a Scanner Class to read in city names and store...

  • is there anyway you can modify the code so that when i run it i can...

    is there anyway you can modify the code so that when i run it i can see all the song when i click the select button all the songs in the songList.txt file can be shown song.java package proj2; public class Song { private String name; private String itemCode; private String description; private String artist; private String album; private String price; Song(String name, String itemCode, String description, String artist, String album, String price) { this.name = name; this.itemCode = itemCode;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT