Question

In: Computer Science

Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private...

Please add comments to this code!

JAVA Code:

import java.text.NumberFormat;
public class Item {

   private String name;
   private double price;
   private int bulkQuantity;
   private double bulkPrice;

   /***
   *
   * @param name
   * @param price
   * @param bulkQuantity
   * @param bulkPrice
   */
   public Item(String name, double price, int bulkQuantity, double bulkPrice) {
       this.name = name;
       this.price = price;
       this.bulkQuantity = bulkQuantity;
       this.bulkPrice = bulkPrice;

   }

   public Item(String name, double price) {
       if (price < 0) {
           throw new IllegalArgumentException();
       }

       this.name = name;
       this.price = price;

   }

   /***
   *
   * @param quantity
   * @return
   */
   public double priceFor(int quantity) {
       double actual = 0;
       if (quantity < 0) {
           throw new IllegalArgumentException();
       } else {
           if (bulkQuantity!=0) {
               actual = (quantity / bulkQuantity) * bulkPrice
                       + (quantity % bulkQuantity) * price;
           } else {
               actual = quantity * price;
           }
       }

       return actual;

   }

   public boolean equals(Item ietm) {
       return this.name.equals(ietm.name);
   }

   @Override
   public String toString() {
       NumberFormat format = (NumberFormat) NumberFormat.getCurrencyInstance();

       format.setMinimumFractionDigits(2);
       format.setMaximumFractionDigits(2);
       String str = "";
       str = name + ", " + format.format(price);
       if (bulkPrice != 0) {
           str += " ( " + bulkQuantity + " for " + format.format(bulkPrice)
                   + " )";
       }
       return str;
   }
}

Thanks!!

Solutions

Expert Solution

import java.text.NumberFormat;
public class Item {

        // instance variale of string name
   private String name;
        // instance variale of double price
   private double price;
        // instance variale of int quantity
   private int bulkQuantity;
        // instance variale of double price
   private double bulkPrice;

   /***
   *
   *Constructor takes 4 arguments and assigns to instance variables
   * @param name
   * @param price
   * @param bulkQuantity
   * @param bulkPrice
   */
   public Item(String name, double price, int bulkQuantity, double bulkPrice) {
           // assinging to name
       this.name = name;
       //assigning to price
       this.price = price;
       //assigning to bulkQuantity 
       this.bulkQuantity = bulkQuantity;
     //assigning to bulkPrice
       this.bulkPrice = bulkPrice;

   }

   /**
    * Another constructor which takes name and price as arguments
 * @param name
 * @param price
 */
public Item(String name, double price) {
        //checking if price is negative than throwing the exception
       if (price < 0) {
           throw new IllegalArgumentException();
       }
       // if everything is good assigning the data
       this.name = name;
       this.price = price;

   }

   /***
   *
   * @param quantity
   * @return totalPrice for given quantity
   */
   public double priceFor(int quantity) {
       double actual = 0;
       //if quantity is negative than throwing exception
       if (quantity < 0) {
           throw new IllegalArgumentException();
       } else {
           if (bulkQuantity!=0) {
                   // calculating the actual price if bulkQuantity is not zero 
               actual = (quantity / bulkQuantity) * bulkPrice
                       + (quantity % bulkQuantity) * price;
           } else {
                   // calculating the actual price if bulkQuantity is  zero
               actual = quantity * price;
           }
       }

       //returning actual price
       return actual;

   }

   //checks the equality of 2 items
   public boolean equals(Item ietm) {
           //checking equality based on the name
       return this.name.equals(ietm.name);
   }

   @Override
   public String toString() {
           //creating NumberFormat to format the price to 2 fraction digits
       NumberFormat format = (NumberFormat) NumberFormat.getCurrencyInstance();
       format.setMinimumFractionDigits(2);
       //setting the fractions 2
       format.setMaximumFractionDigits(2);
       String str = "";
       //formating the price and name
       str = name + ", " + format.format(price);
       if (bulkPrice != 0) {
           str += " ( " + bulkQuantity + " for " + format.format(bulkPrice)
                   + " )";
       }
       return str;
   }
}

Related Solutions

Please add comments to this code! Item Class: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! Item Class: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final ArrayList<ItemOrder> itemOrder;    private double total = 0;    private double discount = 0;    ShoppingCart() {        itemOrder = new ArrayList<>();        total = 0;    }    public void setDiscount(boolean selected) {        if (selected) {            discount = total * .1;        }    }    public double getTotal() {        total = 0;        itemOrder.forEach((order) -> {            total +=...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog {...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog { String catalog_name; ArrayList<Item> list; Catalog(String cs_Gift_Catalog) { list=new ArrayList<>(); catalog_name=cs_Gift_Catalog; } String getName() { int size() { return list.size(); } Item get(int i) { return list.get(i); } void add(Item item) { list.add(item); } } Thanks!
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class Phase1 { /* Translates the MAL instruction to 1-3 TAL instructions * and returns the TAL instructions in a list * * mals: input program as a list of Instruction objects * * returns a list of TAL instructions (should be same size or longer than input list) */ public static List<Instruction> temp = new LinkedList<>(); public static List<Instruction> mal_to_tal(List<Instruction> mals) { for (int...
Fill in the following blanks for java code::: import java.util.NoSuchElementException; public class CircularQueue<E> {    private...
Fill in the following blanks for java code::: import java.util.NoSuchElementException; public class CircularQueue<E> {    private E[] queue;    private int front = 0, rear = 0;    private static final int DEFAULT_CAPACITY = 5;       public CircularQueue(int capacity)    {    queue = (E[]) new Object[capacity + 1];    }       public CircularQueue()    {        this(DEFAULT_CAPACITY); }       //Add a method that will determine if the queue is empty. Recall that the queue is...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence;...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence; Message() { sentence=""; } Message(String text) { setSentence(text); } void setSentence(String text) { sentence=text; } String getSentence() { return sentence; } int getVowels() { int count=0; for(int i=0;i<sentence.length();i++) { char ch=sentence.charAt(i); if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') { count=count+1; } } return count; } int getConsonants() { int count=0; for(int i=0;i<sentence.length();i++)...
Add code to the Account class and create a new class called BalanceComparator. import java.util.*; public...
Add code to the Account class and create a new class called BalanceComparator. import java.util.*; public final class Account implements Comparable {     private String firstName;     private String lastName;     private int accountNumber;     private double balance;     private boolean isNewAccount;     public Account(             String firstName,             String lastName,             int accountNumber,             double balance,             boolean isNewAccount     ) {         this.firstName = firstName;         this.lastName = lastName;         this.accountNumber = accountNumber;         this.balance = balance;         this.isNewAccount = isNewAccount;     }     /**      * TO DO: override equals      */     @Override     public boolean equals(Object other) {...
Please convert this code written in Python to Java: import string import random #function to add...
Please convert this code written in Python to Java: import string import random #function to add letters def add_letters(number,phrase):    #variable to store encoded word    encode = ""       #for each letter in phrase    for s in phrase:        #adding each letter to encode        encode = encode + s        for i in range(number):            #adding specified number of random letters adding to encode            encode = encode +...
With the code that is being tested is: import java.util.Random; public class GVdate { private int...
With the code that is being tested is: import java.util.Random; public class GVdate { private int month; private int day; private int year; private final int MONTH = 1; private final int DAY = 9; private static Random rand = new Random(); /** * Constructor for objects of class GVDate */ public GVdate() { this.month = rand.nextInt ( MONTH) + 1; this.day = rand.nextInt ( DAY );    } public int getMonth() {return this.month; } public int getDay() {return this.day;...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {    public static void main(String[] args)    {        for(int i = 1; i <= 4; i++)               //loop for i = 1 to 4 folds        {            String fold_string = paperFold(i);   //call paperFold to get the String for i folds            System.out.println("For " + i + " folds we get: " + fold_string);        }    }    public static String paperFold(int numOfFolds)  ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT