Group3 ComProg Code

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

TERMS TO DEFINE BEFORE EXPLAINING THE CODE:

 Methods meaning (Also its parts: modifiers, returntype, methodName (arguments)


 Global Variable - is one declared at the start of the code and is accessible to all parts of the program.
Since Java is object-oriented, everything is part of a class.
 Scanner – is a class of java.util package used for obtaining the input of the primitive types like
int, chars, etc. It is the easiest way of reading input in java program.
 Boolean – stores value with 2 states: True or false.
 If-else condition – executes the if block if condition is true otherwise else block is executed.
 Return statement – causes the program control to transfer back to the caller of a method.
 .length – describes how many characters must be displayed.
 If condition – executes the if block if condition is true.
 charAt – is used to find the character associated with a certain position in a string ex. Index 0, 1, 5, 8, etc.
It can also return multiple characters in a string.
 For loop – used to iterate a part of the program several times. Use this if the number of iteration is fixed.
 toUpperCase – use this to convert all the letters to upper case.

EXPLANATION: (refer to the comments in the code for guide)

1. Declares global variable so the password length (which is 8) will be applicable even outside the main.
2. Insert a String in scanner with the name password so that it will be print out and will be analyze by the if-
else condition.
3. The if-else condition is (is_Valid(password)) that uses Boolean as a primitive data type to know if the
password is true or false.
4. After coding the if-else condition, let’s open a new method with its arguments which is the String
password (public static boolean is_Valid(String password)).
5. Open an if conditional statement declaring that the length of the password inputted is less that
the password_length (which is 8 as stated by the global variable). if (password.length() <
password_length)
6. Input the charCount and numCount to make sure that the letters and numbers is indicated and present in
the password. It will be analyze b6 the pass which is the name of the char that is the one who will perform
the analyzation. Also, there is a ++ (increment) to make sure that there’s a presence of numbers and
letters. (correct me if I’m wrong)
7. Open another method with argument – Use Boolean as the returntype to determine if it is true or false. I
mean if there is a letters present.
8. Open another method with argument – Use Boolean as the returntype to determine if it is true or false. I
mean if there is a numbers present.

CODE WITH EXPLANATION:

package Sample;

//To enter any password - import scanner para gumana yung scanner sa main
import java.util.Scanner;

public class Sample {

// Number 1 in the explanation


public static final int password_length = 8;

public static void main(String args[]) {


// Open the scanner
Scanner in = new Scanner(System.in);

// Print the terms and conditions needed


System.out.println("1. A password must have at least eight characters.");
System.out.println("2. A password consists of only letters and digits.");
System.out.println("3. A password must contain at least two digits.");

System.out.println(" ");

System.out.println("Input a password (You are agreeing to the above Terms and


Conditions.):");

// Number 2
String password = in.nextLine();

// Number 3
if (is_Valid(password)) {
System.out.println("Password is Valid: " +password);
}
else {
System.out.println("Password is invalid: " +password);
}}

// Number 4
public static boolean is_Valid(String password) {

// Number 5
if (password.length() < password_length)

// return the value in the if-else statement if the password length is


way greater (correct me if I'm wrong)
return false;

// Input charCount and numCount for later in number 6


// This is just for inputting the value
int charCount = 0;
int numCount = 0;

// Use for loop and set it to zero


//This will help in analyzing each character present in the password
//i=0; i++; meaning is start from index 1 of the password then until before it
reach the length of the password
//Under this for loop is the analyzation of each characters talaga.
for (int i = 0 ; i < password.length(); i++) {

// Insert char with a name pass to call later


// This is for checking each character (each index) using charAt started
from 1 to the end of the length of the password by the help of the i (1, 2, 3)
char pass = password.charAt(i);

// Number 6
if (is_Number(pass)) numCount++;
else if (is_Letter(pass)) charCount++;
// return the value if both are not present (correct me if I'm wrong)
else return false;
}

// return the value of charCount and numCount in the if else with the
increment (?)
// this is to make sure that the Letter and Number must be greater than or
equal to 2
return (charCount >= 2 && numCount >= 2);

}
// Number 7
public static boolean is_Letter(char pass) {

// use toUpperCase to accept either small or big letters ata


pass = Character.toUpperCase(pass);
// characters must have letters
return (pass >= 'A' && pass <= 'Z');
}

// Number 8
public static boolean is_Number(char pass) {

// characters must have numbers


return (pass >= '0' && pass <= '9');

}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy