Package Title: Lab Questions Course Title: Big Java



Download 0.57 Mb.
Page2/4
Date29.06.2021
Size0.57 Mb.
#147166
1   2   3   4
Rewrite the code above again, but this time start by setting the color variable to “red”. How does that change the condition?

4) The relational operators in Java are ==, !=, <, >, <=, and >=.

Assume x and y are integers. Using relational operators, formulate the following conditions in Java:

a) x is positive

b) x is zero or negative

c) x is at least 10

d) x is less than 10

e) x and y are both zero

f) x is even



5) To compare strings in Java you can’t simply use the relational operators. You need to use methods of the String class: equals(), compareTo(), and substring(). Assume the following code:

String word1 = "catalog";

String word2 = "cat";

Write the following conditions in Java:

1) word1 is lexicographically greater than "aaa"

2) word1 is lexicographically equal to word2

3) word1 is lexicographically less than word2

4) word1 and word2 have the same three-letter prefix

6) Copy and run the following program. Explain how the program compares the two strings. How can you modify the program so that str2 and str3 are equal when they are compared?

public class StringEqual

{

public static void main(String[] args)



{

String str1 = "abcd";

String str2 = "abcdefg";

String str3 = str1 + "efg";

System.out.println("str2 = " + str2);

System.out.println("str3 = " + str3);

if (str2 == str3)

{

System.out.println("The strings are equal");



}

else


{

System.out.println("The strings are not equal");

}

}

}


7) Here is a table that describes the planets in our solar system numbered according to their distance from the sun:

1. Mercury

2. Venus

3. Earth

4. Mars

5. Jupiter

6. Saturn

7. Uranus

8. Neptune

Use the code below to input an integer from the user. Write a switch statement that examines the integer and prints out the name of the corresponding planet. Print an error message if an invalid integer is entered.

import java.util.Scanner;

public class Planets

{

public static void main(String[] args)

{

Scanner scan = new Scanner(System.in);

System.out.print("Enter a planet number from the sun: ");

int planet = scan.nextInt();

// Your code goes here …

}

}

8) Remember the childhood game “Rock, Paper, Scissors”? It is a two-player game in which each person simultaneously chooses either rock, paper, or scissors. Rock beats scissors but loses to paper, paper beats rock but loses to scissors, and scissors beats paper but loses to rock. The following code prompts player 1 and player 2 to each enter a string: rock, paper, or scissors. Finish the code by adding nested if statements to appropriately report “Player 1 wins”, “Player 2 wins”, or “It is a tie.”

import java.util.Scanner;

public class RockPaperScissors

{

public static void main(String[] args)



{

Scanner scan = new Scanner(System.in);

System.out.println("Player 1: Choose rock, scissors, or paper:");

String player1 = scan.next().toLowerCase();

System.out.println("Player 2: Choose rock, scissors, or paper:");

String player2 = scan.next().toLowerCase();

(your code goes here…)

}

}



9) The program in Lab 5.8 was heavily nested with if statements, and it can be difficult to follow the logic of any program that is heavily nested in this way. By constructing complex conditions with the && operator, it is possible to simplify the code and remove some of the else alternatives. Rewrite the program in Lab 5.8 using complex conditions and omitting the else construct.


Download 0.57 Mb.

Share with your friends:
1   2   3   4




The database is protected by copyright ©essaydocs.org 2023
send message

    Main page