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

In Java write a ComputerPart class and a ComputerKit class (and a ComputerTest c

ID: 3696146 • Letter: I

Question

In Java write a ComputerPart class and a ComputerKit class (and a ComputerTest client class to test them). The ComputerPart class has two instance variables: a String representing an item (for instance, “cpu” or “disk drive”), and a double representing the price of that item. The ComputerKit class has just one instance variable: a LinkedList of ComputerPart objects (they make up a computer) representing the list of parts for the computer kit (be sure to include JavaDoc style comments to document all methods). You should include the following methods:

- A method returning “expensive” if the total of the prices of the ComputerPart objects is greater than 1000, “cheap” if it is less than 250, and “normal” if it is between 250 and 1000

- A method returning true if a certain item is included in the list of parts; false otherwise

- A method returning how many times a particular item (for instance, “cpu” or “memory”) is found in the list of parts

Explanation / Answer

/** ComputerPart.java **/

public class ComputerPart{
   private String item;
   private double price;

   public ComputerPart(String item, double price){
       this.item = item;
       this.price = price;
   }

   public String getItem(){
       return item;
   }

   public double getPrice(){
       return price;
   }
}

/** ComputerKit.java **/

import java.util.ArrayList;

public class ComputerKit{
   private ArrayList<ComputerPart> items;

   public ComputerKit(){
       items = new ArrayList<ComputerPart>();
   }

   public String price(){
       double total = 0;
       for(ComputerPart part : items)
           total += part.getPrice();
       if(total > 1000)
           return "expensive";
       else if(total < 250)
           return "cheap";
       else
           return "normal";
   }

   public boolean contains(String part){
       for(ComputerPart cpart : items){
           if(cpart.getItem().equalsIgnoreCase(part))
               return true;
       }
       return false;
   }

   public int containsHowMany(String part){
       int count = 0;
       for(ComputerPart cpart : items){
           if(cpart.getItem().equalsIgnoreCase(part))
               count++;
       }
       return count;
   }

   public void add(ComputerPart part){
       items.add(part);
   }
}

/** ComputerKitClient.java **/

public class ComputerKitClient{
   public static void main(String[] args){
       ComputerKit kit = new ComputerKit();
       kit.add(new ComputerPart("cpu",200));
       kit.add(new ComputerPart("memory",80));
       kit.add(new ComputerPart("memory",80));
       kit.add(new ComputerPart("fan",50));
       kit.add(new ComputerPart("graphics card", 250));
       System.out.println(kit.price());
       System.out.println(kit.contains("cpu"));
       System.out.println(kit.containsHowMany("memory"));
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote