Question

In Java: Assume the existence of a Window class with int instance variables width, height, xPos...

In Java:

Assume the existence of a Window class with int instance variables width, height, xPos and yPos.

Define the method toString for the Window class. The String representation of a Window object should be of the form "A 'width'x'height' window at ('xPos','yPos')" where the values within the apostrophes are replaced by the actual values of the corresponding instance variables.

For example, given a Window object whose width is 80, height is 20, xPos is 0 and yPos is 30, toString should return "A 80x20 window at (0,30)."

Homework Answers

Answer #1

Below is your method:

/*
         * Define the method toString for the Window class. The String
         * representation of a Window object should be of the form
         * "A 'width'x'height' window at ('xPos','yPos')" where the values within
         * the apostrophes are replaced by the actual values of the corresponding
         * instance variables.
         */
        public String toString() {
                return "A " + width + "x" + height + " window at (" + xPos + "," + yPos
                                + ").";
        }

Here is your complete code:

public class Window {
        // instance variable
        private int width;
        private int height;
        private int xPos;
        private int yPos;

        // A constructor with paramters
        public Window(int width, int height, int xPos, int yPos) {
                this.width = width;
                this.height = height;
                this.xPos = xPos;
                this.yPos = yPos;
        }

        // default constructor
        public Window() {

        }

        // Getters and Setters
        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;
        }

        public int getxPos() {
                return xPos;
        }

        public void setxPos(int xPos) {
                this.xPos = xPos;
        }

        public int getyPos() {
                return yPos;
        }

        public void setyPos(int yPos) {
                this.yPos = yPos;
        }

        /*
         * Define the method toString for the Window class. The String
         * representation of a Window object should be of the form
         * "A 'width'x'height' window at ('xPos','yPos')" where the values within
         * the apostrophes are replaced by the actual values of the corresponding
         * instance variables.
         */
        public String toString() {
                return "A " + width + "x" + height + " window at (" + xPos + "," + yPos
                                + ").";
        }

        // main method to test the program
        public static void main(String[] args) {
                // creating an object
                Window win = new Window(80, 20, 0, 30);
                // printing the object, it calls toString() internally
                System.out.println(win);
        }

}

Output

A 80x20 window at (0,30).

Know the answer?
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
Similar Questions
Please show fully functioning java code and outputs. Design a Java Animal class (assuming in Animal.java...
Please show fully functioning java code and outputs. Design a Java Animal class (assuming in Animal.java file) and a sub class of Animal named Cat (assuming in Cat.java file).   The Animal class has the following protected instance variables: boolean vegetarian, String eatings, int numOfLegs and the following public instance methods: constructor without parameters: initialize all of the instance variables to some default values constructor with parameters: initialize all of the instance variables to the arguments SetAnimal: assign arguments to all...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName (a String), numSold (an integer that represents the number of that type of game sold), and priceEach (a double that is the price of each of that type of Game). I only want three instance variables!! The class should have the following methods: A constructor that has two parameter – a String containing the name of the Game and a double containing its price....
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String),...
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String), Monthly Salary (double)). Create a constructor that initializes these variables. Define set and get methods for each variable. If the monthly salary is not positive, do not set the salary. Create a separate test class called EmployeeTest that will use the Employee class. By running this class, create two employees (Employee object) and print the annual salary of each employee (object). Then set the...
Write the program in java Implement a class Product. Create instance variables to store product name...
Write the program in java Implement a class Product. Create instance variables to store product name and price and supply the values through constructor. For example new Product(“Toaster’, 29.95). Create methods, getName, getPrice. Write a method productPrinter that prints the product name and its price after reducing it by $5. Create a main class and necessary constructs in the main class to run the Product class.
IN JAVA Inheritance Using super in the constructor Using super in the method Method overriding Write...
IN JAVA Inheritance Using super in the constructor Using super in the method Method overriding Write an application with five classes Vehicle, Car, Bus, Truck, and Tester class with main method in it. The following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassenges, isConvertable, numberSeats, maxWeightLoad, and numberAxels. Characteristics that are applicable to all vehicles should be instance variables in the Vehicle class. The others should be in the class(s) where they belong. Class vehicle should have...
1- Use inheritance to implement the following classes: A: A Car that is a Vehicle and...
1- Use inheritance to implement the following classes: A: A Car that is a Vehicle and has a name, a max_speed value and an instance variable called the number_of_cylinders in its engine. Add public methods to set and get the values of these variables. When a car is printed (using the toString method), its name, max_speed and number_of_cylinders are shown. B: An Airplane that is also a vehicle and has a name, a max_speed value and an instance variable called...
For this assignment, design and implement a class to represent a die (singular of "dice"). A...
For this assignment, design and implement a class to represent a die (singular of "dice"). A normal bag of dice for playing Dungeons and Dragons, for example, contains dice with the following numbers of sides: 4, 6, 8, 10, 12, 20, and 100. In this program, the sides (or faces) of the die are numbered, starting with one. The current face value of the die corresponds to the side that is currently facing upward. By default, the face value is...
****in java Create a class CheckingAccount with a static variable of type double called interestRate, two...
****in java Create a class CheckingAccount with a static variable of type double called interestRate, two instance variables of type String called firstName and LastName, an instance variable of type int called ID, and an instance variable of type double called balance. The class should have an appropriate constructor to set all instance variables and get and set methods for both the static and the instance variables. The set methods should verify that no invalid data is set. Also define...
1. In C++, programmers can use a class to create a large number of instance variables...
1. In C++, programmers can use a class to create a large number of instance variables of the class's type. Group of answer choices True False 2. When a C++ programmer declares a function, they are required to state all of the following except Group of answer choices {} The type of data, if any, returned by the function The function name The type of data, if any, sent to the function 3. When a C++ programmer calls a function,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT