CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2016
    Posts
    1

    How can I make this code more effective?

    Just looking to touch up/clean up to make more effective. thanks for the help

    Code:
    import java.util.Scanner;
    
    public class project3 {
    	
    	public static Scanner input;
    	
    	public static void main(String[] args)
    	{
    		input =new Scanner(System.in);
    		
    		String phrase;
    		StringBuilder key;
    		
    		
    		
    		char last; 
    		
    		
    		System.out.println("What phrase would you like to encrypt?");
    		phrase=input.nextLine();
    		last = phrase.charAt(phrase.length()-1);
    		phrase=phrase.toLowerCase().replaceAll("[^a-zA-Z\\s]","");
    		phrase+=last;
    		
    		String[] words = phrase.split("[|\\s]"); 
    		 
    		key = keyGen();	 
    		
    		encrypt(words,key); 
    		
    		System.out.println();
    		
    		System.out.println("Your key is: " +key); //Print key
    		 System.out.println();
    		System.out.println("Decrypted phrase is: " +phrase);
    	}
    	 
    
    	
    	//Function to encrypt the inputed phrase
    	
    	public static void encrypt(String[] words, StringBuilder key)
    	{		
    		
    		
    			String temp;
    			char temp2; //check for vowel
    			String encrypted="";
    			
    			StringBuilder iString=new StringBuilder();
    			
    			String X1X2 =key.substring(0,2);
    			String Y1 =key.substring(3,4);
    			String X3X4 = key.substring(4,6);
    			String Z =key.substring(7,8);
    			String Y2=key.substring(8,9);
    			
    			
    			
    			System.out.println();
    			
    			int zSkip = Integer.parseInt(Z);
    			
    			for(int i=0; i<words.length; i++)
    			{
    				temp = words[i];
    			
    				temp2=temp.charAt(0);
    				
    				
    				if(temp2 == 'a' || temp2== 'e' || temp2 =='i' || temp2 =='o' || temp2=='u')
    				{
    					encrypted +=temp;
    					encrypted +=Y1 +X3X4 +" ";
    				}
    				else
    				{
    					temp=temp.substring(1);
    					temp+=temp2+X1X2;
    					encrypted +=temp +" ";
    				}
    					
    					
    			}
    		
    			
    			iString.append(encrypted);
    			
    			for(int i=zSkip; i< encrypted.length(); i+=i)
    			{
    				iString.insert(i,Y2);
    			}
    			
    	
    			System.out.println("The encrypted phrase is : "+iString);
    			
    		
    	}
    	
    	//key generator
    	public static StringBuilder keyGen()
    	{
    		int z;
    		int AZ, AZ2, AZ3, AZ4;
    	    z=randomZ(2,5);  
    		
    		 AZ=randomAZ(65,90); 
    		AZ2=randomAZ(65,90);
    		 AZ3=randomAZ(65,90);
    		 AZ4=randomAZ(65,90);
    		
    		
    		int randomS = randomSpecial(33,47); 
    		int randomS2 =randomSpecial(33,47);
    		
    		char X1 = (char)(AZ);  
    		char X2 = (char)(AZ2);
    		char X3 =(char)(AZ3);
    		char X4 =(char)(AZ4);
    		
    		char Y1 = (char)(randomS); 
    		char Y2 =(char)(randomS2);
    		
    		StringBuilder key = new StringBuilder();
    		key.append(X1);
    		key.append(X2);
    		key.append("-");
    		key.append(Y1);
    		key.append(X3);
    		key.append(X4);
    		key.append("-");
    		key.append(z);
    		key.append(Y2);
    		
    		
    		return key;
    		
    	}
    	
    	static int randomSpecial(int min, int max)
    	{
    		int range = (max-min) +1;
    		return(int)(Math.random() *range) +min;
    	}
    	
    	
    	static int randomZ(int min, int max)
    	{
    		int range =(max-min) +1;
    		return (int)(Math.random() *range ) +min;
    	} 
    	
    	static int randomAZ(int min,int max)
    	{
    		int range=(max-min) +1;
    		return (int)(Math.random() *range) +min;
    	}
    	
    	
    }

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: How can I make this code more effective?

    more effective
    Can you explain what that means? Do you mean efficient?

    What is the code supposed to do? I see very few comments in the code.
    Norm

  3. #3
    Join Date
    Sep 2016
    Location
    mumbai
    Posts
    10

    Re: How can I make this code more effective?

    1.Limit your review to the scope of the work. An effective review is laser-focused on the problem that is being worked on. Create follow-up action items (e.g., tickets) for any out-of-scope work that you think needs additional consideration.
    2.Make your review specific. Address specific lines, or components in your review. Avoid making general, sweeping statements.
    3.Make your review actionable. If it’s not immediately obvious how to implement the change you’ve requested, leave it out of your review.
    4.Deliver your review in a timely manner. The longer you wait to give your review, the more the creator will have to draw from their memory on why certain decisions may have been made. I’ve started scheduling review times with one of my co-workers so that we can go through the work together. This isn’t always possible, or desirable, but it can be effective for small teams.
    5.As part of your review, be sure to acknowledge the time spent by the coder on the issue. Sometimes a three line change can take hours and hours of work. Take the time to be a human being and acknowledge the time that’s been spent on the issue.
    6.Be thankful. Especially on open source projects where contributors are generally not *required* to submit anything. And especially at work where co-workers are sometimes forced to work on parts of the project they genuinely hate. Take the time to say “thanks”. It can make all the difference.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: How can I make this code more effective?

    Quote Originally Posted by sampada@123 View Post
    1.Limit your review to the scope of the work. An effective review is laser-focused on the problem that is being worked on. Create follow-up action items (e.g., tickets) for any out-of-scope work that you think needs additional consideration.
    2.Make your review specific. Address specific lines, or components in your review. Avoid making general, sweeping statements.
    3.Make your review actionable. If it’s not immediately obvious how to implement the change you’ve requested, leave it out of your review.
    4.Deliver your review in a timely manner. The longer you wait to give your review, the more the creator will have to draw from their memory on why certain decisions may have been made. I’ve started scheduling review times with one of my co-workers so that we can go through the work together. This isn’t always possible, or desirable, but it can be effective for small teams.
    5.As part of your review, be sure to acknowledge the time spent by the coder on the issue. Sometimes a three line change can take hours and hours of work. Take the time to be a human being and acknowledge the time that’s been spent on the issue.
    6.Be thankful. Especially on open source projects where contributors are generally not *required* to submit anything. And especially at work where co-workers are sometimes forced to work on parts of the project they genuinely hate. Take the time to say “thanks”. It can make all the difference.
    and this provides help for the OP how?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured