# Thread: simple if statements and loops

1. Member Join Date
Sep 2009
Posts
4
Rep Power
0

## simple if statements and loops

I am working on some practice problems and cannot seem to figure out these questions for if statements:

1)public String xeqy(String x, String y){
String msg = "";
// write an if condition that will determine if two strings are equal
if( REPLACE WITH CONDITION) {
msg = "x and y have the same value";
}
return msg;
}

2) public String xnqy(String x, String y){
String msg = "";
// write an if condition that will determine if two strings are NOT equal
if( REPLACE_WITH_CONDITION ) {
msg = "x and y have the different values";
}
return msg;
}

String msg = "";
// complete the if-else block to determine a students grade (A >=90; B >=80, C >=70, D >= 60, F <60)
if( REPLACE_WITH_CONDITION )( g >=90 ) {
msg = "Congratulations, you earned an A";
} else if (REPLACE WITH CONDITION){
msg = "Not bad, you earned a B";
} else if ( REPLACE_WITH_CONDITION ){
msg = "Average, you earned a C";
} else if ( REPLACE_WITH_CONDITION ){
msg = "Passing, you earned a D";
} else {
msg = "Failing, you earned a F";
}
return msg;
}

4) public String redlight(boolean status, String color) {
// if the status of the stop light is true, check the color and deliver the appropriate message
// if the status is false, then proceed with caution.
String msg = "";
if ( REPLACE_WITH_CONDITION ) {
if ( REPLACE_WITH_CONDITION ) {
msg = "stop the car";
} else if ( REPLACE_WITH_CONDITION ){
} else {
msg = "proceed with caution";
}
} else {
msg = "for way stop";
}
return msg;
}

I am trying to also figure out these loop questions. ANY guidelines would be great!

1) public int whilesumkj(int k, int j){
// Complete the method using a while loop that will add the numbers
// from k to j, where j is greater than k
int total = 0;
int i = k;
// TODO: ADD LOOP CODE HERE
}

2)public String arrayprint(){

String msg = "";
String abc[] = new String;

abc = "a";
abc = "b";
abc = "c";
abc = "d";
abc = "e";
abc = "f";
// Create a loop that will output the values stored in the array abc
// using a for loop and the array length
// TODO: ADD LOOP CODE HERE
return msg;
}

3) public String baseballOuts(){
String msg = "";
int totalOuts = 0;
// Write a set of nested for-loops that willdetermine the number of
// outs in a regulation baseball game. Assume: 9 innings per game,
// 2 halves per inning, 3 outs per half inning.
// You solution should include a loop (outer or nested) for each
// of the assumptions.
// TODO: ADD LOOP CODE HERE
msg = "Total number of outs in a regulation baseball game is " + totalOuts + ".";
return msg;
}

4)public String factorial (int n){
String msg = "";
int factnum = 1;
// Use a loop to calculate the factorial of an input integer.
// Note: If the input integer is too high an error may occur even if your
// logic is correct. Why? At what value of input does the error occur?
// How can you adjust the method so that either the error does not occur
// or the method "fails gracefully?" Write your answers in the form of
// a comment here.
// TODO: ADD LOOP CODE HERE
msg = n + "! = " + factnum;
return msg;
}

Thanks!  Reply With Quote

2. ## Control Flow Statements
If Statements
While Statements
The For Statement

These are decent tutorials which should help you to understand all you need to know for this assignment. If you're still stuck after studying these, then please come back with any specific questions you may have. Much luck!

edit: oh, and before I forget, welcome to the Java-Forums.org!  Reply With Quote

3. Member Join Date
Sep 2009
Posts
4
Rep Power
0

## Thanks that would be awesome!! I'm sure tutorials will really help. Thank you!  Reply With Quote

4. Member Join Date
Sep 2009
Posts
4
Rep Power
0

## The only ones I still can't figure out are:

public String redlight(boolean status, String color) {
// if the status of the stop light is true, check the color and deliver the appropriate message
// if the status is false, then proceed with caution.
String msg = "";
if ( REPLACE_WITH_CONDITION ) {
if ( REPLACE_WITH_CONDITION ) {
msg = "stop the car";
} else if ( REPLACE_WITH_CONDITION ){
} else {
msg = "proceed with caution";
}
} else {
msg = "for way stop";
}
return msg;
}

AND

public String xnqy(String x, String y){
String msg = "";
// write an if condition that will determine if two strings are NOT equal
if( REPLACE_WITH_CONDITION ) {
msg = "x and y have the different values";
}
return msg;
}  Reply With Quote

5. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
27

## For the stop lights break it down. In fact the question is broken down already.

For the second one, if you got the xeqy() one sorted this should be no problem as it is (so nearly almost) the same!  Reply With Quote

6. Member Join Date
Sep 2009
Posts
4
Rep Power
0

##   Reply With Quote

7. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Yay you!
...where's that mexican wave smiley gone...:)  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•