Hello Chegg Java Expets. I am working on Setting up a class called Screen in whi
ID: 3764607 • Letter: H
Question
Hello Chegg Java Expets. I am working on Setting up a class called Screen in which pixels are represented by dots (.). I have set it up according to the following specs, but I need some help setting up the last method below in section e part vi. draw(). Please show me how to set up this method with explanations. Thank you!
Develop a Screen(computer screen) class:
a. Screen structure
i. The pixels are represented by dots (.).
b. screenCount – a static integer variable that represents the number of screen objects created in a given run of the program.
c. Instance variables
i. height – an integer representing the height of the screen, in pixels
ii. width – an integer representing the width of the screen, in pixels
d. Constructors - Default (no parameter constructor)
i. sets the instance variables and static variable to zero
ii. Note the program will use mutator methods to change screen’s height and width to values other than zero.
e. Methods
i. getHeight – returns the screen’s height, in pixels.
ii. getWidth – returns the screen’s width, in pixels.
iii. setHeight (int newHeight)
1. Changes the value of the screen’s height, in pixels
2. Ensures the height is within a range of 720 to 1080.
3. Returns a boolean
a. true – indicating height is within range and height is set to parameter value.
b. false – indicating height not in range and height instance variable unchanged.
iv. setWidth (int newWidth)
1. Changes the value of the screen’s width, in pixels
2. Ensures the width is within a range of 1280 to 1920.
3. Returns a boolean
a. true – indicating width is within range and width is set to parameter value.
b. false – indicating height not in range and height instance variable unchanged.
v. getScreenCount – returns the value of the screenCount static variable.
vi. draw
1. Uses the width and height instance variable to draw a screen of the appropriate dimension. The width and height will need to be divided by 100 before being printed so it will fit the console. For example, the 1024 * 768 resolution will be printed as 10 * 7 block of dot characters on the screen.
2. Increments the screenCount variable.
Explanation / Answer
Please find the required solution:
package Temp;
public class Screen {
int height;
int width;
static int screenCount;
Screen() {
height = width = 0;
}
public int getHeight() {
return height;
}
public boolean setHeight(int height) {
if (height < 720 || height > 1280)
return false;
this.height = height;
return true;
}
public int getWidth() {
return width;
}
public boolean setWidth(int width) {
if (width < 1280 || width > 1920)
return false;
this.width = width;
return true;
}
public static int getScreenCount() {
return screenCount;
}
private void draw() {
for (int i = 0; i < height / 100; i++) {
for (int j = 0; j < width / 100; j++) {
System.out.print(".");
}
System.out.println("");
}
screenCount++;
}
public static void main(String[] args) {
Screen screen = new Screen();
screen.setHeight(1000);
screen.setWidth(1300);
screen.draw();
}
}
Sample output:
.............
.............
.............
.............
.............
.............
.............
.............
.............
.............
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.