Question

in java

the program must contain the main method and example( the number 50 should be the input) must be included in the main method to check the correctness of your programs.
Create a GUI program to let user enter a positive integer n through the first window, and then your program prints out all th

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

Code:-

// PrimeNumbers.java

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.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class PrimeNumbers extends Application
{
   int number;
   public String getNo()
   {
       int i, no = 3, count;
       String res="[";
       if (number>=2)
       {
           res+="2";
       }
       while(no <= number)
       {
           count = 0;
           i = 2;
       while(i <= no/2 )
       {
               if(no % i == 0)
           {
               count++;
               break;
           }
           i++;
       }
       if(count == 0 && no != 1 )
               res+=", "+no;
       no++;
       }
       return res+"]";
   }
   @Override
   public void start(final Stage primaryStage)
   {
       Label inputLabel = new Label("Please enter a number below:");
       final TextField tf = new TextField("");
       Button button = new Button();
       button.setText("Submit");
       button.setOnAction(new EventHandler<ActionEvent>()
       {
           @Override
           public void handle(ActionEvent event)
           {
               number = Integer.parseInt(tf.getText());
               Label secondLabel = new Label("List of prime numbers within [1,"+number+"]");
               final TextArea pf = new TextArea(getNo());
               pf.setPrefHeight(200);
               pf.setPrefWidth(400);
               pf.setEditable(false);
               pf.setWrapText(true);
               FlowPane secondaryLayout = new FlowPane();
               secondaryLayout.setOrientation(Orientation.VERTICAL);
               secondaryLayout.setAlignment(Pos.CENTER);
               secondaryLayout.setColumnHalignment(HPos.CENTER);
               secondaryLayout.setRowValignment(VPos.CENTER);
               secondaryLayout.setVgap(10);
               Button btn = new Button("Go Back");
               secondaryLayout.getChildren().add(secondLabel);
               secondaryLayout.getChildren().add(pf);
               secondaryLayout.getChildren().add(btn);
               Scene secondScene = new Scene(secondaryLayout, 450, 280);
               final Stage newWindow = new Stage();
               newWindow.setTitle("Result");
               newWindow.setScene(secondScene);
               newWindow.setX(primaryStage.getX() + 200);
               newWindow.setY(primaryStage.getY() + 100);
               newWindow.show();
               btn.setOnAction(new EventHandler<ActionEvent>()
               {
               @Override
                   public void handle(ActionEvent event)
                   {
                       newWindow.close();
                       tf.setText("");
                   }
               });
           }
       });
       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().add(inputLabel);
       root.getChildren().add(tf);
       root.getChildren().add(button);
       Scene scene = new Scene(root, 300, 180);
       primaryStage.setTitle("Input");
       primaryStage.setScene(scene);
       primaryStage.show();
   }
   public static void main(String[] args)
   {
       launch(args);
   }
}

Code Screenshots:-

1 2 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 import javafx.application. Application; import javafx.event.ActionE} return rest]; } @Override public void start (final Stage primaryStage) { Label inputLabel = new Label(Please enter a numnewwindow.show(); btn.setOnAction (new EventHandler<ActionEvent>() { @Override public void handle (ActionEvent event) { newwi

Output:-

Input Х Please enter a number below: 50 Submit

Result X List of prime numbers within [1,50] [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] Go Back

Please UPVOTE thank you...!!!

Add a comment
Know the answer?
Add Answer to:
in java the program must contain the main method and example( the number 50 should be...
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
  • the program must contain the main method and example(s) must be included in the main method...

    the program must contain the main method and example(s) must be included in the main method to check the correctness of your programs. Problem 3/5 (30 points) Create a GUI program shown below. User enters information through the first window. When "Close" button is clicked, your program stops; when "Show Info” button is clicked, the input information is displayed on the TextArea; while when “Modify" button is clicked, the second window pops up and the information user enters to the...

  • In Java, please write the program for the following program. Please TEST YOUR PROGRAM. You MUST...

    In Java, please write the program for the following program. Please TEST YOUR PROGRAM. You MUST use GUI, that means all user input must be through the windows. The "first window" is the window on the top left of the following picture. Add 5 more items to be purchased. Search is accessed from second window, top right. Thw third window is the bottom left, and the fourth window is the bottom right. The program MUST CONTAIN EVERYTHING IN THE PICTURES....

  • Write a Java program for a fortune teller. The program should begin by asking the user...

    Write a Java program for a fortune teller. The program should begin by asking the user to enter their name. Following that the program will display a greeting. Next, the program will ask the user to enter the month (a number 1-12) and day of the month (a number 1-13) they were born. Following that the program will display their zodiac sign. Next, the program will ask the user their favorites color (the only choices should be: red, green and...

  • Write a complete Java program, including comments in both the main program and in each method,...

    Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...

  • Write a complete JAVA program to do the following program. The main program calls a method...

    Write a complete JAVA program to do the following program. The main program calls a method to read in (from an input file) a set of people's three-digit ID numbers and their donations to a charity (hint: use parallel arrays)Then the main program calls a method to sort the ID numbers into numerical order, being sure to carry along the corresponding donations. The main program then calls a method to print the sorted 1ists in tabular form, giving both ID...

  • Write a program called printGPA. The program should contain at least 3 methods: main, gradeAverage, and...

    Write a program called printGPA. The program should contain at least 3 methods: main, gradeAverage, and letterGrade. The user will type a line of input containing the student's name, then a number that represents the number of scores, followed by that many integer scores (user input is in bold below). The data type used for the input should be one of the primitive integer data types. Here are two example dialogues: Enter a student record: Maria 5 72 91 84...

  • Write a class called Primes. This class should contain a constructor and a method called next_prime....

    Write a class called Primes. This class should contain a constructor and a method called next_prime. The next_prime method returns the next prime number. For example: >>> n = int(input('How many prime numbers?\n')) >>> p = primes() >>> prime_numbers = [ ] >>> for i in range(n): >>> prime = p.next_prime() >>> primes_numbers.append(prime) >>> print(prime_numbers) If the user types 5 in response to the prompt, then the output should be [2, 3, 5, 7, 11]. python 2.7.13

  • Write a Java program that contains a method named ReadAndFindMax. The argument to this method is...

    Write a Java program that contains a method named ReadAndFindMax. The argument to this method is the filename (of String type). The method opens the file specified in the method argument filename and then reads integers from it. This file can contain any number of integers. Next, the method finds the maximum of these integers and returns it. The main method must first ask the user to enter the filename; then call ReadAndFindMax method with the entered filename as an...

  • Write a JAVA program with methods that initializes an array with 20 random integers between 1...

    Write a JAVA program with methods that initializes an array with 20 random integers between 1 and 50 and then prints four lines of output, containing 1)The initialized array. 2)Every element at an even index. 3)Every even element. 4)All elements in reverse order. Requirements (and hints): 1. Build a method that takes any integer array as input and prints the elements of the array. ALL printing in this lab must be done using a call to the printArray method and...

  • Write program in JAVA with a method that returns an array. The method should accept as input a co...

    Write program in JAVA with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through the array...

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