Question
Please complete in JAVA only

PROBLEM: CHMOD is a command in the UNIX computer system. It is the command used for giving users permissions to access and change files and directories. There are 3 classes of users. They are the owner, the group and others. The permissions given are: read(r, write(w) and execute(x). The argument of the CHMOD command is a 3-character octal number (ex. 526). When each digit of that number is converted to binary, the binary digits are paired to represent read, write and execute in that order. 526 would convert to 101 010 110 The first binary conversion gives the user permissions. The second gives the group permissions. The third gives the permissions. So here, the owner has read and execute permissions and that is represented by r-x. The group has only write permission given by -w-. The others class has read and write permissions as shown by rw Putting all of the above together CHMOD 526 101 010 110- others INPUT: There will be 5 lines of input. Lines 1 and 2 will eaclh contain 3 octal digits. Lines 3 and 4 will each contain three 3- digit binary numbers. Line 5 will contain three 3-character stings OUTPUT: For each line of input print the other two equivalent forms of the CHMOD result. Print a space between each grouping as shown bclow. SAMPLE INPUT 1. 5, 2, 6 SAMPLE OUTPUT 1. 101 010 110 and r-x- 2. 7,3, 0 2. 111 011 000 and rwx 3 100, 001, 101 4. 010, 011, 100 3, 415 and r--x r-x 4. 234 and-w--wx r- 5. r-x, rw-, rwx 5. 567 and 101 110 111
TEST INPUT 1.7, 2, 1 TEST OUTPUT 1. 111 010 001 and rwx 2. 011 100 110 and -wx 3. 517 and r-x --x 4. 165 and-x rw-r 5. 725 and 111 010 2.3, 4, 6 r- rW- 3. 101, 001, 111 4.001, 110, 101
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.HashMap;

import java.util.Map;

import java.util.Scanner;

import java.util.StringTokenizer;

public class FilePermission {

// Map to hold letter file permission and equivalent deciaml value

static Map<String, String> fileMap = new HashMap<>();

private static void loadMap() {

fileMap.put("--x", "1");

fileMap.put("-w-", "2");

fileMap.put("-wx", "3");

fileMap.put("r--", "4");

fileMap.put("r-x", "5");

fileMap.put("rw-", "6");

fileMap.put("rwx", "7");

}

//Main function

public static void main(String[] args) {

// TODO Auto-generated method stub

loadMap();

//Scanner to read input

Scanner consoleInput = new Scanner(System.in);

System.out.print("Enter File Permission in Decimal Format: ");

String decinalFormat = consoleInput.nextLine();

System.out.println(decimalToBinary(decinalFormat) + " and " + decimalToLetter(decinalFormat));

System.out.print("Enter File Permission in Decimal Format: ");

decinalFormat = consoleInput.nextLine();

System.out.println(decimalToBinary(decinalFormat) + " and " + decimalToLetter(decinalFormat));

System.out.print("Enter File Permission in Binary Format: ");

String binaryFormat = consoleInput.nextLine();

System.out.println(binaryToDecimal(binaryFormat) + " and " + binaryToLetter(binaryFormat));

System.out.print("Enter File Permission in Binary Format: ");

binaryFormat = consoleInput.nextLine();

System.out.println(binaryToDecimal(binaryFormat) + " and " + binaryToLetter(binaryFormat));

System.out.print("Enter File Permission in Letter Format: ");

String letterFormat = consoleInput.nextLine();

System.out.println(latterToDecimal(letterFormat) + " and " + letterToBinary(letterFormat));

}

// TODO Auto-generated method stub

loadMap();

//Scanner to read input

Scanner consoleInput = new Scanner(System.in);

System.out.print("Enter File Permission in Decimal Format: ");

String decinalFormat = consoleInput.nextLine();

System.out.println(decimalToBinary(decinalFormat) + " and " + decimalToLetter(decinalFormat));

System.out.print("Enter File Permission in Decimal Format: ");

decinalFormat = consoleInput.nextLine();

System.out.println(decimalToBinary(decinalFormat) + " and " + decimalToLetter(decinalFormat));

System.out.print("Enter File Permission in Binary Format: ");

String binaryFormat = consoleInput.nextLine();

System.out.println(binaryToDecimal(binaryFormat) + " and " + binaryToLetter(binaryFormat));

System.out.print("Enter File Permission in Binary Format: ");

binaryFormat = consoleInput.nextLine();

System.out.println(binaryToDecimal(binaryFormat) + " and " + binaryToLetter(binaryFormat));

System.out.print("Enter File Permission in Letter Format: ");

String letterFormat = consoleInput.nextLine();

System.out.println(latterToDecimal(letterFormat) + " and " + letterToBinary(letterFormat));

}

private static String decimalToBinary(String decimalInput) {

String binaryOut = "";

//Split the input based on ,

StringTokenizer token = new StringTokenizer(decimalInput, ",");

while (token.hasMoreTokens()) {

// Get first seqment

int filePerSegment = Integer.parseInt(token.nextToken());

String tempBinary = "";

//Convert into binary

while (filePerSegment > 0) {

tempBinary = (filePerSegment % 2) + tempBinary;

filePerSegment = filePerSegment / 2;

}

//Add leading zero

while (tempBinary.length() != 3) {

tempBinary = "0" + tempBinary;

}

binaryOut = binaryOut + tempBinary + " ";

tempBinary = "";

}

return binaryOut;

}

private static String decimalToLetter(String decimalInput) {

String letterOut = "";

//String array to hold letter permision

String[] filePermission = { "---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx" };

//Split the input based on ,

StringTokenizer token = new StringTokenizer(decimalInput, ",");

while (token.hasMoreTokens()) {

//Get first segment

int filePerSegment = Integer.parseInt(token.nextToken());

//GEt corresponding letter

letterOut = letterOut + filePermission[filePerSegment] + " ";

}

return letterOut;

}

private static String binaryToDecimal(String binaryInput) {

String decimalOut = "";

//Split the input based on ,

StringTokenizer st = new StringTokenizer(binaryInput, ",");

while (st.hasMoreTokens()) {

String binary = st.nextToken().trim();

int decimal = 0;

int pow = 0;

//Convert to decimal

for (int i = 2; i >= 0; i--) {

decimal += Integer.parseInt(binary.charAt(i) + "") * Math.pow(2.0, pow);

pow++;

}

decimalOut = decimalOut + decimal;

}

return decimalOut;

}

private static String binaryToLetter(String binaryInput) {

String letterOut = "";

//Convert binary to decimal

String decimalOut = binaryToDecimal(binaryInput);

String temp = "";

for (int i = 0; i <= 2; i++) {

temp = temp + decimalOut.charAt(i) + ",";

}

temp = temp.substring(0, temp.length() - 1);

//Call decimal to letter function

letterOut = decimalToLetter(temp);

return letterOut;

}

private static String latterToDecimal(String letterInput) {

String decimalOut = "";

//Split the input based on ,

StringTokenizer st = new StringTokenizer(letterInput, ",");

//Conver to decimal

while (st.hasMoreTokens()) {

String temp = st.nextToken();

decimalOut = decimalOut + fileMap.get(temp.trim());

}

return decimalOut;

}

private static String letterToBinary(String letterInput) {

String binaryOut = "";

//Call letter to decimal function to get deciaml format

String decimalOut = latterToDecimal(letterInput);

String temp = "";

for (int i = 0; i <= 2; i++) {

temp = temp + decimalOut.charAt(i) + ",";

}

decimalOut = temp.substring(0, temp.length() - 1);

// call decimal to binary to get binary value

binaryOut = decimalToBinary(decimalOut);

return binaryOut;

}

}

output

sterminated> FilePermission [Java Application] CAProgram FilesUavalj Enter File Permission in Decimal Format: 7,2,1 111010 001--and rwx_w х Enter File Permission in Decimal Format: 3,4,6 011 100 110 and n rw- Enter File Permission in Binary Format: 110,001,111 617 and w--x rwx Enter File Permission in Binary Format: 001,110, 101 Enter File Permission r Letter Format:yon ,E 7 25 and 111 010 101

Add a comment
Know the answer?
Add Answer to:
Please complete in JAVA only PROBLEM: CHMOD is a command in the UNIX computer system. It...
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
  • Objective: Practice common UNIX commands. Procedure: The following list of Unix commands are given for self-learning....

    Objective: Practice common UNIX commands. Procedure: The following list of Unix commands are given for self-learning. Use 'whatis' or 'man' command to find out about each command. Your document should include the description or screen shots of the output from each of the command. Commands: df du gzip file history wget Changing access rights: chmod u+x Dir1.0            adds execute permission for the owner chmod go-w file1              removes write permission for the group and others chmod ugo=rw testfile     sets...

  • Write a program in C using Unix system calls and functions that will change the permissions on a ...

    Write a program in C using Unix system calls and functions that will change the permissions on a file. The executable shall be called “mychmod” and will be executed by: mychmod -u rwx -g rwx -o rwx -U rwx -G rwx -O rwx file1 file2 ... The lowercase options will add permissions while the uppercase options will remove permissions. Each of the switches is optional and should be interpreted as the ones in the Unix command chmod(1), you can review...

  • please need assistance on part B of the question b) A planar manipulator has link lengths L1 2m and L2-1 m.Use the inverse kinematic equations to find the joint angles which will place the end poi...

    please need assistance on part B of the question b) A planar manipulator has link lengths L1 2m and L2-1 m.Use the inverse kinematic equations to find the joint angles which will place the end point at the following positions (x V2 i) Write the forward kinematic equations for the end point. [2 marks] ii) Calculate the link L2 joint angle iii) Calculate the link L1 joint angle [5 marks] [5 marks] [Q1 Total: 20 Marks] Question 2 a) Explain...

  • Programming Assignment 5: For-Loops CSCI 251-Spring 2019 This program will use the concepts in th...

    using matlab Programming Assignment 5: For-Loops CSCI 251-Spring 2019 This program will use the concepts in the decimal to octal program Introduction: In file systems on servers (think of websites like Google, Amazon, etc.), permission to access the files come from the octal number system. Given an octal quadruple, permissions are defined for the user, the group, and other as follows: Permission First Digit Second Digit Third Digit Fourth Digit Always 01 (User-u) 0 」(Group-g) | (Other-o) Read (r) Write...

  • You will use Quartus II to build an 8 bit arithmetic logic unit that performs the...

    You will use Quartus II to build an 8 bit arithmetic logic unit that performs the following functions: Control Value Function                                000 Copy In1 to theResult unchanged 001 Copy In2 to theResult unchanged 010 Add In1 to In2 011 Subtract In2 from In1 100 And In1 and In2 101 Or In1 and In2 110 Shift left In1 by 1 bit 111 Shift right In1 by 1 bit You are allowed to use either gates/logic schematic, or else Verilog. We suggest...

  • III. ASSIGNMENT 2.1 As discussed in class, the example program enumerates all possible strings (or if...

    III. ASSIGNMENT 2.1 As discussed in class, the example program enumerates all possible strings (or if we interpret as numbers, numbers) of base-b and a given length, say l. The number of strings enumerated is b l . Now if we interpret the outputs as strings, or lists, rather than base-b numbers and decide that we only want to enumerate those strings that have unique members, the number of possible strings reduces from b l to b!. Furthermore, consider a...

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