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

Java Background Information: The Unified Modeling Language (UML) provides a usef

ID: 3817646 • Letter: J

Question

Java

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 http://www.uml.org/.

Project Requirements:

1)Develop a Screen(computer screen) class:

a.Screen structure - The pixels are represented by dots (.).

b.Static Variables: 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 5 to 20.

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 10 to 50.

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.

2.Increments the screenCount variable.

2)Create a ScreenBuilder class to draw a computer screen.

a.Creates a Screen object

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

c.Calls the setHeight on the screen objects.

i.If the returned boolean value of the setHeight method is false

1.Display an error message that the height is not within range

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

d.Calls the setWidth on the screen objects

i.If the returned boolean value of the setWidth method is false

1.Display an error message that the width is not within range

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

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

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

i.if so return to step 2b.

ii.if not continue

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

  .

3)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

Hi,

Pleasesee below the classes and sample output.Please comment for any queries/feedbacks.

Thanks,

Anita

Screen.java


public class Screen {
   private static int ScreenCount ;
   private int height ;
   private int width ;
  
   // Default (no parameter constructor)
   public Screen() {
       this.height = 0;
       this.width = 0;
       ScreenCount =0;
   }
  
   //   mutator and accessormethods
   public int getHeight() {
       return height;
   }

   public boolean setHeight(int height) {
       if(height>=5 && height<=20){ //checking the height range
           this.height = height;
           return true;
       }
       return false;
   }

   public int getWidth() {
       return width;
   }

   public boolean setWidth(int width) {
       if(width>=10 && width<=50){ ///checking the width range
           this.width = width;
           return true;
       }
       return false;
   }
  
  
   //to return the screencount
   public int getScreenCount (){
       return ScreenCount;
   }

   //Drawing the screen
   public void draw(){
       //Drawing top side
       for(int i=0;i<width;i++){
           System.out.print("* ");
       }
      
       //Drawing left and right sides
       System.out.println();
       for(int j= 0;j<height;j++){
           for(int i=0;i<width;i++){
               if(i==0 || i== width-1){
                   System.out.print("* ");
               }
               else{
                   System.out.print(" ");
               }
           }
           System.out.println();
       }
       ////Drawing bottom side
       for(int i=0;i<width;i++){
           System.out.print("* ");
       }
       ScreenCount++; //incrementing the screencount
   }
}

ScreenBuilder.java

import java.util.Scanner;


public class ScreenBuilder {

   public static void main(String [] args){
       Scanner scan = new Scanner(System.in);
       String ht ="";
       String wd = "";
       int htInt = 0;
       int wdInt=0;
       Screen screen1 = new Screen();
       String choice="";
       do{
           //Reading from user
           System.out.println("Please enter the height[ in range 5 t0 20]. ");
           ht = scan.nextLine();
           htInt = Integer.valueOf(ht);
           while(!screen1.setHeight(htInt)){
               //error if height isnot in range
               System.out.println("Height is not in range.Please reenter height[ in range 5 t0 20].");
               ht = scan.nextLine();
               htInt = Integer.valueOf(ht);
           }
           System.out.println("Please enter the width[ in range 10 t0 50]. ");
           wd = scan.nextLine();
           wdInt = Integer.valueOf(wd);
           while(!screen1.setWidth(wdInt)){
               //error if width is not in range
               System.out.println("Width is not in range.Please reenter width[ in range 10 t0 50].");
               wd = scan.nextLine();
               wdInt = Integer.valueOf(wd);
           }

           screen1.draw();
           System.out.println(" would you like to draw another screen ? Y/N");
           choice =scan.nextLine();
       }while("Y".equalsIgnoreCase(choice));
      
      
       //Showing the screencount
       int screenCount = screen1.getScreenCount();
       System.out.println("There were "+screenCount+" screens drawn.");
   }

}

Sample output:

Please enter the height[ in range 5 t0 20].
10
Please enter the width[ in range 10 t0 50].
20
* * * * * * * * * * * * * * * * * * * *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* * * * * * * * * * * * * * * * * * * *

would you like to draw another screen ? Y/N
y
Please enter the height[ in range 5 t0 20].
5
Please enter the width[ in range 10 t0 50].
10
* * * * * * * * * *
* *
* *
* *
* *
* *
* * * * * * * * * *

would you like to draw another screen ? Y/N
y
Please enter the height[ in range 5 t0 20].
2
Height is not in range.Please reenter height[ in range 5 t0 20].
4
Height is not in range.Please reenter height[ in range 5 t0 20].
7
Please enter the width[ in range 10 t0 50].
3
Width is not in range.Please reenter width[ in range 10 t0 50].
10
* * * * * * * * * *
* *
* *
* *
* *
* *
* *
* *
* * * * * * * * * *

would you like to draw another screen ? Y/N
n
There were 3 screens drawn.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote