apines 116 Practically a Master Poster Featured Poster

Glad I could help :) Please mark the thread as solved.

apines 116 Practically a Master Poster Featured Poster

Actually this forum is to help and guide with problems and not to do your work for you.

apines 116 Practically a Master Poster Featured Poster

You need to fix the build path the include the other project as well, it's not enough that they are in the same workspace. If you are using eclipse, you need to go to the build path (right click on the project, build path -> configure build path) and add the project by pressing "Add" in the Projects tab.

apines 116 Practically a Master Poster Featured Poster

I'm sure that you can use Google to search for existing code. You can write it by yourself, all the information you need is in the File class.

apines 116 Practically a Master Poster Featured Poster

As you wish, I still think you need to look at this post and with a tiny bit of manipulation you won't have to ask the players who's turn it is.
Good luck, please mark the thread as solved :)

apines 116 Practically a Master Poster Featured Poster

I will not write the code for you. Please read my previous comments again.

apines 116 Practically a Master Poster Featured Poster

Now I think that it is time that you take the code and try to run some examples for yourself, see how it works.

apines 116 Practically a Master Poster Featured Poster

Here is a sample code to demonstrate our problem:

public static void main(String args)
{
  String player1 = "Josh"; 
  String player2 = "David";
  boolean isTurnJosh = true; // if isTurnJosh = true, it's Josh's turn, otherwise it is David's.
  while(true)
  { 
     if(isTurnJosh)
     {
        System.out.println("Hi Josh, I know that this is your turn because I am using a boolean variable");
     }
     else
     {
        System.out.println("Hi David, Where is Josh?"); 
     }
     isTurnJosh = !isTurnJosh;
  }
}

Now, can you see how a boolean variable can be your friend? :)

apines 116 Practically a Master Poster Featured Poster

What have you fixed? you are still asking each turn who's turn it is. Think about it like this - you are inside the switch statement, case 1, meaning you know it's player 1's turn - and at the end you still ask

System.out.println("Next Turn! For Player 1 Hit 1, For Player 2 Hit 2");
playerTurn = input.nextInt();

Is this what the exercise specification state? Why not use the fact that you know who is playing now in order to set who is playing next, and each turn automatically print the screen because the players will want to look at the updated board?

apines 116 Practically a Master Poster Featured Poster

I was not talking about the while loop, I said that the method isBoardFull(char[][] gameBoard) returns the wrong value - look at the method, take different matrices and try to run the method on them, you will see that it returns a wrong value. Regarding the boolean variable - yes it is for eliminating the need for asking the user who's turn it is - read the comments again please regarding how to use it.

apines 116 Practically a Master Poster Featured Poster

Please read my previous comments again. You still ask each turn who's turn to play and you changed your isBoardFull method so now they will not work.

public static boolean isBoardFull(char[][] gameBoard)
{
   boolean result;
   for (int row = 0; row < 3; row++)
   {
      for (int col = 0; col < 3; col++)
      {
          if ((gameBoard[row][col] == 'O')||(gameBoard[row][col] == 'X'))
          {
             return false;
          }
      }
   }
   return true;
}

The first time that the method will encounter a cell (a single cell) with 'O' or 'X' it will return false, as in the board is not full.

apines 116 Practically a Master Poster Featured Poster

I also asked whether the program right now works as you wish it to work or do you encounter any errors (as in exceptions, unexpected output etc')... help me to help you :)

apines 116 Practically a Master Poster Featured Poster

I recommend that you'll start here, read about regular grammar to give you a direction.

apines 116 Practically a Master Poster Featured Poster

Accessing the first element of row i: arr[i][0]; Accessing the last element of row i: arr[i][arr[i].length - 1]; From here you just need to do the adding :) Give it a go, try to implement it.

apines 116 Practically a Master Poster Featured Poster

I don't think you understood me completely - you don't need the isPlayerTurn(int playerTurn) method.

System.out.println("Who Wants to Begin? For Player 1 Hit 1, For Player 2 Hit 2");
playerTurn = input.nextInt();
if(playerTurn == 1)  //you can use a switch statement as well.
{
   playerOneTurn = true;
}
else if(playerTurn == 2)
{
   playerOneTurn = false;  
}
else
{
  //the user entered invalid input
}

Now, in the loop, you know that if playerOneTurn = true; , it's player 1's turn, otherwise player 2's turn. At the end of the turn, inside the loop, simply do

playerOneTurn = !playerOneTurn;

And you have switched the players. No need for another method that will determine who's turn it is.

Does the program, as it is, works? I mean, do you want code review or help with errors you encounter? Both things are fine of course, and we'll be happy to help, I just want to know where you stand right now.

apines 116 Practically a Master Poster Featured Poster

Yes. You always need to initialize a variable before using it, do you understand why?

apines 116 Practically a Master Poster Featured Poster

Again, how will you create int[] gradeArray = new int[numstudents]; on line 16 if you initialize numstudents only on line 21?

apines 116 Practically a Master Poster Featured Poster

Unless the array is sorted, the if statement on line 10 will be entered at least once, which will make swap=false , which means that the for loop will be executed only once. I recommend that you will take a look again at the bubble sort pseudo code, and then try to implement it with Java to work with linked lists.

apines 116 Practically a Master Poster Featured Poster

Ok, few problems that I can see:

  • In line 16 you are creating an array int[] gradeArray = new int[sgrades]; when you didn't initialize sgrades - you need to retrieve the number of students in the class, which is the first line in the file.
  • In the loop in line 21 you are ignoring the fact that the first line is different than the rest of the lines, and treat it as a regular line that holds a student's score.

You have to read the first line, determine the number of students, initialize the array, and only then retrieve the scores.
Also - please explain what you are trying to do in line 28 and beyond.

apines 116 Practically a Master Poster Featured Poster

Your isFilled method is still checking for 0, and every turn you ask who's going first. I didn't get what you have tried to do... Please read my previous comments again.

apines 116 Practically a Master Poster Featured Poster

Yes it will remove the current switch statement, and as I wrote to flip a boolean variable all you have to do is booleanVar = !booleanVar; - whether it will be in a method or not it is up to you, try to implement and see how it goes. Again, the boolean var is not necessary, it is an optional solution to not asking the users who's turn is it all the time - think about it, when you are playing against someone, don't you expect the program to be smart enough to know that if you just played, it's your opponent's turn now and vice versa?

apines 116 Practically a Master Poster Featured Poster

Looks like you are on the right track. Few comments:

  • You fill the board at the beginning with "_" and then with "X" or "O" depending on the user, yet your isFilled method is checking if (gameBoard[row][col] == 0) - will be a little problematic :)
  • As I mentioned before, you don't have to ask the user who's turn it is each time - either you determine that it is always player's one turn (you don't keep scores for multiple games so you don't care) or you can simply use a boolean variable - the first time as who wants to start the game, and then flip the boolean value each time:
    boolean playerOneTurn; //true means it's player 1 turn, false player 2
    System.out.println("Who is going to start? For Player 1 Hit 1, For Player 2 Hit 2");
    playerTurn = input.nextInt();
    if(playerTurn == 1)
    {
       playerOneTurn = true; 
    }
    else
    {
       playerOneTurn = false;
    }

    And at the end of each turn remember to switch:

    playerOneTurn = !playerOneTurn;
  • Not a real problem, but fyi - you are using a variable turns and a while loop, when you have the for loop:
    for(int turns = 0; turns < 9; ++turns)

    Will do the same trick in a more elegant fashion.

apines 116 Practically a Master Poster Featured Poster

Give me a detailed pseudo code of the problem, showing the algorithm that needs to be solved.

apines 116 Practically a Master Poster Featured Poster

jon.kiparsky's approach of using a class member is the one that I would have used. There is another choice - in case you insist not to have class members, you can have your method return the String back to the main method, and from there you can send it to other methods.

public static String entername() //we have changed the return value of the method to String
{
   String a="";
   a = JOptionPane.showInputDialog("Welcome. Enter name ");
   return a; //return the value back to main
}

In the main method, you can use the String as a parameter to send to the other methods.

public static void main(String args[])
{
   String name = entername(); //name will be equal to the value returned from entername()
   mainscreen(name); //now the method mainscreen will have the String name and will be able to use it.
}

Of course, you will have to change the mainscreen method to accept a parameter:

public static void mainscreen(String name)
{
   // implement
}
apines 116 Practically a Master Poster Featured Poster

If you want to know who's turn it is, simply ask the users at the beginning (before the loop and the game actually starts) who wants to go first. You can save a boolean variable firstPlayerTurn - when it's true it's player 1's turn, if false it's player 2's turn. Every loop iteration just do firstPlayerTurn = !firstPlayerTurn .

apines 116 Practically a Master Poster Featured Poster

Problems that I can see here, first in the code in line 38:

if(Character.isLetter(j))
{
   p1array = phrase2.charAt(j).toCharArray();
}

phrase2.charAt(j) results in a char, and there is no such thing as toCharArray() on char. The exact same problem occur in the if on line 49. Second problem is in the code on line 36:

for (int k=0; k< phrase2.length; ++k)

The variable phrase2 is a String, which means you have to use phrase2.length() and not phrase2.length .

apines 116 Practically a Master Poster Featured Poster

Take a look at this thread.

apines 116 Practically a Master Poster Featured Poster

In Java the order of the methods does not matter - inside the class all the methods are visible and outside of it it depends on the modifier (public, private etc')

apines 116 Practically a Master Poster Featured Poster

The methods are visible throughout the class, the members have their scope meaning that their visibility is depending on where they were created.

apines 116 Practically a Master Poster Featured Poster

If all the plane is full, perhaps you want to exit the program and not display the reservation menu again?

apines 116 Practically a Master Poster Featured Poster

First try to implement something that works. Then we will see if indeed it is confusing and can be simplified.

apines 116 Practically a Master Poster Featured Poster

Yes, you can say that first class is from 0-2 instead of 1-3 and so on, it's a good solution.

apines 116 Practically a Master Poster Featured Poster

FileInputStream fstream[] is not initialized and therefore is null. Before you can write the code in lines 14 and beyond, you have to initialize the array itself:

fstream = new FileInputStream[array size]

If the array size is dynamic, you might want to consider a list instead.

apines 116 Practically a Master Poster Featured Poster

They ask you to build a class that will test the class you have previously created.

apines 116 Practically a Master Poster Featured Poster

Explain the problem and your approach, don't expect everyone to guess what you need to do and code it for you.

apines 116 Practically a Master Poster Featured Poster

You need to replace the %d with %f to print float numbers.

System.out.printf("%f\t\t%.1f\n",t, distance);
apines 116 Practically a Master Poster Featured Poster

You know - the best way is to try it yourself. What makes you think that the method will not function properly afterwards?

apines 116 Practically a Master Poster Featured Poster

You don't want your boolean method to change any of the sittings. How changing the sitting to 0 instead of 1 is not changing the sittings?

apines 116 Practically a Master Poster Featured Poster

You want your boolean method to simply check whether the class is full or not, and not change any of the sittings (meaning not to change the values of the array).

apines 116 Practically a Master Poster Featured Poster

Yes - when you make the reservation, not when you check whether it is full or not.

apines 116 Practically a Master Poster Featured Poster

return; simply exits the entire method, you don't need break;

apines 116 Practically a Master Poster Featured Poster

You can just type return; - it will exit the method;

apines 116 Practically a Master Poster Featured Poster

You need to return true in your make reservation methods as you did in the is class full methods - by using break instead of return you are breaking the inner loop only.

apines 116 Practically a Master Poster Featured Poster

One option is to keep a boolean array with a cell for each class, such that if a class is full, the representing cell in the array will become true. If all the array is true - tell the user that everything is full. Every time the user asks for a reservation, if the cell is true you don't need to check via the proper method if the class is full. If the cell is false - you need to run the method to make sure that it is still indeed false, and if all the array is true - game over, no seats.

apines 116 Practically a Master Poster Featured Poster

On the other hand Java is a good language to start from as well, to learn algorithms, design patterns, and then go to pointers and such. Different methods I guess.

apines 116 Practically a Master Poster Featured Poster

Please describe the problem, what is your approach, and what are the results so far.

apines 116 Practically a Master Poster Featured Poster

Then the least you can do is to mark this one as solved.

apines 116 Practically a Master Poster Featured Poster

No problem, please mark the thread as solved.

apines 116 Practically a Master Poster Featured Poster

LinkedHashSet<T> - This class holds a set of unique values of type 'T'.

whats the 'T' stand for? sorry for being a pain

Take a look here.

apines 116 Practically a Master Poster Featured Poster

So

if(num1 > num2) {

is correct? If I assumed correctly, we're only using Integer num1 and num2 to compare in if statement, but using node1 and node to swap? Am I on the right track in terms of swapping goes (I know the swap is wrong but I just wanted to know if I'm on right track). Thanks!

You are definitely on the right track :)