• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unsure of where to start - New Java Programmer  RSS feed

 
Adam Kay
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Above is the test class for an assignment. I am having difficulty in where to start. I started to define the method but am unsure how to check each char individually. I am looking for advice, not the final answer. After all I need to learn!

This is what I have so far:
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

It seems that method isOpen() should return variable open.

What are you trying to implement?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Kay wrote:Above is the test class for an assignment. I am having difficulty in where to start. I started to define the method but am unsure how to check each char individually.

So, what's the idea here? I'm afraid it's not obvious. Why don't you give us the requirements in English, not in Java-ese. That exercise alone might actually help you to work out what you need to do.

I am looking for advice, not the final answer. After all I need to learn!

A very good attitude; and as soon as you do the above, we'll be glad to help.

BTW: Be very careful not to write long lines when you post code. You're OK at the moment, but some of yours are perilously close to the limit.

Winston
 
Adam Kay
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So basically the program has two parts. The first part is checking that the lock works the combo set to three characters 'C' 'A' and 'K'. The second part of the program asks the user for a string for a new combination and the tolerance of errors. A 5 character string is entered and you need to ignore the first two and only focus on 2,3,4 (ignoring 0 and 1).
The part I find most confusing is the method set versus the method pull. I realized that the method isOpen should just return the open status of the lock so I have changed that. However, I do not know how to go about setting the method set. Any help would be appreciated
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to store 5 characters from user. For that java has array or wrapper class ArrayList.

So, create new instance of ArrayList and each time user inputs new character you can add it to ArrayList by its method add(E e).
 
Adam Kay
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately we have not been taught how to use arrays as of yet. And this program is supposed to be able to be written without the need of them (using if and while)

Under the method pull() I had:

If (firstLetter == 'C' && secondLetter == 'A'...)
open = true.

However that would not work with the second part of the program. Is there a way to have one pull and unlock method for both parts of the program even though the first set of characters is predetermined in the test class.

Thanks again for everything.
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably I understand, your level is 0.

In such case you can create 5 variables to store 5 characters from user.
you should know that one variable can hold different values if you use =.

So, create 5 variables .
char letter1;
...
char letter2;
And create setters for them like

 
Adam Kay
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right my level is 0.

What I don't understand is how having 5 setLetter methods will work when the program has 3 at first(referring to the test class). Or have I just mistaken what you have told me
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should create setter for each variable. 5 setters for 5 variables.
And when you have char from user you call appropriate setter.
You may have firstChar variable and thus you create setFirstChar method.
When user inputs first character you call method setFirstChar to assign user's char to firstChar variable.
Then you have secondChar variable and thus setSecondChar method. And so on for five characters
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Kay wrote:Under the method pull() I had:

If (firstLetter == 'C' && secondLetter == 'A'...)
open = true.

However that would not work with the second part of the program. Is there a way to have one pull and unlock method for both parts of the program even though the first set of characters is predetermined in the test class.

As you have correctly stated this code won't work for the second part of the program but it won't work for the first part either. Don't worry about the second part yet, just concentrate on getting the first part working.

Think about what this is doing, you are creating a Lock object passing the code to the constructor. You store the code in three variables so these three variable hold the lock's code. Therefore you need to test the input values (ie the one's passed to the set method) against these 3 variables. There is no point testing the 3 variables directly against C, A & K as you know they contain these values as you have passed them into the constructor.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!