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...
Java program Create two classes based on the java code below. One class for the main...
Java program Create two classes based on the java code below. One class for the main method (named InvestmentTest) and the other is an Investment class. The InvestmentTest class has a main method and the Investment class consists of the necessary methods and fields for each investment as described below. 1.The Investment class has the following members: a. At least six private fields (instance variables) to store an Investment name, number of shares, buying price, selling price, and buying commission...
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...
its java language. 1. Create a class called Car. A Car has instance variables, constructor parameters,...
its java language. 1. Create a class called Car. A Car has instance variables, constructor parameters, setters, and getters for “year manufactured” and “make” and “model” (e.g. 2016 Honda Civic) 2. Create a class called CarLot which has an array of 5 Car references. There are two constructors: (a) One constructor takes no parameters and simply populates the array with these cars: cars[0] = new Car(2016, “honda”, “civic”); cars[2] = new Car(2017, “Lamborghini”, “aventador”); cars[3] = new Car(2000, null, “caravan”);...
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...
C# Programming 1. Create an Employee class with two fields: idNum and hourlyWage. The Employee constructor...
C# Programming 1. Create an Employee class with two fields: idNum and hourlyWage. The Employee constructor requires values for both fields. Upon construction, thrown an ArgumentException if the hourlyWage is less than 7.50 or more than 50.00. Write a program that establishes, one at a time, at least three Employees with hourlyWages that are above, below, and within the allowed range. Immediately after each instantiation attempt, handle any thrown Exceptions by displaying an error message. Save the file as EmployeeExceptionDemo.cs....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT