Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Help with errors Excercise -create class program and objects.

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with errors Excercise -create class program and objects.

    bellow are the questions

    (a) Write a complete Java class definition named PrepaidCard to model a game arcade centre‘s prepaid token card. The class information is as described below:

    Class name: PrepaidCard

    Attributes: private String cardID
    private int tokenBalance

    Constructors:public PrepaidCard(String id)
    Purpose: This constructor has a parameter, which is id (type String). This constructor will instantiate this class object by assigning the parameter value to the object’s cardID attribute.

    public PrepaidCard(String id, int token)
    Purpose: This constructor has two parameters; id (type String) and token (type int). This constructor will instantiate this class object by assigning the parameter values to the object’s cardID and tokenBalance attributes respectively.

    Member
    Methods: public void addToken(int token)
    Purpose: To add the value of the method parameter to the object’s tokenBalance attribute. This method will display the number of tokens successfully added.

    public void deductToken(int token)
    Purpose: To deduct the value of the object’s tokenBalance attribute based on the parameter value. This method should have a checking mechanism. If the value of the object’s tokenBalance is lower than the token value (parameter value) to be deducted, notify the user that the PrepaidCard object has insufficient token, else deduct the token value from the object’s tokenBalance and display the total token successfully deducted.

    public String getCardID()
    Purpose: To return the value of the object’s cardID.

    public int getTokenBalance()
    Purpose: To return the value of the object’s tokenBalance.

    so this is what i did

     import java.util.Scanner;
     
    		public class PrepaidCard {
    			private String cardID;
    			private int tokenBalance = 0;
    			Scanner addToken = new Scanner(System.in);
    			Scanner delToken = new Scanner(System.in);
     
     
    			public PrepaidCard(String id){ //initiate class object by assigning value to id
    				id = cardID;
     
     
    			}
     
    			public PrepaidCard(String id,int token){ //initiate class object by assigning value to id and token
    				id = cardID;
    				token = tokenBalance;
     
     
    			}			
     
    			public void addToken(int token){ //add token to balance
    				System.out.println("Enter Token Addition Amount : ");
    				token = addToken.nextInt();
     
    				tokenBalance = tokenBalance + token;
     
    				System.out.println("Total of : " + addToken + " have been deducted");
    				System.out.println("Card Token Balance : " + tokenBalance);
     
    			}
     
    			public void deductToken(int token) { //deduct token from balance
    				System.out.println("Enter Token Deduction Amount : ");
    				token = delToken.nextInt();
     
    				if (token < tokenBalance) {
     
    				tokenBalance = tokenBalance - token;
     
    				System.out.println("Total of : " + delToken + " have been deducted");
    				System.out.println("Card Token Balance : " + tokenBalance);
    				}
     
    				else {
    					System.out.println("Insufficient Token Amount");
    				}
     
    			}
     
    			public String getcardID(){ //return cardID value
    				System.out.println("Identification: " + cardID);
    				return cardID;
     
     
    			}
     
    			public int gettokenBalance(){//return tokenBalance value
    				System.out.println("Card Token Balance : " + tokenBalance);
    				return tokenBalance;
     
    			}
    		}






    (b) Write a Java application to test the PrepaidCard class. In your application, create two PrepaidCard objects named card1 and card2 using the PrepaidCard(String id) and the PrepaidCard(String id, int token) constructors respectively. Display both objects’ cardID and tokenBalance. Next, ask the user to add token for card2 and display card2 new tokenBalance value. Deduct some token values from card2 and display the card2 new tokenBalance. Finally, deduct a much higher token value from the card2 current tokenBalance to test the checking mechanism of the tokenBalance value. You may use the Scanner class to read the input from user. A sample of the program execution is given below for your reference:

    Enter Card1 cardID: A123
    Enter Card2 cardID: B456
    Enter Card2 initial token: 100
    Card1 ID: A123
    Card1 token balance: 0
    Card2 ID: B456
    Card2 token balance: 100
    Enter total token to be added into Card2: 50
    Total of 50 tokens added
    Card2 token balance: 150
    Enter total token to be deducted from Card2: 80
    Total of 80 tokens deducted
    Card2 token balance: 70
    Enter total token to be deducted from Card2: 100
    Insufficient token. Please top-up
    Card2 token balance: 70

    This is what i did

    import java.util.Scanner;
    import java.io.*;
     
    public class ApplicationPrepaidCard {
     
    	public static void main(String[] args) throws IOException {
     
     
    		Scanner initialToken = new Scanner(System.in);
     
     
    		Scanner readID = new Scanner(System.in);
    		System.out.println("Enter Card1 Identification : ");//enter card1
    		String id = readID.nextLine();
     
    		PrepaidCard card1 = new PrepaidCard(id);
     
    		System.out.println("Enter Card2 Identification : ");//enter card2
    		String id = readID.nextLine();
    		System.out.println("Enter Card2 Initial Token : ");//enter initial 
    		int token = initialToken.nextInt();
     
    		PrepaidCard card2 = new PrepaidCard(id,token);
     
     
     
    		card1.getcardID();		
     
    		card1.gettokenBalance();
     
    		card2.getcardID();
     
    		card2.gettokenBalance();
     
    		card2.addToken();
     
    		card2.delToken();
     
    		card2.delToken();
     
     
     
     
    	}
     
    }


    i have errors at


    1) Duplicate local variable id ()
    String id = readID.nextLine();



    2) The method addToken(int) in the type PrepaidCard is not applicable for the arguments ()
    card2.addToken();
     
    card2.delToken();
     
    card2.delToken();

    i am stuck and i don't know what to do next...


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with errors Excercise -create class program and objects.

    Duplicate local variable id
    There can only be one variable with each name. The compiler sees two variables with the name: id.
    Either change the name of one of them, for example: id2
    or reuse the first variable and do not define a second variable.

    The method addToken(int) in the type PrepaidCard is not applicable for the arguments
    The compiler does not find a definition for the addToken() method that has no args. It does find a definition for addToken() that takes an int value.

    Either change the definition of AddToken() so it has no args
    or add an int arg to the calls to the method
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with errors Excercise -create class program and objects.

    Quote Originally Posted by Norm View Post
    There can only be one variable with each name. The compiler sees two variables with the name: id.
    Either change the name of one of them, for example: id2
    or reuse the first variable and do not define a second variable.
    i am supposed to use the same id for class. but is what you mean by use id2 in object?

    Quote Originally Posted by Norm View Post
    The compiler does not find a definition for the addToken() method that has no args. It does find a definition for addToken() that takes an int value.

    Either change the definition of AddToken() so it has no args
    or add an int arg to the calls to the method
    can you give me example of args i could do? i am actually confused.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with errors Excercise -create class program and objects.

    but is what you mean by use id2 in object?
    If there are two variables with the same name, change the name of one of them:
    String id = something;  //  define variable: id and assign it a value
    ...
    String id2 = somethingelse;  //define new variable: id2 (not id again) and assign it a value
     
    //NOTE the following is an error because id is already defined:
    String id = somethingelse;  // defines id again

    give me example of args
    addToken(1234); //  pass an int arg of 1234
    The actual value of the arg(instead of 1234) depends on what the addToken() method requires. You would need to look at the method to see what value it needs passed to it as an arg.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with errors Excercise -create class program and objects.

    Quote Originally Posted by Norm View Post
    If there are two variables with the same name, change the name of one of them:
    String id = something;  //  define variable: id and assign it a value
    ...
    String id2 = somethingelse;  //define new variable: id2 (not id again) and assign it a value
     
    //NOTE the following is an error because id is already defined:
    String id = somethingelse;  // defines id again
    is ok if i leave the class definition just like this?

    import java.util.Scanner;
     
    		public class PrepaidCard {
    			private String cardID;
    			private int tokenBalance = 0;
    			Scanner addToken = new Scanner(System.in);
    			Scanner delToken = new Scanner(System.in);
     
     
    			public PrepaidCard(String id){ //initiate class object by assigning value to id
    				id = cardID;
     
     
    			}
     
    			public PrepaidCard(String id,int token){ //initiate class object by assigning value to id and token
    				id = cardID;
    				token = tokenBalance;
     
     
    			}

    what i am understanding is i should change the object to as id2 but should i also make a new Scanner ? readID2???
    because if i do will it not cause error to the class??

    		Scanner readID = new Scanner(System.in);
    		System.out.println("Enter Card1 Identification : ");//enter card1
    		String id = readID.nextLine();
     
    		PrepaidCard card1 = new PrepaidCard(id);
     
    		System.out.println("Enter Card2 Identification : ");//enter card2
    		String id2 = readID.nextLine();
    		System.out.println("Enter Card2 Initial Token : ");//enter initial 
    		int token = initialToken.nextInt();
     
    		PrepaidCard card2 = new PrepaidCard(id,token);


    Quote Originally Posted by Norm View Post
    addToken(1234); //  pass an int arg of 1234
    The actual value of the arg(instead of 1234) depends on what the addToken() method requires. You would need to look at the method to see what value it needs passed to it as an arg.
    is it possible to put the arg value base on the key in from scanner? because i already did at the class.


    }			
     
    			public void addToken(int token){ //add token to balance
    				System.out.println("Enter Token Addition Amount : ");
    				token = addToken.nextInt();
     
    				tokenBalance = tokenBalance + token;
     
    				System.out.println("Total of : " + addToken + " have been deducted");
    				System.out.println("Card Token Balance : " + tokenBalance);
     
    			}
     
    			public void deductToken(int token) { //deduct token from balance
    				System.out.println("Enter Token Deduction Amount : ");
    				token = delToken.nextInt();
     
    				if (token < tokenBalance) {
     
    				tokenBalance = tokenBalance - token;
     
    				System.out.println("Total of : " + delToken + " have been deducted");
    				System.out.println("Card Token Balance : " + tokenBalance);
    				}
     
    				else {
    					System.out.println("Insufficient Token Amount");
    				}


    --- Update ---

    this might help.. this is the question..

    PURPOSE
    To create class program and its objects

    REQUIREMENT
    (a) Write a complete Java class definition named PrepaidCard to model a game arcade centre‘s prepaid token card. The class information is as described below:

    Class name: PrepaidCard

    Attributes: private String cardID
    private int tokenBalance

    Constructors: public PrepaidCard(String id)
    Purpose: This constructor has a parameter, which is id (type String). This constructor will instantiate this class object by assigning the parameter value to the object’s cardID attribute.

    public PrepaidCard(String id, int token)
    Purpose: This constructor has two parameters; id (type String) and token (type int). This constructor will instantiate this class object by assigning the parameter values to the object’s cardID and tokenBalance attributes respectively.

    Member
    Methods: public void addToken(int token)
    Purpose: To add the value of the method parameter to the object’s tokenBalance attribute. This method will display the number of tokens successfully added.

    public void deductToken(int token)
    Purpose: To deduct the value of the object’s tokenBalance attribute based on the parameter value. This method should have a checking mechanism. If the value of the object’s tokenBalance is lower than the token value (parameter value) to be deducted, notify the user that the PrepaidCard object has insufficient token, else deduct the token value from the object’s tokenBalance and display the total token successfully deducted.

    public String getCardID()
    Purpose: To return the value of the object’s cardID.

    public int getTokenBalance()
    Purpose: To return the value of the object’s tokenBalance.







    (b) Write a Java application to test the PrepaidCard class. In your application, create two PrepaidCard objects named card1 and card2 using the PrepaidCard(String id) and the PrepaidCard(String id, int token) constructors respectively. Display both objects’ cardID and tokenBalance. Next, ask the user to add token for card2 and display card2 new tokenBalance value. Deduct some token values from card2 and display the card2 new tokenBalance. Finally, deduct a much higher token value from the card2 current tokenBalance to test the checking mechanism of the tokenBalance value. You may use the Scanner class to read the input from user. A sample of the program execution is given below for your reference:

    Enter Card1 cardID: A123
    Enter Card2 cardID: B456
    Enter Card2 initial token: 100
    Card1 ID: A123
    Card1 token balance: 0
    Card2 ID: B456
    Card2 token balance: 100
    Enter total token to be added into Card2: 50
    Total of 50 tokens added
    Card2 token balance: 150
    Enter total token to be deducted from Card2: 80
    Total of 80 tokens deducted
    Card2 token balance: 70
    Enter total token to be deducted from Card2: 100
    Insufficient token. Please top-up
    Card2 token balance: 70

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with errors Excercise -create class program and objects.

    because i already did at the class.
    Is that what the assignment says to do?
    Methods: public void addToken(int token)
    Purpose: To add the value of the method parameter to the object’s tokenBalance attribute. This method will display the number of tokens successfully added.
    That says to pass an int value as an arg

    About the variable: id
    You can use it more than one time
    or you can define a new variable (like id2) and use that the next time.
    It's up to you
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jul 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with errors Excercise -create class program and objects.

    i am so sorry i am at the beginner level so i might be asking silly questions because i don't understand what you mean.


    Quote Originally Posted by Norm View Post
    Is that what the assignment says to do?

    That says to pass an int value as an arg

    About the variable: id
    You can use it more than one time
    or you can define a new variable (like id2) and use that the next time.
    It's up to you

    PrepaidCard(String id)
    Purpose: This constructor has a parameter, which is id (type String). This constructor will instantiate this class object by assigning the parameter value to the object’s cardID attribute.

    public PrepaidCard(String id, int token)
    Purpose: This constructor has two parameters; id (type String) and token (type int). This constructor will instantiate this class object by assigning the parameter values to the object’s cardID and tokenBalance attributes respectively.

    because we have learn about overload constructor but we were not told on how to use it. so i am guessing that that might be what the assignment wants us to use in order to approach to the purpose.

    so if i were to use both id as id. and then on object to let user input both the value of the id, how should i do it?
    is there any example?


    Quote Originally Posted by Norm View Post

    That says to pass an int value as an arg

    It's up to you
    and for the arg. if i set the arguments to let say
    addToken(1234) will my scanner works later??

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with errors Excercise -create class program and objects.

    The asssignment says:
    Member
    Methods: public void addToken(int token)
    Purpose: To add the value of the method parameter to the object’s tokenBalance attribute. This method will display the number of tokens successfully added.
    The int token arg is to be passed to the addToken() method.

    I don't understand the problem with the id variable(s).
    The statement that starts with:
    String id
    defines a variable.
    A variable can only be defined one time in a class or method. The compiler gives an error message if a variable is defined more than once.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Can't create Balloon Objects.
    By Lifeleaf in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 26th, 2014, 03:40 PM
  2. Can't create Balloon Objects.
    By Lifeleaf in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 26th, 2014, 02:20 PM
  3. How do you create Date objects?
    By ojonugwa in forum Java Theory & Questions
    Replies: 4
    Last Post: August 27th, 2013, 01:32 PM
  4. Java Class Excercise (Related to Strings + Cmd Line Arguments)
    By Wwong3333 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 19th, 2012, 09:22 PM
  5. Create Objects on Demand?
    By Programmierer in forum Object Oriented Programming
    Replies: 5
    Last Post: July 7th, 2010, 01:13 PM