Question

In: Computer Science

package questions; public class File {    public String base; //for example, "log" in "log.txt"   ...

package questions;

public class File {
   public String base; //for example, "log" in "log.txt"
   public String extension; //for example, "txt" in "log.txt"
   public int size; //in bytes
   public int permissions; //explanation in toString

   //DO NOT MODIFY
   public String getBase() {
       return base;
   }

   /**
   *
   * @param b
   * if b is null or if empty (""), base should become "default",
   * for all other values of b, base should become b
   */
   public void setBase(String b) {
       //to be completed
  
   }

   //DO NOT MODIFY
   public String getExtension() {
       return extension;
      
   }

   /**
   *
   * @param e
   * if e is null or if empty (""), extension should become "txt",
   * for all other values of e, extension should become e
   */
   public void setExtension(String e) {
       //to be completed
  
   }

   //DO NOT MODIFY
   public int getSize() {
       return size;
   }

   /**
   *
   * @param s
   * if s is less than 0, size should become 0
   * if s is more than or equal to 0, size should become s
   */
   public void setSize(int s) {
       //to be completed
  
   }

   //DO NOT MODIFY
   public int getPermissions() {
       return permissions;
   }

   /**
   *
   * @param p
   * if p is less than 0, permissioons should become 0
   * if p is more than 7, permissions should become 7
   * if p is between 0 and 7 (inclusive on both sides), permissions should become p
   */
   public void setPermissions(int p) {
       //to be completed
  
   }

   //DO NOT MODIFY
   public File() {
       setBase("default");
       setExtension(".txt");
       setSize(0);
       setPermissions(0);
   }
  
   /**
   *
   * @param b: value for base
   * @param e: value for extension
   * @param s: value for size
   * @param p: value for permissions
   */
   public File(String b, String e, int s, int p) {
       //to be completed
   }

   //DO NOT MODIFY
   public String getName() {
       return base + "." + extension;
   }
  
   /**
   *
   * This method should set the size to 0, permission to 0, extension to null and base to null.
   *
   * Note that your File constructor and setters must be correct to pass the JUnit tests.
   *
   */
   public void wipe() {
       //to be completed
   }

   /**
   *
   * @param other
   * @return
   * 1 if calling object is bigger in size than the parameter object
   * -1 if calling object is smaller in size than the parameter object
   * 0 if calling object has the same size as the parameter object
   */
   public int compareTo(File other) {
       return 0; //to be completed
   }

   /**
   *
   * @param other
   * @return true if calling object and parameter object are identical
   * in every aspect (base, extension, size, permissions)
   *
   * NOTE: file name should be checked in case INsensitive manner
   *
   * HINT: Strings are NOT compared using == (s1 == s2 is WRONG)
   * Google "String comparison java" and "String case insensitive comparison java" to
   * see the right way!
   */
   public boolean equals(File other) {
       if(other instanceof File) {

           return (new String("size1").equals(other.size)&&new String("base1").equalsIgnoreCase(other.base)&&new String("permission1").equals(other.permissions) && new String("extension1").equalsIgnoreCase(other.extension));

           }
       return false;
          
   }
  
  
   /**
   * @return a new folder object with the same permission, size, base and extension as the calling object.
   * (In other words, return a deep copy of the calling object.
   */
   public File clone() {
       return null; //to be completed
   }

   /**
   * HD
   * return String representation of the calling object.
   *
   * Size:
   *
   * 1024 bytes = 1 kilobyte (KB)
   * 1024 kilobytes = 1 megabyte (MB)
   * 1024 megabytes = 1 gigabyte (GB
   *
   * for files below 1024 bytes, you should represent size in bytes (B)
   * for files 1024 bytes or more but less than 1024 kilobytes, you should represent size in kilobytes (KB)
   * for files 1024 kilobytes or more but less than 1024 megabytes, you should represent size in megabytes (MB)
   * for files 1024 megabytes or more, you should represent size in gigabytes (GB)
   *
   * only the integer part of size should be displayed.
   *
   * Permissions:
   *
   * Permissions is a number between 0 and 7.
   * 1st bit represents read permission,
   * 2nd bit represents write permission,
   * 3rd bit represents execute permissiono
   *
   * 0: 000 (---)
   * 1: 001 (--x)
   * 2: 010 (-w-)
   * 3: 011 (-wx)
   * 4: 100 (r--)
   * 5: 101 (r-x)
   * 6: 110 (rw-)
   * 7: 111 (rwx)
   *
   * https://www.tutorialspoint.com/unix/unix-file-permission.htm
   *
   * Syntax of toString:
   * <permissions in (r/-)(w/-)(x/-) format> <file name> <integer size in correct magnitude>
   *
   * Some examples:
   * if base = "log", extension = "txt", size = 1056, permissions = 4,
   * the String returned should return
   *
   *                        "r-- log.txt 1KB"
   *
   * if base = "data", extension = "csv", size = 4500000, permissions = 7,
   * the String returned should return:
   *
   *                        "rwx data.csv 4MB"
   */
   public String toString() {
       return ""; //to be completed
   }
}

Solutions

Expert Solution

/**
   *
   * @param b
   * if b is null or if empty (""), base should become "default",
   * for all other values of b, base should become b
   */

public void setBase(String b) {
if (b.isEmtyy() || b == null)
{

base = 'default' ;

}
else

base = b;

}
  
}

}

public void setExtension(String e) {
  
if(e.isEmpty() || e == null)
{
extention = 'txt';
}
else
{
extention = e;
}
}

/**
*
* @param p
* if p is less than 0, permissioons should become 0
* if p is more than 7, permissions should become 7
* if p is between 0 and 7 (inclusive on both sides), permissions should become p
*/
public void setPermissions(int p) {

if(p < 0 )
p = 0;
if(p > 7)
p = 7;

switch( p ) {
case 0:
permissions = '---';
break;
case 1:
permissions = '--x';
case 2:
permissions = '-w-';
break;
case 3:
permissions = '-wx';
break;
case 4:
permissions = 'r--';
break;
case 5:
permissions = 'r-x';
break;
case 6:
permissions = 'rw-';
break;
case 7:
permissions = 'rwx';
break;

default:
// none
}

}

/**
*
* @param b: value for base
* @param e: value for extension
* @param s: value for size
* @param p: value for permissions
*/
public File(String b, String e, int s, int p) {
// constructor used for initialising values

base = b;
extention = e;
size = s;
permissions = p;

}

public void wipe() {
//to be completed

size = 0;
permissions = 0;
extention = null;
base = null;
}

/**
*
* @param other
* @return
* 1 if calling object is bigger in size than the parameter object
* -1 if calling object is smaller in size than the parameter object
* 0 if calling object has the same size as the parameter object
*/
public int compareTo(File other) {

if ( this.equals(other) )
{
return 0;
}
else
{
//copy the logic to compare the file size and permissions from equals method and return -1 or 1 accordingly
}

}

public void setSize(int s) { // if size < 0, then file size will be 0, else file size will be calculated on dividing by 1024

depending upon its size in KB, MB or GB and the integer part of s received will be val of s

if(s<0)
{
size = 0
}
else
{
while(s/1024)
{
s/=1024;
}
size = s;
}
  
}


Related Solutions

QUESTION 11 In util package, public class Example { public static void showMessage( String s ){...
QUESTION 11 In util package, public class Example { public static void showMessage( String s ){ ....... } public static int getInt( String prompt ){ ....... } } Using these method you see above to create a main method in your class called MyExam in the different package to get input and output. The output will tell the user what number is entered. If you entered a 5, the output will be: "The number you entered is 5.". QUESTION 12...
Determine the Output: Package questions; import java.util.*; public class Quiz1 {       public static void main(String[] args)...
Determine the Output: Package questions; import java.util.*; public class Quiz1 {       public static void main(String[] args) {             // TODO Auto-generated method stub             System.out.println("*** 1 ***");             ArrayList<Integer>list1=new ArrayList<Integer>();             for(int i=0;i<30;i++)             {                   list1.add(new Integer(i));             }             System.out.print("[");             for(int i=0;i<list1.size();i++)             {                   System.out.print(list1.get(i)+" ");             }             System.out.println("]");                           System.out.println("*** 2 ***");             ArrayList<Integer>list2=new ArrayList<Integer>();             for(int i=30;i<60;i++)             {                   list2.add(i); //Auto Boxing an Integer not need to use new Integer             }             System.out.println(list2); //toString for an ArrayList --created by the Java Programmers                           System.out.println("*** 3 ***");             ArrayList<Integer>list3=new ArrayList<Integer>();             list3.add(list1.remove(22)); //when...
What is a package? What package is the Scanner class in? What package is the String class in?
What is a package? What package is the Scanner class in? What package is the String class in? 
package datastructure; public class UseQueue { public static void main(String[] args) { /* * Demonstrate how...
package datastructure; public class UseQueue { public static void main(String[] args) { /* * Demonstrate how to use Queue that includes add,peek,remove,pool elements. * Use For Each loop and while loop with Iterator to retrieve data. * */ } }
package datastructure; public class UseMap { public static void main(String[] args) { /* * Demonstrate how...
package datastructure; public class UseMap { public static void main(String[] args) { /* * Demonstrate how to use Map that includes storing and retrieving elements. * Add List<String> into a Map. Like, Map<String, List<string>> list = new HashMap<String, List<String>>(); * Use For Each loop and while loop with Iterator to retrieve data. * * Use any databases[MongoDB, Oracle, MySql] to store data and retrieve data. */ } }
package compstore; public class Desktop{ private String brand; private float price; public Desktop(String brand, float price)...
package compstore; public class Desktop{ private String brand; private float price; public Desktop(String brand, float price) { this.brand = brand; this.price = price; } public String getBrand() { return brand; } public float getPrice() { return price; } } package compstore; public class DeskTopDeals{ // assume proper variables and other methods are here public void dealOfTheDay(Desktop[] items, int numItems){ /**************************** * your code would go here * ****************************/ } } Given the above Desktop class, write code that should go...
package construction; public class Bid{ private String contractor; private float price; public Bid(String contractor, float price)...
package construction; public class Bid{ private String contractor; private float price; public Bid(String contractor, float price) { this.contractor = contractor; this.price = price; } public String getContractor() { return contractor; } public float getPrice() { return price; } } package construction; public class ContractorBids{ // assume proper variables and other methods are here public void winningBid(Bid[] bids, int numBids){ /**************************** * your code would go here * ****************************/ } } You are doing renovations on your building, and multiple contractors...
this won't compile package com.test; public class CatalogItem { private String title; private double price; public...
this won't compile package com.test; public class CatalogItem { private String title; private double price; public CatalogItem(String title, double price) { super(); this.title = title; this.price = price; } public String getTitle() { return title; } public double getPrice() { return price; } } //Book.java package com.test; public class Book extends CatalogItem { private String author; private int ISBN; public Book(String title, double price, String author, int iSBN) { super(title, price); this.author = author; ISBN = iSBN; } public String...
Base Class class TransportationLink { protected: string _name; double _distance; public: TransportationLink(const string &name, double distance);...
Base Class class TransportationLink { protected: string _name; double _distance; public: TransportationLink(const string &name, double distance); const string & getName() const; double getDistance() const; void setDistance(double); // Passes in the departure time (as minute) and returns arrival time (as minute) // For example: // 8 am will be passed in as 480 minutes (8 * 60) // 2:30 pm will be passed in as 870 minutes (14.5 * 60) virtual unsigned computeArrivalTime(unsigned minute) const = 0; }; #endif Derived Classes...
package mac286.LinkedLists; public class Postfix { private rStack<String> S; private String inSt; private ourLinkedList<String> inList, outList;...
package mac286.LinkedLists; public class Postfix { private rStack<String> S; private String inSt; private ourLinkedList<String> inList, outList; public Postfix(String s) { S = new rStack<String>(); inSt = s; outList = new ourLinkedList<String>(); inList = new ourLinkedList<String>(); } public void reset(String s) { S = new rStack<String>(); inSt = s; outList = new ourLinkedList<String>(); inList = new ourLinkedList<String>(); } private boolean isOperator(char c) { if(c=='-'||c=='+'||c=='*'||c=='/') return true; return false; } private boolean isParenthesis(char c) { if(c == '(' || c==')') return true;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT