題目:
Printer
Create a class and demonstrate proper encapsulation techniques
the class will be called Printer
It will simulate a real Computer Printer
It should have fields for the toner Level, number of pages printed, and
also whether it’s a duplex printer (capable of printing on both sides of the paper).
Add methods to fill up the toner (up to a maximum of 100%),
another method to simulate printing a page (which should increase the number of pages printed).
Decide on the scope, whether to use constructors, and anything else you think is needed.
Encapsulation
In this exercise you will create one class and name it Printer.
This class will have two member variables of type int, tonerLevel and pagesPrinted, and one of type boolean called duplex.
All three fields will have private access.
The constructor will accept two of these variables, tonerLevel and duplex, as parameters following these rules:
- tonerLevel must be greater than -1 but less than or equal to 100. If it does not meet these conditions then it should be initialized to -1.
- duplex should be initialized to the value of the parameter.
In addition, pagesPrinted should be initialized to 0.
Three other methods need to be defined in this way:
- addToner will accept one parameter, tonerAmount of type int. First off, tonerAmount should be greater than 0 and less than or equal to 100. If this condition is met a second test must be included to test whether tonerLevel plus tonerAmount is greater than 100. If so, the method should return -1. If not then tonerLevel should have the incoming tonerAmount added to it and the new tonerLevel should then be returned by the method. Also, if the initial condition test fails, then the method should return -1.
- printPages will accept one parameter, pages of type int. A variable called pagesToPrint should be created and initialized to the value of the incoming parameter. A conditional check should be performed on whether the Printer class field, duplex, is either “true” or “false”. If “true” then a calculation must be performed to determine the number of pages needed to print the job double sided. The pagesToPrint value is then added to the class field pagesPrinted, but the value of pagesToPrint should be returned by the method.
- getPagesPrinted has no parameters and merely returns the value of the member variable pagesPrinted.
Example input:
Printer printer = new Printer(50, true);System.out.println(printer.addToner(50));System.out.println("initial page count = " +printer.getPagesPrinted());int pagesPrinted = printer.printPages(4);System.out.println("Pages printed was " + pagesPrinted +" new total print count for printer = " +printer.getPagesPrinted());pagesPrinted = printer.printPages(2);System.out.println("Pages printed was " + pagesPrinted +" new total print count for printer = " +printer.getPagesPrinted());
Example output:
100
initial page count = 0
Printing in duplex mode
Pages printed was 2 new total print count for printer = 2
Printing in duplex modePages printed was 1 new total print count for printer = 3
Tips:
- Remember to only paste the code from Printer class into the exercise evaluator. The Main class does not need to be pasted in.
- You can include a message in the printPages method which informs the user that the printer is printing in duplex mode if duplex is equal to “true” if you want.
- When calculating pagesToPrint if duplex is equal to “true” remember that there are two operators which can help you with this. The division “/” operator divides a number and returns only the quotient without any remainder. And the modulo “%” operator divides the number and returns only the remainder, whether 0 or otherwise.
參考答案:
Printer
public class ChallengePrinter {
private int tonerLevel;
private int numberOfPagePrinted;
private boolean duplexPrinter; public ChallengePrinter(int tonerLevel, boolean duplexPrinter) {
if (tonerLevel > 0 && tonerLevel <= 100) {
this.tonerLevel = tonerLevel;
} else {
this.tonerLevel = 100;
}
this.numberOfPagePrinted = 0;
this.duplexPrinter = duplexPrinter;
if(duplexPrinter){
System.out.println("Duplex mode on, double side be printed");
}
} public void fillUp() {
tonerLevel = 100;
System.out.println("Start fill up.");
System.out.println("tonerLevel is 100 full now.");
} public void print(double page) {
if (page <= tonerLevel) {
tonerLevel -= page;
if (isDuplexPrinter()) {
numberOfPagePrinted += Math.ceil(page / 2); } else {
numberOfPagePrinted += page;
}
System.out.println("--- Start print ---");
System.out.println(getNumberOfPagePrinted() + " total page be printed.");
System.out.println("toner level now left: " + getTonerLevel());
System.out.println("--- Done ---\n");
if (getTonerLevel() == 0){
System.out.println("Toner level is 0 plz fill up.");
}
} else {
System.out.println("Toner level is not enough" + getTonerLevel() + "plz fill up.");
} } public int getTonerLevel() {
return tonerLevel;
} public int getNumberOfPagePrinted() {
return numberOfPagePrinted;
} public boolean isDuplexPrinter() {
return duplexPrinter;
}
}
Encapsulation
public class Printer {
private int tonerLevel;
private int pagesPrinted;
private boolean duplex; public Printer(int tonerLevel, boolean duplex) {
if (tonerLevel > -1 && tonerLevel < 100) {
this.tonerLevel = tonerLevel;
} else {
this.tonerLevel = -1;
}
this.duplex = duplex;
pagesPrinted = 0;
} public int addToner(int tonerAmount) {
if (tonerAmount > 0 && tonerAmount < 100) {
if (tonerLevel + tonerAmount > 100) {
return -1;
}
tonerLevel += tonerAmount;
return tonerLevel;
}
return -1;
} public int printPages(int pages) {
int pagesToPrint = pages;
if (duplex) {
pagesToPrint = (pagesToPrint / 2) + (pagesToPrint % 2);
}
pagesPrinted += pagesToPrint;
return pagesToPrint;
} public int getPagesPrinted() {
return pagesPrinted;
}
}