Write a class called MovableCircle that implements the Movable interface given i
ID: 3605073 • Letter: W
Question
Write a class called MovableCircle that implements the Movable interface given in Lab1 problem. This class contains three int fields x, y, and r, that represents the center and radius of the circle, and one constructor that takes three parameters indicating the initial x and y and radius of the point. Write the constructor and the four methods.
Note: for the MovableCircle, the size() and area() returns the radius and area of the circle,move() method shifts the circle to new position with the specified value of x & y-coordinates given by the user and the printShape prints a string on the console “this is a circle at location: xx, yy”, where the xx, and yy are the location x and y of the circle.
Extend the given testInterface_HW class to creates a MovablePoint object or a MovableCircle object depending on the user’s input.
Sample Output:
Enter 1 for movable point and 2 for movable circle. For the Lab assignment, enter only 1.
2
Enter the x coordinate of the circle as an integer
3
Enter the y coordinate of the circle as an integer
4
Enter the radius of the circle as an integer
5
Choose from one of the choices below
1. Move the graph by specifying the x and y coordinates, your graph will be moved by the specified x and y coordinate
2. Display the size of the graph
3. Display the area of the graph
4. Display the location of the graph
5. Exit the program
1
You have chosen to move the graph
Enter the x coordinate that your graph needs to be moved by
4
Enter the y coordinate that your graph needs to be moved by
5
The graph has been moved by 4,5
Choose from one of the choices below
1. Move the graph by specifying the x and y coordinates, your graph will be moved by the specified x and y coordinate
2. Display the size of the graph
3. Display the area of the graph
4. Display the location of the graph
5. Exit the program
4
This is a circle at the location: 7,9
Choose from one of the choices below
1. Move the graph by specifying the x and y coordinates, your graph will be moved by the specified x and y coordinate
2. Display the size of the graph
3. Display the area of the graph
4. Display the location of the graph
5. Exit the program
2
The size of the graph is 5.0
Choose from one of the choices below
1. Move the graph by specifying the x and y coordinates, your graph will be moved by the specified x and y coordinate
2. Display the size of the graph
3. Display the area of the graph
4. Display the location of the graph
5. Exit the program
3
The area of the graph is 78.53981633974483
Choose from one of the choices below
1. Move the graph by specifying the x and y coordinates, your graph will be moved by the specified x and y coordinate
2. Display the size of the graph
3. Display the area of the graph
4. Display the location of the graph
5. Exit the program
5
You have successfully exited the program
-----------------------------------------------------
import java.util.Scanner;
public class testInterface_HW {
private static MovableGraph graph;
private static Scanner input = new Scanner(System.in);
public static void displayGraphOptions() {
System.out.println("Choose from one of the choices below");
System.out.println(
"1. Move the graph by specifying the x and y coordinates, your graph will be moved by the specified x and y coordinate");
System.out.println("2. Display the size of the graph");
System.out.println("3. Display the area of the graph");
System.out.println("4. Display the location of the graph");
System.out.println("5. Exit the program");
executeGraphOptions(input.nextInt());
}
public static void executeGraphOptions(int choice) {
switch (choice) {
case 1: {
System.out.println("You have chosen to move the graph");
System.out.println("Enter the x coordinate that your graph needs to be moved by");
int xMov = input.nextInt();
System.out.println("Enter the y coordinate that your graph needs to be moved by");
int yMov = input.nextInt();
graph.move(xMov, yMov);
System.out.println("The graph has been moved by "+xMov+","+yMov);
displayGraphOptions();
break;
}
case 2: {
System.out.println("The size of the graph is " + graph.size());
displayGraphOptions();
break;
}
case 3: {
System.out.println("The area of the graph is " + graph.area());
displayGraphOptions();
break;
}
case 4: {
graph.printShape();
displayGraphOptions();
break;
}
case 5: {
System.out.println("You have successfully exited the program");
System.exit(1);
break;
}
default: {
System.out.println("Invalid choice, Please choose again");
displayGraphOptions();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Enter 1 for movable point and 2 for movable circle. For the Lab assignment, enter only 1.");
int choice = input.nextInt();
if (choice == 1) {
System.out.println("Enter the x coordinate of the Point as an integer");
int x = input.nextInt();
System.out.println("Enter the y coordinate of the Point as an integer");
int y = input.nextInt();
graph = new MovablePoint(x, y);
displayGraphOptions();
}
else if (choice == 2) {
//Add java code -- for the takehome assignment
}
}
}
Explanation / Answer
import java.util.Scanner;
// Abstract class MovableGraph definition
abstract class MovableGraph
{
// To store coordinate position
int point1, point2;
// To store radios
double radios;
// Parameterized constructor for Circle
MovableGraph(int x, int y, double r)
{
point1 = x;
point2 = y;
radios = r;
}// End of constructor
// Parameterized constructor for Point
MovableGraph(int x, int y)
{
point1 = x;
point2 = y;
}// End of constructor
// Default constructor
MovableGraph()
{
point1 = 0;
point2 = 0;
radios = 0;
}// End of constructor
// Method to move point
public void move(int x, int y)
{
point1 += x;
point2 += y;
System.out.println(" The graph has been moved by " + x + y);
}// End of method
// Method to return radios
public double size()
{
return radios;
}// End of constructor
// Abstract method declaration
abstract public double area();
abstract public void printShape();
}// End of class
// Class MovableCircle derived from MovableGraph class
class MovableCircle extends MovableGraph
{
int xCordinate, yCordinate;
double radi;
// Parameterized constructor
MovableCircle(int x, int y, double r)
{
// Calls the base class constructor
super(x, y, r);
xCordinate = x;
yCordinate = y;
radi = r;
}// End of constructor
// Method to calculate and return area of circle
public double area()
{
return (3.141 * radios * radios);
}// End of method
// Method to display the current location
public void printShape()
{
System.out.println("This is a circle at the location: " + point1 + ", " + point2);
}// End of method
}// End of class
// Class MovablePoint derived from MovableGraph class
class MovablePoint extends MovableGraph
{
int xCordinate, yCordinate;
// Parameterized constructor
MovablePoint(int x, int y)
{
// Calls the base class constructor
super(x, y);
xCordinate = x;
yCordinate = y;
}// End of constructor
// Method to calculate and return area
public double area()
{
return (radios * radios);
}// End of method
// Method to display the current location
public void printShape()
{
System.out.println("This is a Point at the location: " + point1 + ", " + point2);
}// End of method
}// End of class
// Driver class
public class TestInterface_HW
{
// Declares MovableGraph class object
private static MovableGraph graph = null;
// Creates a Scanner class object to accept data
private static Scanner input = new Scanner(System.in);
// Method to display graph options and accepts user choice
public static void displayGraphOptions()
{
System.out.println("Choose from one of the choices below");
System.out.println("1. Move the graph by specifying the x and y coordinates, your graph will be moved by the specified x and y coordinate");
System.out.println("2. Display the size of the graph");
System.out.println("3. Display the area of the graph");
System.out.println("4. Display the location of the graph");
System.out.println("5. Exit the program");
executeGraphOptions(input.nextInt());
}// End of method
// Method to call appropriate method based on the user choice
public static void executeGraphOptions(int choice)
{
// Checks user choice
switch (choice)
{
case 1:
{
System.out.println("You have chosen to move the graph");
System.out.println("Enter the x coordinate that your graph needs to be moved by");
int xMov = input.nextInt();
System.out.println("Enter the y coordinate that your graph needs to be moved by");
int yMov = input.nextInt();
graph.move(xMov, yMov);
System.out.println("The graph has been moved by "+xMov+","+yMov);
displayGraphOptions();
break;
}
case 2:
{
System.out.println("The size of the graph is " + graph.size());
displayGraphOptions();
break;
}
case 3:
{
System.out.println("The area of the graph is " + graph.area());
displayGraphOptions();
break;
}
case 4:
{
graph.printShape();
displayGraphOptions();
break;
}
case 5:
{
System.out.println("You have successfully exited the program");
System.exit(1);
break;
}
default:
{
System.out.println("Invalid choice, Please choose again");
displayGraphOptions();
}
}// End of switch case
}// End of method
// main method definition
public static void main(String[] args)
{
// TODO Auto-generated method stub
System.out.println("Enter 1 for movable point and 2 for movable circle. For the Lab assignment, enter only 1.");
// Accepts user choice
int choice = input.nextInt();
// Checks the choice if one create MovablePoint class object
if (choice == 1)
{
System.out.println("Enter the x coordinate of the Point as an integer");
int x = input.nextInt();
System.out.println("Enter the y coordinate of the Point as an integer");
int y = input.nextInt();
// Creates an object of MovablePoint class
graph = new MovablePoint(x,y);
displayGraphOptions();
}// End of if
// Checks the choice if two create MovableCircle class object
else if (choice == 2)
{
//Add java code -- for the take home assignment
System.out.println("Enter the x coordinate of the circle as an integer");
int x = input.nextInt();
System.out.println("Enter the y coordinate of the circle as an integer");
int y = input.nextInt();
System.out.println("Enter the radius of the circle as an integer");
double r = input.nextDouble();
// Creates an object of MovableCircle class
graph = new MovableCircle(x, y, r);
displayGraphOptions();
}// End of else if
}// End of main method
}// End of class
Sample Run:
Enter 1 for movable point and 2 for movable circle. For the Lab assignment, enter only 1.
2
Enter the x coordinate of the circle as an integer
3
Enter the y coordinate of the circle as an integer
4
Enter the radius of the circle as an integer
5
Choose from one of the choices below
1. Move the graph by specifying the x and y coordinates, your graph will be moved by the specified x and y coordinate
2. Display the size of the graph
3. Display the area of the graph
4. Display the location of the graph
5. Exit the program
1
You have chosen to move the graph
Enter the x coordinate that your graph needs to be moved by
4
Enter the y coordinate that your graph needs to be moved by
5
The graph has been moved by 45
The graph has been moved by 4,5
Choose from one of the choices below
1. Move the graph by specifying the x and y coordinates, your graph will be moved by the specified x and y coordinate
2. Display the size of the graph
3. Display the area of the graph
4. Display the location of the graph
5. Exit the program
4
This is a circle at the location: 7, 9
Choose from one of the choices below
1. Move the graph by specifying the x and y coordinates, your graph will be moved by the specified x and y coordinate
2. Display the size of the graph
3. Display the area of the graph
4. Display the location of the graph
5. Exit the program
2
The size of the graph is 5.0
Choose from one of the choices below
1. Move the graph by specifying the x and y coordinates, your graph will be moved by the specified x and y coordinate
2. Display the size of the graph
3. Display the area of the graph
4. Display the location of the graph
5. Exit the program
3
The area of the graph is 78.525
Choose from one of the choices below
1. Move the graph by specifying the x and y coordinates, your graph will be moved by the specified x and y coordinate
2. Display the size of the graph
3. Display the area of the graph
4. Display the location of the graph
5. Exit the program
5
You have successfully exited the program
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.