The single statement that generates the random number object winner is:
int winner = new Random().nextInt(49) + 1;
−Phoenix
Tuesday, February 21, 2012
Challenge 046’s Solution
To complete Challenge 46, the following code must be placed into BlueJ™:
import java.lang.Double;
import java.lang.Math;
public class Challenge046
{
public Double squareRoot(Double d)
{
// Pre-condition: d ≥ 0
// Post-condition: returns a Double value equal to √d
double doubD = Double.valueOf(d);
double sqrtD = Math.sqrt(doubD);
return sqrtD;
}
}
−Phoenix
import java.lang.Double;
import java.lang.Math;
public class Challenge046
{
public Double squareRoot(Double d)
{
// Pre-condition: d ≥ 0
// Post-condition: returns a Double value equal to √d
double doubD = Double.valueOf(d);
double sqrtD = Math.sqrt(doubD);
return sqrtD;
}
}
−Phoenix
Tuesday, February 14, 2012
Challenge 045’s Solution
To complete Challenge 45, the following code must be placed into BlueJ™:
import info.gridworld.actor.Actor;
import info.gridworld.actor.Rock;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
public class RollingStone extends Rock
{
public RollingStone()
{
super();
setDirection(270);
}
public void act()
{
if (canMove())
move();
else
turn();
}
public void turn()
{
setDirection(getDirection() + Location.HALF_CIRCLE);
}
public void move()
{
Grid<Actor> gr = getGrid();
if (gr == null)
return;
Location loc = getLocation();
Location next = loc.getAdjacentLocation(getDirection());
Actor neighbor = gr.get(next);
if (neighbor != null)
neighbor.removeSelfFromGrid();
if (gr.isValid(next))
moveTo(next);
else
removeSelfFromGrid();
}
public boolean canMove()
{
Grid<Actor> gr = getGrid();
if (gr == null)
return false;
Location loc = getLocation();
Location next = loc.getAdjacentLocation(getDirection());
if (!gr.isValid(next))
return false;
Actor neighbor = gr.get(next);
return true;
}
}
−Phoenix
import info.gridworld.actor.Actor;
import info.gridworld.actor.Rock;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
public class RollingStone extends Rock
{
public RollingStone()
{
super();
setDirection(270);
}
public void act()
{
if (canMove())
move();
else
turn();
}
public void turn()
{
setDirection(getDirection() + Location.HALF_CIRCLE);
}
public void move()
{
Grid<Actor> gr = getGrid();
if (gr == null)
return;
Location loc = getLocation();
Location next = loc.getAdjacentLocation(getDirection());
Actor neighbor = gr.get(next);
if (neighbor != null)
neighbor.removeSelfFromGrid();
if (gr.isValid(next))
moveTo(next);
else
removeSelfFromGrid();
}
public boolean canMove()
{
Grid<Actor> gr = getGrid();
if (gr == null)
return false;
Location loc = getLocation();
Location next = loc.getAdjacentLocation(getDirection());
if (!gr.isValid(next))
return false;
Actor neighbor = gr.get(next);
return true;
}
}
−Phoenix
Tuesday, February 7, 2012
Challenge 044’s Solution
The code in Challenge 44 can be rewritten using a for loop in the following way:
int sum = 0;
for (int index = 0; index < arr.length; index++)
{
sum += arr[index];
}
It can also be rewritten using a for-each loop in the following way:
.
−Phoenix
int sum = 0;
for (int index = 0; index < arr.length; index++)
{
sum += arr[index];
}
It can also be rewritten using a for-each loop in the following way:
.
int sum = 0;
for (int index : arr)
{
sum += index;
}
for (int index : arr)
{
sum += index;
}
Monday, February 6, 2012
Challenge 043’s Solution
The “for each” loop is used to determine the values of sum1 and sum2. These loops are used for arrays such as the int[] array named arr. The loop creates an int variable named num. Each time the loop runs, num is equal to the next element in the arr array. If num is either positive or odd, then the element value num is added to the total value of sum1. Otherwise, if num is negative and even, then the element value num is added to the total value of sum2. The loop processes every value of the arr array through num. Therefore, the sum1 value output represents the sum of all positive or odd numbers in the arr list, while the sum2 value output represents the sum of all negative and even numbers in the arr list.
−Phoenix
PS: In line 10 of your code, you mistakenly wrote “sum += num;” instead of “sum1 += num;”.
−Phoenix
PS: In line 10 of your code, you mistakenly wrote “sum += num;” instead of “sum1 += num;”.
Thursday, February 2, 2012
Challenge 042’s Solution
To complete Challenge 42, the following code must be placed into BlueJ™:
−Phoenix
import java.util.Arrays;
public class Challenge042
{
public static void main(String[] args)
{
int[] scores = {97,56,78,88};
Arrays.sort(scores);
System.out.println("Original Scores:");
for(int x = 0; x <= scores.length - 1; x++)
{
System.out.println(scores[x]);
}
System.out.println();
curveScores(scores);
}
public static void curveScores(int[] s)
{
int[] sortedScores = s;
Arrays.sort(sortedScores);
int curve = 100 - sortedScores[sortedScores.length - 1];
System.out.println("Curved Scores:");
for(int x = 0; x <= s.length - 1; x++)
{
System.out.println((s[x] + curve));
}
}
}
public class Challenge042
{
public static void main(String[] args)
{
int[] scores = {97,56,78,88};
Arrays.sort(scores);
System.out.println("Original Scores:");
for(int x = 0; x <= scores.length - 1; x++)
{
System.out.println(scores[x]);
}
System.out.println();
curveScores(scores);
}
public static void curveScores(int[] s)
{
int[] sortedScores = s;
Arrays.sort(sortedScores);
int curve = 100 - sortedScores[sortedScores.length - 1];
System.out.println("Curved Scores:");
for(int x = 0; x <= s.length - 1; x++)
{
System.out.println((s[x] + curve));
}
}
}
−Phoenix
Wednesday, February 1, 2012
Challenge 041’s Solution
To complete Challenge 41, the following codes must be placed into BlueJ™:
public class BankAccount
{
public double balance;
public BankAccount()
{
}
public BankAccount(double b)
{
balance = b;
}
public double makeDeposit(double d)
{
System.out.println("You’ve deposited $" + d + ". \nYour current balance is $" + (balance + d) + ".");
return balance += d;
}
public double makeWithdraw(double w)
{
if (w <= balance)
{
System.out.println("You’ve withdrawn $" + w + ". \nYour current balance is $" + (balance - w) + ".");
return balance -= w;
}
else
{
System.out.println("ERROR: You cannot withdraw $" + w + "! \nYour current balance is $" + balance + ".");
return balance;
}
}
public double getBalance()
{
System.out.println("Your current balance is $" + balance + ".");
return balance;
}
}
−Phoenix
public class BankAccount
{
public double balance;
public BankAccount()
{
}
public BankAccount(double b)
{
balance = b;
}
public double makeDeposit(double d)
{
System.out.println("You’ve deposited $" + d + ". \nYour current balance is $" + (balance + d) + ".");
return balance += d;
}
public double makeWithdraw(double w)
{
if (w <= balance)
{
System.out.println("You’ve withdrawn $" + w + ". \nYour current balance is $" + (balance - w) + ".");
return balance -= w;
}
else
{
System.out.println("ERROR: You cannot withdraw $" + w + "! \nYour current balance is $" + balance + ".");
return balance;
}
}
public double getBalance()
{
System.out.println("Your current balance is $" + balance + ".");
return balance;
}
}
public class SavingsAccount extends BankAccount
{
public double interest;
public SavingsAccount()
{
}
public SavingsAccount(double balance, double i)
{
super(balance);
interest = i;
}
public double addInterest()
{
return balance += balance * interest;
}
}
{
public double interest;
public SavingsAccount()
{
}
public SavingsAccount(double balance, double i)
{
super(balance);
interest = i;
}
public double addInterest()
{
return balance += balance * interest;
}
}
public class CheckingAccount extends BankAccount
{
public double minBalance;
public double fee;
public CheckingAccount()
{
}
public CheckingAccount(double balance)
{
super(balance);
minBalance = 50.0;
fee = 2.0;
}
public double makeWithdraw(double w)
{
if (w <= balance)
{
if (balance - w < 50.)
{
System.out.println("You’ve withdrawn $" + w + ", and have been charged $2.00. \nYour current balance is $" + (balance - (w + 2.)) + ".");
return balance -= w + 2.;
}
else
{
System.out.println("You’ve withdrawn $" + w + ". \nYour current balance is $" + (balance - w) + ".");
return balance -= w;
}
}
else
{
System.out.println("ERROR: You cannot withdraw $" + w + "! \nYour current balance is $" + balance + ".");
return balance;
}
}
}
{
public double minBalance;
public double fee;
public CheckingAccount()
{
}
public CheckingAccount(double balance)
{
super(balance);
minBalance = 50.0;
fee = 2.0;
}
public double makeWithdraw(double w)
{
if (w <= balance)
{
if (balance - w < 50.)
{
System.out.println("You’ve withdrawn $" + w + ", and have been charged $2.00. \nYour current balance is $" + (balance - (w + 2.)) + ".");
return balance -= w + 2.;
}
else
{
System.out.println("You’ve withdrawn $" + w + ". \nYour current balance is $" + (balance - w) + ".");
return balance -= w;
}
}
else
{
System.out.println("ERROR: You cannot withdraw $" + w + "! \nYour current balance is $" + balance + ".");
return balance;
}
}
}
−Phoenix
Subscribe to:
Posts (Atom)