Assume the existence of a Window class with the following methods : – widen – ac
ID: 3571640 • Letter: A
Question
Assume the existence of a Window class with the following methods : – widen – accepts no arguments ; doubles the width of the window – setHeight – accepts an integer argument corresponding to the new height of the Window – getWidth and getHeight – accept no arguments , return the width and height of the window respectively.
There is also a subclass, TitledWindow whose constructor accepts a width and height (bot integers ), and a title (a string ) — in that order. TitledWindow also has a method , setText that accepts a string , allowing the title of the window to be changed.
Declare a variable , tWindow, of type TitledWindow variable , and initialize it to a window with a width of 50 and a height of 75, and a title of “Rectangular Window”.
Double the width of the window, using widen, set the height of the window so that it is the same as the width, and set the title to the new value “Square Window”.
Explanation / Answer
Window.java
public class Window {
//Declaring instance variables
private int width;
private int height;
//Paramaterized constructor
public Window(int width, int height) {
super();
this.width = width;
this.height = height;
}
//Setters and getters
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
//This method will double the width of the window
public void widen()
{
this.width= 2*width;
}
//toString() is used to display the contents of an object inside it
@Override
public String toString() {
return " Width=" + width + " Height=" + height;
}
}
___________________
TitledWindow.java
public class TitledWindow extends Window {
//Declaring instance variable
private String titledWindow;
//Parameterized constructor
public TitledWindow(int width, int height, String titledWindow) {
super(width, height);
this.titledWindow = titledWindow;
}
//This method will set the title of the window
public void setText(String title)
{
this.titledWindow=title;
}
//This method will get the title of the window
public String getTitledWindow() {
return titledWindow;
}
//toString() method is used to display the content of an object inside it
@Override
public String toString() {
System.out.println(super.toString());
return "TitledWindow=" + titledWindow;
}
}
___________________
Test.java
public class Test {
public static void main(String[] args) {
//Creating an object to the TitledWindow
TitledWindow twindow=new TitledWindow(50,75,"Rectangular Window");
//Displaying the content of Titled window class object
System.out.println(twindow.toString());
//Calling the widen() method on the TitledWindow class object
twindow.widen();
//setting the height of the TitledWindlow class object
twindow.setHeight(twindow.getWidth());
//Setting the text of the TitledWindow class object
twindow.setText("Square Window");
//Displaying the content of Titled window class object
System.out.println(twindow.toString());
}
}
_______________________
Output:
Width=50
Height=75
TitledWindow=Rectangular Window
Width=100
Height=100
TitledWindow=Square Window
_______Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.