Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Description Write a Java program to define and test a Computer class that aggreg

ID: 3750720 • Letter: D

Question




Description Write a Java program to define and test a Computer class that aggregates a HardDrive class. The Computer class represents a single Computer. Every Computer contains a HardDrive. The HardDrive is manufactured separately and can be used in other Computers. Author/Designer Role The HardDrive class should contain: .An int data member named capacity that indicates the storage capacity in GB . A default no-arg constructor that creates a default HardDrive. Use appropriate mutator methods in your constructors. o Default values should be 256 for capacity A convenience constructor that allows the client to set the capacity on creation. This constructor should call t other default values are set. Use appropriate mutator methods in your constructors Accessor and mutator methods for all data members. These methods should check values as described above and should be used throughout the class as needed. he default constructor with the this keyword to manufacturer sure In the mutator for capacity, manufacturer sure the value being set is not less than 128. If so, set it to 128 o he Computer class should contain: A String data member named manufacturer that sets the manufacturer for the Computer A String data member named model that sets the model for the Computer. A HardDrive data member named hardDrive that contains a HardDrive instance. A default no-arg constructor that creates a default Computer. Use appropriate mutator methods in your constructors. If this constructor is called, a new HardDrive instance should be created and set as the HardDrive for the Computer o Default values should be "OEM" for manufacturer, "Generic" for model A constructor that allows the client to set the HardDrive (by passing a HardDrive instance) Computer manufacturer, and Computer model. Use the appropriate mutator methods. Accessor and mutator methods for all data members. These methods should be used throughout the class as needed.

Explanation / Answer

ScreenShot

Program

//Create a Harddrive class
class HardDrive{
   //Member variable
   @SuppressWarnings("unused")
   private int capacity;
   //Default constructor
   public HardDrive() {
       capacity=256;
   }
   //Parameterized constructor
   public HardDrive(int c) {
       if(c<128) {
           this.capacity=128;
       }
       else {
           this.capacity=c;
       }
   }
   //Mutators
   void setCapacity(int c) {
       if(c<128) {
           this.capacity=128;
       }
       else {
           this.capacity=c;
       }
   }
   //Accessors
   int getCapacity() {
       return capacity;
   }
}
//Create a class computer
class Computer{
   //Member variables
   private String manufacture_name;
   private String model;
   HardDrive hardDrive;
   //Default constructor
   public Computer() {
       manufacture_name="OEM";
       model="Generic";
       hardDrive=new HardDrive();
   }
   //Parameterized constructors
   public Computer(String mn, String m, int Capacity) {
       manufacture_name=mn;
       model=m;
       hardDrive=new HardDrive(Capacity);
   }
   //Mutators
   void setMName(String mn) {
       this.manufacture_name=mn;
   }
   void setModel(String m) {
       this.model=m;
   }
   void setCapacity(int c) {
       hardDrive=new HardDrive(c);
   }
   //Accessors
   String getMName() {
       return manufacture_name;
   }
   String getModel() {
       return model;
   }
   int getCapacity() {
       return hardDrive.getCapacity();
   }
   //display details of the computer
   void getInfo() {
       System.out.println(manufacture_name+" "+model+" Hard Drive capacity: "+hardDrive.getCapacity());
   }
}
//Test class
public class PAssign3 {

   public static void main(String[] args) {
       //Default instance creation
       HardDrive hd=new HardDrive();
       //Create object of computer
       Computer c1=new Computer("Lenovo","X1 Carbon",128);
       //Display details
       c1.getInfo();
       //Create object of computer with default harddrive
       Computer c2=new Computer("HP","EliteDesk",hd.getCapacity());
       //Display details
       c2.getInfo();

   }

}

-------------------------------------------------------

Output

Lenovo X1 Carbon
Hard Drive capacity: 128
HP EliteDesk
Hard Drive capacity: 256