• Log in or sign up for an account here: https://www.sythe.org/login

Java help

MohtasaUnique

Grand Master
Joined
Sep 1, 2007
Sythe Gold
690
Messages
6,681
Java help
Below I have the program as I've made it so far. Now I have to change it to check if vectors are palindromatic instead of a string input. For example:
6fKg1.png

First one is a palindrome, whereas the second isn't.
It HAS to use stacks and queues. I honestly have no idea what the fuck vectors are or how to use them in programming. So halp plz :c

PHP:
package project4;
// Import
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class Project4
{
    public static void main(String[] args) throws IOException
    {
        // Open and read input file
        String fileName = "input.txt";
        
        BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
        String line;
        
        while((line = bufferedReader.readLine()) != null)
        {
            // Return appropriate evaluation of the string
            System.out.print("Reading from file...\n");
            if (is_palindrome(line))// If true, it's a palindrome
                System.out.println("That is a palindrome.");
            else // otherwise, it's not
                System.out.println("That is not a palindrome.");
        }
        
        bufferedReader.close();
    }
    
    public static boolean is_palindrome(String input)
    {
        // Make a new queue, stack, and character object
        Queue<Character> q = new LinkedList<>();
        Stack<Character> s = new Stack<>();
        Character letter;
        int mismatches = 0;
        int i;
        
        for (i = 0; i < input.length(); i++)
        {
            letter = input.charAt(i);
            if (Character.isLetter(letter))
            {
                q.add(letter);
                s.push(letter);
            }
        }
        
        while (!q.isEmpty())
        {
            if (q.remove() != s.pop())
                mismatches++; // Increment "mismatches" if q != s
        }
        
        return (mismatches == 0); // will return true if the method did not change the variable "mismatches"
    }
}
 
Java help
An n-dimensional vector object is just a group of numerical values that is n items long, either horizontally or vertically. In all honesty they can be represented as simple arrays, although Java has a Vector class predefined just for conventional purposes.

So if what I understand is correct, you're reading a list of vectors to check if the LIST is a palindrome, not the actual vector?

A stack reads from a top to bottom / FILO basis (First In Last Out, like a stack of plates, you have to take the top one first and the first one put down is the last to be picked up) whereas a queue works in a FIFO basis (First In First Out, as if the plates were on a conveyor belt at a restaurant).

Based on this, I would create a queue and stack of each Vector object in your list of vectors. In your logic, all you have to do is check that the last item in the stack (the top plate) is equal to the first item in the queue (the first plate on the conveyor), the second-last item on the stack is equal to the second in the queue and so on, so forth and if that logic is true for every item in your stack and queue, then your list of vectors is a palindrome.

If your list of vectors is NOT a palindrome, all you need to do is break from the loop and return that it is not a palindrome. You can check that two vectors equal each other with the Vector.equals() method, e.g.:

Code:
if(v1.equals(v2)) doShit();

Here is the documentation for the Vector class: http://docs.oracle.com/javase/7/docs/api/java/util/Vector.html
 
Java help
PHP:
// Tony White
// CS301
// Project4

package project4;
// Import
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
import java.util.Vector;

public class Project4
{
    public static void main(String[] args) throws IOException
    {
        Vector v1 = new Vector(3);
        Vector v2 = new Vector(3);
        Vector v3 = new Vector(3);
        Vector v4 = new Vector(3);
        ArrayList vectors = new ArrayList();
        v1.add(1);
        v1.add(2);
        v1.add(3);
        v2.add(1);
        v2.add(1);
        v2.add(1);
        v3.add(1);
        v3.add(1);
        v3.add(1);
        v4.add(1);
        v4.add(2);
        v4.add(3);
        vectors.add(v1);
        vectors.add(v2);
        vectors.add(v3);
        vectors.add(v4);
        if (isPalindrome(vectors))// If true, it's a palindrome
            System.out.println("That is a palindrome.");
        else // otherwise, it's not
            System.out.println("That is not a palindrome.");
        
    }
    public static boolean isPalindrome(ArrayList vectors)
    {
        // Make a new queue, stack, and character object
        Queue<Vector> q = new LinkedList<>();
        Stack<Vector> s = new Stack<>();
        for(int i = 0; i < vectors.size(); i++){
            q.add((Vector) vectors.get(i));
            s.add((Vector) vectors.get(i));
        }
        Vector temp = new Vector();
        int mismatches = 0;
        for (int i = 0; i < vectors.size(); i++)
        {
            temp = (Vector) vectors.get(i);
            if (vectors.get(i).equals(vectors.get(i)))
            {
                q.add(temp);
                s.push(temp);
            }
        }
        
        while (!q.isEmpty())
        {
            if (q.remove() != s.pop())
                mismatches++; // Increment "mismatches" if q != s
        }
        
        return (mismatches == 0); // will return true if the method did not change the variable "mismatches"
    }
}

I've got the following code, but I don't think I've put the vectors in right because I can't get anything to print out, and I think all it's doing is comparing the address of the vector objects, and not the actual numbers inside which will always evaluate to false...
 
Java help
Here's one that I wrote myself half-based on your code using regular arrays instead of the vector object. Should help you to understand what to do.

Code:
import java.util.*;

public class vectorpalindromes
{
    public static void main(String[] args)
    {
        // Create list of vectors
        int[][] vectors = {
                {1,2,3},
                {1,1,1},
                {1,2,3}
        };

        int[][] vectors2 = {
                {2,2,2},
                {1,3,1},
                {5,1,1},
                {1,2,3}
        };

        isPalindrome(vectors);
        isPalindrome(vectors2);
    }

    public static boolean isPalindrome(int[][] vectors)
    {
        // Make a new queue, stack, and character object
        Queue<int[]> q = new LinkedList<int[]>();
        Stack<int[]> s = new Stack<int[]>();

        // Note: Checking from start and end of array at same time.
        //       The check will meet at the middle of the array. After that
        //       there is no point continuing.

        // Length of loop
        int vlength = vectors.length / 2;
        // Is the array odd-length or even-length
        boolean divisible = (vectors.length % 2 == 0);
        if(!divisible)
            vlength++; // Allows the loop to go past halfway if odd

        for(int i = 0; i<vlength; i++) {
            // This logic allows for odd-lengthed palindromes
            boolean loop = true;
            // Used this to check something. Java rounds UP for ints if you divide an odd int by 2.
            //System.out.println(vlength);

            // Check the remainder of a division by two. If it is greater than 0 (i.e. not divisible by 2)
            //      and if the loop is currently at the middle of the array, then add the middle element
            //      to the stack and queue and break the loop.

            if(!divisible && i == (vlength-1))
            {
                q.add(vectors[i]);
                s.add(vectors[vlength]);
                loop = false;
            }
            if(!loop) break;

            q.add(vectors[i]);

            // Add to the stack from the middle of the array
            // As that is where the comparison will terminate.
            if(divisible)   // If divisible by 2
                s.add(vectors[vlength + (i)]);
            else    // If odd, subtract 1 due to previous logic.
                s.add(vectors[vlength+(i-1)]);
        }

        System.out.println("Checking vectors ...");
        int mismatches = 0;

        for (int i = 0; i < vlength; i++)
        {
            System.out.println();
            // Queue.poll() and Stack.pop() are the methods used to get each element
            // From the queue and stack respectively. Remember to look at the documentation
            int[] qv = q.poll(), sv = s.pop();
                System.out.print(String.format("{%d,%d,%d} vs {%d,%d,%d}",
                        qv[0], qv[1], qv[2],
                        sv[0], sv[1], sv[2]
                ));

            // This is how you check if two arrays are equal.
            if(Arrays.equals(qv,sv))
                continue; // Skip to next iteration of the loop

            System.out.print(" --mismatch!");
            // Increase mismatch count if arrays aren't equal
            mismatches += 1;
        }
        System.out.println();

        // Print the amount of mismatches
        System.out.println(String.format("%d mismatches", mismatches));
        boolean palindrome = mismatches == 0;
        if(palindrome)
            System.out.println("This is a palindrome.");
        else
            System.out.println("This is not a palindrome.");

        return (palindrome); // will return true if the method did not change the variable "mismatches"    }

    }
}

Edits: Upgraded it a bit. What I mean by "the comparison ends at the middle of the array" is that stacks are FILO and queues are FIFO so the comparisons are technically checking opposite ends of the array.

I included support for vector lists with odd lengths as well. It's a bit of a hack but I'm tired and lazy :P

Here's the console output:
Code:
Checking vectors ...

{1,2,3} vs {1,2,3}
{1,1,1} vs {1,1,1}
0 mismatches
This is a palindrome.
Checking vectors ...

{2,2,2} vs {1,2,3} --mismatch!
{1,3,1} vs {5,1,1} --mismatch!
2 mismatches
This is not a palindrome.

Process finished with exit code 0
 
Java help
Sorry, I never thanked you for your help, thanks swan c:
 
Runescape Gamblingrunescape gambling
Back
Top