View Full Version : Can anyone can finish this JAVA programing?
SuperSonic2005
11-03-05, 04:35 AM
Create a Java application to simulate a security lock that has 26 buttons, one for each letter of the alphabet, and a button for "Enter." The user of the lock will have to choose 3 letters, in order, correctly, to open the lock.
• Your simulation should be text-based.
• The program should first let the user set the 3-letter secret code for the lock.
• The program should then test your lock logic, by asking the user for the first letter, then test it. If it matches the secret code, the program should continue with the next letter, and if it is also correct, the program should get and process the last letter.
• If the user fails to enter the correct sequence, the user should be allowed only one retry.
SuperSonic2005
11-03-05, 11:11 AM
/**
This class implements a combination lock
*/
public class CombinationLock2
{
/**
Constructs a lock with a given combination.
@param aCombination the combination; a string
with three uppercase letters A . . . Z
*/
public CombinationLock2(String aCombination)
{
combination = aCombination;
newPosition = "";
open = false;
}
/**
Set the dial to a position.
@param aPosition a string consisting of a single uppercase
letter A . . . Z
*/
void setPosition(String aPosition)
{
position = aPosition;
newPosition += position;
if(newPosition.length() > 3)
{
int startIndex = newPosition.length() - 3;
int stopIndex = newPosition.length();
newPosition = newPosition.substring(startIndex, stopIndex);
}
}
/**
Try unlocking the lock.
*/
void unlock()
{
if(newPosition.equals(combination))
open = true;
}
/**
Check whether the lock is unlocked.
@return true if the lock is currently open.
*/
boolean isOpen()
{
return open;
}
/**
Close the lock.
*/
void lock()
{
newPosition = "";
position = "";
open = false;
}
private boolean open;
private String newPosition;
private String combination;
private String position;
}
ub3r_n00b
11-03-05, 09:40 PM
thats a really easy program. Essentially its just comparing strings. You prompt the user and take in a string, like "ABC" or something.
Once the user presses enter.. store that string - handle your errors (if the lenght is greater than 3 then call them a retard or something). Then you ask for the first character... if its right then you proceed to ask for the next, if its right then you ask for the next...
otherwise you just stop.
so you should have some function in which you pass the letter to check..
public boolean checkLetter(String a){
System.out.println("Enter input or whatever: ");
String userInput = Keyboard.getString() // get users input
if (userInput == a) {
return true;
} else {
return false;
}
}
Then you have
Boolean errorOccured
public Boolean somename(){
for (Int i=0; i<3; i++){
Boolean something = checkLetter(usersCombination.charAt(i));
if (!something) {
return true;
} else {
if (something && i == 2) {
return false;
}
}
}
then in your main you have
public void main(String[] args){
Boolean askStuff = somename();
if (askStuff) {
askStuff = somename();
}
}
anyways somethign like that - havent touched java in a while.
pogopogo
11-11-05, 01:37 AM
>>ub3r_n00b
hi sir ub3r_n00b
Would you mind if I ask you to write whole program code?
I am actually not familior with boolean stuff so I want to see how it works.
I thought I write this program using if/else if/else statement.
Can we actually use if/else if/ else statement to write this program?
Thanks for readin'.
ub3r_n00b
11-11-05, 10:30 AM
no I am not going to write the whole code. You only learn by doing it yourself. Play around with it, read online, its not hard. Everything is possible with if / else statements - just means you have to "unwrap" loops or recursive functions.
Figure it out - it isn't hard to do.
Preet
you just need to loop at a command prompt waiting for input..read in a line when a user hits enter..make sure the length of the string is 1 character..compare against the first char in the key, if good continue, otherwise restart or abort..it's ez..maybe 50 lines tops..could be written in a main even..no need to OO it
MadDoctor
11-28-05, 08:58 PM
no I am not going to write the whole code. Figure it out - it isn't hard to do.
Preetub3r_n00b has a PayPal account and I'm betting if you made a deposit... he might write some code for you.
;)
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.