Question

Using the Java language program Demonstrate multiway if statement 1.1 Generate an integer random number from...

Using the Java language program

Demonstrate multiway if statement

1.1 Generate an integer random number from -5 to 5.(Hint: Use Math.random())

1.2 Write a multiway if statement that will display whether the number is positive, negative or zero.

1.3 Be sure to test your code with all three cases

Homework Answers

Answer #1
//TestCode.java
import java.util.Random;
public class TestCode {
    public static void main(String[] args) {
        Random rand = new Random();
        int n = rand.nextInt(11)-5;
        System.out.print(n+" is ");
        if(n<0){
            System.out.println("negative");
        }
        else if(n>0){
            System.out.println("positive");
        }
        else{
            System.out.println("zero");
        }
    }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a script that will generate seven random integers in the range [-100:200] inclusive and then...
Write a script that will generate seven random integers in the range [-100:200] inclusive and then print the value of each integer and whether it is positive or negative and whether it is even or odd. An output from this program might be: -57 is negative and odd. 26 is positive and even. If zero is one of the random integers generated, remember that zero is even and neither negative or positive. Hint: even numbers are divisible by 2 with...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will implement Queue Abstract Data Type with the following functions/methods.  Any build-in/pre-defined Queue function/library (e.g., java.util.Queue in Java) is NOT allowed to use in your code. push(Element):  insert the input Element (e.g., String or Integer in Java) to the end of the queue. pop(): remove the head element of the queue and print the head element on screen. count():  return the total number of elements in the queue...
Write a Hack Assembly Language program to calculate the quotient from a division operation. The values...
Write a Hack Assembly Language program to calculate the quotient from a division operation. The values of dividend a and divisor b are stored in RAM[0] (R0) and RAM[1] (R1), respectively. The dividend a is a non-negative integer, and the divisor b is a positive integer. Store the quotient in RAM[2] (R2). Ignore the remainder. Example: if you are given two numbers 15 and 4 as dividend a (R0) and divisor b (R1), respectively, the answer will be 3 stored...
Code a program to reproduce Madame Esmerelda’s clairvoyant test: Mme. Esmerelda conjures up a random number...
Code a program to reproduce Madame Esmerelda’s clairvoyant test: Mme. Esmerelda conjures up a random number 1-10 in her mind and commands the user to guess what it is. If the user’s guess is equal to (remember your double equal sign!) the conjured number, Mme. Esmerelda extends a job offer as a clairvoyant assistant. Add a user-driven loop that allows the user to play repeatedly if desired (but your program should conjure a new random number with every loop iteration)....
1) Write a java programming using a while loop where you will add numbers and when...
1) Write a java programming using a while loop where you will add numbers and when you press number 0 it will add all your numbers and display the results. Use Scanner object. Steps: 1) Declare variables integer called sum which is equal to zero   2) Declare Scanner object   3) Declare while condition where is true   4) Inside of that condition prompt the user to enter the numbers   5) Declare variable integer called number and input the number statement   6)...
In JAVA Find the code for sorts in your language some where online. You will copy...
In JAVA Find the code for sorts in your language some where online. You will copy code from the internet (with citation!) and modify it to test. Make a random array of integers, then perform each search on them. LINEAR SEARCH Write a linear search method to find a number in the list. Call this before each of your sort methods. Include a comment explaining how Linear Search works, when to use it, when you cannot. BINARY SEARCH Write a...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
Classes/ Objects(programming language java ) in software (ATOM) Write a Dice class with three data fields...
Classes/ Objects(programming language java ) in software (ATOM) Write a Dice class with three data fields and appropriate types and permissions diceType numSides sideUp The class should have A 0 argument (default) constructor default values are diceType: d6, numSides: 6, sideUp: randomValue 1 argument constructor for the number of sides default values are diceType: d{numSides}, sideUp: randomValue 2 argument constructor for the number of sides and the diceType appropriate accessors and mutators *theoretical question: can you change the number of...
JAVA Exercise_Pick a Card Write a program that simulates the action of picking of a card...
JAVA Exercise_Pick a Card Write a program that simulates the action of picking of a card from a deck of 52 cards. Your program will display the rank and suit of the card, such as “King of Diamonds” or “Ten of Clubs”. You will need: • To generate a random number between 0 and 51 • To define two String variables: a. The first String should be defined for the card Suit (“Spades”, “Hearts”, “Diamonds”, “Clubs”) b. The second String...
C LANGUAGE CODE WITH COMMAND-LINE ARGUMENTS (NO SCANF TO BE USED ) Q]. Write a program...
C LANGUAGE CODE WITH COMMAND-LINE ARGUMENTS (NO SCANF TO BE USED ) Q]. Write a program that displays all the prime numbers in the given array with the following constraint. Constraint: Only those prime numbers should be displayed whose location is a composite number. Although you may have several prime numbers in the array, only those prime numbers should be displayed which are stored at non-prime locations. Remember that the first position in an array corresponds to the location/index 0....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT