Question

In: Computer Science

Create a Square Class and Create a graphical representation of your Square class - your class...

Create a Square Class and Create a graphical representation of your Square class

- your class will have the following data fields: double width, String color.

- provide a no-args constructor.

- provide a constructor that creates a square with the specific width

- implement method getArea()

- implement method getPerimeter()

- implement method setColor().

- draw a UML diagram for your class

- write a test program that will create a square with the width 30, 40 and 50.

Display the width, area and perimeter of each of your squares.

Solutions

Expert Solution

UML class diagram of Square class

//Square.java

class Square
{
double width;
String color;
//No args constructor
Square()
{
width=0.0;
color="black";
}

//constructor to assign width
Square(double w)
{
width=w;
}

// area=width*width
double getArea()
{
return width*width;
}
//perimeter=4*width
double getPerimeter()
{
return 4*width;
}
// to assign color
void setColor(String c)
{
color=c;
}
public String toString()
{
return "\nWidth = "+this.width+"\nArea ="+ this.getArea()+"\nPerimeter = " +this.getPerimeter()+"\ncolor:"+this.color;
}

}

//Main.java


public class Main
{   public static void main(String[] args) {
Square sq1=new Square(30);
Square sq2=new Square(40);
Square sq3=new Square(50);
sq1.setColor("RED");
sq2.setColor("BLUE");
sq3.setColor("GREEN");
System.out.println("Square 1: ");
System.out.println(sq1);
System.out.println("Square 2: ");
System.out.println(sq2);
System.out.println("Square 3: ");
System.out.println(sq3);
   }
}


Related Solutions

In your initial posting, find and share with us an example of a bad graphical representation...
In your initial posting, find and share with us an example of a bad graphical representation of data. Tell us what is bad or wrong with the graphical representation. Then share with us how the graphical representation can be fixed so that it meets the standards of a good graphical representation. Share anything else with us you find important or interesting about the change.
A _______________ is a graphical representation of how much of a good or service households will...
A _______________ is a graphical representation of how much of a good or service households will want to buy at different prices. A _______________ is a graphical representation of how much of a good or service a firm is willing to sell at some specific price. The intersection of the demand and supply curve is called _______________. This is where the quantity demanded is equalto the quantity supplied. There is a _______________ of a good or service when the quantity...
Explain, with the aid of a graphical representation, how the coefficient of determination (R2) is derived
Explain, with the aid of a graphical representation, how the coefficient of determination (R2) is derived
In Statistics, a histogram is a graphical representation of data using bars of different heights. In...
In Statistics, a histogram is a graphical representation of data using bars of different heights. In a histogram, each bar groups numbers into ranges. Taller bars show that more data falls in that range. A histogram displays the shape and spread of continuous or discrete sample data. In this project, you will write a program that creates a histogram that allows users to visually inspect the frequency distribution of a set of values. REQUIREMENTS: * Read from the data file...
how do I create a graph class to store a adjacency list representation of a graph?...
how do I create a graph class to store a adjacency list representation of a graph? I need some help and dont conpletely understand how to start. if you could provide a code with some steps id appreciate it.
In your book there is a Class method called Square that draws a square either using...
In your book there is a Class method called Square that draws a square either using a border or a solid. Using that class, create a Square2 class that also prompts the user for the character to be printed (i.e. such as #, $, whatever) and if the user enters a blank character it defaults to *. You’ll need to modify the called private methods in Square to accept a character passed. Note that for the purposes of this Lab...
in c#: Create a class named Square that contains fields for area and the length of...
in c#: Create a class named Square that contains fields for area and the length of a side and whose constructor requires a parameter for the length of one side of a Square. The constructor assigns its parameter to the length of the Square’s side field and calls a private method that computes the area field. Also include read-only properties to get a Square’s side and area. Create a class named DemoSquares that instantiates an array of ten Square objects...
what is information system is and explain each of its components. Make a graphical representation
what is information system is and explain each of its components. Make a graphical representation
Write the code to create a class Square implementing the interface Figure with constructor to initialize...
Write the code to create a class Square implementing the interface Figure with constructor to initialize with a size and toString method. Write another class SquareUser that creates and array of Squares and initializing with sides 1, 2,3, 4 and 5. Also write the code to call the toString method of squares in a loop to print the string for all squares in the array.
how does the graphical representation below illustrate the dynamics of directional shifts in demand relative to...
how does the graphical representation below illustrate the dynamics of directional shifts in demand relative to the factors such as changes in income, preferences, or expectations? share an example of each for reference and application for the IPad business model.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT