Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Develop a Java program that Uses selection constructs (if, and if else). Uses th

ID: 3764230 • Letter: D

Question

Develop a Java program that

Uses selection constructs (if, and if else).

Uses the Java iteration constructs (while, do, for).

Uses static variables.

Ensure integer variables input are within a range that will not cause integer overflow.

Uses proper design techniques including reading UML Class Diagrams

Preparatory Readings: Absolute Java textbook, chapter 1 - 5.

Background Information:

The Unified Modeling Language (UML) provides a useful notation for designing and developing object-oriented software systems. One of the basic components of the UML is a class diagram, which are used to depict the attributes and behaviors of a class. A basic class diagram (as shown in the figure below) has three components. The first is the class name. The second component includes the class's attributes or fields. Each attribute is followed by a colon (:) and its data type. The third component includes the class's behaviors or methods. If the method takes parameters, their types are included in parentheses. Each behavior is also followed by a colon (:) and its return type. If the return value of a method is void, the return type can be omitted. For more information on the UML, refer to HYPERLINK "http://www.uml.org/" http://www.uml.org/.

Project Requirements:

Develop a Screen(computer screen) class:

Screen structure

The pixels are represented by dots (.).

screenCount – a static integer variable that represents the number of screen objects created in a given run of the program.

Instance variables

height – an integer representing the height of the screen, in pixels

width – an integer representing the width of the screen, in pixels

Constructors - Default (no parameter constructor)

sets the instance variables and static variable to zero

Note the program will use mutator methods to change screen’s height and width to values other than zero.

Methods

getHeight – returns the screen’s height, in pixels.

getWidth – returns the screen’s width, in pixels.

setHeight (int newHeight)

Changes the value of the screen’s height, in pixels

Ensures the height is within a range of 720 to 1080.

Returns a boolean

true – indicating height is within range and height is set to parameter value.

false – indicating height not in range and height instance variable unchanged.

setWidth (int newWidth)

Changes the value of the screen’s width, in pixels

Ensures the width is within a range of 1280 to 1920.

Returns a boolean

true – indicating width is within range and width is set to parameter value.

false – indicating height not in range and height instance variable unchanged.

getScreenCount – returns the value of the screenCount static variable.

draw

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.

Increments the screenCount variable.

Create a ScreenBuilder class to draw a computer screen.

Creates a Screen object

Prompt user to enter the height, include heights range in prompt.

Calls the setHeight on the screen objects.

If the returned boolean value of the setHeight method is false

Display an error message that the height is not within range

Returns to step 2b and prompt for a new height value.

Calls the setWidth on the screen objects

If the returned boolean value of the setWidth method is false

Display an error message that the width is not within range

Returns to step 2d and prompt for a new width value

Calls the draw on the screen object method display the screen.

Prompts the user if they would like to draw another screen.

if so return to step 2b.

if not continue

Call the getScreenCount method to display the number of screen created by in a statement such as, “There were 4 screens drawn.”

Below is the UML diagram of the Screen class.

Screen

height : int

width : int

static screenCount : int

Screen()

Screen (int, int)

getHeight():int

setHeight(int newHeight)

getWidth(): int

setWidth(int newWidth)

getScreenCount():int

draw()

Screen

height : int

width : int

static screenCount : int

Screen()

Screen (int, int)

getHeight():int

setHeight(int newHeight)

getWidth(): int

setWidth(int newWidth)

getScreenCount():int

draw()

Explanation / Answer

Here is the code as per the specification mentioned above:

Screen.java


public class Screen {

   private int height;
   private int width;
   private static int screenCount = 0;

   public Screen() {
       height = 0;
       width = 0;
   }

   public Screen(int width, int height) {
       setWidth(width);
       setHeight(height);
   }

   public static int getScreenCount() {
       return screenCount;
   }

   public static void setScreenCount(int screenCount) {
       Screen.screenCount = screenCount;
   }

   public int getHeight() {
       return height;
   }

   public boolean setHeight(int height) {
       if (height >= 720 && height <= 1080) {
           this.height = height;
           return true;
       }
       System.out.println("Invalid Height.. Can be range of 720 to 1080");
       return false;
   }

   public int getWidth() {
       return width;
   }

   public void draw() {
       int width = getWidth() / 100;
       int height = getHeight() / 100;

       for (int h = 0; h < height; h++) {
           for (int w = 0; w < width; w++) {
               if (h == 0 || h == height - 1) {
                   System.out.print(" .");
               } else {
                   if (w == 0 || w == width - 1) {
                       System.out.print(" .");
                   } else {
                       System.out.print(" ");
                   }
               }
           }
           System.out.println();
       }
   }

   public boolean setWidth(int width) {
       if (width >= 1280 && width <= 1920) {
           this.width = width;
           return true;
       }
       System.out.println("Invalid Height.. Can be range of 1280 to 1920");
       return false;
   }
}

Screen Builder.java

import java.util.Scanner;

public class ScreenBuilder {

   public static void main(String[] args) {
       String toPrint = "y";
       Screen screen = null;
       Scanner in = null;
       do {
           screen = new Screen();
           in = new Scanner(System.in);
           do {
               System.out.println("Enter Width:");
           } while (!screen.setWidth(in.nextInt()));

           do {
               System.out.println("Enter Height:");
           } while (!screen.setHeight(in.nextInt()));

           screen.draw();
           Screen.setScreenCount(Screen.getScreenCount() + 1);
           System.out.println("Do you want to continue: ");
       } while (toPrint.equalsIgnoreCase(in.next()));
       in.close();
       System.out.println("There were " + Screen.getScreenCount() + " screens drawn");
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote