Announcement

Collapse
No announcement yet.

Java

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Java

    I'm in computer programming class, and I need someone with knowledge of JAVA to help me out to see if my code is correct. This is what I have so far.


    // Tyrus Smith
    // 2991
    // cmps120-6m, Assignment1

    // Problem:
    // Write a Java program to do the following computation:
    // Given the coeffients for two linear equations, find the
    // coordinates of the point of the plane that satifies both
    // equations.
    //

    import javax.swing.JOptionPane;

    public class Assignment 1

    {

    public static void main(String args[])

    {

    // Declaration of variables.
    int a1, a2, b1, b2, c1, c2, x, y;
    String a3, a4, b3, b4, c3, c4;

    // Ask for variables, Echo variables.

    a3=JOptionPane.showInputDialog("Enter A Number for a1");
    a1=Interger.parse.Int(a3);
    System.out.println(" a1 is:"+a1);

    a4=JOptionPane.showInputDialog("Enter A Number for a2");
    a2=Interger.parse.Int(a4);
    System.out.println("a2 is:"+a2);

    b3=JOptionPane.showInputDialog("Enter A Number for b1");
    b1=Interger.parse.Int(b3);
    System.out.println("b1 is:"+b1);

    b4=JOptionPane.showInputDialog("Enter A Number for b2");
    b2=Interger.parse.Int(b4);
    System.out.println("b2 is:"+b2);

    c3=JOptionPane.showInputDialog("Enter A Number for c1");
    c1=Interger.parse.Int(c1);
    System.out.println("c1 is:"+c1);

    c4=JOptionPane.showInputDialog("Enter A Number for c2");
    c2=Interger.parse.Int(c2);
    System.out.println("c2 is:"+c2);

    //formula
    x=((c1*b2)-(c2*b1))/((a1*b2)-(b1*a2));
    y=(c2-a2*x)/b2;

    System.out.println("X is:"+x);
    System.out.println("Y is:"+y);

    System.exit(0);

    }

    }
    Last edited by .143 Beth©; 10-11-2005, 09:21 PM. Reason: FIX PROGRAM


  • #2
    Re: Java

    Am I validating syntax, logic, or both?
    Steam Community? Add me. | Free Remote, Encrypted Backup

    Darkilla: In short, NS is pretty much really fast chess. With guns. Apophis: I haven't seen anyone say that SM's are better than non-SMs. Nordbomber: This is THE first server I've seen where either side can comeback from out of seemingly nowhere with the right teamwork. en4rcment: I have NEVER experienced the type of gameplay that I have found here. Nightly I am amazed at the personalities and gaming talent. Zephyr: Apophis is clearly a highly sophisticated self-aware AI construct that runs on a highly modified toaster oven in Wyzcrak's basement.

    Comment


    • #3
      Re: Java

      Both

      Comment


      • #4
        Re: Java

        It's been too long for me with Swing, but I did notice the following. I don't have a Java IDE at home to test this, so you may find other stuff.
        Originally posted by .143 Beth©
        import javax.swing.JOptionPane;

        public class Assignment1

        {

        public static void main(String args[])

        {

        // Declaration of variables.
        int a1, a2, b1, b2, c1, c2, x, y;
        String a3, a4, b3, b4, c3, c4;

        // Ask for variables, Echo variables.

        a3=JOptionPane.showInputDialog("Enter A Number for a1");
        a1=Integer.parseInt(a3);
        System.out.println(" a1 is:"+a1);

        a4=JOptionPane.showInputDialog("Enter A Number for a2");
        a2=Integer.parseInt(a4);
        System.out.println("a2 is:"+a2);

        b3=JOptionPane.showInputDialog("Enter A Number for b1");
        b1=Integer.parseInt(b3);
        System.out.println("b1 is:"+b1);

        b4=JOptionPane.showInputDialog("Enter A Number for b2");
        b2=Integer.parseInt(b4);
        System.out.println("b2 is:"+b2);

        c3=JOptionPane.showInputDialog("Enter A Number for c1");
        c1=Integer.parseInt(c1);
        System.out.println("c1 is:"+c1);

        c4=JOptionPane.showInputDialog("Enter A Number for c2");
        c2=Integer.parseInt(c2);
        System.out.println("c2 is:"+c2);

        //formula
        x=((c1*b2)-(c2*b1))/((a1*b2)-(b1*a2));
        y=(c2-a2*x)/b2;

        System.out.println("X is:"+x);
        System.out.println("Y is:"+y);

        System.exit(0);

        }

        }
        Class names cannot have spaces.
        There is no Interger class, and the method you're looking for is parseInt
        Steam Community? Add me. | Free Remote, Encrypted Backup

        Darkilla: In short, NS is pretty much really fast chess. With guns. Apophis: I haven't seen anyone say that SM's are better than non-SMs. Nordbomber: This is THE first server I've seen where either side can comeback from out of seemingly nowhere with the right teamwork. en4rcment: I have NEVER experienced the type of gameplay that I have found here. Nightly I am amazed at the personalities and gaming talent. Zephyr: Apophis is clearly a highly sophisticated self-aware AI construct that runs on a highly modified toaster oven in Wyzcrak's basement.

        Comment


        • #5
          Re: Java

          Thank you very much

          Comment


          • #6
            Re: Java

            How can I get a program to compile the .java ?
            and execute my program

            Comment


            • #7
              Re: Java

              Code:
              c1 = Integer.parseInt(c1);
              parseInt() takes a String parameter. c1 is an int. So it won't compile. Same with c2.
              At the same time, I note that you're not using variables c3 and c4.
              So, figuring you probably meant to parse those for an int value I substituted those in.

              Now it compiles and runs. I didn't look over the formula, though. I presume your algorithm does the right thing.

              Your code convention could use work, and the class is badly in need of refactoring.

              It's less readable and maintainable to declare your variables in one line like you did:
              Code:
                      String a3, a4, b3, b4, c3, c4;
              All of those chunks of code do the same thing. Refactor them into a common method for "code re-use":
              Code:
                  private static int getValue(String label) {
                      String number = JOptionPane.showInputDialog("Enter A Number for " + label);
                      int c2 = Integer.parseInt(number);
                      System.out.println(label + " is:" + c2);
                      return c2;
                  }
              Next, handle the possible NumberFormatException that could be thrown if someone types in "three" or "phreak". To do that, surround any calls to getValue with a catch which shoves the dialog back in their face and demands they enter a valid number - or however you want to handle it.

              And ... give your variables long, meaningful names... :) Make your code describe itself.
              "You live and learn. Or you don't live long."
              - Lazarus Long

              Comment


              • #8
                Re: Java

                Thank you for the help guys and thatnks for catching that simple error Strike, but we didnt learn how to use the refactor thing yet, and the teacher likes us t otake it slow.
                Here is my up to date code:

                // Tyrus Smith
                // 2991
                // cmps120-6m, Assignment1

                // Problem:
                // Write a Java program to do the following computation:
                // Given the coeffients for two linear equations, find the
                // coordinates of the point of the plane that satifies both
                // equations.
                //

                import javax.swing.JOptionPane;

                public class Assignment1

                {

                public static void main(String args[])

                {

                // Declaration of variables.
                int a1, a2, b1, b2, c1, c2, x, y;
                String a3, a4, b3, b4, c3, c4;

                // Ask for variables, Echo variables.

                a3=JOptionPane.showInputDialog("Enter A Number for a1");
                a1=Integer.parseInt(a3);
                System.out.println(" a1 is:"+a1);

                a4=JOptionPane.showInputDialog("Enter A Number for a2");
                a2=Integer.parseInt(a4);
                System.out.println("a2 is:"+a2);

                b3=JOptionPane.showInputDialog("Enter A Number for b1");
                b1=Integer.parseInt(b3);
                System.out.println("b1 is:"+b1);

                b4=JOptionPane.showInputDialog("Enter A Number for b2");
                b2=Integer.parseInt(b4);
                System.out.println("b2 is:"+b2);

                c3=JOptionPane.showInputDialog("Enter A Number for c1");
                c1=Integer.parseInt(c3);
                System.out.println("c1 is:"+c1);

                c4=JOptionPane.showInputDialog("Enter A Number for c2");
                c2=Integer.parseInt(c4);
                System.out.println("c2 is:"+c2);

                //formula
                x=((c1*b2)-(c2*b1))/((a1*b2)-(b1*a2));
                y=(c2-a2*x)/b2;

                System.out.println("X is:"+x);
                System.out.println("Y is:"+y);

                System.exit(0);

                }

                }

                Comment


                • #9
                  Re: Java

                  So how can I get the thing to compile the program, and run the programs myself?

                  Comment


                  • #10
                    Re: Java

                    :)
                    http://www.mindview.net/Books/TIJ/
                    www.thinkinginjava.com

                    Your JDK comes with a compiler (javac.exe) and a 'runner' (java.exe)
                    "You live and learn. Or you don't live long."
                    - Lazarus Long

                    Comment


                    • #11
                      Re: Java

                      Has the teacher taught how to compile and run Java code? Or is this knowledge a prerequisite for the class?

                      For simple Java programs, I use Textpad to compile and run code. Install the Java SDK then Textpad and you're ready to go.

                      I think it might be better to use a float variable instead of int since your results will probably be real numbers.

                      - It's who you game with.

                      Comment


                      • #12
                        Re: Java

                        I got the compiler down packed on my pc. He taught us how to do it on the school's pc. I got that under control now but It gives me an "cannot read file " error.

                        Comment


                        • #13
                          Re: Java

                          Latest version of code DUE FRIDAY
                          // Tyrus Smith
                          // 2991
                          // cmps120-6m, Assignment1

                          // Problem:
                          // Write a Java program to do the following computation:
                          // Given the coeffients for two linear equations, find the
                          // coordinates of the point of the plane that satifies both
                          // equations.
                          //

                          import javax.swing.JOptionPane;

                          public class Assignment1

                          {

                          public static void main(String args[])

                          {

                          // Declaration of variables.
                          double a1, a2, b1, b2, c1, c2, x, y;

                          String a3, a4, b3, b4, c3, c4;

                          // Ask for variables, Echo variables.

                          a3=JOptionPane.showInputDialog("Enter A Number for a1");
                          a1=Double.parseDouble(a3);
                          System.out.println(" a1 is:"+a1);

                          a4=JOptionPane.showInputDialog("Enter A Number for a2");
                          a2=Double.parseDouble(a4);
                          System.out.println("a2 is:"+a2);

                          b3=JOptionPane.showInputDialog("Enter A Number for b1");
                          b1=Double.parseDouble(b3);
                          System.out.println("b1 is:"+b1);

                          b4=JOptionPane.showInputDialog("Enter A Number for b2");
                          b2=Double.parseDouble(b4);
                          System.out.println("b2 is:"+b2);

                          c3=JOptionPane.showInputDialog("Enter A Number for c1");
                          c1=Double.parseDouble(c3);
                          System.out.println("c1 is:"+c1);

                          c4=JOptionPane.showInputDialog("Enter A Number for c2");
                          c2=Double.parseDouble(c4);
                          System.out.println("c2 is:"+c2);

                          //formula
                          x=((c1*b2)-(c2*b1))/((a1*b2)-(b1*a2));
                          y=(c2-a2*x)/b2;

                          System.out.println("X is:"+x);
                          System.out.println("Y is:"+y);

                          System.exit(0);

                          }

                          }

                          Comment


                          • #14
                            Re: Java

                            Originally posted by .143 Beth©
                            I got the compiler down packed on my pc. He taught us how to do it on the school's pc. I got that under control now but It gives me an "cannot read file " error.
                            Give us the command you're using to invoke the compiler.
                            Steam Community? Add me. | Free Remote, Encrypted Backup

                            Darkilla: In short, NS is pretty much really fast chess. With guns. Apophis: I haven't seen anyone say that SM's are better than non-SMs. Nordbomber: This is THE first server I've seen where either side can comeback from out of seemingly nowhere with the right teamwork. en4rcment: I have NEVER experienced the type of gameplay that I have found here. Nightly I am amazed at the personalities and gaming talent. Zephyr: Apophis is clearly a highly sophisticated self-aware AI construct that runs on a highly modified toaster oven in Wyzcrak's basement.

                            Comment


                            • #15
                              Re: Java

                              NVM, I got it to work, and my test variables work perfectly. Thanks guys for all the help. This is just one more example why I pay to be in this community, to have freinds. JK
                              I know you guys would have helped me out even though I was a newbie to TG.
                              Thanks again, and I'm sure I'll be asking more JAVA related questions in the future.

                              Comment

                              Connect

                              Collapse

                              TeamSpeak 3 Server

                              Collapse

                              Advertisement

                              Collapse

                              Twitter Feed

                              Collapse

                              Working...
                              X