-
Really need method help
No matter how many times I've been explained the theory behind methods, I can't determine how to write them to apply to programs. Here is a simple program that work, but I need to apply 4 different methods to it.
setUsername() – A mutator method that sets the username for the associated account. (Use any
8 letters to represent the initial username.)
getPassword() - An accessor method that asks for the password associated with the account.
isUsernameValid() -This method checks whether the username entered meets the required rules
for usernames.
•
isPasswordValid() - This method checks whether the string entered is valid.
//Using the JOptionPane class to promt the user for input
import javax.swing.JOptionPane;
class JavaAssignment5
{
public static void main(String[]args){
//Promting the user to enter a user name and assigning it to 'x'
String x = JOptionPane.showInputDialog(null,"Enter User Name: ", "Login", JOptionPane.QUESTION_MESSAGE);
//Assigning 'y' as the password entered
String y = JOptionPane.showInputDialog(null,"Password: ", "Login", JOptionPane.QUESTION_MESSAGE);
//Setting the correct user name
String user = new String("muzukashi");
//Setting the correct password
String pass = new String("Ja57pan");
//Boolean expression to check if what the person entered is correct for both username and password
if(user.equals(x) && pass.equals(y)){
//Displays a message to inform them that they entered both correctly
JOptionPane.showMessageDialog(null,"Valid user Name","Good job!", JOptionPane.WARNING_MESSAGE);
}
else{
//Informs the user that they entered something wrong, but not actually telling them
JOptionPane.showMessageDialog(null,"Invalid user Name","Dumbass", JOptionPane.WARNING_MESSAGE);
}
}
}
-
Please use CODE tags (the # icon in the editor) when posting code.
Code:
//Using the JOptionPane class to promt the user for input
import javax.swing.JOptionPane;
class JavaAssignment5
{
public static void main(String[] args) {
// Promting the user to enter a user name and assigning it to 'x'
String x = JOptionPane.showInputDialog(null, "Enter User Name: ", "Login", JOptionPane.QUESTION_MESSAGE);
// Assigning 'y' as the password entered
String y = JOptionPane.showInputDialog(null, "Password: ", "Login", JOptionPane.QUESTION_MESSAGE);
// Setting the correct user name
String user = new String("muzukashi"); [COLOR="Blue"]// there's no need for [I][B]new String()[/B][/I] here -- [I][B]String user = "muzukashi";[/B][/I] is fine[/COLOR]
// Setting the correct password
String pass = new String("Ja57pan"); [COLOR="blue"]// same here[/COLOR]
// Boolean expression to check if what the person entered is correct for both username and password
if (user.equals(x) && pass.equals(y)) {
// Displays a message to inform them that they entered both correctly
JOptionPane.showMessageDialog(null, "Valid user Name","Good job!", JOptionPane.WARNING_MESSAGE);
}
else{
// Informs the user that they entered something wrong, but not actually telling them
JOptionPane.showMessageDialog(null, "Invalid user Name", "Dumbass", JOptionPane.WARNING_MESSAGE);
}
}
}
OK, so you know some basics of Java syntax, but you're confounded by methods. Let's take them one by one.
setUsername() – A mutator method that sets the username for the associated account. (Use any
8 letters to represent the initial username.)
A mutator method is one that changes the value of an instance variable or field of the Object. In this case, we seem to want a method that will set the value of a username field, so we'd better create the field first.
Code:
class JavaAssignment5
{
[COLOR="Blue"] private String username;
[/COLOR]
public static void main(String[] args) {
Now we can write the method. Since it's meant to update a field, and not to get any information from the Object, its return type should be void. It's likely that we'll want other code to be able to invoke this method, so we'll make it public. We'll need to know what value to set the username field to, so we will need a String argument.
Code:
class JavaAssignment5
{
[COLOR="Blue"] private String username;
public void setUsername(String username) {
this.username = username;
}
[/COLOR]
public static void main(String[] args) {
The body of the method says to assign the contents of the username argument that was passed in to the method to the username field of the Object. We need to say this.username to indicate we mean the Object's field, or Java will assume we mean the argument. (Some people advise giving either the field or the argument a different name, as they find this syntax confusing. I think it's more confusing to start giving different names to what is essentially the same piece of information.)
OK, that's one example. Try writing one of the other methods, and post what you come up with. Don't try invoking the methods yet, as there's another point we need to clear up before that will make sense.
-Gary-
-
I think these are basic methods:
getPassword(){
return password;
}
browse for any example of get, set, is(return boolean)
-