Author Topic: Java assignments  (Read 20806 times)

0 Members and 2 Guests are viewing this topic.

Offline Dark Nebulae

  • Peasant
  • *
  • Posts: 117
  • Cookies: -79
  • Unleash the Hacker within you
    • View Profile
Re: Java assignments
« Reply #30 on: December 13, 2012, 12:53:52 pm »
Yes, many times.  And since I don't have my main computer running right now, I just copied and pasted the code I posted into a new project on this computer.  My code works perfectly.  I have no idea what you're talking about.  Care to elaborate on what error you're getting, or how it doesn't work?
When I ran this program,Here is what I got 8) 8)
Code: [Select]
Enter the string you would like to cipher:
abc
Enter the amount you would like to shift the string by:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1516)
at Main.getInput(Main.java:34)
at Main.main(Main.java:25)
Java Result: 1
Trust is like a piece of paper.Once it is crumbled,it can never be perfect.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java assignments
« Reply #31 on: December 13, 2012, 01:21:08 pm »
When I ran this program,Here is what I got 8) 8)
Code: [Select]
Enter the string you would like to cipher:
abc
Enter the amount you would like to shift the string by:
Exception in thread "main" java.util.NoSuchElementException: No line found
   at java.util.Scanner.nextLine(Scanner.java:1516)
   at Main.getInput(Main.java:34)
   at Main.main(Main.java:25)
Java Result: 1

That's because of the stream closing. See my last post.
___________________________________________________

@Mr. Perfect: When you are done, try to answer the questions in preparation for assignment 4. I added one question and gave some help there.

You don't have to know how to code it in order to answer them. I will help you with the implementation once you have a plan in your mind.

I will also create an encrypted message that you can use for testing. (Will add it in the first post then)

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Re: Java assignments
« Reply #32 on: December 15, 2012, 07:55:59 am »
At last, my assignment 3.
Code: [Select]
/**
 * Assignment 3
 * @author Mr. Perferct
 */
import java.util.Scanner;

public class Assignment_3 {
   
    private static Scanner input = new Scanner(System.in);
   
    public static void main(String[] args) {
        while (true) {
            System.out.println("Your input?");
            String userinput = userInput();
            if (userinput.equalsIgnoreCase("quit")
                    || userinput.equalsIgnoreCase("exit")) {
                break;
            }
            System.out.println("Enter the amount you want to move the cipher?");
            int move = amount();
            System.out.print("The encrypted string is: ");
            System.out.println(encrypt(userinput, move));
        }
        input.close();
    }

    public static String userInput() {
        String userinput = input.nextLine();
        return userinput;
    }

    private static int amount() {
        String p = input.nextLine();
        int places = Integer.parseInt(p);
        return places;
    }

    private static String encrypt(String input, int places) {
          char[] charArray = input.toCharArray();
        for(int index = 0; index < charArray.length; index++)
        {
           
           
            charArray[index] += places;
           
        }
        String s = new String(charArray);
        return s;
    }
}
« Last Edit: December 15, 2012, 12:58:36 pm by Mr. Perfect »

Offline Dark Nebulae

  • Peasant
  • *
  • Posts: 117
  • Cookies: -79
  • Unleash the Hacker within you
    • View Profile
Re: Java assignments
« Reply #33 on: December 15, 2012, 07:59:56 am »
At last, my assignment 3.
Code: [Select]
/**
 * Assignment 3
 * @author Mr. Perferct
 */
import java.util.Scanner;

public class Assignment_3 {
   
    private static Scanner input = new Scanner(System.in);
   
    public static void main(String[] args) {
        while (true) {
            System.out.println("Your input?");
            String userinput = userInput();
            if (userinput.equalsIgnoreCase("quit")
                    || userinput.equalsIgnoreCase("exit")) {
                break;
            }
            System.out.println("Enter the amount you want to move the cipher?");
            int move = amount();
            System.out.print("The encrypted string is: ");
            System.out.println(encrypt(userinput, move));
        }
        input.close();
    }

    public static String userInput() {
        String userinput = input.nextLine();
        return userinput;
    }

    private static int amount() {
        String p = input.nextLine();
        int places = Integer.parseInt(p);
        return places;
    }

    private static String encrypt(String input, int places) {
          char[] charArray = input.toCharArray();
        for(int index = 0; index < charArray.length; index++)
        {
            if(places < 0)
            {
                charArray[index]=(char) (126 + places);
            }else{
            charArray[index] += places;
            }
        }
        String s = new String(charArray);
        return s;
    }
}
Well Mr. Perfect, this code looks perfect.You are a good learner. +1 for that spirit.
Trust is like a piece of paper.Once it is crumbled,it can never be perfect.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java assignments
« Reply #34 on: December 16, 2012, 07:40:08 am »
At last, my assignment 3.
Code: [Select]
/**
 * Assignment 3
 * @author Mr. Perferct
 */
import java.util.Scanner;

public class Assignment_3 {
   
    private static Scanner input = new Scanner(System.in);
   
    public static void main(String[] args) {
        while (true) {
            System.out.println("Your input?");
            String userinput = userInput();
            if (userinput.equalsIgnoreCase("quit")
                    || userinput.equalsIgnoreCase("exit")) {
                break;
            }
            System.out.println("Enter the amount you want to move the cipher?");
            int move = amount();
            System.out.print("The encrypted string is: ");
            System.out.println(encrypt(userinput, move));
        }
        input.close();
    }

    public static String userInput() {
        String userinput = input.nextLine();
        return userinput;
    }

    private static int amount() {
        String p = input.nextLine();
        int places = Integer.parseInt(p);
        return places;
    }

    private static String encrypt(String input, int places) {
          char[] charArray = input.toCharArray();
        for(int index = 0; index < charArray.length; index++)
        {
           
           
            charArray[index] += places;
           
        }
        String s = new String(charArray);
        return s;
    }
}

Try to put the bounds in. Do you need more help with it?

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Re: Java assignments
« Reply #35 on: December 16, 2012, 11:56:40 am »
Try to put the bounds in. Do you need more help with it?
Here is my bounded program.
Code: [Select]
/**
 * Assignment 3
 * @author Mr. Perferct
 */
import java.util.Scanner;

public class Assignment_3 {
   
    private static Scanner input = new Scanner(System.in);
   
    public static void main(String[] args) {
        while (true) {
            System.out.println("Your input?");
            String userinput = userInput();
            if (userinput.equalsIgnoreCase("quit")
                    || userinput.equalsIgnoreCase("exit")) {
                break;
            }
            System.out.println("Enter the amount you want to move the cipher?");
            int move = amount();
            System.out.print("The encrypted string is: ");
            System.out.println(encrypt(userinput, move));
        }
        input.close();
    }

    public static String userInput() {
        String userinput = input.nextLine();
        return userinput;
    }

    private static int amount() {
        String p = input.nextLine();
        int places = Integer.parseInt(p);
        return places;
    }

    private static String encrypt(String input, int places) {
          char[] charArray = input.toCharArray();
        for(int index = 0; index < charArray.length; index++)
        {
            charArray[index] += places;
           
             if((int)(charArray[index]) > 126)
            {
             charArray[index]=(char)((int)(charArray[index]) % 126);
             charArray[index]+=32;
            }   
        }
        String s = new String(charArray);
        return s;
    }
}

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java assignments
« Reply #36 on: December 17, 2012, 09:13:53 am »
That's very good. You got the upper bounds right. Now do the lower bounds in case the user enters a negative shift (if shifted character < 32 do ...)

If you want to you can also add a decrypt method and give the user the choice to de- or encrypt the message.

That would be additional, you may also proceed trying to find a vulnerability that can help you to crack a message. You already told me about pattern and that is right. But which pattern is it? What is the difference between a random text and a shift-encrypted message (take a long message as given, i.e. 200 words)?
« Last Edit: December 17, 2012, 09:23:03 am by Deque »

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Re: Java assignments
« Reply #37 on: December 23, 2012, 07:57:21 am »
Here is my program with lower bounds.
Code: (Java) [Select]
/**
 * Assignment 3
 * @author Mr. Perferct
 */
import java.util.Scanner;

public class Assignment_3 {
   
    private static Scanner input = new Scanner(System.in);
   
    public static void main(String[] args) {
        while (true) {
            System.out.println("Your input?");
            String userinput = userInput();
            if (userinput.equalsIgnoreCase("quit")
                    || userinput.equalsIgnoreCase("exit")) {
                break;
            }
            System.out.println("Enter the amount you want to move the cipher?");
            int move = amount();
            System.out.print("The encrypted string is: ");
            System.out.println(encrypt(userinput, move));
        }
        input.close();
    }

    public static String userInput() {
        String userinput = input.nextLine();
        return userinput;
    }

    private static int amount() {
        String p = input.nextLine();
        int places = Integer.parseInt(p);
        return places;
    }

    private static String encrypt(String input, int places) {
          char[] charArray = input.toCharArray();
        for(int index = 0; index < charArray.length; index++)
        {
            charArray[index] += places;
           
             if((int)(charArray[index]) > 126)
            {
             charArray[index]=(char)((int)(charArray[index]) % 126);
             charArray[index]+=32;
            } 
          else if((int)(charArray[index]) < 32){
             charArray[index]=(char)((int)(charArray[index] % 32));
               charArray[index]-=126;

}
        }
        String s = new String(charArray);
        return s;
    }
}

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java assignments
« Reply #38 on: December 26, 2012, 02:44:51 pm »
Here is my program with lower bounds.
Code: (Java) [Select]
/**
 * Assignment 3
 * @author Mr. Perferct
 */
import java.util.Scanner;

public class Assignment_3 {
   
    private static Scanner input = new Scanner(System.in);
   
    public static void main(String[] args) {
        while (true) {
            System.out.println("Your input?");
            String userinput = userInput();
            if (userinput.equalsIgnoreCase("quit")
                    || userinput.equalsIgnoreCase("exit")) {
                break;
            }
            System.out.println("Enter the amount you want to move the cipher?");
            int move = amount();
            System.out.print("The encrypted string is: ");
            System.out.println(encrypt(userinput, move));
        }
        input.close();
    }

    public static String userInput() {
        String userinput = input.nextLine();
        return userinput;
    }

    private static int amount() {
        String p = input.nextLine();
        int places = Integer.parseInt(p);
        return places;
    }

    private static String encrypt(String input, int places) {
          char[] charArray = input.toCharArray();
        for(int index = 0; index < charArray.length; index++)
        {
            charArray[index] += places;
           
             if((int)(charArray[index]) > 126)
            {
             charArray[index]=(char)((int)(charArray[index]) % 126);
             charArray[index]+=32;
            } 
          else if((int)(charArray[index]) < 32){
             charArray[index]=(char)((int)(charArray[index] % 32));
               charArray[index]-=126;

}
        }
        String s = new String(charArray);
        return s;
    }
}

Still not working right. Why do you do (charArray[index] % 32)? That doesn't make sense.

You still have an off-by-one-error in the upper bounds part. Imagine I use ~ as input and 0 shift. By doing (charArray[index]) % 126) this will result into 0. You add 32 afterwards, which will result into the space character. That means you have shifted it by 1.

I appreciate that you try very hard. But I don't want to give you the solution. I will however give you a similar one that I wrote. It is not exactly the same, so it will train you in reading code of others. This shift cipher is for letters A-Z (bounds are 65-90). It ignores whitespaces (which is not your task) and every letter a-z is made to an uppercase one before shifted (which is also not your task).

Code: (Java) [Select]
public String encrypt(String plainText, String key) {
        plainText = prepareString(plainText);
        StringBuffer code = new StringBuffer();
        int shift = parseKeyToInt(key);
   
        for (char c : plainText.toCharArray()) {
            if (Character.isWhitespace(c)) {
                code.append(c);
            } else {
                int s = ((c % 'A' + shift) % 26);
                if (s > 25) {
                    s = s - 26;
                } else if (s < 0) {
                    s = s + 26;
                }
                code.append((char) (s + 'A'));
            }
   
        }
        return code.toString();
}

private String prepareString(String code) {
        code = code.toUpperCase();
        code = code.replaceAll("[^A-Z\\s]", "");
        return code;
}

private int parseKeyToInt(String key) {
        int number;
        try {
            number = Integer.parseInt(key);
        } catch (NumberFormatException e) {
            number = 0;
        }
        return number;
}


Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Re: Java assignments
« Reply #39 on: January 21, 2013, 05:45:16 am »
Here is the modified program.  :)
Code: (Java) [Select]

/**
 * Assignment 3
 * @author Mr. Perferct
 */
import java.util.Scanner;
 
public class Assignment {
 
    private static Scanner input = new Scanner(System.in);
 
    public static void main(String[] args) {
        while (true) {
            System.out.println("Your input?");
            String userinput = userInput();
            if (userinput.equalsIgnoreCase("quit")
                    || userinput.equalsIgnoreCase("exit")) {
                break;
            }
           try{
            System.out.println("Enter the amount you want to move the cipher?");
            int move = amount();
            System.out.print("The encrypted string is: ");
            System.out.println(encrypt(userinput, move));
           }catch(NumberFormatException e)
           {
               System.out.println("You entered a wrong key! \n The key should be in the integer format.");
           }
        }
        input.close();
    }
 
    public static String userInput() {
        String userinput = input.nextLine();
        return userinput;
    }
 
    private static int amount() {
        String p = input.nextLine();
        int places = Integer.parseInt(p);
        return places;
    }
 
    private static String encrypt(String input, int places) {
        char[] charArray = input.toCharArray();
        int upperBound = 126;
        int lowerBound = 32;
        for (int index = 0; index < charArray.length; index++) {
           if (!Character.isWhitespace(charArray[index])){
            int c = charArray[index];
            c += places;
 
            if (c > upperBound) {
                c -= lowerBound;
                c = c % (upperBound - lowerBound);
                c += lowerBound;
            } else {
                while (c < 32) {
                    c = c + (upperBound - lowerBound);
                }
            }
            charArray[index] = (char) c;
           }
        }
        String s = new String(charArray);
        return s;
    }
}

Deque, I've edited the program to handle exceptions and if there is any whitespace,then it will leave it as it is. :)
« Last Edit: January 21, 2013, 06:00:59 am by Mr. Perfect »

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java assignments
« Reply #40 on: January 21, 2013, 10:59:44 am »
Here is the modified program.  :)
Code: (Java) [Select]

/**
 * Assignment 3
 * @author Mr. Perferct
 */
import java.util.Scanner;
 
public class Assignment {
 
    private static Scanner input = new Scanner(System.in);
 
    public static void main(String[] args) {
        while (true) {
            System.out.println("Your input?");
            String userinput = userInput();
            if (userinput.equalsIgnoreCase("quit")
                    || userinput.equalsIgnoreCase("exit")) {
                break;
            }
           try{
            System.out.println("Enter the amount you want to move the cipher?");
            int move = amount();
            System.out.print("The encrypted string is: ");
            System.out.println(encrypt(userinput, move));
           }catch(NumberFormatException e)
           {
               System.out.println("You entered a wrong key! \n The key should be in the integer format.");
           }
        }
        input.close();
    }
 
    public static String userInput() {
        String userinput = input.nextLine();
        return userinput;
    }
 
    private static int amount() {
        String p = input.nextLine();
        int places = Integer.parseInt(p);
        return places;
    }
 
    private static String encrypt(String input, int places) {
        char[] charArray = input.toCharArray();
        int upperBound = 126;
        int lowerBound = 32;
        for (int index = 0; index < charArray.length; index++) {
           if (!Character.isWhitespace(charArray[index])){
            int c = charArray[index];
            c += places;
 
            if (c > upperBound) {
                c -= lowerBound;
                c = c % (upperBound - lowerBound);
                c += lowerBound;
            } else {
                while (c < 32) {
                    c = c + (upperBound - lowerBound);
                }
            }
            charArray[index] = (char) c;
           }
        }
        String s = new String(charArray);
        return s;
    }
}

Deque, I've edited the program to handle exceptions and if there is any whitespace,then it will leave it as it is. :)

Great. Now add a decrypt-method too. (shouldn't be more than one line in the body ;) )
Do you want to do assignment 4 or go ahead to GUI programming?
In the latter case I would like to see your GUI code to give you suggestions for your architecture.

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Re: Java assignments
« Reply #41 on: January 21, 2013, 01:48:41 pm »
Made a bit more awesome for n00bs. ;) Couldn't do it in one line. :(
Let's do the 4th assignment.For the GUI,what about the design I made for my Text Encrypter/Decrypter?
Here's the 3rd assignment. ;D
Code: (Java) [Select]

/**
 * Assignment 3
 * @author Mr. Perferct
 */
import java.util.Scanner;
 
public class Assignment {
 
    private static Scanner input = new Scanner(System.in);
 private static  Scanner hello = new Scanner(System.in);
    public static void main(String[] args) {
               while (true) {
                   Scanner hello = new Scanner(System.in);
                    System.out.println("Advanced Text Encrypter/Decrypter");
                    System.out.println("What do you want to do: \n1.)Encrypt \n2.)Decrypt ");
                    System.out.println("Enter your choice: ");
                    int choice = hello.nextInt();
            System.out.println("Your input?");
            String userinput = userInput();
            if (userinput.equalsIgnoreCase("quit")
                    || userinput.equalsIgnoreCase("exit")) {
                break;
            }
           try{
            System.out.println("Enter the amount you want to move the cipher?");
            int move = amount();
            if (choice == 1){
            System.out.print("The encrypted string is: ");           
            System.out.println(encrypt(userinput, move));}
            else {
            System.out.print("The decrypted string is: ");           
            System.out.println(encrypt(userinput, move));   
            }
           }catch(NumberFormatException e)
           {
               System.out.println("You entered a wrong key! \n The key should be in the integer format.");
           }
        }
        input.close();
        hello.close();
    }
 
    public static String userInput() {
        String userinput = input.nextLine();
        return userinput;
    }
 
    private static int amount() {
        String p = input.nextLine();
        int places = Integer.parseInt(p);
        return places;
    }
 
    private static String encrypt(String input, int places) {
        char[] charArray = input.toCharArray();
        int upperBound = 126;
        int lowerBound = 32;
        for (int index = 0; index < charArray.length; index++) {
           if (!Character.isWhitespace(charArray[index])){
            int c = charArray[index];
            c += places;
 
            if (c > upperBound) {
                c -= lowerBound;
                c = c % (upperBound - lowerBound);
                c += lowerBound;
            } else {
                while (c < 32) {
                    c = c + (upperBound - lowerBound);
                }
            }
            charArray[index] = (char) c;
           }
        }
        String s = new String(charArray);
        return s;
    }
}
« Last Edit: January 21, 2013, 01:54:27 pm by Mr. Perfect »

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java assignments
« Reply #42 on: January 21, 2013, 07:02:40 pm »
Made a bit more awesome for n00bs. ;) Couldn't do it in one line. :(
Let's do the 4th assignment.For the GUI,what about the design I made for my Text Encrypter/Decrypter?
Here's the 3rd assignment. ;D

I meant that you should make a method for decryption. But I am ok with this too. But shouldn't you call:
Code: (Java) [Select]
encrypt(userinput, -move)for decryption (note the minus)?

About the GUI:
That's what I asked you about: Do you want to get feedback for the GUI part now or do you want to do Assignment 4? I'd rather concentrate on one thing at a time.
So either I give you feedback for the GUI first, or I help you with assignment 4 (we can do the GUI part later of course).