Any way to make my program simpler?

Discussion in 'Programming General' started by Snow Fury, Feb 4, 2012.

Any way to make my program simpler?
  1. Unread #1 - Feb 4, 2012 at 1:15 AM
  2. Snow Fury
    Joined:
    Sep 10, 2011
    Posts:
    165
    Referrals:
    1
    Sythe Gold:
    0

    Snow Fury Active Member
    $5 USD Donor New

    Any way to make my program simpler?

    Hey I've been working on an encryption program just for fun and I need to know a way to make my code easier to read or if there is a simpler way to do this. I tried using a switch but I couldn't get that to work and I don't want to do a big line of if statements for the whole entire alphabet.

    Here's my code:
     
  3. Unread #2 - Feb 4, 2012 at 9:16 AM
  4. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    Any way to make my program simpler?

    You could change all of the replace() function calls to replaceAll() so that it replaces all occurrences of that character. This would remove the need of the for loops iterating over the string length. However, it would still be wildly inefficient because replaceAll() is a regular expression search and thus inherently slower than just a plain string search.

    Since you did not make all of your encrypted versions the same number of characters as each other it is quite complex to make the decryption any faster than just using replaceAll() method calls.

    Encryption can be made efficient because every input is the same length (one character). Your best bet is probably be iterating over the string one character at a time and looking up its encrypted version in an array. Then, adding the corresponding encrypted character(s) to an output string (using StringBuilder for even more efficiency).

    Here's some code on how to do encryption. I did not compile this example but it should give you an idea, feel free to ask more questions. This only works for lowercase but it can be adapted to handle other types of characters with just a couple extra if statements.
    Code:
    // Lookup table for encrypting characters
    String[] encrypted = new String[] { "%3", "!83&~",  ".2", "!83&`" }
    StringBuilder output = new StringBuilder();
    for(i=0; i<s.length(); i++){
       // If the table has an encrypted version of this character add it to output
       if(s.charAt(i) - 'a' < encrypted.length) {
          output.append(encrypted[s.charAt(i)-'a']);
       }
    }
    return output.toString();
    
     
  5. Unread #3 - Feb 4, 2012 at 10:09 AM
  6. Snow Fury
    Joined:
    Sep 10, 2011
    Posts:
    165
    Referrals:
    1
    Sythe Gold:
    0

    Snow Fury Active Member
    $5 USD Donor New

    Any way to make my program simpler?

    I actually do have a few more questions I'm trying to prepare for my upcoming test for my Comp Sci course and my text books I downloaded aren't helping...

    These are some of the things I need to know:
    Construct switch blocks:
    Is that a switch block?

    Explain why non-static variables cannot be referenced from a static context:
    I don't know the differences between static variables and non-static, could you explain please?






    Yea that's all that I need help on really and incase you wanted to know what else is on the test is here:
     
  7. Unread #4 - Feb 4, 2012 at 1:18 PM
  8. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    Any way to make my program simpler?

    I added you on MSN. Can answer more on there.
     
  9. Unread #5 - Feb 13, 2012 at 8:27 PM
  10. Omer
    Joined:
    Feb 8, 2012
    Posts:
    19
    Referrals:
    0
    Sythe Gold:
    0

    Omer Newcomer

    Any way to make my program simpler?

    One thing that you really should do is indent...
     
  11. Unread #6 - Feb 17, 2012 at 12:26 AM
  12. Snow Fury
    Joined:
    Sep 10, 2011
    Posts:
    165
    Referrals:
    1
    Sythe Gold:
    0

    Snow Fury Active Member
    $5 USD Donor New

    Any way to make my program simpler?

    I do indent, its just when the copied pasted it over, it ruined all the indentations
     
  13. Unread #7 - Feb 17, 2012 at 8:00 AM
  14. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    Any way to make my program simpler?

    Use code blocks not quote blocks to preserve indentation.
    Code:
    for name in names:
       print name
     
< [.NET][JAVA]RSAPI - A Client Developers API | I need help for my computer science class >

Users viewing this thread
1 guest


 
 
Adblock breaks this site