Results 1 to 4 of 4
Thread: Really need method help
- 12-14-2010, 12:13 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 1
- Rep Power
- 0
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);
}
}
}
- 12-14-2010, 01:18 PM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Please use CODE tags (the # icon in the editor) when posting code.
OK, so you know some basics of Java syntax, but you're confounded by methods. Let's take them one by one.Java 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); } } }
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.
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.Java Code:class JavaAssignment5 { [COLOR="Blue"] private String 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.)Java Code:class JavaAssignment5 { [COLOR="Blue"] private String username; public void setUsername(String username) { this.username = username; } [/COLOR] public static void main(String[] args) {
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-
- 12-14-2010, 01:22 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 19
- Rep Power
- 0
I think these are basic methods:
getPassword(){
return password;
}
browse for any example of get, set, is(return boolean)
- 12-14-2010, 01:23 PM #4
Similar Threads
-
Thread problem, calling method in run method
By majk in forum Threads and SynchronizationReplies: 4Last Post: 09-27-2010, 11:40 AM -
method not abstract, does not override actionperformed method.
By Theman in forum New To JavaReplies: 2Last Post: 03-26-2010, 05:12 PM -
ArrayLists compareTo method, equals method
By random0munky in forum New To JavaReplies: 2Last Post: 10-26-2009, 07:20 PM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 07:55 PM -
cannot call private method from static method
By jon80 in forum New To JavaReplies: 3Last Post: 05-07-2008, 08:37 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks