Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.Random; public class Card { int face, suit; static final int SUITS = 4; static final int FACES = 13; public Card() { Random

import java.util.Random;

public class Card

{

int face, suit;

static final int SUITS = 4;

static final int FACES = 13;

public Card()

{

Random gen = new Random();

face = gen.nextInt(FACES) + 1;

suit = gen.nextInt(SUITS) + 1;

}

public Card(int face, int suit)

{

this.face = face;

this.suit = suit;

}

public void setFace(int face)

{ this.face = face; }

public void setSuit(int suit)

{ this.suit = suit; }

public int getPoints()

{ int points = 0;

switch (face)

{ case 2: case 3:

case 4: case 5: case 6:

case 7: case 8: case 9:

points = face;

break;

case 1:

points = 11;

break;

default:

points = 10;

}

return points;

}

public String toString()

{ String faceString, suitString;

switch (face)

{ case 10: case 2: case 3:

case 4: case 5: case 6:

case 7: case 8: case 9:

faceString = Integer.toString(face);

break;

case 1:

faceString = "Ace"; break;

case 11:

faceString = "Jack"; break;

case 12:

faceString = "Queen"; break;

case 13:

faceString = "King"; break;

default:

faceString = "Unknown face value";

}

switch (suit)

{ case 1:

suitString = "Hearts"; break;

case 2:

suitString = "Diamonds"; break;

case 3:

suitString = "Clubs"; break;

case 4:

suitString = "Spades"; break;

default:

suitString = "Unknown suit";

}

return (faceString + " of " + suitString);

}

}

import java.util.Scanner;

public class CardGame { public static void main (String[] args) { Scanner in = new Scanner (System.in);

Card myCard1 = new Card(); Card myCard2 = new Card();

while (myCard2.suit == myCard1.suit && myCard2.face == myCard1.face) { System.out.println("Same card"); myCard2 = new Card(); }

System.out.println(myCard1.toString()); System.out.println(myCard2.toString()); System.out.println("Points = " + (myCard1.getPoints() + myCard2.getPoints())); }

} image text in transcribed

Please provide the code with the required actions and the updated code.

Complete the following actions using a printed copy a Java class. Place a check beside each box as the task is completed. Locate vulnerabilities in data members instance variables and constants): O Place a beside each data member with private access. Write V1 beside each data member with public access. O Write V2 beside each data member with no access modifier (also called the default modifier or package-private modifier). (The protected modifier is related to inheritance and is not covered in this lab.) Locate vulnerabilities in methods: Place a beside the method header of each method that does not return a value and does not update any data member's value. O Write V3 beside the method header of each method that returns the value of a data member. These methods are often called accessors or getters. o Write V4 beside the method header of each method that modifies the value of data members. These methods are often called mutators or setters. O Write an P beside the method header of each method (including constructors) that uses a parameter list. All parameter values should be validated to ensure they fall within the bounds of the method's intended purpose. For each parameter, write V5 over that parameter's first appearance in code within the method. Eliminate vulnerabilities where feasible: For each data member marked with V1: Consider changing the access modifier to private. Unless there is a compelling reason, each data members should be declared using the private access modifier to protect it from direct access and/or manipulation from outside classes. If the value of the data member is needed by external classes, use the private access modifier and create an accessor method for that data member. Mark any changes you made on your program listing. For each data member marked with V2: Consider adding the private access modifier. Unless there is a compelling reason, each data members should be declared using the private access modifier to protect it from direct access and/or manipulation from outside classes. If the value of the data member is needed by external classes, use the private access modifier and create an accessor method for that data member. Mark any changes you made on your program listing. O For each method marked with V3: These are accessor methods which return the value of a data member. Consider the purpose of this method. Does an external class really need this value? Secure coding guidelines recommend avoiding accessor methods that are not truly needed. Strike-through the method header for each accessor method that you recommend deleting from the class you are reviewing. If there are data members that you feel need an accessor method, consider the sensitivity of that data item (such as social security numbers, credit card numbers, etc.) Write E (for encryption) beside the method header for any accessor method that you recommend using encryption on the returned value. For each method marked with V4: These are mutator methods which modify the value of one or more data members. Review the code for each data members being modified. Is this method needed? Do not include a mutator method for a data member unless it is absolutely needed. Strike-through the method header for each mutator method that you recommend deleting from the class you are reviewing. For each parameter marked with V5: Review the method's code for each use of the parameter to ensure the parameter's value is validated before it is used in the method. For any parameter that is not validated, write code, on the program listing, that will validate the parameter's value and handle invalid data properly. 3. Using your marked-up Card.java listing, update the class to improve class encapsulation. 4. Compile your modified version of the Card class. Debug the program to ensure it is working properly with CardGame.java. Modify CardGame.java, if needed (i.e. remove references to methods you may have removed from Card.java). Complete the following actions using a printed copy a Java class. Place a check beside each box as the task is completed. Locate vulnerabilities in data members instance variables and constants): O Place a beside each data member with private access. Write V1 beside each data member with public access. O Write V2 beside each data member with no access modifier (also called the default modifier or package-private modifier). (The protected modifier is related to inheritance and is not covered in this lab.) Locate vulnerabilities in methods: Place a beside the method header of each method that does not return a value and does not update any data member's value. O Write V3 beside the method header of each method that returns the value of a data member. These methods are often called accessors or getters. o Write V4 beside the method header of each method that modifies the value of data members. These methods are often called mutators or setters. O Write an P beside the method header of each method (including constructors) that uses a parameter list. All parameter values should be validated to ensure they fall within the bounds of the method's intended purpose. For each parameter, write V5 over that parameter's first appearance in code within the method. Eliminate vulnerabilities where feasible: For each data member marked with V1: Consider changing the access modifier to private. Unless there is a compelling reason, each data members should be declared using the private access modifier to protect it from direct access and/or manipulation from outside classes. If the value of the data member is needed by external classes, use the private access modifier and create an accessor method for that data member. Mark any changes you made on your program listing. For each data member marked with V2: Consider adding the private access modifier. Unless there is a compelling reason, each data members should be declared using the private access modifier to protect it from direct access and/or manipulation from outside classes. If the value of the data member is needed by external classes, use the private access modifier and create an accessor method for that data member. Mark any changes you made on your program listing. O For each method marked with V3: These are accessor methods which return the value of a data member. Consider the purpose of this method. Does an external class really need this value? Secure coding guidelines recommend avoiding accessor methods that are not truly needed. Strike-through the method header for each accessor method that you recommend deleting from the class you are reviewing. If there are data members that you feel need an accessor method, consider the sensitivity of that data item (such as social security numbers, credit card numbers, etc.) Write E (for encryption) beside the method header for any accessor method that you recommend using encryption on the returned value. For each method marked with V4: These are mutator methods which modify the value of one or more data members. Review the code for each data members being modified. Is this method needed? Do not include a mutator method for a data member unless it is absolutely needed. Strike-through the method header for each mutator method that you recommend deleting from the class you are reviewing. For each parameter marked with V5: Review the method's code for each use of the parameter to ensure the parameter's value is validated before it is used in the method. For any parameter that is not validated, write code, on the program listing, that will validate the parameter's value and handle invalid data properly. 3. Using your marked-up Card.java listing, update the class to improve class encapsulation. 4. Compile your modified version of the Card class. Debug the program to ensure it is working properly with CardGame.java. Modify CardGame.java, if needed (i.e. remove references to methods you may have removed from Card.java)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Visualizing Health And Healthcare Data Creating Clear And Compelling Visualizations To See How Youre Doing

Authors: Katherine Rowell ,Lindsay Betzendahl ,Cambria Brown

1st Edition

1119680883, 978-1119680888

More Books

Students also viewed these Databases questions