Question

In: Computer Science

Output with Vars.java A variable like userNum can store a value like an integer. Extend the...

Output with Vars.java

A variable like userNum can store a value like an integer. Extend the given program as indicated.

  1. Output the user's input. (2 pts)
  2. Output the input squared and cubed. Hint: Compute squared as userNum * userNum. (2 pts)
  3. Get a second user input into userNum2, and output the sum and product. (1 pt)


Note: This zyLab outputs a newline after each user-input prompt. For convenience in the examples below, the user's input value is shown on the next line, but such values don't actually appear as output when the program runs.

Enter integer:
4
You entered: 4
4 squared is 16 
And 4 cubed is 64!!
Enter another integer:
5
4 + 5 is 9
4 * 5 is 20

import java.util.Scanner;

public class OutputWithVars {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int userNum;

System.out.println("Enter integer:");
userNum = scnr.nextInt();
  
  
}
}

Solutions

Expert Solution

//OutputWithVars.java
import java.util.Scanner;

public class OutputWithVars {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        int userNum;

        System.out.println("Enter integer:");
        userNum = scnr.nextInt();
        System.out.println("you entered: "+userNum);//(1) task completed.

        int s=userNum * userNum;
        System.out.println(userNum+" square is "+s);
        int c=userNum * userNum * userNum;
        System.out.println("And "+userNum+ " cube is "+c+"!!");//(2) task completed.
        int userNum2=0; //declaration and initialization of another varaible
        System.out.println("Enter another integer:");
        userNum2 = scnr.nextInt(); //reading input from the keyboard by using scanner objecct.
        int a =userNum+userNum2;
        int m=userNum*userNum2;
        System.out.println(+userNum+ " + " +userNum2 +" is " +a);
        System.out.println(+userNum+ " * " +userNum2 +" is " +m);
    }
}

 


Related Solutions

Update the function playScratchOffs to do the following Declare an integer variable to store the type...
Update the function playScratchOffs to do the following Declare an integer variable to store the type of scratch off (i.e. type) Declare an integer variable to store the number of scratch off (i.e. count) Declare an integer variable for loop control (i.e. c) Declare a variable of data type struct OneDollar (i.e. oneSO) Declare a variable of data type struct TwoDollar (i.e. twoSO) Declare a variable of data type struct FiveDollar (i.e. fiveSO) Write a series of printf statements to...
A binary variable can be introduced to a mixed integer program to allow for a “threshold...
A binary variable can be introduced to a mixed integer program to allow for a “threshold constraint.” A threshold constraint says that if any units are used, at least a specified minimum amount must be used. Define X as the number of students that will go on a planned field trip. The school will rent a bus only if at least 20 students plan to go on the trip. Define Y as a binary variable that equals 1 if X...
A Switch/case statement allows multiway branching based on the value of an integer variable. In the...
A Switch/case statement allows multiway branching based on the value of an integer variable. In the following example, the switch variable s is specified to be assumed to be one of three values ​​of [0, 2], and a different action for each case becomes: case 0: a = a - 1; break; case 1: a = a + 2; break; case 2: b = 3 * b; break; Shows how such statements can be compiled into MiniMIPS assembly instructions.
A switch/case statement allows multiway branching based on the value of an integer variable. In the...
A switch/case statement allows multiway branching based on the value of an integer variable. In the following example, the switch variables can assume one of the three values in [0, 2] and a different action is specified for each case. switch (s) {    case 0: a=a+1; break;    case 1: a=a-1; break;    case 2: b=2*b; break; } Show how such a statement can be compiled into MiniMIPS assembly instructions.
1. Can you extend an abstract class? In what situation can you not inherit/extend a class?...
1. Can you extend an abstract class? In what situation can you not inherit/extend a class? 2. Can you still make it an abstract class if a class does not have any abstract methods?
Questions: 1) // declare integer variable sum equal to zero // declare variable integer i //...
Questions: 1) // declare integer variable sum equal to zero // declare variable integer i // declare while loop condition where i is less then 25 // inside of brackets calculate the sum of i (addition) // increment i // outside the loop print the sum of values ============================================= 2) Create a sentinel value example if I press number 0 it will display the sum of data // create a scanner // prompt the user to to enter the numbers...
Define an array named PeopleTypes that can store a maximum of 50 integer values that will be entered at the keyboard.
Define an array named PeopleTypes that can store a maximum of 50 integer values that will be entered at the keyboard. Enter a series of 1s, 2s,3s and 4s, into the array, where a represent an infant, a 2 represent a child, a 3 represents a teenager, and a 4 represents an adult who was present at a local school functionp.412 PE 4Write a C function that finds and displays the maximum value in a two dimensional array of integers....
Use DevC++ to implement a program that can store and output 5 integers, where the user...
Use DevC++ to implement a program that can store and output 5 integers, where the user can input its value into an array named arrayValue. The program should implement the following: 1. Calculate the sum and average of the array. 2. Output the content (value) that stored in the array of five elements. 3. Continue to ask the user to store for storing data into another array. c++ ONLY PLEASE NEED IT FAST AS POSSIBLE
We will extend project 2 by wrapping our input and output in a while loop. Repeatedly...
We will extend project 2 by wrapping our input and output in a while loop. Repeatedly do the following: Prompt the user for their name, get and store the user input. Prompt the user for their age, get and store the user input. We will assume that the user will enter a positive integer and will do no error checking for valid input. Determine and store a movie ticket price based on the user's age. If their age is 12...
Auto Repair Insurance. A person calls people to ask if they would like to extend their...
Auto Repair Insurance. A person calls people to ask if they would like to extend their automobile insurance beyond the normal 3 years. The probability that the respondent says yes is about 33%. If she calls 12 people, find the probability that the first person to say yes will occur with the fourth customer.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT