In: Computer Science
The language is java
Write a class called Tablet that stores information about a
tablet's age, capacity (in GB), and current usage (in GB). You
should not need to store any more information
Write actuators and mutators for all instance data
Write a toString method
When you print a tablet, the info should be presented as such:
This tablet is X years old with a capacity of Y gb and has Z gb used. There is A gb free on the tablet, which means it is B % full
Write a driver class called MyTablets that creates these Tablets and store them in an ArrayList
2 years old, 512gb capacity, 200gb used
5 years old, 256gb capacity, 18gb used
2 months old, 512gb capacity, 308.5gb used
Print the 3 tablets using a loop.
Delete everything from the 2 year old tablet's hard drive
Up the capacity on the 2 month old tablet to 1 tb
Add 25gb of usage to the 5 year old tablet
Print the 3 tablets using a loop.
Delete the 5 year old tablet from the ArrayList
Add another tablet to the ArrayList that is 10 years old, has 128gb of storage and has 127gb of storage used
Print the 3 tablets
Print the total storage capacity of the 3 tablets combined, as well as the total free space.
Given below is the code for the question. Please do rate the answer if it helped. Thank you.
Tablet.java
----
public class Tablet
{
private double age;
private double capacity;
private double used;
public Tablet()
{
}
public Tablet(double age, double capacity, double
used) {
this.age = age;
this.capacity = capacity;
this.used = used;
}
public double getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getCapacity() {
return capacity;
}
public void setCapacity(double capacity) {
this.capacity = capacity;
}
public double getUsed() {
return used;
}
public void setUsed(double used) {
this.used = used;
}
public String toString(){
double free = capacity -
used;
double percent = used * 100 /
capacity;
String ageStr;
if(age < 1)
ageStr =
(int)(age * 12) + " months";
else
ageStr =
String.format("%.1f years", age);
return String.format("This tablet
is %s old with a capacity of %.1f gb and has %.1f gb used." +
" There is %.1f gb free on the
tablet, which means it is %.1f%% full", ageStr, capacity, used,
free, percent);
}
}
MyTablets.java
----------------
import java.util.ArrayList;
public class MyTablets {
public static void main(String[] args) {
ArrayList<Tablet> tabs = new
ArrayList<Tablet>();
Tablet t1, t2, t3;
t1 = new Tablet(2, 512, 200);
t2 = new Tablet(5, 256, 18);
t3 = new Tablet(2.0/12, 512,
308.5);
System.out.println("adding the 3
tablets to arraylist");
tabs.add(t1);
tabs.add(t2);
tabs.add(t3);
for(Tablet t : tabs)
System.out.println(t);
System.out.println("\nupdating the
3 tablets");
t1.setUsed(0);
t3.setCapacity(1000);
t2.setUsed(t2.getUsed() +
25);
for(Tablet t : tabs)
System.out.println(t);
System.out.println("\ndeleting the
5 year old tablet");
tabs.remove(t2);
System.out.println("\nadding 10
year old tablet");
tabs.add(new Tablet(10, 128,
127));
for(Tablet t : tabs)
System.out.println(t);
double totalCapacity = 0, totalFree
= 0;
for(Tablet t : tabs) {
totalCapacity +=
t.getCapacity();
totalFree +=
t.getCapacity() - t.getUsed();
}
System.out.println("Total capacity
= " + totalCapacity + " gb");
System.out.println("Total free
space = " + totalFree + " gb");
}
}