Question

Using the classes you've developed in Labs 5.1-4, write an app that loads window data from a file, creates the proper window object, loading them up into an array of type Window, and then traversi...

Using the classes you've developed in Labs 5.1-4, write an app that loads window data from a file, creates the proper window object, loading them up into an array of type Window, and then traversing the array performing Window-level actions (displaying, setting visibility, resizing).

The Details

The window.data file contains window data for the various (concrete) Window subclasses (ColoredWindow and BorderedWindow). THe problem is detecting what kind of window data is coming up next — the read method of ColoredWindowtakes a width, height, and background color character, while the read method of BorderedWindow takes only a width and height. There are several ways of resolving this; hat we will do is prefix the data for a window with an indicator (or sentinel) that indicates the type of window whose data follows — a C for ColoredWindow, and a B for BorderedWindow.i You can seen a example of this in the sample run below. When your application reads in the objects from the file, it (the application) reads in the indicator, and then —based on its value (C or B) — calls the proper read method and loads the returned (Window) object into the array. Your application should:

  • declare and create an array of element type Window (create the array with 25 elements)
  • open the file window.data
  • Using the technique described above, call the appropriate (static) read method of one of the Window subclasses to read in and return a newly created object of the proper type, and displaying it
  • Iterate through the array, turning on visibility, and displaying each window
  • Iterate through the array resizing (flip the width and height), and displaying each window
  • Iterate through the array minimizing, and displaying each window

The name of your class should be WindowApp.

Please do not submit any other classes, other than WindowApp.

For example, if the file window.data contains:

C 4 9 O
C 3 4 ;
B 4 6
C 2 3 +
B 5 7

the expected output of the WindowApp application is:

Creating window data from file data, loading it into an array, and displaying
--- Window 0: a 4x9 window with background color 'O'
(Nothing to see here)

--- Window 1: a 3x4 window with background color ';'
(Nothing to see here)

--- Window 2: a 4x6 window with a border
(Nothing to see here)

--- Window 3: a 2x3 window with background color '+'
(Nothing to see here)

--- Window 4: a 5x7 window with a border
(Nothing to see here)


Turning on visibility
--- Window 0: a 4x9 window with background color 'O'
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO

--- Window 1: a 3x4 window with background color ';'
;;;
;;;
;;;
;;;

--- Window 2: a 4x6 window with a border
+----+
|    |
|    |
|    |
|    |
|    |
|    |
+----+

--- Window 3: a 2x3 window with background color '+'
++
++
++

--- Window 4: a 5x7 window with a border
+-----+
|     |
|     |
|     |
|     |
|     |
|     |
|     |
+-----+


Resizing (flipping width and height) and displaying
--- Window 0: a 9x4 window with background color 'O'
OOOOOOOOO
OOOOOOOOO
OOOOOOOOO
OOOOOOOOO

--- Window 1: a 4x3 window with background color ';'
;;;;
;;;;
;;;;

--- Window 2: a 6x4 window with a border
+------+
|      |
|      |
|      |
|      |
+------+

--- Window 3: a 3x2 window with background color '+'
+++
+++

--- Window 4: a 7x5 window with a border
+-------+
|       |
|       |
|       |
|       |
|       |
+-------+


Minimizing and Displaying
--- Window 0: a 9x4 window with background color 'O'
[a 9x4 window with background color 'O' (minimized)]
--- Window 1: a 4x3 window with background color ';'
[a 4x3 window with background color ';' (minimized)]
--- Window 2: a 6x4 window with a border
[a 6x4 window with a border (minimized)]
--- Window 3: a 3x2 window with background color '+'
[a 3x2 window with background color '+' (minimized)]
--- Window 4: a 7x5 window with a border
[a 7x5 window with a border (minimized)]
0 0
Add a comment Improve this question Transcribed image text
Answer #1

JAVA CODE:

WindowApp.java

import java.io.*;
import java.util.Scanner;

class WindowApp{
public static void main(String [] args) {
  
    try {
       Window [] window = read();
     System.out.println("Creating window data from file data, loading it into an array, and displaying");
   
    for (int i= 0; i < window.length; i++) {
      if(window[i] == null) {break;}
      System.out.println("--- Window " + i+ ": " + window[i]);
      window[i].display();
      System.out.println();
    }
    System.out.println("\nTurning on visibility");
    for (int i= 0; i < window.length; i++) {
      if(window[i] == null) {break;}
      System.out.println("--- Window " + i+ ": " + window[i]);
      window[i].setVisible(true);
      window[i].displayNormal();
      System.out.println();
    }
     System.out.println("\nResizing (flipping width and height) and displaying");
    for (int i= 0; i < window.length; i++) {
      if(window[i] == null) {break;}
      window[i].resize(window[i].getHeight(), window[i].getWidth());
      System.out.println("--- Window " + i+ ": " + window[i]);
      window[i].displayNormal();
      System.out.println();
    }
      System.out.println("\nMinimizing and Displaying");
    for (int i= 0; i < window.length; i++) {
      if(window[i] == null) {break;}
      window[i].minimize();
      System.out.println("--- Window " + i+ ": " + window[i]);
      window[i].display();
    }

    
    }
    catch(Exception e) { }
  
}


public static Window[] read() throws Exception{
  
    Window [] window = new Window[25];
  
    Scanner scanner = new Scanner(new File("window.txt"));
  
    int size = 0;
    while(scanner.hasNext()) {
      String typeOfWindow = scanner.next();
      if(typeOfWindow.equals("C")){
        window[size] = ColoredWindow.read(scanner);
      }
      else if (typeOfWindow.equals("B")) {
        window[size]= BorderedWindow.read(scanner);
      }
      size++;
    }
    return window;
}

}

Window.java

abstract class Window implements GUIComponent {
private int height, width;
private boolean isItVisible, minimized;

public Window(int width, int height){
    this.height = height;
    this.width = width;
  
    
}

public int getHeight() { return height; }
public int getWidth() { return width; }

@Override
public String toString() { return "a " + width + "x" + height + " minimal window"; }


public void display() {
    if (!isItVisible) {
      System.out.println("(Nothing to see here)");
    }
   else if (!minimized) { System.out.println(".......................\n:" + toString() + ":\n.......................");
   }

   else {
    System.out.println("[" + this.toString() + " (minimized)]");
   }

}
public void minimize(){ minimized = true; }


public void setVisible(boolean isItVisible) { this.isItVisible = isItVisible; }
public boolean isVisible() { return isItVisible; }
public void restore(){ minimized=false;}


public void resize(int width, int height){
   this.width = width;
   this.height = height;
}

abstract void displayNormal();


}


GUIComponent.java

interface GUIComponent {

void display();
void setVisible(boolean isItvisible);
boolean isVisible();
}

ColoredWindow.java

import java.util.Scanner;
class ColoredWindow extends Window{
private char color = '.';

public ColoredWindow(int width, int height, char color) {
    this(width, height);
    this.color=color;
}
public ColoredWindow(int width, int height) {
    super(width, height);
}

public void setColor(char thisColor){ color= thisColor; }
public char getColor(){ return color; }


public String toString(){
   return "a " + getWidth() + "x" + getHeight()+ " window with background color '" + color + "'" ;
}


public void displayNormal() {
   int height = getHeight();
    while(height > 0){
    
      for(int i=0; i< getWidth(); i++){
        System.out.print(color);
      }
      System.out.println();
      height--;
    }
}
public static ColoredWindow read(Scanner scanner){
if (!scanner.hasNext()) return null;
int width = scanner.nextInt();
int height = scanner.nextInt();
String stringcolor = scanner.next();
char color = stringcolor.charAt(0);
return new ColoredWindow(width, height, color);
}
}


BorderedWindow.java

import java.util.Scanner;
class BorderedWindow extends Window {

public BorderedWindow(int width, int height) {
    super(width, height);
}

public String toString(){
   return "a " + getWidth() + "x" + getHeight()+ " window with a border" ;
}


public void displayNormal() {
String delim = "%" + getWidth() + "s";
String base = "+" + String.format(delim, "").replace(' ', '-') + "+";
String wall= "|" + String.format(delim, "") + "|";
System.out.println(base);
for(int i = 0; i<getHeight(); i++){
    System.out.println(wall);
}
System.out.println(base);
}

public static BorderedWindow read(Scanner scanner){
if (!scanner.hasNext()) return null;
int width = scanner.nextInt();
int height = scanner.nextInt();
return new BorderedWindow(width, height);
}

}

window.txt

C 4 9 O
C 3 4 ;
B 4 6
C 2 3 +
B 5 7  

OUTPUT:

L Problems @ Javadoc DeclarationConsole terminated> WindowAppava Application] C:AProgram Files Javaire1.8.0_201\binljavaw.exe

Add a comment
Know the answer?
Add Answer to:
Using the classes you've developed in Labs 5.1-4, write an app that loads window data from a file, creates the proper window object, loading them up into an array of type Window, and then traversi...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT