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
Problem 2 Write a program in Java to implement a recursive search function int terSearch(int A[],...
Problem 2 Write a program in Java to implement a recursive search function int terSearch(int A[], int l, int r, int x) that returns the location of x in a given sorted array of n integers A if x is present, otherwise -1. The terSearch search function, unlike the binary search, must consider two dividing points int d1 = l + (r - l)/3 int d2 = d1 + (r - l)/3 For the first call of your recursive search...
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.
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
Define what an array index value is in JAVA. Then, write a JAVA program that passes...
Define what an array index value is in JAVA. Then, write a JAVA program that passes an array to a method and finds the average value or mean value (add up the numbers in the array and divide by the number of values) of the array and prints it out.
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 program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code. Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT