In: Computer Science
Please write a java program (using dialog box statements for user input) that has the following methods in it: (preferably in order)
Design Notes:
Please copy the java code along with a screen print (snippet) of the final output. I would like both to review the work that I already have done. Thanks in advance!
Following is the java code for the above problem:
import javax.swing.JOptionPane;
public class Dialog_box_stmt_eg {
static String name(){
String name;
// Get the university name.
name = JOptionPane.showInputDialog("What is university
name? ");
// Display university name.
JOptionPane.showMessageDialog(null, "University name
is=" + name);
return name;
}
static int Students_enrolled(){
String input; // To hold number of
students enrolled
int no_of_students;
// Prompt user to input number of students
enrolled
input = JOptionPane.showInputDialog("Enter number of
students enrolled");
// Convert the String input to an int.
no_of_students = Integer.parseInt(input);
return no_of_students;
}
static double Calculate_fees(int stud_enrolled){
double fees; // To hold fees
fees= 20000*stud_enrolled;
// Display calculated fees
JOptionPane.showMessageDialog(null,
"Total tution fees for "+stud_enrolled+" are =
"+stud_enrolled+"*20000 = " + fees);
return fees;
}
static void print(int stud_enrolled, String name ,
double tution_fees ){
JOptionPane.showMessageDialog(null,
stud_enrolled+" students enrolled in "+name+ " university with
total tution fees of = "+tution_fees);
System.out.println( stud_enrolled+"
students enrolled in "+name+ " university with total tution fees of
= "+tution_fees);
}
public static void main(String[] args) {
String univ_name= name();
System.out.println("univ name
is="+univ_name);
int
stud_enrolled=Students_enrolled();
System.out.println("Number of
students enrolled="+stud_enrolled +" in "+ univ_name + "
university");
double
tution_fees=Calculate_fees(stud_enrolled);
System.out.println("total tution
fees = "+tution_fees);
print(stud_enrolled, univ_name,
tution_fees);
}
}
Program screenshots:
Explanation: JOptionPane.showInputDialog -- is used for dialog box statements for user input as mentioned in question. Additional import statement is need to be added "import javax.swing.JOptionPane;"
Output Images of the code are below:
Output in console:
univ name is=ABCDEFGH
Number of students enrolled=10 in ABCDEFGH university
total tution fees = 200000.0
10 students enrolled in ABCDEFGH university with total tution fees
of = 200000.0
NOTE: You can add various validations as per your choice as it is not mentioned in the question, so i have not added those in order not to complex the answer. Also you can remove or add DISPLAY or System.out.println(); lines as per your choice. I have added in order to give more understanding/readability to the code. If you dont want these lines you can comment it out and execute the rest of the code.