Given the String variables name1 and name2, write a fragment of code that assigns the larger of the two to the variable first (assume that all three are already declared and that name1 and name2 have been assigned values). (Note: "larger" here means alphabetically larger, not "longer". Thus, "mouse" is larger than "elephant" because "mouse" comes later in the dictionary than "elephant"!)

Respuesta :

Answer:

if ( name1 > name2) {

    first = name1;

} else {

    first = name2;

}

Explanation:

First you need take a decision for that reason you need and if - else structure to decide if asign the variable name1 or the variable name2 to the result. Later you can use an > or < to compare two strings because it use the ASCII code to compare wich one is larger than another.

if ( name1 > name2) {

    first = name1;

} else {

    first = name2;

}