Question

In: Computer Science

Write a Java program to move data among variables: (1) define 4 int variable pos1, pos2,...

Write a Java program to move data among variables:

(1) define 4 int variable pos1, pos2, pos3, pos4, input 4 integer numbers from console and assign them to these variables; display message in the following format: “pos1=a-number; pos2=a-number; pos3=a-number; pos4=a-number”. “anumber” is replaced with real number.

(2) Left shift data in these variables: for each variable to get the value of the successive variable, with pos4 getting pos1's value. Display pos1, pos2, pos3, and pos4

(3) Right shift data in these variables: for each variable to get the value of the previous variable, with pos1 getting pos4's value. Display pos1, pos2, pos3, and

Solutions

Expert Solution

Java Program to move data among variables:

import java.util.Scanner; // importing Scanner class
class ShiftData {


public static void main(String args[]) {
        //Defining 4 int variables
        int pos1,pos2,pos3,pos4;
        
        Scanner sc = new Scanner(System.in);  //Using Scanner for getting input from console
        
        //Prompting for input and storing value to variables
        System.out.println("Enter value of pos1: ");
        pos1 = sc.nextInt();
        System.out.println("Enter value of pos2: ");
        pos2 = sc.nextInt();
        System.out.println("Enter value of pos3: ");
        pos3 = sc.nextInt();
        System.out.println("Enter value of pos4: ");
        pos4 = sc.nextInt();
        
        //Displaying value of all 4 variables
        
        System.out.println("pos1="+pos1+";pos2="+pos2+";pos3="+pos3+";pos4="+pos4);
        
        leftShift(pos1,pos2,pos3,pos4); //Calling leftShift method
        rightShift(pos1,pos2,pos3,pos4); // Calling rightShift method
        
}
static void leftShift(int pos1,int pos2,int pos3,int pos4) {
        int  temp; //This variable is used to hold one pos value
        //Shiftimg left (Getting value of successive variable)
        temp = pos1;
        pos1 = pos2;
        pos2 = pos3;
        pos3 = pos4;
        pos4 = temp;
        System.out.println("After left shift:");
        System.out.println("pos1="+pos1+";pos2="+pos2+";pos3="+pos3+";pos4="+pos4); //Print variables after left shift
}
static void rightShift(int pos1,int pos2,int pos3,int pos4) {
        int  temp; //This variable is used to hold one pos value
        //Shiftimg left (Getting value of previous variable)
        temp = pos4;
        pos4 = pos3;
        pos3 = pos2;
        pos2 = pos1;
        pos1 = temp;
        
        System.out.println("After right shift:");
        System.out.println("pos1="+pos1+";pos2="+pos2+";pos3="+pos3+";pos4="+pos4); //Print variables after right shift
}

}

Sample Output :


Related Solutions

Write the program in Java. Demonstrate that you know how to define variables, create conditional statements,...
Write the program in Java. Demonstrate that you know how to define variables, create conditional statements, write a loop, use arrays, obtain input from the user, display output, test and create a JavaDoc. Create project PaystubTest2 that is a Paystub calculator with program Paystub##.java (where ## is your initials) based on user input. (10 points) You must use the ReadStringFromUser and ReadFloatFromUser methods in the program to obtain user input. (20 points) This program will calculate the Gross Earnings, FICA...
Write a program in JAVA to create the move set of a Pokémon, and save that...
Write a program in JAVA to create the move set of a Pokémon, and save that move set to a file. This program should do the following: Ask for the pokemon’s name. Ask for the name, min damage, and max damage of 4 different moves. Write the move set data into a file with the pokemon’s name as the filename. The format of the output file is up to you, but keep it as simple as possible
Write a Java program that takes an array of 10 "Int" values from the user and...
Write a Java program that takes an array of 10 "Int" values from the user and determines if all the values are distinct or not. Return TRUE if all the values of the array are distinct and FALSE if otherwise.
JAVA JAVA JAVA JAVA, My array has 1000 int variables with random values from 1-100, I...
JAVA JAVA JAVA JAVA, My array has 1000 int variables with random values from 1-100, I want to be able to scan and output which number appears the most and the least. int x =1000 int[] array = new array[x] for(int i = 0 ; i < x; i++){ array[i] = random.nextInt(101); }
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are...
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are that it returns an array of all the elements of the int[] array numbers which are coprime with x } Note that the array that is returned may be an empty array--you will have to count how many times gcf(x, numbers[i]) == 1. ASSUME numbers is not null and not empty.
Write a java program calls the following methods: a. printStars(): Takes an int (n) as parameter...
Write a java program calls the following methods: a. printStars(): Takes an int (n) as parameter and prints n stars (*) using for loop. ex. 6 ******
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
Java Implement a class MyInteger that contains: • An int data field/instance variable named value that...
Java Implement a class MyInteger that contains: • An int data field/instance variable named value that stores the int value represented by this object. • A constructor that creates a MyInteger object for a default int value. What default value did you choose? What other values did you consider? Why did you make this choice? • A constructor that creates a MyInteger object for a specified int value. • A getter method, valueOf(), that returns value. • A setter method,...
Write Java program Lab43.java which declares and initializes numeric array of 5 elements (int num[5]) and...
Write Java program Lab43.java which declares and initializes numeric array of 5 elements (int num[5]) and produces as output the sum of integers in an array, the largest and the smallest element in an array. Your program should contain the following methods: public static int sum(int[]) public static int findLargest(int[]) public static int findSmallest(int[])
Please use Java language with comments! Thanks! Write a program that will display multiple dots move...
Please use Java language with comments! Thanks! Write a program that will display multiple dots move across the Frame and randomly change direction when they hit the edge of the screen. Do this by creating a Dot class and making each dot an object of that class. You may reuse code written in class for this part of the assignment. Create a subclass of the Dot class called PlayerDot that is controlled by the player through keyboard input (arrow keys)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT