Review
Branches
Branches are conditional statements that allow blocks of code to be executed depending on the truth value of a statement.
We see these statements in multiple forms, like if, if-else, if-else if-else, and switch statements.
Classes
Classes are the blueprints of Java. They are a structure that holds variables and associated methods. From them, we can create, or instantiate, objects. If classes are the blueprints, objects are the buildings. Many buildings can be made from one blueprint, and they may have different properties, but they still have the same core structure.
Class Variables
Class variables exist outside of any method, including main. They are associated with the instantiation of the class, so if multiple objects are created from one class, their variables will have different values.
These variables can, like methods, be declared as public or private. If they are declared as private, they will not be visible to any other class or method.
Class variables also are often accessed or changed by accessor and mutator methods, also known as “getters” and “setters”. Sometimes, a class variable needs to be protected from changes or certain classes so it is marked as private, but it still may need to be accessed by a method or class. This is where accessor methods come in handy, as they exist within the class to return the value of the class variable. Mutator methods can change the class variable values.
Constructors
Constructors are a special kind of method that exist within a class to initialize a newly instantiated object. If one is not defined in a class, there is an implicit no-args constructor which means that the compiler knows to create the object, and it doesn’t need to do anything extra.
Constructors can take parameters in just like any other method. Often times, constructors initialize the class variables based on the parameters, or on default values.
“This” Keyword
Say you were defining a constructor for a class named Dinosaur and wanted to initialize a class variable called name from a parameter in the constructor. It might look something like this:
public class Dinosaur{
String name;
public Dinosaur(String name){
name = name;
}
}
The variable name exists as a class variable and an argument, so how does the compiler know which one you’re talking about? That’s where the this keyword comes in handy. It can be used before a class variable to let the compiler know you’re talking about the class variable.
Now, our Dinosaur class should look like this:
public class Dinosaur{
String name;
public Dinosaur(String name){
this.name = name;
}
}
The this keyword can also be used before method calls in order to refer to the current object that the method is called on.
Last lab, we worked on your practical assignment involving encoding and encryption. This lab, we will continue working with branches and classes, like the String class.
We will be creating a lab that simulates creating an account. Many websites require an account to have access to their functionality, and require a username and password and have specific limitations on the password. Today, we will write a few methods that resemble this process.
For this lab, we have provided:
- a method to test your code called testMethod() that you should first complete, then uncomment as you progress through the lab
- a method called printAccountInfo() that will print the associated information (username, password, verification status) for the object
- method signatures for confirmAccount(), checkPasswordStrength(), and corruptedData()
You may want to review the Strings class in Zybooks and previous labs, as well as using the Random and Integer classes.
Substring
Substring() is one of the methods in the String class that allows you to select specifc parts of a string. It can be used in two different ways:
String shortString = str.substring(beginIndex);
String shortString = str.substring(beginIndex, endIndex);
When using just one parameter, it will return the string from the beginning index to the end of the string. When using two parameters, it will return the string starting at and including the beginning index, up until but not including the ending index. Remember that strings are indexed starting from 0.
Step 1 : Class Variables
Declare three class variables:
- password: a string that should not be visible to methods/classes outside of the Account class
- username: a string that is visible to anyone
- verified: a boolean that is visible to anyone
Consider why we don’t want password to be visible to anyone else. Is there a way to access the variable if we were in a different class? For testing purposes, create an accessor method for the password variable.
Create a constructor for the Account class that takes in two String parameters: one for each String class variable. Initialize the verified variable to be false.
PLEASE make sure that your constructor takes parameters in the order username then password. (username, password)
Hint: If your parameter names for the constructor are the same as the class variables, refer to the introduction to see how you can differentiate between the two.
Testing the Constructor
Uncomment the lines in testMethod() to see that your constructor is working. Check that the output is as expected.
Step 2: confirmAccount(String confirmedPassword)
In the method confirmAccount(), confirm that the String passed into the method is equivalent to the class variable for the password.
You will not be given the method signature for this method, but it should:
- be named confirmAccount
- take in one String as a parameter
- not return any type
- require an instance of the class to be called
If the parameter and class variable are equal, modify the verified class variable to be true.
Hint: Remember how to compare strings and determine if they are equal.
Testing
Uncomment the lines in testMethod() to see that your method is working. Check that the output is as expected.
Step 3: checkPasswordStrength()
In the method checkPasswordStrength, we will be checking the password’s length and returning a String message that notifies the user of their password strength.
If the password length is greater than 16, the method should return “Password strength: strong”.
If the password length is greater than 8 but less than 16, the method should return “Password strength: moderate”.
If the password length is less than 8 (or not one of the options above), the method should return “Password weak, strengthening now.” but before this, it should modify the class variable password.
The following changes should be made to password if it does not meet the length requirements:
- All ‘a’ characters should be replaced with ‘@’.
- All ‘o’ characters should be replaced with ‘0’.
- All ‘s’ characters should be replaced with ‘$’.
- Using the random number generator (we have provided a random object for you to use), add a random integer number less than 10 to the end of the password.
Testing
Uncomment the lines in testMethod() to see that your method is working. Check that the output is as expected.
Step 4: corruptedData(String corrupted, char delimiter)
In the method corruptedData(), we will be working with the String class and associated methods. For documentation about the String class, check out the Java Docs, or these examples from outside resources.
The method has the following method signature:
public int corruptedData(String corrupted, char delimiter){ }
It will take in a corrupted string that you will decode to find a new username, password, and a key to the corrupted data. The corrupted string will have the following format.
[key part 1][delimiter][username][delimiter][password][delimiter][key part 2]
The delimiter, which is defined as a sequence of one or more characters that divides a string into independent parts, will be given as a char parameter.
The method should parse each component of the corrupted data using the .substring() and indexOf() methods.
For example, if you were to call:
corruptedData( "123!username!password!456", '!')
It should modify the class variables username and password to hold “username” and “password” respectively, and it should return 123456 as an int not a String.
Hint: How would you convert two Strings that contain digits to a single integer? Could you use a method in the Integer class?
Testing
Uncomment the lines in testMethod() to see that your method is working. Check that the output is as expected.