Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Description: Encapsulation is the idea of defining where and how certain parts of the program can be accessed, which has many benefits, namely reducing human

image text in transcribedimage text in transcribedimage text in transcribed

Description: Encapsulation is the idea of defining where and how certain parts of the program can be accessed, which has many benefits, namely reducing human error, and in doing so, improving the security of the program.* In this lab, you will use the private keyword to create variables that are restricted to only be accessible within their class, and the public keyword to create getters and setters, methods used to access those private variables outside of the class. *As a note, encapsulation is not a security measure per se, but helps to prevent compromising other security measures Steps: 1. Type the following starter code in a new file called CreditCard.java: public class CreditCard\{ //Two private variables here //The private modifier means that the variables here can only be accessed within this class, external access will have to be done with non-private methods. private String name; private String creditCardNumber; \} 2. Add another private String called expirationDate, and a private int called cvv 3. Type the following code below your instance variables: //This is a setter, and just as it's name would suggest, it sets the name variable. public void setName(String name) \{ this . name = name; \} //Also clearly named, here we get the name variable public string getName() \{ return name; \} /ote how both methods have the public modifier. This allows us to access the methods anywhere throughout the program. 4. Create the remaining getters and setters in CreditCard.java 5. Create a new File called CardFactory.java 6. Create your class declaration 7. In the CardFactory.java file create a method called createCard which has a return type of a CreditCard. 8. The createCard method should accept 4 parameters for the name, creditCardNumber, expirationDate, and CVV. 9. Create a CreditCard object and use the setters to set the values of name, creditCardNumber, expirationDate, and cvv to the values that the method will take in. 10. Create a print statement that will print "Your card details:" 11. Create one or more print statements that will get the values of the name, creditCardNumber, expirationDate, and cvv. Be sure to include a description of what each are in your print statement. For example (This code snippit is not part of the program): System. out.println( "The persons name is: " + ); 12. Finish the method by returning the correct return value. For example (This code snippit is not part of the program): public string greeting()\{ return "Hello! Hope you enjoy coding!"; \} 13. Back in Main.java, Create a new card factory object. 14. Call the createCard method and set the name to "Annabeth Chase", the number to "3242542123112744", the expiration date to "01/25", and the CVV to 633 15. Call the createCard method again and set the name value to "Orange Blossom", the number to "3242542123112744" , the expiration date to "06/33", and the cvv to 234 Test: Use the test provided. Tests can be found in the checkmark on the bottom of the left-hand menu Sample output: Your card details: Cardholder Name: Annabeth Chase Card Number: 3242542123112744 Expiration Date: 01/25 CVV: 633 Your card details: Cardholder Name: Orange Blossom Card Number: 3242893475982634 Expiration Date: 06/33 CVV: 234

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions