Computer Science Canada

Lock.java (don't know how to construct a method)

Author:  nelsonkkyy [ Tue Oct 18, 2011 11:41 am ]
Post subject:  Lock.java (don't know how to construct a method)

Quote:


/**
This is class tests the Lock objects.
*/
import java.util.* ;

public class Main
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in) ;

Lock lock = new Lock('C', 'A', 'K') ;
lock.set('C') ; // first turn
System.out.print("The lock will open? ") ;
lock.pull() ; // try
System.out.println(lock.isOpen()) ;
System.out.println("Expected: false") ; // only one turn

lock.set('A') ; // second turn
System.out.print("The lock will open? ") ;
lock.pull() ; // try
System.out.println(lock.isOpen()) ;
System.out.println("Expected: false") ; // only two turns

lock.set('K') ; // third turn
System.out.print("The lock is open? ") ;
System.out.println(lock.isOpen()) ;
System.out.println("Expected: false") ; // haven't pulled yet
System.out.print("The lock will open? ") ;
lock.pull() ; // try
System.out.println(lock.isOpen()) ;
System.out.println("Expected: true") ; // correct combination

lock.set('L') ; // turning dial while lock is open
System.out.print("The lock is open? ") ;
System.out.println(lock.isOpen()) ;
System.out.println("Expected: true") ; // it is still open

lock.close() ; // closing the lock resets dials
System.out.print("The lock is open? ") ;
System.out.println(lock.isOpen()) ;
System.out.println("Expected: false") ; // we just closed it

System.out.println("Give the combination of a new lock as a 3 letter string") ;
String combo = scanner.next() ;
System.out.println("Give the tolerance of this lock as an int") ;
int tolerance = scanner.nextInt() ;

Lock lock = new Lock(combo.charAt(0), combo.charAt(1),
combo.charAt(2), tolerance) ;
System.out.println("Give a 5 letter string which represents an effort to open the lock.") ;
combo = scanner.next() ;
lock.set(combo.charAt(0)) ;
lock.set(combo.charAt(1)) ;
lock.set(combo.charAt(2)) ;
lock.set(combo.charAt(3)) ;
lock.set(combo.charAt(4)) ;
System.out.print("The lock will open? ") ;
lock.pull() ; // try
System.out.println(lock.isOpen()) ;

}
}




this is the method

Quote:

public class Lock
{
private string letter1,letter2,letter3 ;

public Lock (string letter1,letter2,letter3)
{
this.letter1 = letter1;
this.letter2 = letter2;
this.letter3 = letter3;
}

public string set()
{


}

public string pull()
{
if (letter1 = letter2

}


public boolean isOpen()
{

}

public string close()
{


}
}




Anyone can give me a hand???[/quote]

Author:  Tony [ Tue Oct 18, 2011 12:24 pm ]
Post subject:  RE:Lock.java (don\'t know how to construct a method)

give you a hand with what?

Author:  nelsonkkyy [ Tue Oct 18, 2011 12:50 pm ]
Post subject:  Re: Lock.java (don't know how to construct a method)

like how to make a method to read the (C A K) from the LockTester
How to read them one by one.

Author:  Tony [ Tue Oct 18, 2011 1:14 pm ]
Post subject:  RE:Lock.java (don\'t know how to construct a method)

method arguments
code:

lock.set('C') ; // first turn

method set() receives an argument with a value 'C'

Author:  nelsonkkyy [ Tue Oct 18, 2011 7:16 pm ]
Post subject:  Re: Lock.java (don't know how to construct a method)

kk so when i compile it, it says the scanner was not found. What should i do??

Author:  Tony [ Tue Oct 18, 2011 7:45 pm ]
Post subject:  RE:Lock.java (don\'t know how to construct a method)

You should tell the compiler where to find Scanner.

Author:  nelsonkkyy [ Wed Oct 19, 2011 12:08 am ]
Post subject:  Re: Lock.java (don't know how to construct a method)

Quote:

public class Lock
{

//instance variables
char word;
char password;
boolean status = true;
/**
Constructs the object and sets the instance variables to given values.
@param a one of the three values
@param b one of the three values
@param c one of the three values
*/
public Lock(char x ,char y ,char z )
{

password = x + y + z; ////how can i add up the three chars??////
System.out.println(password);
}

Author:  Tony [ Wed Oct 19, 2011 12:17 am ]
Post subject:  RE:Lock.java (don\'t know how to construct a method)

sure, you can do that
code:

'a' + 1 == 'b'

Not sure what meaning x+y+z would have though. What character do you want password to be?
Quote:

char password;

Author:  nelsonkkyy [ Wed Oct 19, 2011 9:22 am ]
Post subject:  Re: Lock.java (don't know how to construct a method)

The password is (char x char y and char z) which are (C, A, K) from
Quote:
Lock lock = new Lock('C', 'A', 'K') ;


: