Question

In: Computer Science

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 with sides that have values of 1 through 10. Display the values for each Square. Save the class as DemoSquares.cs

PLEASE SHOW THE OUTPUT.

Solutions

Expert Solution

checking with array size of 5 you can change it to what ever you like

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace DemoSquares

{

class Square

{

public int length;

public int area;

public Square() // constructor

{

Length = 1; //intializing to 1 because its from 1 to 10

}

public int Length // length getter and setter

{

get

{

return length;

}

set

{

length = value;

findArea();

}

}

public int Area

{

get

{

return area;

}

}

private void findArea()

{

area = length * length; // calculate area

}

}

public class DemoSquares

{

public static void Main()

{

Square[] sqr = new Square[5];

int i;

for (i = 0; i < sqr.Length; ++i)

sqr[i] = new Square();

sqr[1].Length = 2;

sqr[2].Length = 3;

sqr[3].Length = 4;

sqr[4].Length = 5;

Console.WriteLine();

Console.WriteLine(" Square 1 length is {0}, and area is {1}", sqr[0].length, sqr[0].area);

Console.WriteLine();

Console.WriteLine();

Console.WriteLine(" Square 2 length is {0},and area is {1}", sqr[1].length, sqr[1].area);

Console.WriteLine();

Console.WriteLine();

Console.WriteLine(" Square 3 length is {0}, and area is {1}", sqr[2].length, sqr[2].area);

Console.WriteLine();

Console.WriteLine();

Console.WriteLine(" Square 4 length is {0}, and area is {1}", sqr[3].length, sqr[3].area);

Console.WriteLine();

Console.WriteLine();

Console.WriteLine(" Square 5 length is {0},and area is {1}", sqr[4].length, sqr[4].area);

Console.ReadLine();

}

}

}

OUTPUT:

IF YOU HAVE ANY QUERY PLEASE COMMENT DOWN BELOW

PLEASE GIVE A THUMBS UP


Related Solutions

Question 2: Design a class named Square that contains data fields side and surfaceArea, and a...
Question 2: Design a class named Square that contains data fields side and surfaceArea, and a method called getSurfaceArea(). Create a child class named Cube. Cube contains a getCubeSurfaceArea() method that calculates the cube surface area. Write a program called DemoSquare that instantiates a Square object and a Cube object and displays the surface areas of the objects. Use the methods created to set the value of the side, and return the surface area. Hint: square area= side*side, Cube surface...
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
Create a class named Sandwich that contains the following data fields: MainIngredient - of type String...
Create a class named Sandwich that contains the following data fields: MainIngredient - of type String Bread - of type String Price - of type Double Include get and set methods for these fields. The methods should be prefixed with 'get' or 'set' respectively, followed by the field name using camel case. For example, setMainIngredient. Use the application named TestSandwich that instantiates one Sandwich object and demonstrates the use of the set and get methods to test your class. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------...
Create a class named “Car” which has the following fields. The fields correspond to the columns...
Create a class named “Car” which has the following fields. The fields correspond to the columns in the text file except the last one. i. Vehicle_Name : String ii. Engine_Number : String iii. Vehicle_Price : double iv. Profit : double v. Total_Price : double (Total_Price = Vehicle_Price + Vehicle_Price* Profit/100) 2. Write a Java program to read the content of the text file. Each row has the attributes of one Car Object (except Total_Price). 3. After reading the instances of...
write program in java Create a class named PersonalDetails with the fields name and address. The...
write program in java Create a class named PersonalDetails with the fields name and address. The class should have a parameterized constructor and get method for each field.  Create a class named Student with the fields ID, PersonalDetails object, major and GPA. The class should have a parameterized constructor and get method for each field. Create an application/class named StudentApp that declare Student object. Prompts (GUI input) the user for student details including ID, name, address, major and GPA....
Introduction to Inheritance (Exercise 1) Write the class named Horse that contains data fields for the...
Introduction to Inheritance (Exercise 1) Write the class named Horse that contains data fields for the name, color, and birth year. Include get and set methods for these fields. Next, finish the subclass named RaceHorse, which contains an additional field that holds the number of races in which the horse has competed and additional methods to get and set the new field. Run the provided DemoHorses application that demonstrates using objects of each class. DemoHorses.java public class DemoHorses { public...
Java Q1: Create a class named Triangle, the class must contain: Private data fields base and...
Java Q1: Create a class named Triangle, the class must contain: Private data fields base and height with setter and getter methods. A constructor that sets the values of base and height. A method named toString() that prints the values of base and height. A method named area() that calculates and prints the area of a triangle. Draw the UML diagram for the class. Implement the class. Q2: Write a Java program that creates a two-dimensional array of type integer...
This is 1 java question with its parts. Thanks! Create a class named Lease with fields...
This is 1 java question with its parts. Thanks! Create a class named Lease with fields that hold an apartment tenant’s name, apartment number, monthly rent amount, and term of the lease in months. Include a constructor that initializes the name to “XXX”, the apartment number to 0, the rent to 1000, and the term to 12. Also include methods to get and set each of the fields. Include a nonstatic method named addPetFee() that adds $10 to the monthly...
In java, create a class named Contacts that has fields for a person’s name, phone number...
In java, create a class named Contacts that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five Contact objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to demonstrate that it works. Include javadoc...
Create a class named Salesperson. Data fields for Salesperson include an integer ID number and a...
Create a class named Salesperson. Data fields for Salesperson include an integer ID number and a doubleannual sales amount. Methods include a constructor that requires values for both data fields, as well as get and set methods for each of the data fields. Write an application named DemoSalesperson that declares an array of 10 Salesperson objects. Set each ID number to 9999 and each sales value to zero. Display the 10 Salesperson objects. public class DemoSalesperson { public static void...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT