Results 1 to 7 of 7
  1. #1
    merfen58 is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Default 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!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please have a look here:

    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!

  3. #3
    merfen58 is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Default

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

  4. #4
    merfen58 is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Default

    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 ){
    msg = "go ahead";
    } 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;
    }

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,010
    Rep Power
    20

    Default

    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!

  6. #6
    merfen58 is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Default

    Thank you for your help! I figured them all out already.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,010
    Rep Power
    20

Similar Threads

  1. Help with if-else statements
    By porchrat in forum New To Java
    Replies: 4
    Last Post: 03-23-2009, 04:24 PM
  2. While and do while statements questions
    By hungdukie in forum New To Java
    Replies: 39
    Last Post: 12-10-2008, 02:08 PM
  3. if else statements and using images
    By Joshr in forum New To Java
    Replies: 8
    Last Post: 10-05-2008, 06:51 PM
  4. Help with if else statements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 07:56 PM

Posting Permissions

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