1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
| // 1a
public int getScore()
{
int score = 0;
// Check if this is a bonus game
if (isBonus()) {
// If it is, triple the final score
score = 3;
}
// Iterate through each level
for (int i = 1; i <= 3; i++) {
// Check if the goal for the current level has been reached
if (level[i].goalReached()) {
// If it has, add the points for that level to the score
score += level[i].getPoints();
}
}
// Return the final score
return score;
}
// 1b
public int playManyTimes(int num)
{
// Keep track of the highest score seen so far
int highestScore = 0;
// Iterate for the number of games specified in the num parameter
for (int i = 0; i < num; i++) {
// Create a new game
Game game = new Game();
// Simulate the play of the game
game.play();
// Compute the score for the game
int score = game.getScore();
// If the score for this game is higher than the highest score seen so far,
// update the highest score
if (score > highestScore) {
highestScore = score;
}
}
// Return the highest score seen
return highestScore;
}
// 2
public class Textbook extends Book {
private int edition;
/**
* Creates a new Textbook with given title, price, and edition number.
*
* @param bookTitle the title of the textbook
* @param bookPrice the price of the textbook
* @param bookEdition the edition number of the textbook
*/
public Textbook(String bookTitle, double bookPrice, int bookEdition) {
super(bookTitle, bookPrice);
this.edition = bookEdition;
}
/**
* Returns the edition number of the textbook.
*
* @return the edition number of the textbook
*/
public int getEdition() {
return edition;
}
/**
* Returns the title, price, and edition number of the Textbook as a string.
*
* @return the title, price, and edition number of the Textbook as a string
*/
@Override
public String getBookInfo() {
return super.getBookInfo() + "-" + edition;
}
/**
* Returns true if this Textbook is a valid substitute for the Textbook referenced by the parameter
* of the canSubstituteFor method. Returns false otherwise.
*
* @param other the Textbook to compare to this Textbook
* @return true if this Textbook is a valid substitute for the Textbook referenced by the parameter
* of the canSubstituteFor method, false otherwise
*/
public boolean canSubstituteFor(Textbook other) {
return this.getTitle().equals(other.getTitle()) && this.getEdition() >= other.getEdition();
}
}
// 3a
public double getAverageRating()
{
double totalRating = 0;
int numReviews = allReviews.length;
for (Review review : allReviews)
{
totalRating += review.getRating();
}
return totalRating / numReviews;
}
// 3b
public ArrayList<String> collectComments()
{
ArrayList<String> formattedComments = new ArrayList<>();
for (int i = 0; i < allReviews.length; i++)
{
String comment = allReviews[i].getComment();
if (comment.contains("!"))
{
formattedComments.add(i + "-" + comment);
if (!(comment.endsWith(".") || comment.endsWith("!")))
{
formattedComments.set(formattedComments.size() - 1, formattedComments.get(formattedComments.size() - 1) + ".");
}
}
}
return formattedComments;
}
// 4a
public void repopulate()
{
Random rand = new Random();
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
int val = rand.nextInt(MAX - 1) + 1;
while (val % 10 != 0 || val % 100 == 0) {
val = rand.nextInt(MAX - 1) + 1;
}
grid[i][j] = val;
}
}
}
// 4b
public int countIncreasingCols()
{
int count = 0;
for (int i = 0; i < grid[0].length; i++) {
boolean isIncreasing = true;
for (int j = 1; j < grid.length; j++) {
if (grid[j][i] < grid[j - 1][i]) {
isIncreasing = false;
break;
}
}
if (isIncreasing) {
count++;
}
}
return count;
} |
Partager