Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please do in JAVA add a new package called abstractClass Inside this new package, implement the following class hierarchy based on the UML class diagram

Please do in JAVA

add a new package called abstractClass

Inside this new package, implement the following class hierarchy based on the UML class diagram and the corresponding descriptions below.

UML DIAGRAM

image text in transcribed

Notice that the class name Printer and the method name print are italicized. In the context of a UML class diagram, this indicates that Printer is an abstract class and print is an abstract method. Also, notice that the field count is underlined. In UML class diagrams, underlining indicates that the class member is static.

  • In the constructor do the following: Update the value of the field count by incrementing it by one. Initialize the field model based on the value provided as an argument. Calculate the value of the field serialNumber by adding the updated value of count to the number 12345.
  • The methods getModel and getSerialNumber have the typical functionality of a getter.
  • The method print is abstract. Nonetheless, it needs a doc comment that describes the functionality that should be provided by subclasses. Look what the print methods in class InkjetPrinter and LaserPrinter are doing. See what they have in common and generalize the behavior so it remains a valid description in case other subclasses (e.g. MatrixPrinter) were added later.
  • In method toString do the following: Return a string of the following format: {className}: {model} #{serialNumber} Where {className}, {model}, and {serialNumber} are the actual values of the corresponding fields. e.g.: InkjetPrinter Canon Pixma #12346 e.g.: LaserPrinter HP LaserJet Pro #12347

LaserPrinter and InkjetPinter:

  • In the constructor do the following: Set the value of remainingToner / remainingCartridge to 100, which indicates that the toner/cartridge is 100% full, and initialize the superclass.
  • The methods getRemainingToner / getRemainingCartridge have the typical functionality of a getter.
  • The methods refillToner / refillCartridge set the fields remainingToner / remainingCartridge to 100.
  • In method print do the following: Check the field value to find out whether the toner/cartridge is empty.
    • If it is empty, print one of the following messages: The toner is empty. or The cartridge is empty. The field values remain unchanged.
    • If the toner/cartridge is not empty, reduce the value of the field remainingToner / remainingCartridge by 10. In addition, print a message that informs the user which printer is printing and how much remaining toner/cartridge is left. e.g.: HP LaserJet Pro is printing. Remaining toner: 90% e.g.: Canon Pixma is printing. Remaining cartridge: 80%

      JUnit Tests: Use the following three test classes to test your code. The JUnit tests need to be included in the video and should not be modified..

package abstractClass;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

/**

* JUnit tests to test class Printer from Lab Abstract.

* This test class should not be changed.

*/

class PrinterTest {

private static InkjetPrinter printer1 = new InkjetPrinter("Canon Pixma");

@Test

void testPrinter_incrementSerialNumbersBy1() {

LaserPrinter printer2 = new LaserPrinter("HB LaserJet Pro");

LaserPrinter printer3 = new LaserPrinter("Lexmark B2338DW");

assertEquals("HB LaserJet Pro", printer2.getModel());

assertEquals("Lexmark B2338DW", printer3.getModel());

assertEquals(12347, printer2.getSerialNumber());

assertEquals(12348, printer3.getSerialNumber());

}

@Test

void getModel_returnsModel() {

assertEquals("Canon Pixma", printer1.getModel());

}

@Test

void getSerialNumber_firstSerialNumber12346() {

assertEquals(12346, printer1.getSerialNumber());

}

@Test

void testToString() {

assertEquals("InkjetPrinter: Canon Pixma #12346", printer1.toString());

}

}

package abstractClass;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.BeforeEach;

import org.junit.jupiter.api.Test;

/**

* JUnit tests to test class LaserPrinter from Lab Abstract.

* This test class should not be changed.

*/

class LaserPrinterTest {

private LaserPrinter printer;

@BeforeEach

void setUp() throws Exception {

printer = new LaserPrinter("Samsung Xpress");

}

@Test

void testLaserPrinter_full() {

LaserPrinter p = new LaserPrinter("HB LaserJet Pro");

assertEquals(100, p.getRemainingToner());

}

@Test

void print_once_reduceTonerBy10percent() {

printer.print();

assertEquals(90, printer.getRemainingToner());

}

@Test

void print_threeTimes_reduceTonerBy30percent() {

printer.print();

printer.print();

printer.print();

assertEquals(70, printer.getRemainingToner());

}

@Test

void print_elevenTimes_reduceTonerUntil0() {

for(int i = 1; i

printer.print();

assertEquals(100 - i * 10, printer.getRemainingToner());

}

printer.print();

assertEquals(0, printer.getRemainingToner()); // still 0

}

@Test

void refillToner_full_remainsFull() {

printer.refillToner();

assertEquals(100, printer.getRemainingToner());

}

@Test

void refillToner_notFull_fillsUpTo100() {

for(int i = 0; i

printer.print();

}

printer.refillToner();

assertEquals(100, printer.getRemainingToner());

}

}

package abstractClass;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.BeforeEach;

import org.junit.jupiter.api.Test;

/**

* JUnit tests to test class InkjetPrinter from Lab Abstract.

* This test class should not be changed.

*/

class InkjetPrinterTest {

private InkjetPrinter printer;

@BeforeEach

void setUp() throws Exception {

printer = new InkjetPrinter("Canon TS202");

}

@Test

void testInkejetPrinter_full() {

InkjetPrinter p = new InkjetPrinter("Canon Pixma");

assertEquals(100, p.getRemainingCartridge());

}

@Test

void print_once_reduceCartridgeBy10percent() {

printer.print();

assertEquals(90, printer.getRemainingCartridge());

}

@Test

void print_threeTimes_reduceCartridgeBy30percent() {

printer.print();

printer.print();

printer.print();

assertEquals(70, printer.getRemainingCartridge());

}

@Test

void print_elevenTimes_reduceCartridgeUntil0() {

for(int i = 1; i

printer.print();

assertEquals(100 - i * 10, printer.getRemainingCartridge());

}

printer.print();

assertEquals(0, printer.getRemainingCartridge()); // still 0

}

@Test

void refillCartridge_full_remainsFull() {

printer.refillCartridge();

assertEquals(100, printer.getRemainingCartridge());

}

@Test

void refillCartridge_notFull_fillsUpTo100() {

for(int i = 0; i

printer.print();

}

printer.refillCartridge();

assertEquals(100, printer.getRemainingCartridge());

}

}

  • PrinterApp: The class PrinterApp is a test client and it includes the main method.

  • Create an array of printers and add two printers: an InkjetPrinter of model Canon TS202 a LaserPrinter of model Samsung Xpress
  • Loop through the array. For each of the printers do the following:
    • Print the printer (the string provided by the toString method)
    • Call the print method 11 times (avoid code duplication)
    • Print an empty line
  • Refill the toner / cartridge of the printers.
  • Dynamically access and display the remaining toner / cartridge after refilling. Use labels (descriptive text) to match the expected output. FYI: "dynamically access" means that the number 100 should not be hard-coded. Instead, the remaining amount of toner/cartridge should be obtained at run-time by calling a method.

Expected output

InkjetPrinter: Canon TS202 #12346 Canon TS202 is printing. Remaining cartridge: 90% Canon TS202 is printing. Remaining cartridge: 80% Canon TS202 is printing. Remaining cartridge: 70% Canon TS202 is printing. Remaining cartridge: 60% Canon TS202 is printing. Remaining cartridge: 50% Canon TS202 is printing. Remaining cartridge: 40% Canon TS202 is printing. Remaining cartridge: 30% Canon TS202 is printing. Remaining cartridge: 20% Canon TS202 is printing. Remaining cartridge: 10% Canon TS202 is printing. Remaining cartridge: 0% The cartridge is empty.

LaserPrinter: Samsung Xpress #12347 Samsung Xpress is printing. Remaining toner: 90% Samsung Xpress is printing. Remaining toner: 80% Samsung Xpress is printing. Remaining toner: 70% Samsung Xpress is printing. Remaining toner: 60% Samsung Xpress is printing. Remaining toner: 50% Samsung Xpress is printing. Remaining toner: 40% Samsung Xpress is printing. Remaining toner: 30% Samsung Xpress is printing. Remaining toner: 20% Samsung Xpress is printing. Remaining toner: 10% Samsung Xpress is printing. Remaining toner: 0% The toner is empty.

Remaining cartridge after refilling: 100% Remaining toner after refilling: 100%

* C Get Homework Help With Cheg: x + assignments/7193172 Instruction Den the Java project 1418_LABS and add a new package called abstractClass side this new package, implement the following dass hierarchy based on the UML class diagram an Printer - model : String - count:int - serialNumberint # > Printer (model : String) + getModel(): String + getSerialNumber(); int + print () + toString(): String Laser Printer - remaining Tonerint + > Inkjet Printer (model : String + getRemainingCartridge (): int refill Cartridge ( ) print ) Printer Notice that the class name Printer and the method name print are italicized. In the context of a UM is an abstract method. Also, notice that the field count is underlined. In UML class diagrams, underlining indicates that the clas In the constructor do the following: Update the value of the field count by incrementing it by one. Initialize the field model based on the value provided as an argument. Calculate the value of the field serial Number by adding the updated value of count to the number The methods getModel and getSerial Number have the typical functionality of a getter

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Business Process Driven Database Design With Oracle PL SQL

Authors: Rajeev Kaula

1st Edition

1795532386, 978-1795532389

More Books

Students also viewed these Databases questions