Consider using Python to create a class that represent disk based storage. Write
ID: 3678854 • Letter: C
Question
Consider using Python to create a class that represent disk based storage. Write a class Disk in Python with appropriate attributes and methods.
Disk should have a fixed capacity set when it is created (measured in Gigabytes).
An add method should add a fixed amount of data to the store
A delete method should remove a fixed amount of data from the store
A wipe method should clear the disk of all data
A freespace method should return the current amount of free space available on the disk
A full method should return whether or not the disk is currently full
The Class should contain appropriate checks to make sure it is always in a consistent state.
Explanation / Answer
python program:
#!/usr/bin/python
class Disk:
'Common base class for all employees'
def __init__(self, size):
print "Disk is created with ",size,"MB"
self.size = size
self.used = 0
self.frees = size
self.ful = 0
def add(self,datasize):
if self.used==self.size:
print "Disk is full"
elif self.frees < datasize:
print "No sufficient space for add ",datasize,"MB"
else:
self.used=self.used+datasize
self.frees=self.frees-datasize
print datasize,"MB data is added to Disk"
def delete(self,datasize):
if self.used==0:
print "Disk is Empty"
elif self.used<datasize:
print "There is no sufficient amount of space in disk to delete",datasize,"MB","used is:",self.used
else:
self.uesd=self.used-datasize
self.frees=self.frees+datasize
print datasize,"MB data is deleted from Disk"
def wipe(self):
self.used = 0
self.frees =self.size
print "Disk is cleared"
def free(self):
print "Total Free space in disk is %d" % self.frees
def full(self):
if(self.frees==0):
print "Disk is Full"
else:
print "Disk is not Full"
"This would create first object of Disk class"
disk1 = Disk(1024)
disk1.add(500)
disk1.free()
disk1.delete(256)
disk1.add(1024)
disk1.wipe()
disk1.full()
disk1.add(780)
disk1.full()
disk1.add(244)
disk1.full()
output:
Disk is created with 1024 MB
500 MB data is added to Disk
Total Free space in disk is 524
256 MB data is deleted from Disk
No sufficient space for add 1024 MB
Disk is cleared
Disk is not Full
780 MB data is added to Disk
Disk is not Full
244 MB data is added to Disk
Disk is Full
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.