Question

Java 1. Create an application named NumbersDemo whose main() method holds two integer variables. Assign values...

Java

1. Create an application named NumbersDemo whose main() method holds two integer variables. Assign values to the variables. In turn, pass each value to methods named displayTwiceTheNumber(), displayNumberPlusFive(), and displayNumberSquared(). Create each method to perform the task its name implies.

2. Modify the NumbersDemo class to accept the values of the two integers from a user at the keyboard.

This is the base code given:

public class NumbersDemo {
public static void main (String args[]) {
// Write your code here
}
public static void displayTwiceTheNumber(int n) {
// Write your code here
}
public static void displayNumberPlusFive(int n) {
// Write your code here
}
public static void displayNumberSquared(int n) {
// Write your code here

}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class NumbersDemo {
    public static void main(String args[]) {
        int n1 = 10;
        int n2 = 7;
        displayTwiceTheNumber(n1);
        displayNumberPlusFive(n1);
        displayNumberSquared(n1);
        displayTwiceTheNumber(n2);
        displayNumberPlusFive(n2);
        displayNumberSquared(n2);
    }

    public static void displayTwiceTheNumber(int n) {
        System.out.println(2 * n);
    }

    public static void displayNumberPlusFive(int n) {
        System.out.println(n + 5);
    }

    public static void displayNumberSquared(int n) {
        System.out.println(n * n);
    }
}

import java.util.Scanner;

public class NumbersDemo {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        int n1 = in.nextInt();
        int n2 = in.nextInt();
        displayTwiceTheNumber(n1);
        displayNumberPlusFive(n1);
        displayNumberSquared(n1);
        displayTwiceTheNumber(n2);
        displayNumberPlusFive(n2);
        displayNumberSquared(n2);
    }

    public static void displayTwiceTheNumber(int n) {
        System.out.println(2 * n);
    }

    public static void displayNumberPlusFive(int n) {
        System.out.println(n + 5);
    }

    public static void displayNumberSquared(int n) {
        System.out.println(n * n);
    }
}
Add a comment
Know the answer?
Add Answer to:
Java 1. Create an application named NumbersDemo whose main() method holds two integer variables. Assign values...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a program named IntegerFacts whose Main() method declares an array of 10 integers. Call a...

    Create a program named IntegerFacts whose Main() method declares an array of 10 integers. Call a method named FillArray to interactively fill the array with any number of values up to 10 or until a sentinel value (999) is entered. If an entry is not an integer, reprompt the user. Call a second method named Statistics that accepts out parameters for the highest value in the array, lowest value in the array, sum of the values in the array, and...

  • Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method...

    Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method, but for now leave it empty. Then write a Java method getAverage which takes an array of integers as it’s argument. The method calculate the average of all the elements in the array, and returns that average. The method should work for an array of integers of any length greater than or equal to one. The method signature is shown below: public static double getAverage(...

  • Create a DataEntryException class whose getMessage() method returns information about invalid integer data. Write a program...

    Create a DataEntryException class whose getMessage() method returns information about invalid integer data. Write a program named GetIDAndAge that continually prompts the user for an ID number and an age until a terminal 0 is entered for both. If the ID and age are both valid, display the message ID and Age OK. Throw a DataEntryException if the ID is not in the range of valid ID numbers (0 through 999), or if the age is not in the range...

  • What I need: Create a new program named Reverse4 and declare four integer variables with the...

    What I need: Create a new program named Reverse4 and declare four integer variables with the values 23, 45, 55, and 67. Add a method named Reverse that reverses the positions of four integer variables. The Reverse method should accept the variables as references. Write a Main() method that displays the four variables both before and after reversing them to ensure the method works correctly. What I have: using System; class Reverse4 { public static void reverse(ref int num1, ref...

  • in java Part 1 In this section, we relook at the main method by examining passing...

    in java Part 1 In this section, we relook at the main method by examining passing array as parameters. Often we include options/flags when running programs. On command line, for example, we may do “java MyProgram -a -v". Depending on options, the program may do things differently. For example, "a" may do all things while "-v" display messages verbosely. You can provide options in Eclipse instead of command line: "Run ... Run Configurations ... Arguments". Create a Java class (Main.java)....

  • This is for a java program public class Calculation {    public static void main(String[] args)...

    This is for a java program public class Calculation {    public static void main(String[] args) { int num; // the number to calculate the sum of squares double x; // the variable of height double v; // the variable of velocity double t; // the variable of time System.out.println("**************************"); System.out.println(" Task 1: Sum of Squares"); System.out.println("**************************"); //Step 1: Create a Scanner object    //Task 1. Write your code here //Print the prompt and ask the user to enter an...

  • In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in...

    In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in 'Dog' : name (String type) age (int type) 2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables 3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with...

  • Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB....

    Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB. This interface should specify this abstract method: public abstract Product get(String productCode); Modify the ProductDB class so it implements the IProductDB interface. Write the code for the new ‘get’ method. Then remove the getProductByCode method. In the Main class, modify the code so it works with the new ProductDB class. This code should create an instance of the IProductDB interface like this: IProductDB db...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • PrintArray vi Create a class called PrintArray. This is the class that contains the main method....

    PrintArray vi Create a class called PrintArray. This is the class that contains the main method. Your program must print each of the elements of the array of ints called aa on a separate line (see examples). The method getArray (included in the starter code) reads integers from input and returns them in an array of ints. Use the following starter code: //for this program Arrays.toString(array) is forbidden import java.util.Scanner; public class PrintArray { static Scanner in = new Scanner(System.in);...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT