A. Introduction
for loops allows us to repeat some operations on large amount of data. However, we can't store these data in hundreds of variables. Instead, we use arrays to create hundreds or thousands of variables in one statement.
B. See details at 6.1
C. Try
1. Without using a standard library function, how will you find the max/min number in an int array? How to sort an array?
import java.io.*;
import java.util.Scanner;
class chmod {
public static String getBinary(int input){
int binary[] = new int[3];
int total = 0;
int count = 0;
for(int i=2; i>=0; i--){
if(total+Math.pow(2,i)<=input){
binary[count]=1;
total += Math.pow(2,i);
}
else{binary[count]=0;}
count++;
}
String output = "";
for(int i=0; i<3; i++){
output = output + binary[i];
}
return output;
}
public static String getPermissions(String Binary){
String permissions = "";
for(int i=0; i<3; i++){
if(Binary.charAt(i)== "1".charAt(0)){
switch(i){
case 0: permissions = permissions + "r"; break;
case 1: permissions = permissions + "w"; break;
case 2: permissions = permissions + "x"; break;
}
}
else{permissions = permissions + "-";}
}
return permissions;
}
public static void main(String[] args) throws IOException{
Scanner Jordan = new Scanner(new FileReader("data/input.txt"));
for(int i=0; i<5; i++){
String input = Jordan.nextLine();
String inputs[] = input.split(", ");
for(int j=0; j<3; j++){
String output=getBinary(Integer.parseInt(inputs[j]));
System.out.print(output + " ");
}
System.out.print("and ");
for(int j=0; j<3; j++){
String output=getPermissions(getBinary(Integer.parseInt(inputs[j])));
System.out.print(output + " ");
}
System.out.println();
}
}
}