Question

Write a Java program that prompts the user for the page size used in a virtual...

Write a Java program that prompts the user for the page size used in a virtual memory system; this will be a power of two between 512 (29) and 16384 (214), inclusive. Your program should check the user input for page size to make sure it is one of the allowable inputs (must be a power of 2 and cannot be smaller than 512 or larger than 16384), and should then prompt the user for a virtual address (assume 32-bit virtual addressing, so your code must be able to accept any input between 0 and 4294967295, which is 232-1). Given this input, the program must output the virtual page number and the offset within the page. Sample output might look like this:

Please enter the system page size:

1024

Please enter the virtual address:

10000

This address is in virtual page:

9

At offset:

784

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

import java.util.Scanner;

public class Paging {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter the system page size: ");
//reading the size
int size = sc.nextInt();
long adddress = 0;
long pageNumber = 0;

//checking if given size is valid
if(size >= 512 && size <=16384){
System.out.print("Enter the virtual address: ");
adddress = sc.nextLong();
//reading address and checking that is in given range
if(adddress >= 0 && adddress <= 4294967295L){
   //finding page number
pageNumber = adddress/size;
System.out.println("This address is in virtual page: "+pageNumber);
//finding offset
System.out.println("At offset: " + (adddress-(pageNumber*size)));
}
else{
System.out.println("Invalid input");
}
}
else{
System.out.println("Invalid input");
}
}
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Answer #2

Sure! Here's a Java program that prompts the user for the page size and virtual address, and then calculates the virtual page number and offset:

javaCopy codeimport java.util.Scanner;public class VirtualMemorySystem {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        // Prompt for page size
        System.out.print("Please enter the system page size: ");        int pageSize = scanner.nextInt();        // Check if page size is valid
        if (!isValidPageSize(pageSize)) {
            System.out.println("Invalid page size. Page size must be a power of 2 between 512 and 16384.");            return;
        }        // Prompt for virtual address
        System.out.print("Please enter the virtual address: ");        long virtualAddress = scanner.nextLong();        // Calculate virtual page number and offset
        int virtualPageNumber = calculateVirtualPageNumber(virtualAddress, pageSize);        int offset = calculateOffset(virtualAddress, pageSize);        // Output the results
        System.out.println("This address is in virtual page: " + virtualPageNumber);
        System.out.println("At offset: " + offset);
    }    // Check if the page size is valid
    private static boolean isValidPageSize(int pageSize) {        return (pageSize >= 512 && pageSize <= 16384) && ((pageSize & (pageSize - 1)) == 0);
    }    // Calculate the virtual page number
    private static int calculateVirtualPageNumber(long virtualAddress, int pageSize) {        return (int) (virtualAddress / pageSize);
    }    // Calculate the offset
    private static int calculateOffset(long virtualAddress, int pageSize) {        return (int) (virtualAddress % pageSize);
    }
}

In this program, we first prompt the user to enter the page size, then we check if the input is a valid page size using the isValidPageSize() method. If the page size is valid, we prompt the user to enter the virtual address. We then calculate the virtual page number and offset using the calculateVirtualPageNumber() and calculateOffset() methods, respectively. Finally, we output the results to the console.



answered by: Mayre Yıldırım
Add a comment
Know the answer?
Add Answer to:
Write a Java program that prompts the user for the page size used in a virtual...
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
  • In C++ l. Assume that a system has a 32-bit virtual address with a 4-KB page...

    In C++ l. Assume that a system has a 32-bit virtual address with a 4-KB page size. Write a program that is passed a virtual address (in decimal) on the command line and have it output the page number and offset for the given address. As an example, your program would run as follows: yourprogram 19986 Your program would output: The address 19986 contains: Page number = 4 Offset = 3602 Writing this program will require using the appropriate data...

  • please use java Write a program that prompts the user to enter line of text (including...

    please use java Write a program that prompts the user to enter line of text (including whitespace). The program then replaces the following vocals with numbers: a = 1, e = 2, i = 3, o = 4, u = 5 and outputs the result to the console. The program does not consider case, so a = 1, A = 1, e = 2, E = 2, etc. Example: Input: Hello, how are you? Output: H2ll4, h4w 1r2 y45? The...

  • Please use the python to write Assume that a system has a 32-bit virtual address with...

    Please use the python to write Assume that a system has a 32-bit virtual address with a 4-KB page size. Write a program that is passed a virtual address (in decimal) on the command line and have it output the page number and offset for the given address. As an example, your program would run as follows: ./addresses 19986 Your program would output: The address 19986 contains: page number = 4 offset = 3602 Writing this program will require using...

  • 1. Palindrome Write a program that prompts the user for a positive integer number and output whet...

    Language is C 1. Palindrome Write a program that prompts the user for a positive integer number and output whether the number is a palindrome or not. A palindrome number is a number that is the same when reversed. For example, 12321 is a palindrome; 1234 is not a palindromoe. You will write a function named as palindrome. The function will have two arguments, number and isPalindrome. Both arguments are pass-by-reference. The argument number is a pointer to a variable...

  • write in C++ Exercise#1: Is it a Prime? Write a program that prompts the user to...

    write in C++ Exercise#1: Is it a Prime? Write a program that prompts the user to enter a positive integer in the range [100, 100000]. If the integer is not in the specified range the program prompts the user again. The program prints whether the integer is a prime number or not. Sample input/output nter an integer in the range [10e, 10eeee]: 39 nter an integer in the range [100, 100000]: 120453 Enter an integer in the range [10e, 10e000e]:...

  • python program sample output Problem 3 (Taking User Input) Write a function called userGuess(n) that takes...

    python program sample output Problem 3 (Taking User Input) Write a function called userGuess(n) that takes an integer n as an argument. This function should display n to the user and prompt them to enter the number num that is the largest power of 2 less than or equal to n. Have your function return the user's input num (it will be used in the next problem) Problem 4 (Making a Game) Finally, create a main() function for your program....

  • Exercise #3: write a Java program that prompts the user to enter a sentence. The program...

    Exercise #3: write a Java program that prompts the user to enter a sentence. The program has to find the print: a. the position of vowels in the sentence. b. the number of vowels in the sentence (A, a, U, u, E, e, O, o, I, i) c. the number of characters as numbers or special characters (other than English letters a.z, A..Z). Hint: remember to ignore the spaces. Sample input-output: Enter the sentnse: UAEU is the university of the...

  • In Java - Write a program that prompts the user to enter two integers and displays...

    In Java - Write a program that prompts the user to enter two integers and displays their sum. If the input is incorrect, prompt the user again. This means that you will have a try-catch inside a loop.

  • C++ program by netBeans java language Exercise #2: Write a java program that prompts the user...

    C++ program by netBeans java language Exercise #2: Write a java program that prompts the user to enter a sentence. The program has to find the print: a. the position of vowels in the sentence. b. the number of vowels in the sentence (A, a, U, u, E, e, 0, o, I, i) c. the number of characters as numbers or special characters (other than English letters a..z, A..Z). Hint: remember to ignore the spaces. Sample input-output: Enter the sentnse:...

  • Please help me with this program in Java: Write a Java program that prompts the user...

    Please help me with this program in Java: Write a Java program that prompts the user to enter a password that matches a specific pattern. Your program must approve the user's entry.. Here is the pattern, in this order: 1 or more upper case letters two lower case letters 1 or 2 digits zero or 1 upper case letters any two of this group @#$%^&

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