Question

In: Computer Science

2. Program containing two modules: a. Create a class Code one programmer-written constructor Create several instance...

2. Program containing two modules:

a. Create a class Code one programmer-written constructor

Create several instance data attributes

Create 3 methods which will each be invoked from the driver program

- one method will not receive any parameters, but will return a value to driver program

- one method will receive one parameter, and will also return a value to driver program

- a display method will receive 2 parameters from driver program

Define a constant properly; use the constant in a calculation

Generate getters and setters for each data attribute

b. Create a driver program

Instantiate objects using the programmer-written constructor

Call each of 3 methods, for every instantiated object, saving any values being returned

Call display method passing necessary values to the method that resides in the class

Solutions

Expert Solution

If you have any problem with the code feel free to comment.

Circle PART A

import java.awt.Point;

class Circle {
        // constant variable
        private static final double PI = 3.14;
        // instance variables
        private int x;
        private int y;
        private double radius;

        // constructor
        public Circle(int x, int y, double radius) {
                this.x = x;
                this.y = y;
                this.radius = radius;
        }

        // calculate area of the circle
        public double getArea() {
                return PI * radius * radius;
        }

        // chekcing if a point is inside the circle
        public boolean isPointInside(Point p) {
                double xPart = (p.getX() - x) * (p.getX() - x);
                double yPart = (p.getY() - y) * (p.getY() - y);
                double radiusSquare = radius * radius;
                if (xPart + yPart <= radiusSquare)
                        return true;
                return false;
        }

        // displaying circle propertise
        public void display(String color, String fillColor) {
                System.out.println("Location: (" + x + ", " + y + ")");
                System.out.println("Rdaius: " + radius + "units");
                System.out.println("Color: " + color);
                System.out.println("Fill Color: " + fillColor);
        }

        // ALL GETTERS AND SETTERS
        public int getX() {
                return x;
        }

        public void setX(int x) {
                this.x = x;
        }

        public int getY() {
                return y;
        }

        public void setY(int y) {
                this.y = y;
        }

        public double getRadius() {
                return radius;
        }

        public void setRadius(double radius) {
                this.radius = radius;
        }
}

Test PART B

import java.awt.Point;

//demo class for testing 
public class Test {
        public static void main(String[] args) {
                Point p = new Point(2, 2);
                Circle circle = new Circle(2, 3, 10);

                System.out.println("Area of the circle: " + circle.getArea() + "units");
                System.out.println("Is point(2, 2) inside the circle? " + circle.isPointInside(p));
                System.out.println("\nCircle Stats:-");
                circle.display("Black", "Orange");
        }
}

Output


Related Solutions

Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of...
Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of the account holder. a method, getBalance, that returns a double corresponding to the account balance. a method withdraw that accepts a double, and deducts the amount from the account balance. Write a class definition for a subclass, CheckingAccount, that contains: a boolean instance variable, overdraft. (Having overdraft for a checking account allows one to write checks larger than the current balance). a constructor that...
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.
Several modules of the GoHRM system were introduced in the class. Please choose one of the...
Several modules of the GoHRM system were introduced in the class. Please choose one of the following (or any others if you wish) to describe what you have learned. Then, please evaluate their functions in terms of areas such as the advantages and disadvantages, change of role of HR due to this function, comprehensiveness of this function, reporting, etc. Altogether, the answers should be not more than 100 words. 1 – Recruitment 2 – Leave management 3 – Payroll and...
Java program Create a constructor for a class named Signal that will hold digitized acceleration data....
Java program Create a constructor for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp data The constructor...
Programming Problem 2 - Cycle [A] Create a class called “Cycle” which has two instance integer...
Programming Problem 2 - Cycle [A] Create a class called “Cycle” which has two instance integer variables as properties, “numberOfWheels” and “weight.” Create a constructor with two parameters, using the same variable names in the parameter list. Assign each variable to numberOfWheels” and “weight” respectively. Write a separate application to test the class and display its properties. Note: Do not change the names of the instance variables or the variables listed in the constructor’s parameter list. [B] Edit your class...
This program is written in Java and should be modularized in methods in one class. This...
This program is written in Java and should be modularized in methods in one class. This program will calculate the Gross Earnings, FICA tax (Medicare and Social Security taxes), Federal Tax Withheld, and Net Amount of the payroll check for each employee of a company. The output must contain numbers with 2 decimal places. The user input must be validated – if incorrect data is entered, send an error message to the user. INPUT The application must be able to...
The following program will be written in JAVA. Create a class called complex performing arithmetic with...
The following program will be written in JAVA. Create a class called complex performing arithmetic with complex numbers. Write a program to test your class.                         Complex numbers have the form:                         realPart + imaginaryPart * i                                               ___                         Where i is sqrt(-1)                                                 Use double variables to represent data of the class. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in...
Code in Java Write a Student class which has two instance variables, ID and name. This...
Code in Java Write a Student class which has two instance variables, ID and name. This class should have a two-parameter constructor that will set the value of ID and name variables. Write setters and getters for both instance variables. The setter for ID should check if the length of ID lies between 6 to 8 and setter for name should check that the length of name should lie between 0 to 20. If the value could not be set,...
c++ (1) Create a class, named Board, containing at least one data member (two-dimensional array) to...
c++ (1) Create a class, named Board, containing at least one data member (two-dimensional array) to store game states and at least two member functions for adding players’ moves and printing the game board. Write a driver to test your class. (2). The data type of the two-dimensional array can be either int or char. As a passlevel program, the size of the board can be hardcoded to 10 or a constant with a pre-set value, anything between 4 and...
C++ * Program 2:      Create a date class and Person Class.   Compose the date class...
C++ * Program 2:      Create a date class and Person Class.   Compose the date class in the person class.    First, Create a date class.    Which has integer month, day and year    Each with getters and setters. Be sure that you validate the getter function inputs:     2 digit months - validate 1-12 for month     2 digit day - 1-3? max for day - be sure the min-max number of days is validate for specific month...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT