Question

In JAVA

1. Create a class called Rectangle that has integer data members named - length and width. Your class should have a default c

0 0
Add a comment Improve this question Transcribed image text
Answer #1

\color{blue}\underline{1:}

import java.util.Scanner;

public class Rectangle {

    private int width;
    private int length;

    public Rectangle() {
        width = 5;
        length = 10;
    }

    public Rectangle(int width, int length) {
        this.width = width;
        this.length = length;
    }

    public int getWidth() {
        return width;
    }

    public int getLength() {
        return length;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Rectangle rectangle1 = new Rectangle();
        System.out.println("Rect1 width: " + rectangle1.getWidth());
        System.out.println("Rect1 length: " + rectangle1.getLength());
        System.out.print("Enter a width: ");
        int width = in.nextInt();
        System.out.print("Enter a length: ");
        int length = in.nextInt();
        Rectangle rectangle2 = new Rectangle(width, length);
        System.out.println("Rect2 width: " + rectangle2.getWidth());
        System.out.println("Rect2 length: " + rectangle2.getLength());
    }
}

Rect1 width: 5 Recti length: 10 Enter a width: 20 Enter a length: 50 Rect2 width: 20 Rect2 length: 50 Process finished with e

as per guidelines we are only allowed to answer first question Please make new post for second question Thanks

Add a comment
Know the answer?
Add Answer to:
In JAVA 1. Create a class called Rectangle that has integer data members named - length...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class...

    Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class should have a zero-argument constructor that initializes each data member to 0. It should have member functions named calcPerimeter() and calcArea() that calculate the perimeter and area of a rectangle respectively, a member function setLength() and setWidth() to set the length and width, member functions getLength() and getWidth() to return the length and width, and a member function showData() that displays the rectangle’s length,...

  • This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets...

    This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets and sets methods for the variables. Create a No-Arg constructor and a constructor that accepts all three values. At the end of the class add a main method that accepts variables from the user as input to represent the length and width of a room in feet and the price of carpeting per square foot in dollars...

  • For this question you must write a java class called Rectangle and a client class called...

    For this question you must write a java class called Rectangle and a client class called RectangleClient. The partial Rectangle class is given below. (For this assignment, you will have to submit 2 .java files: one for the Rectangle class and the other one for the RectangleClient class and 2 .class files associated with these .java files. So in total you will be submitting 4 files for part b of this assignment.) // A Rectangle stores an (x, y) coordinate...

  • Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default...

    Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default constructor -- that calls the default super class constructor and sets the default height to 0- Overloaded constructor with three parameters (length, width and height) – that calls the two parameterized super class constructor passing in length and width passed and sets the height to passed height.- setDimension method with three parameters (length, width, height) – call the super class setDimension and pass in...

  • Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The...

    Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The ColoredRectangle class will have an additional private String field named Color. The ColoredRectangle class should have four constructors: a no-arg constructor; a three-arg constructor; a two-arg constructor that accepts a Rectangle object and a color; and a copy constructor. The ColoredRectangle class should have an Equals and toString methods. The ColoredRectangle class should have mutators and accessors for all three fields. public class Rectangle...

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • JAVA 1. Write a class called Rectangle that represents a rectangular two-dimensional region. Your Rectangle objects...

    JAVA 1. Write a class called Rectangle that represents a rectangular two-dimensional region. Your Rectangle objects should have the following fields and methods: int width, int height Where width and height are the dimensions of the rectangle. Write accessor methods (i.e. getWidth(), getHeight()) that retrieve the values stored in the fields above. And mutator methods (i.e. setWidthl), setHeight() ) that change the field's values. Finally create a method called getAreal) that returns the area of the rectangle. Like we did...

  • Create a Java Project in Eclipse with the following: Include the Rectangle class supplied below. Override...

    Create a Java Project in Eclipse with the following: Include the Rectangle class supplied below. Override the toString method for Rectangle. Override the equals method for Rectangle. Implement the comparable Interface for Rectangle (Compare by area) Rectangle class: public class Rectangle {       private double length;    private double width;       public Rectangle(double l, double w)    {        length = l;        width = w;    }       public double getLength()    {   ...

  • java language please. thank you T-Mobile 9:00 AM For this assignment, create a Java class named...

    java language please. thank you T-Mobile 9:00 AM For this assignment, create a Java class named "MyRectangle3d" Your class must have the following attributes or variables · x (the x coordinate of the rectangle, an integer) * y (the y coordinate of the rectangle, an integer) * length (the length of the rectangle, an integer) * width (the width of the rectangle, an integer) * height (the height of the rectangle, an integer) * color( the color of the rectangle,...

  • C# for beginners (Methods and Properties, Data types) WorkStation:- Visual Studio 2019 Create a class called...

    C# for beginners (Methods and Properties, Data types) WorkStation:- Visual Studio 2019 Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables – a part number (type int), a part description (type string), a quantity of the item being purchased (type int) and a price per item (type decimal). Your class should have a constructor that initializes...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT