hey all,
i am now making a gpa calculator.
i am using 4 classes which form into seperate linked lists. one linked list is for the custom gui, and the other linked list is for the data list.
i am using custom images for this program which i import using the LoadImageApp.java, so if you need the images to execute this program, just tell me.
there are a couple of problems that i have encountered.
firstly, when i use clean and build using netbeacs 6.7 it creates my jar file. when i try to execute the jar file it does not execute.
secondly, the menu bar gives me exceptions. the code for the
guiList.java. i would like to know how to fix these exceptions. the code for the menu bar can be found in the file guiNode.java.
thirdly, my cgpa does not calculate. actually there are two options that the user has when he enters the data. he can either submit and add a new course or submit and not add a new course.
when the user enters only one course and then selects the submit and do not enter a new course the correct cgpa is shown, however if the user decides to add more courses, and then finaly selects the submit and do not add a new course the value for cgpa comes as 0.0 which was set in the constructor in the file gpaList.java.
the data is entered into the linked list correctly as the list is printed and then the cgpa is supposed to print.
please help
following are the files,
guiList.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Mustafa Neguib
*/
public class guiList{
private guiNode rootGui;
public guiList()
{
rootGui=null;
}//end constructor
public void buildGui(int page)
{
guiNode temp= new guiNode(page);
guiNode ptr;
if(rootGui==null)
{
rootGui=temp;
rootGui.rootGuiNode=rootGui;
}//end if
else
{
ptr=rootGui;
while(ptr.next!=null)
{//execute the while statement as long as the next is not null
ptr=ptr.next;
}//end while
ptr.next=temp; //point ptr to the new node
}//end else
}//end function buildGui
public void run()
{
rootGui.setVisible(true);
}//end function run
public static void main(String [] args)
{
guiList listGui;
listGui=new guiList();
int i=1;
while(i<5)
{
listGui.buildGui(i);
i++;
}//end while
listGui.run();
}//end function main
}//end class guiList
guiNode.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Mustafa Neguib
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class guiNode extends JFrame implements ActionListener {
/** Declaring variables for the gui. */
public int id;
public Container contentPane;
public JPanel header;
public JPanel back;
public JPanel menu;
public JPanel content;
public JPanel footer;
public JPanel textField;
public JTextField nameOfCourse;
public JTextField creditHoursOfCourse;
public JTextField gpaOfCourse;
public JLabel name;
public JLabel courseGpa;
public JLabel hours;
public JLabel coursesListLabel;
public JButton startSoftware;
public JButton exitSoftware;
public JButton submit;
public JButton addMore;
public JLabel contentLabel;
public JLabel footerLabel;
public JMenu fileMenu;
public JMenuItem item;
public JMenuBar menuBar;
public guiNode next;
public static gpaList nodeGpaList;
public static guiNode rootGuiNode;
public guiNode(int page)
{
next=null;
switch(page)
{
case 1:
id=1;
contentPane= getContentPane();
setSize(640,680);
setResizable(false);
setTitle("MN Tech Solutions GPA Calculator");
setLocation(0,0);
/** Creating JPanel objects */
back=new JPanel();
header = new JPanel();
menu=new JPanel();
content=new JPanel();
footer=new JPanel();
//** Build the menu of the menu bar */
//** Build File Menu */
fileMenu= new JMenu("File");
item= new JMenuItem("Main Page");
item.addActionListener(this);
fileMenu.add(item);
fileMenu.addSeparator();
item= new JMenuItem("Exit Software");
item.addActionListener(this);
fileMenu.add(item);
menuBar= new JMenuBar();
setJMenuBar(menuBar);
menuBar.add(fileMenu);
back.setLayout(null); //i am using absolute positioning
/** Set the background color of each JPanel object to black. */
menu.setBackground(Color.BLACK);
header.setBackground(Color.BLACK);
content.setBackground(Color.BLACK);
footer.setBackground(Color.BLACK);
/** I am setting the bounds of the JPanel objects so that they can be displayed on the screen
and i am also inserting the data to be shown in the JPanel objects.
*/
header.setBounds(0,0,640,80);
header.add(new LoadImageApp("images/logo.gif"));
menu.setBounds(0,80,640,60);
menu.add(new LoadImageApp("images/menu.gif"));
content.setBounds(0,140,640,400);
content.add(new LoadImageApp("images/content.gif"));
contentLabel=new JLabel("MN Tech Solutions GPA Calculator");
contentLabel.setBounds(40,180,200,20);
contentPane.add(contentLabel);
contentLabel=new JLabel("Software House/Developer: MN Tech Solutions");
contentLabel.setBounds(40,220,500,20);
contentPane.add(contentLabel);
contentLabel=new JLabel("Programmer/Software Developer: Mustafa Neguib");
contentLabel.setBounds(40,240,500,20);
contentPane.add(contentLabel);
contentLabel=new JLabel("Programming Language: JAVA");
contentLabel.setBounds(40,260,500,20);
contentPane.add(contentLabel);
contentLabel=new JLabel("Website: www.mntechsolutions.net");
contentLabel.setBounds(40,280,500,20);
contentPane.add(contentLabel);
contentLabel=new JLabel("Software Name: MN Tech Solutions GPA Calculator");
contentLabel.setBounds(40,300,500,20);
contentPane.add(contentLabel);
contentLabel=new JLabel("Comments:");
contentLabel.setBounds(40,320,500,20);
contentPane.add(contentLabel);
contentLabel=new JLabel("This software is a gpa calculator. The user gives the number of courses taken.");
contentLabel.setBounds(40,340,500,20);
contentPane.add(contentLabel);
contentLabel=new JLabel("The user then enters the course name, gpa earned and the credit hours of that course.");
contentLabel.setBounds(40,360,500,20);
contentPane.add(contentLabel);
/** Creating JButtons */
startSoftware=new JButton("Start Software");
exitSoftware=new JButton("Exit Software");
startSoftware.setBounds(40,420,120,30);
startSoftware.addActionListener(this);
contentPane.add(startSoftware);
exitSoftware.setBounds(170,420,120,30);
exitSoftware.addActionListener(this);
contentPane.add(exitSoftware);
/** Creating the footer and adding its content */
footer.setBounds(0,520,640,220);
footer.add(new LoadImageApp("images/footer.gif"));
footerLabel=new JLabel("MN Tech Solutions GPA Calculator COPYRIGHT 2009 MN Tech Solutions");
footerLabel.setBounds(80,560,500,20);
contentPane.add(footerLabel);
/** I am adding the JPanels to the outer JPanel.*/
back.add(header);
back.add(menu);
back.add(content);
back.add(footer);
contentPane.add(back);
//register 'Exit upon closing' as a default close operation
setDefaultCloseOperation( EXIT_ON_CLOSE );
break;
case 2:
id=2;
contentPane= getContentPane();
setSize(640,680);
setResizable(false);
setTitle("MN Tech Solutions GPA Calculator");
setLocation(0,0);
/** Creating JPanel objects */
back=new JPanel();
header = new JPanel();
menu=new JPanel();
content=new JPanel();
footer=new JPanel();
/** Build the menu of the menu bar */
//** Build File Menu */
fileMenu= new JMenu("File");
item= new JMenuItem("Main Page");
item.addActionListener(this);
fileMenu.add(item);
fileMenu.addSeparator();
item= new JMenuItem("Exit Software");
item.addActionListener(this);
fileMenu.add(item);
menuBar= new JMenuBar();
setJMenuBar(menuBar);
menuBar.add(fileMenu);
back.setLayout(null); //i am using absolute positioning
/** Set the background color of each JPanel object to black. */
menu.setBackground(Color.BLACK);
header.setBackground(Color.BLACK);
content.setBackground(Color.BLACK);
footer.setBackground(Color.BLACK);
/** I am setting the bounds of the JPanel objects so that they can be displayed on the screen
and i am also inserting the data to be shown in the JPanel objects.
*/
header.setBounds(0,0,640,80);
header.add(new LoadImageApp("images/logo.gif"));
menu.setBounds(0,80,640,60);
menu.add(new LoadImageApp("images/menu.gif"));
content.setBounds(0,140,640,400);
content.add(new LoadImageApp("images/content.gif"));
contentLabel=new JLabel("Enter the course name, course gpa, and credit hours earned in that course: ");
contentLabel.setBounds(40,180,600,20);
contentPane.add(contentLabel);
/** Creating JButtons */
submit=new JButton("Submit And Do Not Add A New Course");
submit.addActionListener(this);
submit.setBounds(40,320,300,30);
contentPane.add(submit);
addMore=new JButton("Submit And Add A New Course");
addMore.addActionListener(this);
addMore.setBounds(40,360,300,30);
contentPane.add(addMore);
/** Creating the JTextField object */
int i=0,height=220;
nameOfCourse=new JTextField();
creditHoursOfCourse=new JTextField();
gpaOfCourse=new JTextField();
name=new JLabel("Course Name: ");
courseGpa=new JLabel("GPA: ");
hours=new JLabel("Credit Hours: ");
name.setBounds(30,height,122,22);
contentPane.add(name);
nameOfCourse.setColumns(22);
nameOfCourse.setBounds(120,height,122,22);
contentPane.add(nameOfCourse);
height=height+20;
hours.setBounds(30,height,122,22);
contentPane.add(hours);
creditHoursOfCourse.setColumns(22);
creditHoursOfCourse.setBounds(120,height,122,22);
contentPane.add(creditHoursOfCourse);
height=height+20;
courseGpa.setBounds(30,height,122,22);
contentPane.add(courseGpa);
gpaOfCourse.setColumns(22);
gpaOfCourse.setBounds(120,height,122,22);
contentPane.add(gpaOfCourse);
/** Creating the footer and adding its content */
footer.setBounds(0,520,640,220);
footer.add(new LoadImageApp("images/footer.gif"));
footerLabel=new JLabel("MN Tech Solutions GPA Calculator COPYRIGHT 2009 MN Tech Solutions");
footerLabel.setBounds(80,560,500,20);
contentPane.add(footerLabel);
/** I am adding the JPanels to the outer JPanel.*/
back.add(header);
back.add(menu);
back.add(content);
back.add(footer);
contentPane.add(back);
//register 'Exit upon closing' as a default close operation
setDefaultCloseOperation( EXIT_ON_CLOSE );
break;
case 3:
id=3;
contentPane= getContentPane();
setSize(640,680);
setResizable(false);
setTitle("MN Tech Solutions GPA Calculator");
setLocation(0,0);
/** Creating JPanel objects */
back=new JPanel();
header = new JPanel();
menu=new JPanel();
content=new JPanel();
footer=new JPanel();
/** Build the menu of the menu bar */
//** Build File Menu */
fileMenu= new JMenu("File");
item= new JMenuItem("Main Page");
item.addActionListener(this);
fileMenu.add(item);
fileMenu.addSeparator();
item= new JMenuItem("Exit Software");
item.addActionListener(this);
fileMenu.add(item);
menuBar= new JMenuBar();
setJMenuBar(menuBar);
menuBar.add(fileMenu);
back.setLayout(null); //i am using absolute positioning
/** Set the background color of each JPanel object to black. */
menu.setBackground(Color.BLACK);
header.setBackground(Color.BLACK);
content.setBackground(Color.BLACK);
footer.setBackground(Color.BLACK);
/** I am setting the bounds of the JPanel objects so that they can be displayed on the screen
and i am also inserting the data to be shown in the JPanel objects.
*/
header.setBounds(0,0,640,80);
header.add(new LoadImageApp("images/logo.gif"));
menu.setBounds(0,80,640,60);
menu.add(new LoadImageApp("images/menu.gif"));
content.setBounds(0,140,640,400);
content.add(new LoadImageApp("images/content.gif"));
contentLabel=new JLabel("Following is the Cumulative Grade Point Average of your courses: ");
contentLabel.setBounds(40,180,600,20);
contentPane.add(contentLabel);
/** Creating JButtons */
submit=new JButton("Start Again");
submit.addActionListener(this);
submit.setBounds(40,470,100,30);
contentPane.add(submit);
JScrollPane contentScroll;
coursesListLabel=new JLabel(" ");
coursesListLabel.setLayout(null);
coursesListLabel.setBounds(40,170,100,200);
contentScroll=new JScrollPane(coursesListLabel);
contentScroll.setBounds(new Rectangle(50,230,200,200));
contentScroll.setSize(200,200);
contentPane.add(contentScroll);
// contentPane.add(coursesListLabel);
/** Creating the footer and adding its content */
footer.setBounds(0,520,640,220);
footer.add(new LoadImageApp("images/footer.gif"));
footerLabel=new JLabel("MN Tech Solutions GPA Calculator COPYRIGHT 2009 MN Tech Solutions");
footerLabel.setBounds(80,560,500,20);
contentPane.add(footerLabel);
/** I am adding the JPanels to the outer JPanel.*/
back.add(header);
back.add(menu);
back.add(content);
back.add(footer);
contentPane.add(back);
//register 'Exit upon closing' as a default close operation
setDefaultCloseOperation( EXIT_ON_CLOSE );
break;
default: break;
}//end switch
}//end constructor
public void actionPerformed(ActionEvent event)
{
/** actions of the menu of the menu bar */
String menuName;
menuName = event.getActionCommand();
if (menuName.equals("Exit Software")) {
System.exit(0);
}//end if
else if(menuName.equals("Main Page"))
{
setVisible(false);//do not show the present window
rootGuiNode.setVisible(true);//display the first page
}//end else if
JButton clickedButton;
String buttonText;
clickedButton= (JButton) event.getSource();
buttonText=clickedButton.getText();
if( buttonText.equals("Start Software"))
{//go to the main menu
nodeGpaList=new gpaList(); //create a new object to the gpaList which will create a new list
setVisible(false);//do not show the present window
rootGuiNode.next.setVisible(true);//display the second page
}//end if
else if(buttonText.equals("Exit Software"))
{
System.exit(0);
}//end else if
else if(buttonText.equals("Submit And Add A New Course"))
{//add the course to the linked list and then again display the input dialog
String nameCourse,courseGpa1,creditHourCourse;
nameCourse=new String();
courseGpa1=new String();
creditHourCourse=new String();
nameCourse=nameOfCourse.getText();//person
courseGpa1=gpaOfCourse.getText();//place
creditHourCourse=creditHoursOfCourse.getText();//date
nodeGpaList.insertNode(nameCourse, Double.parseDouble(courseGpa1),Integer.parseInt(creditHourCourse));
//** the following lines reset the text fields */
rootGuiNode.next.nameOfCourse.setText("");
rootGuiNode.next.creditHoursOfCourse.setText("");
rootGuiNode.next.gpaOfCourse.setText("");
setVisible(false);//do not show the present window
rootGuiNode.next.setVisible(true);
}//end else if
else if(buttonText.equals("Submit And Do Not Add A New Course"))
{//submit the course to the linked list and then display the cgpa
String nameCourse,courseGpa1,creditHourCourse;
nameCourse=new String();
courseGpa1=new String();
creditHourCourse=new String();
nameCourse=nameOfCourse.getText();//person
courseGpa1=gpaOfCourse.getText();//place
creditHourCourse=creditHoursOfCourse.getText();//date
nodeGpaList.insertNode(nameCourse, Double.parseDouble(courseGpa1),Integer.parseInt(creditHourCourse));
gpaNode ptr;
String contentList=new String();
if(nodeGpaList.rootGpa==null)
{//the linked list is empty
contentList="No courses exist. The list is empty.";
}//end if
else
{//the linked list is not empty
ptr=nodeGpaList.rootGpa;
nodeGpaList.calculateValue(ptr); //calculate the cgpa. the cgpa is stored in the data member of the gpaList class.
contentList="<html>";
while(ptr!=null)
{//get the contents of the linked list
contentList=contentList+"Course Name: "+ptr.courseName+"<br>"+"Course GPA: "+ptr.gpa+"<br>"+"Credit Hours: "+ptr.creditHour+"<br><br>";
ptr=ptr.next;
}//end while
contentList=contentList+"cgpa: "+nodeGpaList.cgpa+"<br>";
contentList=contentList+"</html>";
}//end else
rootGuiNode.next.next.coursesListLabel.setText(contentList);
setVisible(false);//do not show the present window
rootGuiNode.next.next.setVisible(true);//display the second page
}//end else if
else if(buttonText.equals("Start Again"))
{
//** the following lines reset the text fields */
rootGuiNode.next.nameOfCourse.setText("");
rootGuiNode.next.creditHoursOfCourse.setText("");
rootGuiNode.next.gpaOfCourse.setText("");
nodeGpaList=null; //set the variable/pointer to null
nodeGpaList=new gpaList(); //create a new object to the gpaList which will create a new list
setVisible(false);//do not show the present window
rootGuiNode.next.setVisible(true);//display the second page
}//end else if
}//end actionPerformed
}//end class guiNode
gpaList.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Mustafa Neguib
*/
import javax.swing.*;
public class gpaList {
public gpaNode rootGpa;
public double cgpa;
public int totalCreditHours;
public gpaList()
{
/** i am not using the reserved key this because i am not using any local variables */
rootGpa=null;
cgpa=0.0;
totalCreditHours=0;
}//end construtor
public void insertNode(String courseName, double gpa, int creditHour)
{
gpaNode temp=new gpaNode(courseName,gpa, creditHour);
gpaNode ptr;
if(rootGpa==null)
{
rootGpa=temp;
}//end if
else
{
ptr=rootGpa;
while(ptr.next!=null)
{//execute the while statement as long as the next is not null
ptr=ptr.next;
}//end while
ptr.next=temp; //point ptr to the new node
}//end else
}//end function insertNode
public void calculateValue(gpaNode ptr)
{
gpaNode ptr1=ptr;
totalCreditHours=0;
while(ptr1!=null)
{//get the contents of the linked list and calculate the total number of credit hours
totalCreditHours=totalCreditHours+ptr1.creditHour; //calculate the total number of credit hours
ptr1=ptr1.next;
}//end while
while(ptr!=null)
{//get the contents of the linked list and calculate the cgpa
cgpa=cgpa+((ptr.gpa)*(ptr.creditHour/totalCreditHours));
ptr=ptr.next;
}//end while
}//end function calculateValue
}//end class gpaList
gpaNode.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Mustafa Neguib
*/
public class gpaNode {
/** data members of class gpaNode */
public String courseName;
public double gpa;
public int creditHour;
public gpaNode next;
public gpaNode(String courseName, double gpa, int creditHour)
{
/** i am using the reserved this so that i can use the local variable of the same name so that no confusion takes place */
this.courseName=courseName;
this.gpa=gpa;
this.creditHour=creditHour;
next=null;
}//end constructor
}//end class gpaNode
LoadImageApp.java
/*
* Copyright (c) 1995 - 2008 Sun Microsystems, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Sun Microsystems nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
/**
* This class demonstrates how to load an Image from an external file
*/
public class LoadImageApp extends Component {
BufferedImage img;
public void paint(Graphics g) {
g.drawImage(img, 0, 0, null);
}
public LoadImageApp(String image) {
try {
img = ImageIO.read(new File(image));
} catch (IOException e) {
JOptionPane.showMessageDialog(null,"error");
}
}
public Dimension getPreferredSize() {
if (img == null) {
return new Dimension(100,100);
} else {
return new Dimension(img.getWidth(null), img.getHeight(null));
}
}
/*
public static void main(String[] args) {
JFrame f = new JFrame("Load Image Sample");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.add(new LoadImageApp());
f.pack();
f.setVisible(true);
}
*/
}