Results 1 to 10 of 10

Thread: Understand Code

  1. #1
    Quizzle23 is offline Member
    Join Date
    Feb 2011
    Posts
    34
    Rep Power
    0

    Default Understand Code

    My HW assignment is to read and understand the code below. I pretty much understand the whole thing except for a few things.
    1-How is the author able to use decimal formater with out it being declaired?
    2-What do the %d and %s mean? When %d first appears I see it is for the number 1,2,3,....etc to show after "Enter Password #1,2,3...etc" but how is that declaired and why and what is %s and how it is declaired and why?


    Java Code:
    import java.util.Scanner;
    
    public class Password
    {
    	private final int MIN_LENGTH = 6;
    	private Scanner input;
    	private String password;
    	
    	private int count;
    	private int data;
    	private int valid;
    	private String reportString;
    	private boolean lengthOK;
    	private boolean lowercaseOK;
    	private boolean uppercaseOK;
    	private boolean digitOK;
    	
    	public Password()
    	{
    		input = new Scanner(System.in);
    		count = 0;
    		data  = 0;
    		valid = 0;
    	}
    	
    	public int getNumberOfPasswords()
    	{
    		return count;
    	}
    	
    	public void startPasswordChecker()
    	{
    		System.out.print("Ready to check your passwords? ");
    		
    		System.out.print("\n1Enter the number of passwords to check: ");
    		count = input.nextInt();
    		
    		reportString = "\nHere are your results!\n";
    		
    		reportString += String.format("Passwords checked:\n");
    	}
    	
    	public void inputPassword()
    	{
    		data++;
    		
    		System.out.printf("Enter password #%d: ", data);
    		
    		password = input.next();
    		
    		reportString += String.format("  [COLOR="lime"]%d. %s[/COLOR]", data, password);
    	}
    	
    	public void checkLength()
    	{
    		lengthOK = (password.length() >= MIN_LENGTH);
    	}
    	
    	public void checkLowercase()
    	{
    		lowercaseOK = false;
    		
    		for (int i = 0; i < password.length(); i++)
    		{
    			if (Character.isLowerCase(password.charAt(i)))
    			{
    				lowercaseOK = true;
    				break;
    			}
    		}
    	}
    	
    	public void checkUppercase()
    	{
    		uppercaseOK = false;
    		
    		for (int i = 0; i < password.length(); i++)
    		{
    			if (Character.isUpperCase(password.charAt(i)))
    			{
    				uppercaseOK = true;
    				break;
    			}
    		}
    	}
    	
    	public void checkDigit()
    	{
    		digitOK = false;
    		
    		for (int i = 0; i < password.length(); i++)
    		{
    			if (Character.isDigit(password.charAt(i)))
    			{
    				digitOK = true;
    				break;
    			}
    		}
    	}
    	
    	public void reportStat()
    	{
    		if (lengthOK && lowercaseOK && uppercaseOK && digitOK)
    		{
    			reportString += "  ** valid password";
    			valid++;
    		}
    		
    		if (!lengthOK)
    			reportString += "  ** too short";
    		
    		if (!lowercaseOK)
    			reportString += "  ** doesn't contain a lowercase letter";
    			
    		if (!uppercaseOK)
    			reportString += "  ** doesn't contain a uppercase letter";
    			
    		if (!digitOK)
    			reportString += "  ** doesn't contain a digit";
    			
    		reportString += "\n";
    	}
    	
    	public void reportStatistics()
    	{
    		reportString += "Statistics:\n";
    		reportString += String.format("  Number of passwords checked: [COLOR="Lime"]%d[/COLOR]\n", count);
    		reportString += String.format("  Number of valid passwords:  [COLOR="lime"]%d[/COLOR](%.1f%%)\n", valid, (double)(valid*100)/count);
    		reportString += String.format("  Number of invalid passwords:  [COLOR="lime"]%d[/COLOR](%.1f%%)\n", count - valid, (double)((count - valid)*100)/count);
    	}
    	
    	public void printReport()
    	{
    		System.out.println(reportString);
    		System.out.println("End of report");
    	}
    	
    	public static void main(String[] args)
    	{
    		// instantiate a Password object
    		Password password = new Password();
    		
    		// call startPasswordChecker
    		password.startPasswordChecker();
    		
    		// for loop (all passwords)
    		for (int i = 0; i < password.getNumberOfPasswords(); i++)
    		{
    			// call inputPassword method
    			password.inputPassword();
    			
    			// call checkLength method
    			password.checkLength();
    			
    			// call checkLowercase method
    			password.checkLowercase();
    			
    			// call checkUppercase method
    			password.checkUppercase();
    			
    			// call checkDigit method
    			password.checkDigit();
    			
    			// call reportStat method
    			password.reportStat();
    		}
    		
    		// call reportStatistics method
    		password.reportStatistics();
    		
    		// call printReport method
    		password.printReport();
    	}
    }[LIST=1][/LIST]

    Thanks

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I don't see where he is using a decimal formatter without declaring it, paste just the part where you see this problem.

    Check out:
    Formatter (Java 2 Platform SE 5.0)

    You can also look into String.format.

  3. #3
    Quizzle23 is offline Member
    Join Date
    Feb 2011
    Posts
    34
    Rep Power
    0

    Default

    %d(%.1f%%)

    When this prints out it looks something like this:

    Percentage of Vaild Password:1 (50%)
    Percentage of Invaild Password:1 (50%)

    How does he do that?

  4. #4
    Quizzle23 is offline Member
    Join Date
    Feb 2011
    Posts
    34
    Rep Power
    0

    Default

    Ok so I see he used the String.format that makes sense. Is that what the %d is as well?

    reportString += "Statistics:\n";
    reportString += String.format(" Number of passwords checked: %d\n", count);
    reportString += String.format(" Number of valid passwords: %d(%.1f%%)\n", valid, (double)(valid*100)/count);
    reportString += String.format(" Number of invalid passwords: %d(%.1f%%)\n", count - valid, (double)((count - valid)*100)/count);

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    yup, the link I used explains it pretty in depth, but there are special characters that can be used in String.format and System.out.printf().

    for example if you see %d, it needs an int argument and it puts that int argument in the string. The use of String.format and printf can be very challenging to learn but they can offer a lot of power in how your strings look.

    here are some examples of using printf in different ways
    System.out.printf*«*java.lang*«*Java by API

    but the general format is
    Java Code:
    System.out.printf("Hello there, there is %d days left", 20);
    where it says %d you swap the 20 for the %d, so it would say
    Java Code:
    Hello there, there is 20 days left
    You could get more complex too
    Java Code:
    System.out.printf("hello there, there are %d days in this %d day month", 20, 21);
    This isn't very complex but it should print out
    Java Code:
    hello there, there are 20 days left in this 21 day month
    there is an appropriate item for each type in java(bool, float, double, etc)

    If you have a textbook or a decent java book check the index and see if you can find anything on this. It may also be in the Strings chapter. Thinking in Java has a pretty good bit on the use of this.

  6. #6
    Quizzle23 is offline Member
    Join Date
    Feb 2011
    Posts
    34
    Rep Power
    0

    Default

    Ok. Can you give me an easy way to do this? My assignment is to re-write the code. I will speak with my teacher about using the methods above we just have not learned them yet so I don't want to turn in something we have not went over.

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What's the assignment? I thought you just needed to understand the code.

  8. #8
    Quizzle23 is offline Member
    Join Date
    Feb 2011
    Posts
    34
    Rep Power
    0

    Default

    The assignment to read and understand the code and to re-write what you didn't understand in another way.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Oh alright, since you didn't understand it that way you would have to hardcode
    Java Code:
    System.out.printf("hi %d", someVal);
    Becomes
    Java Code:
    System.out.println("hi " + someVal);

  10. #10
    Quizzle23 is offline Member
    Join Date
    Feb 2011
    Posts
    34
    Rep Power
    0

    Default

    Super simple, thanks. I think I'll try doing it in JOptionPane.

Similar Threads

  1. need to understand code
    By Masken2 in forum New To Java
    Replies: 2
    Last Post: 02-17-2011, 04:21 PM
  2. understand the code
    By prof.deedee in forum New To Java
    Replies: 8
    Last Post: 11-11-2009, 02:43 AM
  3. help to understand the ? mark in this code
    By carolain79@hotmail.com in forum New To Java
    Replies: 2
    Last Post: 10-13-2009, 06:57 AM
  4. Trying to understand this code
    By new2java2009 in forum New To Java
    Replies: 2
    Last Post: 09-09-2009, 07:18 PM
  5. I don´t understand
    By Manikyr in forum New To Java
    Replies: 6
    Last Post: 02-22-2009, 11:22 PM

Posting Permissions

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