Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Need Help with using classes [HELP]

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Need Help with using classes [HELP]

    Alright so i am new to the forums and sort of a beginner. I need to use a different class in my program.

    I need to use this:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /*
     * CelsiusConverterGUI.java
     *
     * Created on Mar 5, 2011, 12:59:24 AM
     */
    /**
     *
     * @author Andrew
     */
    public class CelsiusConverterGUI extends javax.swing.JFrame {
     
        /** Creates new form CelsiusConverterGUI */
        public CelsiusConverterGUI() {
            initComponents();
        }
     
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            X1label = new javax.swing.JLabel();
            Y1label = new javax.swing.JLabel();
            X2label = new javax.swing.JLabel();
            Y2label = new javax.swing.JLabel();
            X1field = new javax.swing.JTextField();
            Y1field = new javax.swing.JTextField();
            X2field = new javax.swing.JTextField();
            Y2field = new javax.swing.JTextField();
            MathButton = new javax.swing.JButton();
            answerlabel = new javax.swing.JLabel();
            answerlabel1 = new javax.swing.JLabel();
            endlabel = new javax.swing.JLabel();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Math Y2-Y1/ X2-X1");
     
            X1label.setFont(new java.awt.Font("Tahoma", 0, 12));
            X1label.setText("X1");
     
            Y1label.setText("Y1");
     
            X2label.setText("X2");
     
            Y2label.setText("Y2");
     
            MathButton.setText("Do Math");
            MathButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    MathButtonActionPerformed(evt);
                }
            });
     
            answerlabel.setText("Answer");
     
            answerlabel1.setText("y-intercept Answer");
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(X2label)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(X2field, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(X1label)
                                    .addGap(2, 2, 2)
                                    .addComponent(X1field, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(MathButton)
                            .addGap(66, 66, 66)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(endlabel)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(answerlabel1)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(answerlabel)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(Y1label)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(Y1field, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(Y2label)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(Y2field, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE))))))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(X1label)
                        .addComponent(Y1label)
                        .addComponent(Y1field, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(X1field, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(7, 7, 7)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(X2label)
                        .addComponent(X2field, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(Y2label)
                        .addComponent(Y2field, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(MathButton)
                        .addComponent(answerlabel))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(answerlabel1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(endlabel)
                    .addContainerGap(46, Short.MAX_VALUE))
            );
     
            pack();
        }// </editor-fold>                        
     
        private void MathButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        int X1 = (int)(Double.parseDouble(X1field.getText()));
        int X2 = (int)(Double.parseDouble(X2field.getText()));
        int Y1 = (int)(Double.parseDouble(Y1field.getText()));
        int Y2 = (int)(Double.parseDouble(Y2field.getText()));
        int answer = Y2 - Y1;
        int answer1 = X2 - X1;
        int answer3 = answer / answer1;
        answerlabel.setText("m = "+ answer + "/" + answer1);
        int b;
        int answer2 = b = Y1 - (answer3 * X1);
        answerlabel1.setText("b = " + answer2);
        endlabel.setText("Y="+ answer3 +"X+"+ answer2);
        }                                          
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new CelsiusConverterGUI().setVisible(true);
                }
            });
        }
     
        // Variables declaration - do not modify                     
        private javax.swing.JButton MathButton;
        private javax.swing.JTextField X1field;
        private javax.swing.JLabel X1label;
        private javax.swing.JTextField X2field;
        private javax.swing.JLabel X2label;
        private javax.swing.JTextField Y1field;
        private javax.swing.JLabel Y1label;
        private javax.swing.JTextField Y2field;
        private javax.swing.JLabel Y2label;
        private javax.swing.JLabel answerlabel;
        private javax.swing.JLabel answerlabel1;
        private javax.swing.JLabel endlabel;
        // End of variables declaration                   
     
    }

    In this: (under the h == 0)
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.util.Scanner;
     
    public class Graphing{
    	static Scanner input = new Scanner(System.in);
     
    	public static void main(String[] args) {
    		int h;
    		System.out.println("Enter the equation you want to do: ");
    		System.out.println("If you want the Equation y = mx + b type 0.");
    		System.out.println("If you want to know what your grade was on a test type 1.");
    		h = input.nextInt();
     
    		if(h == 0){
     
    		}
     
     
    		if(h == 1){
    			double grade, questions, answered, percent;
    			System.out.println("Enter the total possible points: ");
    			questions = input.nextDouble();
     
    			System.out.println("Enter the total points earned: ");
    			answered = input.nextDouble();
     
    			percent = answered/questions;
     
    			grade = percent * 100;
    			System.out.println("The number of your grade was a " + grade);
     
    			if (grade >= 97)
    				System.out.print("Your grade is an A+");
    			else if (grade >= 93)
    				System.out.println("Your grade is an A");
    			else if (grade >= 90)
    				System.out.println("Your grade is a A-");	
    			else if (grade >= 87)
    				System.out.println("Your grade is a B+");	
    			else if (grade >= 83)
    				System.out.println("Your grade is a B");
    			else if (grade >= 80)
    				System.out.println("Your grade is a B-");	
    			else if (grade >= 77)
    				System.out.println("Your grade is a C+");	
    			else if (grade >= 73)
    				System.out.println("Your grade is a C");
    			else if (grade >= 70)
    				System.out.println("Your grade is a C-");	
    			else if (grade >= 65)
    				System.out.println("Your grade is a D");	
    			else
    				System.out.print("Your grade is a F");
    		}
     
    		if(h == 2){
    			int inputGraph, x, y, m;
     
    			System.out.println("Enter the x axis: ");
    			x = input.nextInt();
     
    			System.out.println("Enter the y axis: ");
    			y = input.nextInt();
     
    			System.out.println("The x axis is = " + x);
    			System.out.println("===================================");
    			System.out.println("The y axis is = " + y);
    		}
     
    	}
     
    }

    please DONT LEACH MY CODE.

    but maybe it would be easier if i made it in a seperate file so there isnt soo much code.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need Help with using classes [HELP]

    I need to use a different class in my program.
    Can you explain what "to use" means here? Most of a java program is made up of different usages of classes.
    To create an instance of a class, use the new statement.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Need Help with using classes [HELP]

    Feel free to ask a question.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Need Help with using classes [HELP]

    Hello dragon40226,

    Welcome to the Java Programming Forums.

    It's easy to make the CelsiusConverterGUI class run from within the Graphing class.

    As you know, a programs starting point is the main method. Because Graphing is our main class where the program will run, there is no need for CelsiusConverterGUI to have a main method. The contents of the CelsiusConverterGUI main method are still needed but we need to rename it so we can call it.

    Below is the original main method of the CelsiusConverterGUI class.
    You can see how I have renamed it to start() and removed the methods arguments.

        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new CelsiusConverterGUI().setVisible(true);
                }
            });
        }

        public static void start() {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new CelsiusConverterGUI().setVisible(true);
                }
            });
        }

    Now we have this start method, you can call it from your Graphing class.

    		if(h == 0){
     
    			CelsiusConverterGUI CCG = new CelsiusConverterGUI();
    			CCG.start();
     
    		}

    I hope this is what you mean
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. The Following User Says Thank You to JavaPF For This Useful Post:

    dragon40226 (May 19th, 2011)

  6. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with using classes [HELP]

    Thank you Very much! JavaPF!

Similar Threads

  1. ClassLoader and inner classes
    By sidd in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 10th, 2011, 11:07 AM
  2. Classes problem
    By Skyton in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2011, 03:26 PM
  3. cant run the program with 2 classes
    By clone_hiryu in forum Object Oriented Programming
    Replies: 5
    Last Post: November 18th, 2010, 12:28 AM
  4. connecting two classes?
    By chronoz13 in forum Object Oriented Programming
    Replies: 9
    Last Post: September 1st, 2009, 03:15 PM
  5. [SOLVED] Java program using two classes
    By AZBOY2000 in forum Object Oriented Programming
    Replies: 7
    Last Post: April 21st, 2009, 06:55 AM