Question

In: Computer Science

WRITE A JAVA PROGRAM - define a class that maintains information about circles in 2 dimensional...

WRITE A JAVA PROGRAM - define a class that maintains information about circles in 2 dimensional plane. Be sure to include required data members and methods to initialize a circle object. provide information on the circle and reposition the circle. do not specify any other methods . in addition make sure data members are accessible in derived classes..

Solutions

Expert Solution

Code:

//class Circle
public class Circle{
    //data members -- X and Y coordinates and the radius
    public double x;
    public double y;
    public double radius;

    //constructor which only takes one parameter radius and sets others as zero
    Circle(double _radius){
        this.x = 0;
        this.y = 0;
        this.radius = _radius;        
    }

    //constructor with 3 parameters to define all the properties
    Circle(double _x,double _y, double _radius){
        this.x = _x;
        this.y = _y;
        this.radius = _radius;
    }

    //repositioning the circle requires to change the X and Y coordinates hence the parameters
    void reposition(double _x,double _y){
        this.x = _x;
        this.y = _y;
    }

    //return the information about circle, its X, Y and Radius
    String getInfo(){
        return "X: "+x+" | Y: "+y+" | Radius: "+radius;
    }
}

Refer to the screenshot for better understanding of the code:

A 2D circle would have the X and Y coordinates and its radius. Therefore we have these 3 data members in the Circle class. For initializing the object, we can have 2 constructors, one takes only the radius and sets X and Y to be 0, whereas other defines all the three properties.

The reposition method returns nothing. It only changes the value of X and Y as received in the parameters. getInfo() mehtod returns a string which has all the properties of the circle inside it.

To make the data members accessible in the derived classes, we have used the 'public' keyword before them. It makes them accessible in the sub-class of all the packages and classes.


Related Solutions

Write a Java program for a simple bank account. You shall define a Customer class: A...
Write a Java program for a simple bank account. You shall define a Customer class: A customer has a first name, last name, and social security number. The social security number is a String variable and must comply with this format: xxx-xx-xxxx where 'x' is a digit between 0-9. If a customer is supplied with an invalid SSN, a message must be printed stating SSN of the customer is invalid; however, the account still is created. You shall define a...
Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
JAVA LANGUAGE Write a program that declares a 2-Dimensional Array with 4 rows and 4 columns...
JAVA LANGUAGE Write a program that declares a 2-Dimensional Array with 4 rows and 4 columns to store integer values, and then: fill elements with values as the sum of its column index and row index, e.g., the element at row index 0 and column index 0 is (0+0=0), the element at row index 0 and column index 1 is (0+1=1). compute the sum of elements at the second row. compute the sum of elements at the third column. compute...
Write a Java program to process the information for a bank customer.  Create a class to manage...
Write a Java program to process the information for a bank customer.  Create a class to manage an account, include the necessary data members and methods as necessary.  Develop a tester class to create an object and test all methods and print the info for 1 customer.  Your program must be able to read a record from keyboard, calculate the bonus and print the details to the monitor.  Bonus is 2% per year of deposit, if the amount is on deposit for 5 years...
2-Dimensional Array Operations. Use a Java class with a main method. In this class (after the...
2-Dimensional Array Operations. Use a Java class with a main method. In this class (after the main method), define and implement the following methods: public static void printArray. This method accepts a two-dimensional double array as its argument and prints all the values of the array, separated by a comma, each row in a separate line. public static double getAverage. This method accepts a two-dimensional double array as its argument and returns the average of all the values in the...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to fill the 2-dimensional array with (random numbers, range 0 - 30). The array has rows (ROW) and columns (COL), where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5....
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to find...
Write a program in Java to: Read a file containing ones and zeros into a two-dimensional...
Write a program in Java to: Read a file containing ones and zeros into a two-dimensional int array. Your program must (1) read the name of the file from the command-line arguments, (2) open the file, (3) check that each line has the same number of characters and (4) check that the only characters in a line are ones and zeros. If there is no such command-line argument or no such file, or if any of the checks fail, your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT