1.
ALGORITHM:
1. Set variables for the following data stored already in the program.
a. String word = keyboard.next()
b. Scanner keyboard = new Scanner(System.in)
2. Set variable for the user’s data entry.
3. Prompt the user for enter the variable and store it
a. System.out.println("Enter a word")
4. Logic
a. The word entered would go into a loop the first part of the loop would be if
the first and last character are equals a message would be display. The next
part of the loop would be if the first and last character are NOT equals.
5. Displayed
a. If the first and last character are equals the bottom message will display:
i. "First and last character are equals: " + word
b. If the first and last character are NOT equals the bottom message will display:
i. "First and last character are NOT equals: " + word
2.
ALGORITHM:
1. Set variables for the following data stored already in the program.
a. Double balance = 0.0,
b. yearRate = 0.025
c. Scanner keyboard = new Scanner(System.in)
d. String transaction = keyboard.next()
2. Set variable for the user’s data entry.
3. Prompt the user for enter one of the 3 variables and store it
a. Enter d for deposit
b. Enter w for withdrawal
4. Logic
a. A while statement will be transaction.equals("x"). If the balance is greater
than zero balance = balance + year rate. If “d” is entered, then balance = balance
+ (number entered). If “w” is entered, then balance = balance - (number entered)
5. Displayed
a. Starting message:
i. "Enter d for deposit, w for withdrawal”
b. If “d” is entered the bottom message will display:
ii. “Enter deposit amount”
c. If “w” is entered the bottom message will display:
iii. “Enter withdrawal amount"
d. End message:
iv. "Balance after transaction: " + balance
5.
ALGORITHM:
1. Set variables for the following data stored already in the program.
a. int s = 0;
b. int t = 1;
c. int i = 0;
d. int j = i;
2. Set variable for the user’s data entry.
3. Prompt the user for enter the variables and store it
a. There is nothing for the user to input.
4. Logic
a. If i is smaller than 10, s would be added to i equaling new s then j would
equal i. if j is greater than 0, t would equal t multiplied by (j-j) and then j--.
Lastly s would equal s multiplied by t.
5. Displayed
a. T’s would display the following:
i. T is 1, T is 0, T is 0, T is 0, T is 0, T is 0, T is 0, T is 0, T is 0 and T is 0
b. S’s Would display the following:
i. S is 0
Code:
int s = 0;
int t = 1;
int i = 0;
while(i < 10)
{
s = s + i;
int j = i;
while(j > 0)
{
t = t * (j - i);
j--;
}
s = s * t;
System.out.println("T is " + t);
i++;
}
System.out.println("S is " + s);
10.
ALGORITHM:
1. Set variables for the following data stored already in the program.
a. Scanner keyboard = new Scanner(System.in)
b. int heads = 0;
c. tails = 0;
d. String numEnding = "th";
e. numEnding = "st";
f. numEnding = "nd";
g. numEnding = "rd";
h. 2. Set variable for the user’s data entry.
i. 3. Prompt the user for enter the variables and store it.
a. System.out.println("For each coin toss enter either h for heads or t for tails.")
j. 4. Logic
a. i is equal to one and in the for loop it adds one to i in order to get the 1st toss
after the loop end it ask for the user to input a h or t then the loop starts over
moving on to 2nd toss and so on. Then the cold will add the numbers of h and
t then divided by 8 in order to get the percentage.
5. Displayed
a. The message should display the following:
k. For each coin toss enter either h for heads or t for tails.
l. 1st toss:
m. Input: (h or t)
n. 2nd toss:
o. Input: (h or t)
p. 3rd toss:
q. Input: (h or t)
r. 4th toss:
s. Input: (h or t)
t. 5th toss:
u. Input: (h or t)
v. 6th toss:
w. Input: (h or t)
x. 7th toss:
y. Input: (h or t)
z. 8th toss:
aa. Input: (h or t)
ab. Number of heads: (number of h’s) (%)
ac. Number of tails: (number of t’s) (%)
ad. Code:
ae. Scanner keyboard = new Scanner(System.in);
af. int heads = 0,
ag. tails = 0;
ah. System.out.println("For each coin toss enter either h for heads or t for tails.");
ai. for(int i = 1; i <= 8; i++)
aj. {
ak. String numEnding = "th";
al. if(i == 1)
am. {
an. numEnding = "st";
ao. }
ap. else if(i == 2)
aq. {
ar. numEnding = "nd";
as. }
at. else if(i == 3)
au. {
av. numEnding = "rd";
aw. }
ax. System.out.println(i + numEnding + " toss:");
ay. String type = keyboard.next();
az. if(type.equals("h"))
ba. {
bb. heads++;
bc. }
bd. else
be. {
bf. tails++;
bg. }
bh. }
bi. System.out.println("Number of heads: " + heads + " (" + (((double)heads / 8.0) *
100) + "%)");
bj. System.out.println("Number of tails: " + tails + " (" + (((double)tails / 8.0) * 100) +
"%)");
Powered by TCPDF (www.tcpdf.org)