Question

Write a program that displays a convex regular polygon with m or n sides, where m...

Write a program that displays a convex regular polygon with m or n sides, where m is the last digit of your student number plus 3, n is the second last digit plus 3. Use two buttons named “before” and “after” to change the number of sides of the polygon. Click on “before” button will show a convex regular polygon with m sides and click on “after” will show a convex regular polygon with n sides. For example, a student with student ID number 100123456 should show 9-side polygon. Click on “after” button will show 8-sidepolygon and click on “before” button will show 9- side polygon. Please provide java code

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below are the screenshots of code & output:

Х Java - primeWindow.cpp/src/RegularPolygon.java - Eclipse File Edit Source Refactor Navigate Search Project Run Window HelpХ Java - primeWindow.cpp/src/RegularPolygon.java - Eclipse File Edit Source Refactor Navigate Search Project Run Window Help

Below is the java code for same. The roll number needs to be written in main & the program wuld update the values of N & M accordingly.

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.stage.Stage;

public class RegularPolygon extends Application {
int N,M;/**the number input**/
static String rollNo;
@Override
public void start(final Stage primaryStage) {
   final double centerX=200;/**center of polygon**/
       final double centerY=200;/**center of polygon**/
       final double radius=150;/**circumradius of polygon**/
   N = rollNo.charAt(rollNo.length()-2)-'0'+3;/**getting value of N from 2nd last digit of roll no.**/
   M = rollNo.charAt(rollNo.length()-1)-'0'+3;/**getting value of M from last digit of roll no.**/
final Polygon initial = new Polygon();/**Polygon**/
initial.setFill(Color.BLUE);
for(int i =0 ; i < M ; i++)/**initially creating a polygon with M sides**/
   initial.getPoints().addAll(centerX+radius*Math.cos(2*i*Math.PI/M),centerY+radius*Math.sin(2*i*Math.PI/M));
Button before = new Button("Before");
Button after = new Button("After");
  
before.setOnAction(new EventHandler<ActionEvent>() {/**if before is pressed**/
@Override
public void handle(ActionEvent event) {
initial.setFill(Color.BLUE);
   initial.getPoints().clear();
for(int i =0 ; i < M ; i++)/**updating to polygon with M sides**/
   initial.getPoints().addAll(centerX+radius*Math.cos(2*i*Math.PI/M),centerY+radius*Math.sin(2*i*Math.PI/M));
}
});   
after.setOnAction(new EventHandler<ActionEvent>() {/**if after is pressed**/
@Override
public void handle(ActionEvent event) {
initial.setFill(Color.ORANGE);
   initial.getPoints().clear();
for(int i =0 ; i < N ; i++)/**updating to polygon with N sides**/
   initial.getPoints().addAll(centerX+radius*Math.cos(2*i*Math.PI/N),centerY+radius*Math.sin(2*i*Math.PI/N));
}
});
/**input window layout properties**/
FlowPane root = new FlowPane();
root.setOrientation(Orientation.VERTICAL);
root.setAlignment(Pos.CENTER);
root.setColumnHalignment(HPos.CENTER);
root.setRowValignment(VPos.CENTER);
root.setVgap(10);
root.getChildren().addAll(initial,before,after);
Scene scene = new Scene(root, 500, 500);
primaryStage.setTitle("Regular Polygon");
primaryStage.setScene(scene);
primaryStage.show();
}

public static void main(String[] args) {
   rollNo = "100123456";/**put your roll no. here**/
launch(args);
}

}

Add a comment
Know the answer?
Add Answer to:
Write a program that displays a convex regular polygon with m or n sides, where m...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • //include comments Opts (Regular polygon) An n-sided regular polygon has n sides of the same length...

    //include comments Opts (Regular polygon) An n-sided regular polygon has n sides of the same length and all angles have the same degree (i.e., the polygon is both equilateral and equiangular). Design a class named RegularPolygon that contains: Aprivate int data field named n that defines the number of sides in the polygon with default value 3. A private double data field named side that stores the length of the side with default value 1. A private double data field...

  • A regular polygon is an n-sided polygon in which all sides are of the same length...

    A regular polygon is an n-sided polygon in which all sides are of the same length and all angles have the same degree (i.e., the polygon is both equilateral and equiangular). The formula for computing the area of a regular polygon isArea =Here, s is the length of a side. Write a program that prompts the user to enter the number of sides and their length of a regular polygon and displays its area. Here is a sample run:

  • (Geometry: n-sided regular polygon) An n-sided regular polygon’s sides all have the same length and all...

    (Geometry: n-sided regular polygon) An n-sided regular polygon’s sides all have the same length and all of its angles have the same degree (i.e., the polygon is both equilateral and equiangular). Design a class named RegularPolygon that contains: ■ A private int data field named n that defines the number of sides in the polygon. ■ A private float data field named side that stores the length of the side. ■ A private float data field named x that defines...

  • Java Turtles Drawing A Regular Polygon Write the code to have the turtle draw a regular...

    Java Turtles Drawing A Regular Polygon Write the code to have the turtle draw a regular polygon with the specified number of sides and specified side length. Draw the Specified number of regular polygons, each starting at the same place, however, after each polygon is drawn the turtle turns 360 / numpolys degrees

  • Problem 2 9.9 Geometry: n-sided regular polygon pg. 362 (25 points) Follow instructions as provided on...

    Problem 2 9.9 Geometry: n-sided regular polygon pg. 362 (25 points) Follow instructions as provided on page 362, reprinted below 9.9 (Geometry: n-sided regular polygon) In an n-sided regular polygon, all sides have the same length and all angles have the same degree (i.e., the polygon is both equilateral and equiangular) Design a class named RegularPolygon that contains A private int data field named n that defines the number of sides in the polygon with default value of 3 A...

  • JavaFX Write a program that displays a histogram to show the number occurrences of each number...

    JavaFX Write a program that displays a histogram to show the number occurrences of each number in an input sequence. There should be a text filed to accept users input numbers and button to show the histogram. Enter your student number (9 digit number) to generate your unique histogram. A student with ID 100013456 will give a histogram like the following one (note the text filed and button are not shown in the following histogram, BUT is required for the...

  • N-Sided Polygon An n-sided polygon is a planed figure whose sides have the same length and...

    N-Sided Polygon An n-sided polygon is a planed figure whose sides have the same length and whose angles have the same degree. Write a class called NSidedPolygon that contains the following components: Private members to store the name of a polygon, the number of sides, and the length of each side. (You are expected to select the appropriate data types for each member.) A constructor that accepts the number of sides and the length of each side. A private method...

  • Write a program that displays a histogram to show the number occurrences of each number in...

    Write a program that displays a histogram to show the number occurrences of each number in an input sequence. There should be a text filed to accept users input numbers and button to show the histogram. Enter your student number to generate your unique histogram. A student with 100013456 will give a histogram like the following one (note the text filed and button are not shown in the following histogram, BUT is required for the application) java code is needed...

  • QUESTION 3 [49 MARKS 3.1) Write the Java statements for a class called Calculator to produce...

    QUESTION 3 [49 MARKS 3.1) Write the Java statements for a class called Calculator to produce the GUI below. Use 3 panels to arrange the components. You must use an array when defining the numbered buttons (o-9. Do not declare 10 buttons individually. The textfield must be initialized with the text as shown below, and be able to accommodate up to 25 characters. The spacing between the numbered buttons is 10. (28) for caloula Add Equals 3.2 Rewrite any existing...

  • Project overview: Create a java graphics program that displays an order menu and bill from a...

    Project overview: Create a java graphics program that displays an order menu and bill from a Sandwich shop, or any other establishment you prefer. In this program the design is left up to the programmer however good object oriented design is required. Below are two images that should be used to assist in development of your program. Items are selected on the Order Calculator and the Message window that displays the Subtotal, Tax and Total is displayed when the Calculate...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT