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 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....
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...
IN JAVA Write a program that uses a two-dimensional array to store the highest and lowest...
IN JAVA Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. Prompt the user for 12 months of highest and lowest.   Write two methods : one to calculate and return the average high and one to calculate and return the average low of the year. Your program should output all the values in the array and then output the average high and the average low. im trying to...
The language is java Write a class called Tablet that stores information about a tablet's age,...
The language is java Write a class called Tablet that stores information about a tablet's age, capacity (in GB), and current usage (in GB). You should not need to store any more information Write actuators and mutators for all instance data Write a toString method When you print a tablet, the info should be presented as such: This tablet is X years old with a capacity of Y gb and has Z gb used. There is A gb free on...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT