Thread: Help me in OOP

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Exclamation Help me in OOP


    Hello

    I'm a beginner in OOP Java

    Please can someone help me in this hw ?

    A credit card account is defined by its ID, the name of customer, the balance, and the transactions fee related to this credit card and that may take value between 2% and 4%. Define the class CreditAcc that describes a credit card account. In this class:
    a.All sensitive data members should be private. The ID is generated by concatenating the first 5 letters of the name of the customere followed by 6 random digits between 0 and 9.
    b.Define at least one constructor.
    c.Define the method transfer(CreditACC CC, double amount) that permits to transfer an amount of money to the object sent as argument if possible. This function has the following signature :
    public boolean transfer(CreditAcc CC, double amount)
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    so what have you tried already? What's you concrete questions?

    If you expect us to do your homework for you, that won't happen. We can help you with concrete issues and bugs, but this is no "homework cheating service" or something.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by Jacques1
    Hi,

    so what have you tried already? What's you concrete questions?

    If you expect us to do your homework for you, that won't happen. We can help you with concrete issues and bugs, but this is no "homework cheating service" or something.

    Thank you for exlaining this for me

    Surely I have tried ,, this what I wrote it :



    public class CreditAcc {

    private int ID;
    private String[] name;
    private int balance;
    private int transactions;
    String[] a;
    int b;

    //Default Constructor
    public CreditAcc(){
    }

    //Constructor with specified datafileds
    public CreditAcc(String [] n,int I,int t){

    name = n;

    for(int i=0;i<5;i++){
    a+=n[i];

    for(int i=0;i<6;i++)
    b=Math.random()*10;

    ID = a.concat(b);

    transactions=t;

    }

    public boolean transfer(CreditAcc CC, double amount({

    //here I don't know what to write how can i know that it is possible to transfer an amount of money or not ?


    }




    }
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    On Earth!!
    Posts
    9
    Rep Power
    0
    Originally Posted by Angel lebanon
    Thank you for exlaining this for me

    Surely I have tried ,, this what I wrote it :



    public class CreditAcc {

    private int ID;
    private String[] name;
    private int balance;
    private int transactions;
    String[] a;
    int b;

    //Default Constructor
    public CreditAcc(){
    }

    //Constructor with specified datafileds
    public CreditAcc(String [] n,int I,int t){

    name = n;

    for(int i=0;i<5;i++){
    a+=n[i];

    for(int i=0;i<6;i++)
    b=Math.random()*10;

    ID = a.concat(b);

    transactions=t;

    }

    public boolean transfer(CreditAcc CC, double amount({

    //here I don't know what to write how can i know that it is possible to transfer an amount of money or not ?


    }




    }
    Try to post you code by using [CODE] tags..
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    The overall structure looks good, but I don't think you've actually tested your code.

    You have several syntax and logic errors that you should fix before moving on to the transfer() method. It might be a good idea to get an IDE (integrated development environment) like Netbeans or Eclipse so that you'll be notified of obvious bugs.

    When you have little experience in a language, you should generally do everything step by step and test each small piece of code. If you write down a full method and realize it doesn't do what you expect (or maybe won't even compile), debugging it can be pretty hard.

    Also, use sensible variable names. Cryptic abbreviations like "n" or "I" or "t" make the code very hard to read for others and also you yourself.

    OK, so the ID starts with the first 5 characters of the customer's name. Start with that. You can either use String#substring or String#charAt. The bracket syntax you're using doesn't exist in Java. Also note that you've two nested loops and missing braces in the inner loop (without braces, only the first statement belongs to the loop).

    When you've extracted the characters, test the code. Do an actual test run and see if the method works as expected.

    Then move on to the random digits. I suggest using the Random class, which has a nextInt method.
    Last edited by Jacques1; March 30th, 2013 at 05:21 AM.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0
    Okay Thank you Dr.Jacques1 for helping me ,, I just test the codes on netbeans and found this error (in red)


    public class CreditAcc {


    private int ID;
    private String Name;
    private int Balance;
    private int Transactions;
    private String a;
    private int b;
    private int id;

    //Default Constructor
    public CreditAcc(){
    }

    //Constructor with specified datafileds
    public CreditAcc(String name,int balance,int transactions){

    Name = name;
    Balance=balance;

    a=name.substring(0,5);
    b=(int)Math.random()*10;
    id=a.concat(b);

    ID=id;

    Transactions=transactions;

    }

    public boolean transfer(CreditAcc CC, double amount){
    return true ;
    }

    }//end class

    1-How can I cocatinate a string with int?
    2- With respect to the transfer method ,based on what should I return a true or false?

    Thank you in advance
  12. #7
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by Angel lebanon
    1-How can I cocatinate a string with int?
    With the addition operator:
    Code:
    a + b
    But your ID has to be a String, not an int. The ID consists of characters.



    Originally Posted by Angel lebanon
    2- With respect to the transfer method ,based on what should I return a true or false?
    Again, finish one method before you continue with the next method.

    When you fix the concat and the type error in the constructor, you'll get the first 5 characters of the name and one "random" digit -- actually, the digit is always 0, because the type cast has precedence over the multiplication.

    You need 6 random digits. Have you considered using the Random class as suggested above? It's easier to use and doesn't require any scaling or casting magic.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0
    You need 6 random digits. Have you considered using the Random class as suggested above? It's easier to use and doesn't require any scaling or casting magic.
    How can I generate 6 numbers?
    This statement gives me only one random number

    b=Random.nextInt(10);

    and if I have to do a loop I should define the variable b as array , then I will not be abl to concatinate a with b
  16. #9
  17. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Make a loop and concatenate each random number with the current ID string:

    java Code:
    public class CreditAcc {
     
    	// create random generator (it's static to avoid the Random class being instantiated and seeded again and again)
    	private static Random rand = new Random();
     
    	// I've cleaned up the attributes and named them according to the Java conventions (camelCase starting with a lowercase letter)
    	private String id;
    	private String customerName;
    	private int balance;
    	public float transactionFee;
     
    	public CreditAcc(String customerName, int balance, int transactionFee) {
    		this.customerName = customerName;	// I'd use an explicit "this" to distinguish between attributes and local variables
    		this.balance = balance;
    		this.transactionFee = transactionFee;
    		this.id = customerName.substring(0, 5);
    		// generate 6 random digits and append them to the ID
    		for (int i = 1; i <= 6; i++) {
    			this.id += CreditAcc.rand.nextInt(10);
    		}
     
    		// check the attributes
    		System.out.println("customer name: " + this.customerName);
    		System.out.println("balance: " + this.balance);
    		System.out.println("transaction fee: " + this.transactionFee);
    		System.out.println("generated ID: " + this.id);
    	}
     
    }
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0
    Thank you a lot

IMN logo majestic logo threadwatch logo seochat tools logo