Question

In: Computer Science

The language is java Write a class called Tablet that stores information about a tablet's age,...

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.

Solutions

Expert Solution

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");
   }
}


Related Solutions

Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will...
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will accept a variable length parameter list of Strings and concatenate them into one string with a space in between and return it. 2. Overload this method with two parameters, one is a boolean named upper and one is a variable length parameter list of Strings. If upper is true, return a combined string with spaces in upper case; otherwise, return the combined string as...
(In java language) Write an abstract class called House. The class should have type (mobile, multi-level,...
(In java language) Write an abstract class called House. The class should have type (mobile, multi-level, cottage, etc.) and size. Provide the following methods: A no-arg/default constructor. A constructor that accepts parameters. A constructor that accepts the type of the house and sets the size to 100. All other required methods. An abstract method for calculating heating cost. Come up with another abstract method of your own. Then write 2 subclasses, one for mobile house and one for cottage. Add...
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called...
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called without instantiating the class and returns a random Date between Jan 1, 2000 and Dec 31, 2010.
Java - Write an abstract class called Shape with a string data field called colour. Write...
Java - Write an abstract class called Shape with a string data field called colour. Write a getter and setter for colour. Write a constructor that takes colour as the only argument. Write an abstract method called getArea()
language is java Use method overloading to code an operation class called CircularComputing in which there...
language is java Use method overloading to code an operation class called CircularComputing in which there are 3 overloaded methods as follows: computeObject(double radius)-compute area of a circle computeObject(double radius, double height)-compute area of a cylinder computeObject(double radiusOutside, double radiusInside, double height)-compute volume of a cylindrical object These overloaded methods must have a return of computing result in each Then override toString() method so it will return the object name, the field data, and computing result Code a driver class...
Java Write a class called Triangle that can be used to represent a triangle. Write a...
Java Write a class called Triangle that can be used to represent a triangle. Write a class called Describe that will interface with the Triangle class The Server • A Triangle will have 3 sides. It will be able to keep track of the number of Triangle objects created. It will also hold the total of the perimeters of all the Triangle objects created. • It will allow a client to create a Triangle, passing in integer values for the...
In the programming language java: Write a class encapsulating the concept of a telephone number, assuming...
In the programming language java: Write a class encapsulating the concept of a telephone number, assuming a telephone number has only a single attribute: aString representing the telephone number. Include a constructor, the accessor and mutator, and methods 'toString' and 'equals'. Also include methods returning the AREA CODE (the first three digits/characters of the phone number; if there are fewer than three characters in the phone number of if the first three characters are not digits, then this method should...
program language: JAVA For this project, you get to design and write a WeightedCourseGrade class to...
program language: JAVA For this project, you get to design and write a WeightedCourseGrade class to keep track of a student's current grade. You also get to design and write WeightedCourseGradeDriver class that requests input from the user and interacts with the WeightedCourseGrade class. Your WeightedCourseGrade class should store the following information: Weighted subtotal (the sum of all of the categories multiplied by the grade category weight) Total category weights (the sum of all the grade category weights) Provide the...
//Using Java language Write a copy instructor for the following class. Be as efficient as possible....
//Using Java language Write a copy instructor for the following class. Be as efficient as possible. import java.util.Random; class Saw {    private int x;    private Integer p; //------------------------------------------ public Saw() { Random r = new Random();        x = r.nextInt();        p = new Integer(r.nextInt()); } }
IN JAVA PLEASE Create a class called Child with an instance data values: name and age....
IN JAVA PLEASE Create a class called Child with an instance data values: name and age. a. Define a constructor to accept and initialize instance data b. include setter and getter methods for instance data c. include a toString method that returns a one line description of the child
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT