Can you write a function that will ask the questions on the screen without printing them to the file?

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Can you write a function that will ask the questions on the screen without printing them to the file?

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;

import java.io.*;
import java.util.Random;

public class Generate {

//static String[] file_list = ((new File(Bank.dir)).list());
static ChoiceBox<Integer> choices = new ChoiceBox<>();

static void getChoice() {

Image image = new Image(Generate.class.getResourceAsStream("abstract.jpg"));
// new BackgroundSize(width, height, widthAsPercentage, heightAsPercentage, contain, cover)
BackgroundSize backgroundSize = new BackgroundSize(800, 500, true, true, true, false);
// new BackgroundImage(image, repeatX, repeatY, position, size)
BackgroundImage backgroundImage = new BackgroundImage(image, BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, backgroundSize);
// new Background(images...)
Background background = new Background(backgroundImage);
System.out.println(Bank.dir);
int num = new File(Bank.dir).list().length;

Label l = new Label("Choose number of questions");
l.setTextFill(Color.WHITE);
l.setFont(new Font("NanumMyeongjo", 40));

Button next = new Button("Next");
next.setFont(new Font("NanumMyeongjo", 20));
next.setOnAction(event -> {
try {
generateQuestions();
} catch (IOException e) {
}
});

Button cancel = new Button("Cancel");
cancel.setFont(new Font("NanumMyeongjo", 20));
cancel.setOnAction(event -> {
choices.getItems().clear();
Bank.window.setScene(Bank.scene);
});

for (int i = 1; i <= num; i++)
choices.getItems().add(i);

VBox box = new VBox();
box.setBackground(background);
box.setAlignment(Pos.CENTER);
box.setPadding(new Insets(15, 12, 15, 12));
box.setSpacing(10);
//box.setStyle("-fx-background-color: radial-gradient(radius 100%, red, #6a6a6a, #f4fdff)");
box.getChildren().addAll(l, choices, next, cancel);
Scene choice_scene = new Scene(box, 800, 500);
Bank.window.setScene(choice_scene);
}

private static void generateQuestions() throws IOException {
String[] file_list = ((new File(Bank.dir)).list());
//int max = Integer.parseInt(file_list[file_list.length - 1].substring(0, 1));
int required = choices.getSelectionModel().getSelectedItem();
int[] available = new int[100];
for (String aFile_list : file_list)
available[Integer.parseInt(aFile_list.substring(0, 1))] = 1;

FileWriter writer = new FileWriter("C:\\Users\\Furkan\\IdeaProjects\\QuestionBank-master\\src\\questions\\Question_Paper.txt");
writer.write("\t\t\tBirla Institute of Technology and Science, Pilani\n\t\t\t\tDiscrete Structures for Computer Science\n\n");
FileWriter swriter = new FileWriter("C:\\Users\\Furkan\\IdeaProjects\\QuestionBank-master\\src\\questions\\Solutions.txt");
writer.flush();
writer.close();
swriter.close();
/*
for(int i=1; i<=10; i++)
System.out.println(available[i]);*/

int qnum = 1;
while (required > 0) {
Random r = new Random();
int number = r.nextInt(20);
if (available[number] == 1) {
available[number] = 0;
required--;
try {
printToFile(number, qnum++);
} catch (IOException e) {
e.printStackTrace();
}
}

}
AlertBox.display("SUCCESS", "Question Paper has been generated and stored in Desktop.");
choices.getItems().clear();
Bank.window.setScene(Bank.scene);
}

private static void printToFile(int fnum, int qnum) throws IOException {

BufferedReader br = new BufferedReader(new FileReader(Bank.dir + "/" + fnum + ".txt"));
FileWriter writer = new FileWriter("C:\\Users\\Furkan\\IdeaProjects\\QuestionBank-master\\src\\questions\\Question_Paper.txt", true);
FileWriter swriter = new FileWriter("C:\\Users\\Furkan\\IdeaProjects\\QuestionBank-master\\src\\questions\\Solutions.txt", true);
String qtype = br.readLine();
writer.write(qnum + ".\t" + br.readLine() + "\n");
char opt = 'a';
if (qtype.equals("1")) {
for (int i = 1; i <= 4; i++) {
br.readLine();
writer.write(opt);
writer.write(".\t" + br.readLine() + "\n");
opt++;
}
}
writer.write("\n");
writer.flush();
writer.close();
br.readLine();
swriter.write(qnum + ".\t" + br.readLine() + "\n");
swriter.flush();
swriter.close();
}

}

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY