₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,330,578 members, 8,446,107 topics. Date: Thursday, 16 July 2026 at 06:53 AM

Toggle theme

Urgently Need Assistant On JAVA GUI - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingUrgently Need Assistant On JAVA GUI (797 Views)

1 Reply (Go Down)

Urgently Need Assistant On JAVA GUI by Akprof(op): 10:22pm On Apr 18, 2015
Please Programmers, how do I use GUI so solve problems where some variable are integer and others are float?
E.g
Code a program that output your Grade Point (GP)
where GP=(Grade Point X Course Unit)/Total Unit.
Taking CSC 400 as a the only course you did with unit 3.
Pls code this by using the GUI and not scanner.

I am expecting your instant responses
thanks.
Re: Urgently Need Assistant On JAVA GUI by Nobody: 10:54pm On Apr 18, 2015
You can use JOptionpane. Its not a hard assignment since you are taking just one course. What IDE are you using to write your code?
Re: Urgently Need Assistant On JAVA GUI by Akprof(op): 10:57pm On Apr 18, 2015
I have used JOptionPane, but final output I want does show. I am using eclipse
Re: Urgently Need Assistant On JAVA GUI by Nobody: 11:01pm On Apr 18, 2015
can you paste your code here? Am more familiar with netbeans but lets see.
Re: Urgently Need Assistant On JAVA GUI by Akprof(op): 11:06pm On Apr 18, 2015
import javax.swing.JOptionPane;
import java.util.*;
public class myjavaass2 {
public static void main (String args[]){
int score_p, load,t_unit;
Float gp;
String x,y,z,u = null;
//Capturing Data
x=JOptionPane.showInputDialog("Input Your Score:"wink;
y=JOptionPane.showInputDialog("Enter Your Credit Load Unit:"wink;
z=JOptionPane.showInputDialog("Input Your Total Credit Unit:"wink;
//Convert
score_p=Integer.parseInt(x);
load=Integer.parseInt(y);
t_unit=Integer.parseInt(z);
gp=Float.parseFloat(u);
gp=(float) (score_p*load/t_unit);
//Outputting
JOptionPane.showMessageDialog(null,"My Grade Point is: "+gp);
}
}
Re: Urgently Need Assistant On JAVA GUI by Nobody: 11:11pm On Apr 18, 2015
Since you already declared your variable with the correct data type, why converting it again?


Modified: You captured your data as string. Most programming languages are very stubborn at converting strings to integer or float, i will advise you to collect your data as int and float as the case may be. Just change where you declared them as string to int or float as the case may be.

Also, while calculating your gp, the assignment you did there looks strange, you put float in the bracket. Why? For clarity and accuracy, always do your calculations using bodmas and store your result into a variable for each operation.
Re: Urgently Need Assistant On JAVA GUI by Akprof(op): 11:21pm On Apr 18, 2015
U need to convert because GUI is always in string form
Re: Urgently Need Assistant On JAVA GUI by olyjosh(m): 9:14am On Apr 19, 2015
Are you getting error? Null pointer I guess. That is beacause variable u is null and you are passing it to float.
What is u in your program
Re: Urgently Need Assistant On JAVA GUI by kudaisi(m):
Try this should work...
package package.name;

import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GPACalculator {
public static void main(String[] args){
JTextField gradeField = new JTextField(5);
JTextField unitField = new JTextField(5);
int noOfCourses = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter number of courses:" ));
int grades[] = new int[noOfCourses];
int sumOfGrades = 0;
int totalUnits = 0;
JPanel myPanel = new JPanel();
myPanel.add(new JLabel("Enter grade for course"wink);
myPanel.add(gradeField);
myPanel.add(Box.createHorizontalStrut(15)); // a spacer
myPanel.add(new JLabel("Enter units for course"wink);
myPanel.add(unitField);
for (int i = 0; i < noOfCourses; i++){
int result = JOptionPane.showConfirmDialog(null, myPanel,
"Please Enter Grades and Unit value (e.g A and 3)", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
switch(gradeField.getText().charAt(0)){
case 'A':
sumOfGrades += Integer.parseInt(unitField.getText())*5;
break;
case 'B':
sumOfGrades += Integer.parseInt(unitField.getText())*4;
break;
case 'C':
sumOfGrades += Integer.parseInt(unitField.getText())*3;
break;
case 'D':
sumOfGrades += Integer.parseInt(unitField.getText())*2;
break;
case 'E':
sumOfGrades+= Integer.parseInt(unitField.getText())*1;
break;
default:
JOptionPane.showMessageDialog(null, "Invalid grade"wink;
break;
}
totalUnits += Integer.parseInt(unitField.getText());
unitField.setText(""wink;
gradeField.setText(""wink;

}
}
double gpa = sumOfGrades/(double)totalUnits;
JOptionPane.showMessageDialog(null, "Total Credit :"+totalUnits+" Total Score:"+sumOfGrades+"\n GPA is "+ gpa);
}
}
1 Reply

I Need Help On JavaHow Do I Make My Java GUI Stuff Executable (like desktop shortcut)Urgently Need Assistant On JAVA GUI234

How Is Corel Draw Software Created?E-commerce influences Purchase DecisionsHow Performance May Be Improve With The Installation Of Cis