MyProgrammingLab Java

myprogramminglab java20787 – 20931

20787

A String variable , fullName, contains a name in one of two formats:

last name , first name (comma followed by a blank), or
first name last name (single blank)
Extract the first name into the String variable firstName and the last name into the String variable lastName. Assume the variables have been declared and fullNamealready initialized . You may also declare any other necessary variables .

SOLUTION
if (fullName.indexOf(“, “) != -1) {
lastName = fullName.substring(0, fullName.indexOf(“, “));
firstName = fullName.substring(fullName.indexOf(“, “) + 2, fullName.length());
} else {
firstName = fullName.substring(0, fullName.indexOf(” “));
lastName = fullName.substring(fullName.indexOf(” “) + 1, fullName.length());
}


20921
Assume that a boolean variable isQuadrilateral has been declared , and that another variable , numberOfSides has been declared and initialized . Write a statement that assigns the value true if numberOfSides is exactly 4 and false otherwise.

SOLUTION
if (numberOfSides == 4)
isQuadrilateral = true;
else
isQuadrilateral = false;


20927
Assume that the variables gpa , deansList and studentName , have been declared and initialized . Write a statement that adds 1 to deansList and prints studentName to standard out if gpa exceeds 3.5.

SOLUTION
if (gpa > 3.5) {
deansList += 1;
System.out.print(studentName); }


20928
Assume that isIsosceles is a boolean variable , and that the variables isoCount , triangleCount , and polygonCount have all been declared and initialized . Write a statement that adds 1 to each of these count variables ( isoCount , triangleCount , and polygonCount )if isIsosceles is true .
SOLUTION
if(isIsosceles) {
isoCount++;
triangleCount++;
polygonCount++; }


20929
Write a statement that adds 1 to the variable reverseDrivers if the variable speed is less than 0 ,adds 1 to the variable parkedDrivers if the variable speed is less than 1 ,adds 1 to the variable slowDrivers if the variable speed is less than 40 ,adds 1 to the variable safeDrivers if the variable speed is less than or equal to 65 , and otherwise adds 1 to the variable speeders .

SOLUTION
if(speed < 0) {
reverseDrivers ++;
} else if (speed < 1) {
parkedDrivers ++;
} else if (speed < 40) {
slowDrivers ++;
} else if (speed <= 65) {
safeDrivers++;
} else if (speed>65) {
speeders ++;
}


20930
Write a statement that compares the values of score1 and score2 and takes the following actions. When score1 exceeds score2 , the message “player1 wins” is printed to standard out. When score2 exceeds score1 , the message “player2 wins” is printed to standard out. In each case, the variables player1Wins, , player1Losses , player2Wins, and player2Losses, , are incremented when appropriate.

Finally, in the event of a tie, the message “tie” is printed and the variable tieCount is incremented .

SOLUTION
if (score1 > score2) {
cout << “player1 wins” << endl;
++player1Wins;
++player2Losses;
} else if (score2 > score1) {
cout << “player2 wins” << endl;
++player2Wins;
++player1Losses;
} else {
cout << “tie” << endl;
++tieCount;
}


20931
Online Book Merchants offers premium customers 1 free book with every purchase of 5 or more books and offers 2 free books with every purchase of 8 or more books. It offers regular customers 1 free book with every purchase of 7 or more books, and offers 2 free books with every purchase of 12 or more books.

Write a statement that assigns freeBooks the appropriate value based on the values of the boolean variable isPremiumCustomer and the int variable nbooksPurchased .

SOLUTION
freeBooks = 0;
if (isPremiumCustomer == true)
{
if (nbooksPurchased >= 5)
freeBooks = 1;
if (nbooksPurchased >= 8)
freeBooks = 2;
}

else if (isPremiumCustomer == false)
{
if (nbooksPurchased >= 7)
freeBooks = 1;
if (nbooksPurchased >= 12)
freeBooks = 2;
}

%d bloggers like this: