Created
August 7, 2023 15:23
-
-
Save edwinteo96/87cf5e4302856e2a0efe8bd1eef02819 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license | |
| * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template | |
| */ | |
| package javaapplication1; | |
| import java.util.Scanner; | |
| /** | |
| * | |
| * @author jiahan.teo | |
| */ | |
| public class CheckPassword { | |
| public static void main(String[] args) { | |
| Scanner scanner = new Scanner(System.in); | |
| System.out.println("Enter a password:"); | |
| String password = scanner.nextLine(); | |
| boolean valid = true; | |
| if (password.length() > 7) { | |
| // do something | |
| int countLetter = 0; | |
| int countDigit = 0; | |
| for (int i = 0; i < password.length(); i++) { | |
| // input : password | |
| // char = 'p', 'a' or 's', 's' .... | |
| char ch = password.charAt(i); | |
| if (Character.isLetter(ch)) { | |
| countLetter++; | |
| } else if (Character.isDigit(ch)) { | |
| countDigit++; | |
| } else { | |
| valid = false; | |
| } | |
| } | |
| // OR checking | |
| if (countLetter == 0 || countDigit == 0) { | |
| valid = false; | |
| } | |
| } else { | |
| valid = false; | |
| } | |
| // boolean - true /false | |
| if (valid) { | |
| System.out.println("Valid Password"); | |
| } else { | |
| System.out.println("Invalid Password"); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license | |
| * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template | |
| */ | |
| package javaapplication1; | |
| /** | |
| * | |
| * @author jiahan.teo | |
| */ | |
| public class P5Q5 { | |
| public static void main (String [] args ) { | |
| // P4 Q3 , static contributionForQuiz -> static method, can be anywhere in the program , shared function | |
| Student student1 = new Student("112345", "John Lim", "FASC"); | |
| System.out.println("Is student valid ? " + Student.validateStudentID(student1)); | |
| System.out.println(student1.toString()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license | |
| * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template | |
| */ | |
| package javaapplication1; | |
| /** | |
| * | |
| * @author jiahan.teo | |
| */ | |
| public class Student { | |
| private String studentID; | |
| private String name; | |
| private String school; | |
| // - studentID: String , - meaning private field | |
| public Student(String studentID, String name, String school) { | |
| this.studentID = studentID; | |
| this.name = name; | |
| this.school = school; | |
| } | |
| public static boolean validateStudentID(Student student) { | |
| String studentId = student.getStudentID(); | |
| char firstCharacter = Character.toUpperCase(studentId.charAt(0)); | |
| // A12345 | |
| String registrationNumber = studentId.substring(1, studentId.length()); | |
| // negate function | |
| if (!Character.isLetter(firstCharacter)) { | |
| return false; | |
| } | |
| // negate function ( terbalik/ opposite) | |
| for (int i = 0; i < registrationNumber.length(); i++) { | |
| if (!Character.isDigit(registrationNumber.charAt(i))) { | |
| return false; | |
| } | |
| } | |
| boolean valid = false; | |
| switch (firstCharacter) { | |
| case 'A': | |
| valid = student.getSchool().equals("FASC"); | |
| break; | |
| case 'B': | |
| valid = student.getSchool().equals("FAFB"); | |
| break; | |
| } | |
| return valid; | |
| } | |
| @Override | |
| public String toString() { | |
| return "Student{" + "studentID=" + studentID + ", name=" + name + ", school=" + school + '}'; | |
| } | |
| public String getStudentID() { | |
| return studentID; | |
| } | |
| public void setStudentID(String studentID) { | |
| this.studentID = studentID; | |
| } | |
| public String getName() { | |
| return name; | |
| } | |
| public void setName(String name) { | |
| this.name = name; | |
| } | |
| public String getSchool() { | |
| return school; | |
| } | |
| public void setSchool(String school) { | |
| this.school = school; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment