Martrinex Learning Center
HOME CONTENTS
 
This website is underconstruction.
Author: Martin Sykes
Challenge: I have challenged myself to 1 article per day, and framework improvements! Enjoy
Challenge end: unknown

Public / Private

Ok a simple tutorial!
You have learn't classes, functions and variables, you will be created alot of them while you learn more about programming, sometimes you may not want to access them from everywhere in your program, you will limit your own access for the security of the program and also for ease of access.

For example if you created a class LOGON:
You may have:

Consider security:
setUsername and getUsername could be used to read and write to strUsername these are ok to be public.
setPassword could write to strPassword and could be public
You may not want your entire program to read the password for security reasons so getPassword could be made private
Usually you want to hide your variables what point is there in hiding 'getPassword' if you can just type var = strPassword?
What about ease of use?
Bare in mind you created the class LOGON to seperate your logon code from your application, you want it as easy to use as possible, then you want to move on and make the rest of your program knowing LOGON is complete you may even want to use it in other programs. When you do:
logon = new LOGON();
logon.
You are faced with a handy drop down list of choices thanks to eclipse, you may not want to see (loaddatabase, checkusername, checkpassword, marklaston and dologoff) all these functions will be called from within the class by (logon and logoff) so you can also make these private!
Finally
For documentation / design / collegues and college you may have to document your functions you should know: public is denoted by '+'
private is denoted by '-'
Lets write the list again properly:


Task

To hammer in both classes and public private, we are going to make the logon example!
Create a new project 'myLogon' with the main class.
Create the classes 'LOGON', 'REGISTER', 'DATABASE', 'USER'

Notice:
our database will just be an array of USER's'
The plan:

LOGON
Name Type Scope
strUsername variable, string private
strPassword variable, string private
dbData variable, DATABASE private
getUsername function, string public
setUsername function, void public
getPassword function, string private
setPassword function, void public
logon function, boolean public
dologon function, boolean private
REGISTER
Name Type Scope
strUsername variable, string private
strPassword variable, string private
dbData variable, DATABASE private
getUsername function, string public
setUsername function, void public
getPassword function, string private
setPassword function, void public
register function, boolean public
doregister function, boolean private
DATABASE
Name Type Scope
users variable,array of USER private
addUser function, bool public
getUser function, USER public
USER
Name Type Scope
strUsername variable, string public
strPassword variable, string public


1. Class USER

Code:
public class USER {

public String strUsername="";
public String strPassword="";

public USER(){
}

public USER(String user, String pass){

strUsername = user;
strPassword = pass;
}
}
Very simple custom data type. :-)
2. Class DATABASE
Code:
public class DATABASE {

private USER[] users = new USER[50];
private int count = 0;

public boolean addUser(String user, String pass){

if(getUser(user,pass)!=null) return false; // user exists
if(count==users.length) return false; // memory full
users[count]=new USER(user,pass);
count++;
return true;
}

public USER getUser(String user, String pass){

for(int i=0; i<count; i++){

if(users[i].strUsername.compareTo(user)==0 &&
users[i].strPassword.compareTo(pass)==0){

return users[i];
}
}
return null;
}
}
DATABASE, stores our users in an array
getUser tries to find the user (username and password) or returns NULL (nothing);
addUser adds the user if it cannot find the user with getUser.
3. Class REGISTER
Code:
public class REGISTER {

private String strUsername = "";
private String strPassword = "";
private DATABASE dbData = null;

public REGISTER(DATABASE data){

dbData = data;
}

public String getUsername(){

return strUsername;
}

public void setUsername(String user){

strUsername = user;
}

private String getPassword(){

return strPassword;
}

public void setPassword(String pass){

strPassword = pass;
}

public boolean register(){

return doRegister();
}

private boolean doRegister(){

return dbData.addUser(getUsername(), getPassword());
}
}
Adds new users
Set the username and password using setUsername and setPassword, this is then stored in the variables strUsername, strPassword
Calls register, which calls doRegister which calls dbData.addUser, kinda pointless but in a real program their maybe more code in "doRegister".
4. CLASS LOGON
Code:
public class LOGON {

private String strUsername = "";
private String strPassword = "";
private DATABASE dbData = null;

public LOGON(DATABASE data){

dbData = data;
}

public String getUsername(){

return strUsername;
}

public void setUsername(String user){

strUsername = user;
}

private String getPassword(){

return strPassword;
}

public void setPassword(String pass){

strPassword = pass;
}

public boolean logon(){

return doLogon();
}

private boolean doLogon(){

USER user = dbData.getUser(getUsername(), getPassword());
return (user!=null);
}
}
Same code as REGISTER, except the logon and doLogon part, again same deal except it calls getUser instead of addUser in the database.
5. CLASS myLogon (main)
Code:
public class myLogon {

public static void main(String[] args) {

// TODO Auto-generated method stub
DATABASE data = new DATABASE();
REGISTER register = new REGISTER(data);
LOGON logon = new LOGON(data);
// logon even though no users, should fail
logon.setUsername("Martin");
logon.setPassword("123");
System.out.println("Logon with Martin, 123: "+logon.logon());
// register 1st user, should work
register.setUsername("Martin");
register.setPassword("123");
System.out.println("Add Martin, 123: "+register.register());
// logon with that user, should work
logon.setUsername("Martin");
logon.setPassword("123");
System.out.println("Logon with Martin, 123: "+logon.logon());
// try and register same user again, should fail
register.setUsername("Martin");
register.setPassword("123");
System.out.println("Add Martin, 123: "+register.register());
}
}
Simple test of the other code.
Incase something went wrong: (remember to remove the project before importing)
Download

myLogon.zip [5.9kb]

Add Comment

Help the web master by commenting on the tutorials, thankyou.
Name:
Email: (this will not be shown)
Comment:
  Send email to admin, don't post.

 

Comments

Nobody has commented on this article.
Be the first, the webmaster needs comments good or bad to improve this site.



Copyright (c) 2008, Martin Sykes.
Learning Center is a branch of Martrinex Systems, Martrinex.net
Do not copy any materials from this site without permission from the auther.
Martrinex Learning Center[X]
  Introduction Beginner Intermediate

Important useful well commented source codes
Please read the import tutorial to know how to use them.

Source