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...
Create a class called Employee that should include four pieces of information as instance variables—a firstName...
Create a class called Employee that should include four pieces of information as instance variables—a firstName (type String), a lastName (type String), a mobileNumber (type String) and a salary (type int). Your class (Employee) should have a full argument constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. The validation for each attribute should be like below: mobileNumber should be started from “05” and the length will be limited to 10...
Write a Java class called Rectangle that represents a rectangular two-dimensional region. It should have following...
Write a Java class called Rectangle that represents a rectangular two-dimensional region. It should have following 4 fields: int x (x-coordinate for its top left corner) int y (y coordinate for its top left corner) double height (height of the rectangle) double width (width of the rectangle) Your rectangle objects should have the following methods: public Rectangle(int newx, int newy, int newwidth, int newheight) Constructor that initializes the x-coordinate, y-coordinate for the top left corner and its width and height....
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...
public class Bicycle { // Instance variables (fields) private String ownerName; private int licenseNumber; // Constructor...
public class Bicycle { // Instance variables (fields) private String ownerName; private int licenseNumber; // Constructor public Bicycle( String name, int license ) { ownerName = name;    licenseNumber = license; } // Returns the name of the bicycle's owner public String getOwnerName( ) { return ownerName; } // Assigns the name of the bicycle's owner public void setOwnerName( String name ) { ownerName = name; } // Returns the license number of the bicycle public int getLicenseNumber( ) {...
Java Programming COMP-228 LAB ASSIGNMENT #1 >> Apply the naming conventions for variables, methods, classes, and...
Java Programming COMP-228 LAB ASSIGNMENT #1 >> Apply the naming conventions for variables, methods, classes, and packages: - variable names start with a lowercase character for the first word and uppercase for every other word - classes start with an uppercase character of every word - packages use only lowercase characters - methods start with a lowercase character for the first word and uppercase for every other word Java Programming COMP-228 Exercise #1: [5 marks] Write a Java application using...
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.
1. Circle: Implement a Java class with the name Circle. It should be in the package...
1. Circle: Implement a Java class with the name Circle. It should be in the package edu.gcccd.csis. The class has two private instance variables: radius (of the type double) and color (of the type String). The class also has a private static variable: numOfCircles (of the type long) which at all times will keep track of the number of Circle objects that were instantiated. Construction: A constructor that constructs a circle with the given color and sets the radius to...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT