• Post Reply Bookmark Topic Watch Topic
  • New Topic

PhoneUnlocker- methods  RSS feed

 
haruki jay
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a program called PhoneUnlocker that takes in a 4 digit passcode. This program must have 5 private variables- 4 integers and 1 boolean (to say whether or not the phone is unlocked).

Then, I must set the 4 values as 0 and locked.

Then in another constructor, make the 4 integers the same as the 4 integers at the beginning, and mark the phone as unlocked.

Create a Boolean method called setPasscode(), which has 4 parameters in the method and sets the 4 integers to the 4 integers in the step above. This could only function is the phone is locked. If it happens that the phone is unlocked, this method must return “false”, not changing the passcode. If the phone is locked, it must return true and change the passcode to the new one.

Make a method called LockPhone(), which locks the phone and another method called IsPhoneUnlocked() which returns t or f depending on whether or not the phone is locked.

Then, make another method called UnlockPhone(), which has 4 parameters and will check if the passcode entered in right. If it is right, to make the phone unlocked, returning true and otherwise, making the phone locked and returning false.

Another method called Tries() (also 4 parameters). It will return the amount of actual correct integers in the entered code.

A toString() method where the 4 correct values of the code will be returned and lastly, an equals() method to test whether or not the codes for the 2 objects of the class PhoneUnlocker are the same.

Here is the code i wrote for this program. I am having trouble figuring out my mistakes, mainly in the methods tries(), UnlockPhone() and LockPhone()

Below is the code i wrote. Thank you for your help




}
 
Liutauras Vilda
Sheriff
Posts: 4918
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What has happened?
 
Liutauras Vilda
Sheriff
Posts: 4918
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This method in general is plainly wrong. If you were to fix if statement's condition, that would be wrong too.

You wrote way too much code mate without testing and verifying single line correctness. I'm very doubted it even compiles.
Probably you'll need to start over.
 
haruki jay
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
What has happened?


In line 31, i was trying to code that if the phone is locked, it will set the values a, b, c and d as the values passed at the beginning.
 
Liutauras Vilda
Sheriff
Posts: 4918
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see in the instructions you posted are your interpretations incorporated or you were given incorrect ones.

Could you please post exact instructions you were given?
 
haruki jay
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:

This method in general is plainly wrong. If you were to fix if statement's condition, that would be wrong too.

You wrote way too much code mate without testing and verifying single line correctness. I'm very doubted it even compiles.
Probably you'll need to start over.




how should i go about writing this method? Create a Boolean method called setPasscode(), which has 4 parameters in the method and sets the 4 integers to the 4 integers in the step above. This could only function is the phone is locked. If it happens that the phone is unlocked, this method must return “false”, not changing the passcode. If the phone is locked, it must return true and change the passcode to the new one.
 
Liutauras Vilda
Sheriff
Posts: 4918
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have very limited time now, up to 20 mins.

Don't rush as you rushing now. It is clear enough you are lost about what you are doing. Almost everything is wrong without fully reading instructions you have posted.

Post small part of instructions. I think it is safe to say - you can delete what you have so far, just don't worry about that.
 
haruki jay
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:I have very limited time now, up to 20 mins.

Don't rush as you rushing now. It is clear enough you are lost about what you are doing. Almost everything is wrong without fully reading instructions you have posted.

Post small part of instructions. I think it is safe to say - you can delete what you have so far, just don't worry about that.


i will post the question in smaller parts, thank you!
 
haruki jay
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Create a class named UnlockPhone
􏰀 4 private instance variables that store the 3 combination numbers required to unlock the phone and a Boolean variable that will indicate whether the phone is locked or unlocked. (true if open, false if closed).
􏰀 2 constructors: default constructor
- sets the 4 int passcode numbers to 0 and marks the phone as locked
- 4 parameter constructor - sets the 4 passcode int values to the 4 passed integers and marks the phone as unlocked
􏰀  A Boolean mutator method called setPasscode() which is a 3 parameter method and sets the 3 combination numbers to the 3 passed numbers. It can only change the combination if the lock is closed. If the lock is open it returns false as it could not change the combination. If the lock is closed it returns true after it has reset the combination.
How should i go about fixing my setPasscode() method? any tips, as i am a beginner programmer and would like to understand where i went wrong.

 
Liutauras Vilda
Sheriff
Posts: 4918
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instructions wrote:1. Create a class named UnlockPhone


Adri Ge wrote:public class PhoneUnlocker

Do you see what I mean?

And delete all that code, mentioned already - it is totally wrong.

Implement first instruction (first sentence only). And please post.
 
haruki jay
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Instructions wrote:1. Create a class named UnlockPhone


Adri Ge wrote:public class PhoneUnlocker

Do you see what I mean?


oh yes!! theres supposed to be a class as follows at the beginning:

 
 
haruki jay
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Instructions wrote:1. Create a class named UnlockPhone


Adri Ge wrote:public class PhoneUnlocker

Do you see what I mean?


i will start over.
 
Liutauras Vilda
Sheriff
Posts: 4918
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instructions wrote:4 private instance variables that store the 3 combination numbers required to unlock the phone and a Boolean variable that will indicate whether the phone is locked or unlocked.


You wrote: private int a;
    private int b;
    private int c;
    private int d;
    private boolean status;
    int counter=0;
6 in total.
 
Liutauras Vilda
Sheriff
Posts: 4918
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adri Ge wrote:oh yes!! theres supposed to be a class as follows at the beginning:

Do you spot any differences in the names of what has been asked and what you did?
 
Liutauras Vilda
Sheriff
Posts: 4918
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Such exercises are of the kind when you don't need to think  - just do exactly as you are asked. So, not overcomplicate here - just do EXACTLY what is being asked.
 
haruki jay
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Adri Ge wrote:oh yes!! theres supposed to be a class as follows at the beginning:

Do you spot any differences in the names of what has been asked and what you did?


yes i do, that was my mistake. I meant to call my class UnlockPhone, not PhoneUnlocker.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!