Question #60930

i have an assignment to create a subject marks calculator in java netbeans, I've already done the GUI which includes the labels : mark, total, percentage, textfields for those 3. i have to find the percentage and the coding must be done in a calculate button and the percentage must appear in the percentage label when running the program. This is the code i have :

public static void main(String[] args){
int total, score;
float percentage;
Scanner inputNumScanner = new Scanner(System.in);

System.out.println("Enter the total, or max, score: ");
total = inputNumScanner.nextInt();

System.out.println("Enter the score obtained: ");
score = inputNumScanner.nextInt();

percentage = (score * 100/ total);

System.out.println("The percentage is = " + percentage + " %");
}

but the problem is that it is not appearing in the GUI.
1

Expert's answer

2016-07-25T13:45:02-0400

Answer on Question #60930, Programming & Computer Science

i have an assignment to create a subject marks calculator in java netbeans, I've already done the GUI which includes the labels : mark, total, percentage, textfields for those 3. i have to find the percentage and the coding must be done in a calculate button and the percentage must appear in the percentage label when running the program. This is the code i have :


public static void main(String[] args){
int total, score;
float percentage;
Scanner inputNumScanner = new Scanner(System.in);
System.out.println("Enter the total, or max, score: ");
total = inputNumScanner.nextInt();
System.out.println("Enter the score obtained: ");
score = inputNumScanner.nextInt();
percentage = (score * 100 / total);
System.out.println("The percentage is = " + percentage + " %");
}


but the problem is that it is not appearing in the GUI.

Solution.

XML FORM:


<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" xmlns="http://www.intellij.com/udesigner/form/" xmlns:="http://www.intellij.com/udesigner">
<grid col-="27dc6" height="400" id="27dc6" name-size-horizontally="false" of="27dC6" size-horizontally="false" td="" version="7" width="500"></grid>
<margin height="400" top="0" topt="0" vgap="-1" vgap="-1" y="453" y:="372" y:="453"></margin>
<constraints>
<xy a9cd3"="" bottom="0" cadd="none" height="400" x="372" y="453" z="372"></xy>
<constraints>
<properties></properties>
<border type="none"></border>
<children>
<component class="javax.swing.JButton" id="a9cd3" binding="button1" default-binding="true" id="a9cd3">
<constraints>
<grid and="" col-span="4" col-span="1" max-spin="0" max-spin-policy="0" mincor="0" maxpsi="3" maxrow="0" maxrow-="3" maxrow--pp="3" rsw="4" rsw-span="3"></grid>
</constraints>
</properties></com></program>
<properties>
<text value="Calculate"></text>
</properties>
<component id="b9d66" class="javax.swing.JTextField" binding="textField1" default-binding="true">
<constraints>
<grid col-span="4" height="-1" row-cpan="1" row="0" row="1" value="Variable"></grid>
<preferred-size width="150" use-parent-layout="false" height="-1"></preferred-size>
</constraints>
<properties>
<text value=""></text>
</properties>
</component>
<component class="javax.swing.JLabel" id="41837">
<constraints>
<grid and="" col-span="1" color="8" fill="0" inident="0" row-span="1" vsize-policy="0" row-span="1" xsize-policy="0" xsize-policy="0" ytext="javascript" ytextvalue="1"></grid>
</constraints>
<properties>
<text value="Mark"></text>
</properties>
</component>
<component id="576dc" class="javax.swing.JTextField" binding="textField2" default-binding="true">
<constraints>
<grid and="" color="8" fill="1" inident="0" row-span="1" vsize-policy="0" row-span="1" xsize-policy="0" ytext="javascript" ytextvalue="1"></grid>
<preferred-size width="150" use-parent-layout="false" height="-1"></preferred-size>
</constraints>
</properties>
<properties></properties>
<component id="d1074" class="javax.swing.JLabel">
<constraints>
<grid amount="0" height="1" row="2" col-span="1" col-span="1" nsize-policy="0" os-span="1" vsize-policy="0" xmax-policy="0" ymax-policy="0" zmax-policy="0" zmax-swing="false" zswing="false" zswing-aver="0">
</constraints>
<properties>
<text value="Total"></text>
</properties>
</component>
<component class="javax.swing.JLabel" id="d47ed">
<constraints>
<grid amount="0" height="1" row="5" col-span="1" col-span="1" nsize-policy="0" os-span="1" vsize-policy="0" xmax-policy="0" ymax-policy="0" zmax-policy="0" zmax-swing="false" zswing="false" zswing-aver="0">
</constraints>
<properties>
<text value="percentage"></text>
</properties>
</component>
<component class="javax.swing.JTextField" id="37e36" binding="textField3" default-binding="true" id="textField3" textfield="0">
<constraints>
<grid amount="0" height="1" row="6" col-span="4" col-span="4" nsize-policy="0" os-span="6" vsize-policy="6" xmax-policy="0" ymax-policy="0" zmax-policy="0" zswing="false" zswing-aver="0">
</constraints>
</properties>
</component>
<fieldren>
</fieldren>
package sample;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainClass {
public static void main(String[] args) {
MainForm frame = new MainForm();
}
public static class MainForm extends JFrame {
private JPanel Calculator;
private JButton button1;
private JTextField textField1;
private JTextField textField2;
private JTextField textField3;
private String String1, String2;
public MainForm() {
setContentPane(Calculator);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String1 = textField1.getText();
String2 = textField2.getText();
textField3.setText("The percentage is = " +
Calculate(String1, String2) + " %");
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(500, 250));
pack();
setVisible(true);
}
public String Calculate(String str1, String str2) {
System.out.println(str1);
double Mark = Double.parseDouble(str1),
Total = Double.parseDouble(str2);
return Double.toString(Mark * 100 / Total);
}
}


http://www.AssignmentExpert.com

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS