Question

In: Computer Science

Objectives: use Scite Use recursion to solve a problem Create classes to model objects Problem :...

Objectives: use Scite

  1. Use recursion to solve a problem
  2. Create classes to model objects

Problem : The Rectangle class (Filename: TestRectangle.java)

Design a class named Rectangle to represent a rectangle.

The class contains:

Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height.

A no-arg constructor that creates a default rectangle.

A constructor that creates a rectangle with the specified width and height.

A method named getArea() that returns the area of this rectangle.

A method named getPerimeter() that returns the perimeter.

Write a test program that creates two Rectangle objects. For the first object use the no-arg constructor. For the second object use the second constructor and set the width to 4 and height to 40. Display the width, height, area, and perimeter of each rectangle in this order.

Solutions

Expert Solution

The 2 java files are given below within their respective tables. You are requested to create 2 java files namely Rectangle.java and TestRectangle.java within your project and copy the codes given below in their respective tables and paste into the created files accordingly. Finally, run the driver file TestRectangle.java.

Rectangle.java
public class Rectangle {

    // 2 private data fields representing
    // width and height of the rectangle
    private double width;
    private double height;

    /**
     * No-Arg constructor which initializes the data members
     * with 1
     */
    public Rectangle()
    {
        this.width = 1.0;
        this.height = 1.0;
    }

    /**
     * Constructor to create a rectangle with specified user values
     * @param width
     * @param height
     */
    public Rectangle(double width, double height)
    {
        this.width = width;
        this.height = height;
    }

    /**
     * Method to calculate and return the area
     * @return Area
     */
    public double getArea()
    {
        return (width * height);
    }

    /**
     * Method to calculate and return the area of the Perimeter
     * @return Perimeter
     */
    public double getPerimeter()
    {
        return 2*(width + height);
    }

    /**
     * Method to return the width of the rectangle
     * @return width
     */
    public double getWidth()
    {
        return this.width;
    }

    /**
     * Method to return the height of the rectangle
     * @return height
     */
    public double getHeight()
    {
        return this.height;
    }

}
TestRectangle.java
public class TestRectangle {

    /**
     * main method to test the Triangle Class
     * @param args
     */
    public static void main(String[] args)
    {
        // creating 2 rectangle objects
        Rectangle rectangle1 = new Rectangle(); // with no-arg constructor
        Rectangle rectangle2 = new Rectangle(4, 40); // with arg constructor

        // printing the 1st Rectangle properties
        System.out.println("Rectangle #1");
        System.out.println("---------------");
        System.out.println("Width = " + rectangle1.getWidth() + " units");
        System.out.println("Height = " + rectangle1.getHeight() + " units");
        System.out.println("Area = " + rectangle1.getArea() + " sq units");
        System.out.println("Perimeter = " + rectangle1.getPerimeter() + " units");

        System.out.println();

        // printing the 2nd Rectangle properties
        System.out.println("Rectangle #2");
        System.out.println("---------------");
        System.out.println("Width = " + rectangle2.getWidth() + " units");
        System.out.println("Height = " + rectangle2.getHeight() + " units");
        System.out.println("Area = " + rectangle2.getArea() + " sq units");
        System.out.println("Perimeter = " + rectangle2.getPerimeter() + " units");
    }

}

Console Output

NOTE: All the java codes are same for any ide, hence you can use the codes to run in any of your desired Programming Environment / IDE. Just follow the IDE specific guidelines on how to create a Java Project and classes (graphically) and to run it. It's not possible to upload project files here, its not supported on this portal. We can help you with codes and pictures only. Kindly, comment your doubts within the comments section and please give a thumbs up if you liked the solution. Thanks.


Related Solutions

Objectives: use Scite 1. Use recursion to solve a problem 2. Create classes to model objects...
Objectives: use Scite 1. Use recursion to solve a problem 2. Create classes to model objects Problem 1: Compute greatest common divisor using recursion (filename: TestRecusiveGCD.java) The gcd(m, n) can be defined recursively as follows: If m % n is 0, gcd (m, n) is n. Otherwise, gcd(m, n) is gcd(n, m % n). Write a recursive method to find the GCD of two given integers. Write a test program that prompts the user to enter two integers, calls the...
Objectives: 1. Create classes to model objects Instructions: 1. Create a new folder named Lab6 and...
Objectives: 1. Create classes to model objects Instructions: 1. Create a new folder named Lab6 and save all your files for this lab into this new folder. 2. You can use any IDE (such as SciTE, NetBeans or Eclipse) or any online site (such as repl.it or onlinegdb.com) to develop your Java programs 3. All your programs must have good internal documentation. For information on internal documentation, refer to the lab guide. Problems [30 marks] Problem 1: The Account class...
Objectives: 1. Create classes to model objects Instructions: 1. Create a new folder named Lab6 and...
Objectives: 1. Create classes to model objects Instructions: 1. Create a new folder named Lab6 and save all your files for this lab into this new folder. 2. You can use any IDE (such as SciTE, NetBeans or Eclipse) or any online site (such as repl.it or onlinegdb.com) to develop your Java programs 3. All your programs must have good internal documentation. For information on internal documentation, refer to the lab guide. Problem : The Fraction class (Filename: TestFraction.java) Design...
Longest ‘A’ Path Objectives Manipulating two-dimensional lists Using recursion to solve a problem Problem Specification Write...
Longest ‘A’ Path Objectives Manipulating two-dimensional lists Using recursion to solve a problem Problem Specification Write a Python application to find the longest ‘A’ path on a map of capital (uppercase) letters. The map is represented as a matrix (2-dimensional list) of capital letters. Starting from any point, you can go left, right, up and down (but not diagonally). A path is defined as the unbroken sequence of letters that only covers the spots with the letter A. The length...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type float. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type float. For each class, provide its getter and setter functions, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for...
IN C# WITH SCREENSHOTS OF THE CODE RECURSION Objectives • Learn the basics of recursion. Background...
IN C# WITH SCREENSHOTS OF THE CODE RECURSION Objectives • Learn the basics of recursion. Background There are many problems that loops simplify, such as displaying every pixel to a screen or receiving repetitive input. However, some situations that can be simplified with looping are not easily solvable using loops. This includes problems that require back tracking and being able to use information from previous iterations, which would normally be lost when using an iterative loop. In those cases, it...
PLESE CODE IN C# not java RECURSION Objectives • Learn the basics of recursion – Part...
PLESE CODE IN C# not java RECURSION Objectives • Learn the basics of recursion – Part II (last week was Part I) Submission Guidelines: You will turn in 2 program files (one for each lab problem) Tasks This lab has two parts: Write a driver program that calls this method from the Main program. • • Write a driver program that calls this method from the Main program. Note: Your program name should match the name of your java /...
OBJECTIVES In this paper you will create a small case or use a situation/problem from real...
OBJECTIVES In this paper you will create a small case or use a situation/problem from real life. You will discuss this situation together with a discussion that works through a solution of your own to the problem posed. Problems or issues from your own work (e.g., service operations or overhead functions) can be excellent situations, although they need not be chosen. In the first part of the paper you should specify the situation, while in the second part of the...
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In...
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In my case the project would be called rghanbarPart1. Select the option to create a main method. Create a new class called Vehicle. In the Vehicle class write the code for: • Instance variables that store the vehicle’s make, model, colour, and fuel type • A default constructor, and a second constructor that initialises all the instance variables • Accessor (getters) and mutator (setters) methods...
Using Python to play Checkers: 1) Create classes to represent the necessary objects for game play....
Using Python to play Checkers: 1) Create classes to represent the necessary objects for game play. This will include, at least, the game board, the two types of pieces and the two sides. You will need to determine the best way to represent the relationship between them. 2) Set up one side of the board. Print the status of the board.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT