Question

In: Computer Science

Part A: (Chapter 8) Make the following updates to the original code below: Override the toString()...

Part A: (Chapter 8) Make the following updates to the original code below:

  • Override the toString() method and equals() method inside your class [2 points]
  • Create a Copy Constructor in your class [2 point]
  • Create a static field in your class called counter. It should increment every time an object is created. [1 point]
  • Create a static method that displays the counter value. Also call this method at the end of your main method. [1 point]
  • Use the "this" reference in all your setter (mutator) methods and also use the "this" reference in your default constructor to call the parameterized constructor. [1 point]
  • Demonstrate that the copy Constructor, toString, and equals methods work in your main method [2 point]

Part B : (Chapter 10) Make the following updates to your Assignment 1

  • Create a sub class that inherits everything from your base class. (For example, if Car is the base class then SportsCar could be your sub class) [1 point]
  • Provide at least one additional attribute to your subclass [1 point]
    • Create gettter/setter methods for it.
  • Create a default constructor for the subclass, that uses super to call the base class default constructor. [1 point]
    • It should set all attributes in the subclass as well as the super class to default values
  • Create a parameterized constructor for the subclass, that uses keyword super to pass the inherited parameters to the base class. [2 point]
    • It should set all attributes in the subclass as well as the super class to the values that are passed in to the constructor.
  • Override the display() method to print out all the instance variable values from the base class, and also from the sub class. [2 point]
  • In your main method, create 2 new object using your subclass, set the data, and call the display method.
    • Create one object using the no-arg (default) constructor of your sub-class. [1 point]
      • Call the set methods to set all attribute data associated to that object.
      • Call the display method for that object
    • Create one object using the parameterized constructor of your sub-class. [1 point]
      • Call the display() method for this object too
  • Add all these objects from Assignment 2 to the same array you created in Assignment 1, and loop through all objects to call the display method. (This demonstrates polymorphism)  [2 point]

----

Original Code:

public class Coffee {
    private int Number;
    private double price;
    private String title;

    public Coffee() {
    }

    public Coffee (int Number, double price, String title) {
        this.Number = Number;
        this.price = price;
        this.title = title;
    }

    public void display() {
        System.out.println("Coffee [price=" + this.price + ", title=" + this.title + ", Number=" + this.Number + "]");
    }

    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getNumber() {
        return this.Number;
    }

    public void setNumber(int Number) {
        this.Number = Number;
    }

    public double getPrice() {
        return this.price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}

-----

Driver (demo) code:

public class Driver {

        public static void main(String[] args) {
            Coffee C1 = new Coffee();
            C1.setPrice(2.50);
            C1.setTitle("Black");
            C1.setNumber(1);
            System.out.println("C1:");
            C1.display();

            Coffee C2 = new Coffee (2, 6.00, "Latte");
            System.out.println("\nC2:");
            C2.display();
        }
}

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

Result:

C1: Coffee [price=2.5, title=Black, Number=1]

C2: Coffee [price=6.0, title=Latte, Number=2]

Process finished with exit code 0

Solutions

Expert Solution

Find below the java code that meets all the Assignment conditions and all points are properly described using comments.

Java Code:

class Coffee {
        private int Number;
        private double price;
        private String title;
        private static int counter;

        // increment every time when object is created
        {
                counter += 1;
        }

        // static method that displays the counter value
        public static void displayCounter() {
                System.out.println("\nCounter: " + counter);
        }

        public Coffee() {
                this(0, 0, null); // "this" reference to call the parameterized constructor
        }

        public Coffee(int Number, double price, String title) {
                this.Number = Number;
                this.price = price;
                this.title = title;
        }

        // copy constructor
        public Coffee(Coffee coffee) {
                this.Number = coffee.Number;
                this.price = coffee.price;
                this.title = coffee.title;
        }

        public void display() {
                System.out.println("Coffee [price=" + this.price + ", title=" + this.title + ", Number=" + this.Number + "]");
        }

        public String getTitle() {
                return this.title;
        }

        public void setTitle(String title) {
                this.title = title;
        }

        public int getNumber() {
                return this.Number;
        }

        public void setNumber(int Number) {
                this.Number = Number;
        }

        public double getPrice() {
                return this.price;
        }

        public void setPrice(double price) {
                this.price = price;
        }

        // override equals methods of Coffee class
        @Override
        public boolean equals(Object o) {
                // If the object is compared with itself then return true
                if (o == this) {
                        return true;
                }

                /*
                 * Check if o is an instance of Complex or not "null instanceof [type]" also
                 * returns false
                 */
                if (!(o instanceof Coffee)) {
                        return false;
                }

                // typecast o to Coffee so that we can compare data members
                Coffee c = (Coffee) o;

                // Compare the data members and return accordingly
                return Double.compare(price, c.price) == 0 && Integer.compare(Number, c.Number) == 0 && title.equals(c.title);
        }
        
        // override toString methods of Coffee class
        @Override
        public String toString() {
                return "Coffee [Number=" + Number + ", price=" + price + ", title=" + title + "]";
        }

}

// subclass that extends Coffee class
class PremiumCoffee extends Coffee {
        private boolean premium; // one additional attribute of subClass

        // default constructor
        public PremiumCoffee() {
                super();                        // call base class default constructor
                premium = false;
        }

        public PremiumCoffee(boolean premium, int Number, double price, String title) {
                super(Number, price, title);            // call base class parameterized constructor
                this.premium = premium;

        }
        
        /*
         * Override the display() method to print out all the instance variable
         * values from the base class, and also from the sub class 
         */
        @Override
        public void display() {
                System.out.println("PremiumCoffee [price=" + getPrice() + ", title=" + getTitle() + ", Number=" + getNumber() + ", Premium=" + this.premium + "]");
        }
        

        public boolean isPremium() {
                return premium;
        }

        public void setPremium(boolean premium ) {
                this.premium = premium;
        }
        
}

public class Driver {

        public static void main(String[] args) {
                
                // using the no-arg (default) constructor
                Coffee C1 = new Coffee();
                C1.setPrice(2.50);
                C1.setTitle("Black");
                C1.setNumber(1);
                System.out.println("C1:");
                C1.display();
                
                // creating object using copy Constructor 
                Coffee C2 = new Coffee(C1);
                System.out.println("\nCopy Constructor(C2):");
                C2.display();
                // check overriden methods toString() and equals()
                System.out.println("\ntoString() : "+C2);
                System.out.println("Is C1 is equals to C2? "+C1.equals(C2));
                
                //using the parameterized constructor
                Coffee C3 = new Coffee(2, 6.00, "Latte");
                System.out.println("\nC3:");
                C3.display();
                System.out.println("Is C1 is equals to C3? "+C1.equals(C3));
                
                // using the no-arg (default) constructor
                PremiumCoffee PC1 = new PremiumCoffee();
                PC1.setPrice(5.50);
                PC1.setTitle("AMERICANO");
                PC1.setNumber(3);
                PC1.setPremium(true);
                System.out.println("\nPC1:");
                PC1.display();
                
                //using the parameterized constructor
                PremiumCoffee PC2 = new PremiumCoffee(false, 4, 8.00, "CAPPUCCINO");
                System.out.println("\nPC2:");
                PC2.display();
                
                // call displayCounter() to get the total number of objects created
                Coffee.displayCounter();
                
        }
}
 
 

Related Solutions

I Have posted my Java code below. Fix the toString, add, and remove implementations so that...
I Have posted my Java code below. Fix the toString, add, and remove implementations so that the following test cases work. Note: I have removed all the unnecessary inherited List implementations. I have them to: throw new UnsupportedException(); For compilation, you could also add //TODO. Test (Main) List list = new SparseList<>(); list.add("0"); list.add("1"); list.add(4, "4"); will result in the following list of size 5: [0, 1, null, null, 4]. list.add(3, "Three"); will result in the following list of size...
Using the following VHDL code for an 8 bit adder, make the sum be displayed on...
Using the following VHDL code for an 8 bit adder, make the sum be displayed on the seven segment display of an Elbert V2 Spartan 3A FPGA Board. VHDL: library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; entity state_bit_adder is Port ( clk : in STD_LOGIC; reset : in STD_LOGIC;            D : in STD_LOGIC;            Enable : out STD_LOGIC_vector (2 downto 0);            input: in std_logic_vector(7 downto 0);            SUM: out...
The following is coded in C++. Please point out any changes or updates you make to...
The following is coded in C++. Please point out any changes or updates you make to the existing code with comments within the code. Start with the provided code for the class linkedListType. Be sure to implement search, insert, and delete in support of an unordered list (that code is also provided). Now, add a new function called insertLast that adds a new item to the END of the list, instead of to the beginning of the list. (Note: the...
The following is coded in C++. Please point out any changes or updates you make to...
The following is coded in C++. Please point out any changes or updates you make to the existing code with comments within the code. Start with the provided code for the class linkedListType. Be sure to implement search, insert, and delete in support of an unordered list (that code is also provided). Also, add a new function called insertLast that adds a new item to the END of the list, instead of to the beginning of the list. (Note: the...
The following is coded in C++. Please point out any changes or updates you make to...
The following is coded in C++. Please point out any changes or updates you make to the existing code with comments within the code. Start with the provided code for the class linkedListType. Be sure to implement search, insert, and delete in support of an unordered list (that code is also provided). Now, add a new function called insertLast that adds a new item to the END of the list, instead of to the beginning of the list. (Note: the...
Chapter 8 Programming exercise 6 "Days of each month" Original Exercise: Design a program that displays...
Chapter 8 Programming exercise 6 "Days of each month" Original Exercise: Design a program that displays the number of days in each month. The program’s output should be similar to this: January has 31 days. February has 28 days. March has 31 days. April has 30 days. May has 31 days. June has 30 days. July has 31 days. August has 31 days. September has 30 days. October has 31 days. November has 30 days. December has 31 days. The...
Explain each of the four parenting styles that is discussed in Chapter 8. As a part...
Explain each of the four parenting styles that is discussed in Chapter 8. As a part of your explanation, please include some of the effects on parental forms of discipline. Also, what was your parents/guardians parenting style? Explain.
Original C code please. Part 1: You can do A, B, and C in one program...
Original C code please. Part 1: You can do A, B, and C in one program with multiple loops (not nested) or each one in a small program, it doesn’t matter. A. Create a loop that will output all the positive multiples of 9 that are less than 99. 9 18 27 36 45        …. B. Create a loop that will output all the positive numbers less than 200 that are evenly divisible by both 2 and 7. 14        28       ...
Depreciation and Sale of Business Property (chapter 8). What are the differences between these three code...
Depreciation and Sale of Business Property (chapter 8). What are the differences between these three code sections: 1231, 1245 and 1250, and when do you apply each? What is Section 179 and When may this Section 179 be used? How does the depreciation recapture rules work?
Depreciation and Sale of Business Property (chapter 8). What are the differences between these three code...
Depreciation and Sale of Business Property (chapter 8). What are the differences between these three code sections: 1231, 1245 and 1250, and when do you apply each? What is Section 179 and When may this Section 179 be used? How does the depreciation recapture rules work?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT