Question

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 application)

Edit: use student ID 100473587

Occurrence Histogram 3.5 الفة 2.5 2 1.5 1 1 0.5 0 1 2 3 3 4 5 6 7 8 9

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


import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.stage.Stage;

/**
*
* @author BB2
*/
public class Hitogram extends Application {
  
@Override
public void start(Stage primaryStage) {
  
  
  
VBox vbox=new VBox();
vbox.setSpacing(10);
  
  
  
TextField tf=new TextField();
tf.setMinWidth(280);
  
  
Button btn = new Button();
btn.setText("Get Histogram");
btn.setOnAction(new EventHandler<ActionEvent>() {
  
@Override
public void handle(ActionEvent event) {
String seq=tf.getText();
long val=Long.parseLong(seq);
  
int [] digits=new int[10];
for(int i=0;i<10;i++){
digits[i]=0;
}
while(val>0){
int i=(int) (val%10);
digits[i]++;
val=val/10;
}
int max=digits[0];
for(int i=1;i<10;i++){
if(digits[i]>max){
max=digits[i];
}
}

vbox.getChildren().clear();
  
int n=2*max+1;
Rectangle[] r=new Rectangle[10];

Line v=new Line();
Line h=new Line();


v.setStartX(5);
v.setStartY(5);
v.setEndX(5);
v.setEndY(250);
v.setStroke(Color.SILVER);

h.setStartX(5);
h.setStartY(250);
h.setEndX(370);
h.setEndY(250);
h.setStroke(Color.SILVER);

Group gp=new Group();
gp.getChildren().add(v);
gp.getChildren().add(h);


Line [] lines=new Line[n];

int ed=240/n;
int ehl=35;

for(int i=0;i<n;i++){
lines[i]=new Line();
lines[i].setStartX(5);
lines[i].setStartY(250-ed*(i+1));
lines[i].setEndX(370);
lines[i].setEndY(250-ed*(i+1));
  
lines[i].setStroke(Color.SILVER);
gp.getChildren().add(lines[i]);
}

for(int i=0;i<10;i++){
r[i]=new Rectangle();
r[i].setHeight(2*digits[i]*ed);
r[i].setWidth(ehl);
r[i].setX(5+((ehl*(i))));
r[i].setY(250-2*digits[i]*ed-1);
r[i].setFill(Color.BLUE);
gp.getChildren().add(r[i]);
}



Label l0=new Label("0");
Label l1=new Label("1");
Label l2=new Label("2");
Label l3=new Label("3");
Label l4=new Label("4");
Label l5=new Label("5");
Label l6=new Label("6");
Label l7=new Label("7");
Label l8=new Label("8");
Label l9=new Label("9");

Label [] lbls=new Label[n];

Label title=new Label("Occurrence Histogram");
title.setFont(new Font(25));
title.setTextFill(Color.SILVER);
title.setPadding(new Insets(0, 70, 0, 100));

HBox below=new HBox();

below.setMargin(l0, new Insets(8, 14, 8, 14));
below.setMargin(l1, new Insets(8, 14, 8, 14));
below.setMargin(l2, new Insets(8, 14, 8, 14));
below.setMargin(l3, new Insets(8, 14, 8, 14));
below.setMargin(l4, new Insets(8, 14, 8, 14));
below.setMargin(l5, new Insets(8, 14, 8, 14));
below.setMargin(l6, new Insets(8, 14, 8, 14));
below.setMargin(l7, new Insets(8, 14, 8, 14));
below.setMargin(l8, new Insets(8, 14, 8, 14));
below.setMargin(l9, new Insets(8, 14, 8, 14));
  
ObservableList list = below.getChildren();
  
list.addAll(l0,l1,l2,l3,l4 ,l5,l6,l7,l8,l9);   

Label [] lblv=new Label[n+1];
VBox left=new VBox();

for(int i=0;i<=n;i++){
String str=""+(.5*i);
lblv[i]=new Label(str);
}

left.setMargin(lblv[n], new Insets(240-n*ed, 8,0, 8));

for(int i=n-1;i>=0;i--){
left.setMargin(lblv[i], new Insets((ed)/2, 8, 0, 8));

}

ObservableList list1 = left.getChildren();
  
for(int i=n;i>=0;i--){
list1.add(lblv[i]);
}


VBox vb=new VBox();
vb.getChildren().add(gp);
vb.getChildren().add(below);


HBox mdl=new HBox();
mdl.getChildren().add(left);
mdl.getChildren().add(vb);


vbox.getChildren().add(title);
vbox.getChildren().add(mdl);
//vbox.getChildren().add(below);
  
}
});
  
HBox hbox=new HBox();
hbox.setSpacing(15);
  
  
hbox.getChildren().add(tf);
hbox.getChildren().add(btn);
  
StackPane root = new StackPane();
root.getChildren().add(hbox);
  
  
  
VBox grp=new VBox();
  
grp.getChildren().add(root);
grp.getChildren().add(vbox);
  
Scene scene = new Scene(grp, 450, 350);
  
primaryStage.setTitle("HISOGRAM");
primaryStage.setScene(scene);
primaryStage.show();
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
  
}
HISOGRAM 100013456 Get Histogram Occurrence Histogram 3.5 3.0 2.5 2.0 1.5 1.0 0.5 0.0 0 1 2 3 4 5 6 7 8 1 9

Add a comment
Know the answer?
Add Answer to:
JavaFX Write a program that displays a histogram to show the number occurrences of each number...
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
  • 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...

  • 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...

  • Character Count Write a program that reads a file and counts the number of occurrences of...

    Character Count Write a program that reads a file and counts the number of occurrences of each character in the file (case sensitive). The file name is "char.txt". You should use a dictionary to hold the number of occurrences of each character, and print out that dictionary. For example, if the file contains only six characters, "tactic", your dictionary should contain the following key-value pairs (order can be different): {'a': 1, 'c': 2, 'i': 1, 't': 2} Sample Input: Follow...

  • Write a python program that will create a histogram of the number of times each character occurs ...

    Write a python program that will create a histogram of the number of times each character occurs in a file. Check https://en.wikipedia.org/wiki/Histogram and http://interactivepython.org/runestone/static/thinkcspy/Functions/ATurtleBarChart.html for help. Your program will read any input text file and print the histogram. If you have a huge file you should have a graph similar to: Note the y-axis shows the percentage of each letter. The height of each bar is percentage of that letter. Program MUST show the graph with percentage marks on y-axis...

  • Modify the JavaFX TipCalculator application program to allow the user to enter the number of persons...

    Modify the JavaFX TipCalculator application program to allow the user to enter the number of persons in the party through a text field. Calculate and display the amount owed by each person in the party if the bill is to be split evenly among the party members. /////////// TipCalculatorController.java: import java.math.BigDecimal; import java.math.RoundingMode; import java.text.NumberFormat; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.Label; import javafx.scene.control.Slider; import javafx.scene.control.TextField; public class TipCalculatorController { // formatters for currency and percentages private...

  • Python, given a code in class i just need help with the third bullet point ; using a and n (defin...

    Python, given a code in class i just need help with the third bullet point ; using a and n (defined in the second picture of python code) find the first digit for k! for k =1,...,n. you dont have to type in all this code just help me write the code in the first picture where it says: def benford(a): b = [0 for d in range(1,10)] #Do everthything in here return b 2.2 Generating data In this assignment...

  • Design a program using Python and using from flask Import flask that generates a lottery number...

    Design a program using Python and using from flask Import flask that generates a lottery number but in a website.. The program should have an Integer array with 9 elements. Write a loop that steps through the array, randomly generating a number in the range of 0 through 42 for each element. (Use the random function) Then write another loop that displays the contents of the array. Each number should be displayed as a list, the numbers should be generated...

  • Java: In this assignment, you will create an accumulator accumulator-calculator that displays a sad face “...

    Java: In this assignment, you will create an accumulator accumulator-calculator that displays a sad face “ :-( “ whenever the number displayed by the calculator is negative and a happy face “ :-) ” whenever the number displayed is positive. The calculator responds to the following commands: num + , num - , num * , num / and C ( Clear ). After initial run, if the user clicks 8 and then presses the “+” button for example, the...

  • You have been asked to write a program to grade several multiple-choice exams. The exam has...

    You have been asked to write a program to grade several multiple-choice exams. The exam has 20 questions, each answered with a letter in the range of ‘a’ through ‘f’. The answers key is declared in the program as constant of type string. An example of answer key is “abcdefabcdefabcdefab”. Your program should work for any other answer key. The program should first ask users for the number of students to be graded. Then it should have a while loop...

  • using java Program: Please read the complete prompt before going into coding. Write a program that...

    using java Program: Please read the complete prompt before going into coding. Write a program that handles the gradebook for the instructor. You must use a 2D array when storing the gradebook. The student ID as well as all grades should be of type int. To make the test process easy, generate the grade with random numbers. For this purpose, create a method that asks the user to enter how many students there are in the classroom. Then, in each...

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