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;
}
3)public String grades(double g){
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 ){
msg = "go ahead";
} 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
return total;
}
2)public String arrayprint(){
String msg = "";
String abc[] = new String[6];
abc[0] = "a";
abc[1] = "b";
abc[2] = "c";
abc[3] = "d";
abc[4] = "e";
abc[5] = "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!