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...
In java beginner coding language ,Write a class called Sphere that contains instance data that represents...
In java beginner coding language ,Write a class called Sphere that contains instance data that represents the sphere’s diameter. Define the Sphere constructor to accept and initialize the diameter, and include getter and setter methods for the diameter. Include methods that calculate and return the volume and surface area of the sphere. Include a toString method that returns a one-line description of the sphere. Create a driver class called MultiSphere, whose main method instantiates and updates several Sphere objects.
in Java language, in most simple algorithm Using a stack class, write a static method called...
in Java language, in most simple algorithm Using a stack class, write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example parse(“{3 + {4/2} }”)   would return -1...
LANGUAGE: JAVA Create a New Project called YourLastNameDomainName. Write a DomainName class that encapsulates the concept...
LANGUAGE: JAVA Create a New Project called YourLastNameDomainName. Write a DomainName class that encapsulates the concept of a domain name, assuming a domain name has a single attribute: the domain name itself. Include the following: - Constructor: accepts the domain name as an argument. - getDomain: an accessor method for the domain name field. - setDomain: a mutator method for the domain name field. - prefix: a method returning whether or not the domain name starts with www. - extension:...
Using Java: Create a class that will hold the information about a clock, called Clock. You...
Using Java: Create a class that will hold the information about a clock, called Clock. You need to keep minutes, hours and seconds in military time (24-hour) format. Include member functions that will setTime ( this will set the hours, minutes and seconds of the clock), getHours (return the hours), getMinutes (return the minutes) and getSeconds (return the seconds). Another method printTime which will print the time out like this 03:13:09 (hours:minutes:seconds). Overloaded constructors that will take in hours, minutes...
Java programming language should be used Implement a class called Voter. This class includes the following:...
Java programming language should be used Implement a class called Voter. This class includes the following: a name field, of type String. An id field, of type integer. A method String setName(String) that stores its input into the name attribute, and returns the name that was just assigned. A method int setID(int) that stores its input into the id attribute, and returns the id number that was just assigned. A method String getName() that return the name attribute. A method...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods will make use of two text files. a. The first text file contains the names of cities. However, the first line of the file is a number specifying how many city names are contained within the file. For example, 5 Dallas Houston Austin Nacogdoches El Paso b. The second text file contains the distances between the cities in the file described above. This file...
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()
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT