Jump to content

how do i do this

- - - - -

  • Please log in to reply
10 replies to this topic

#1
atoivan

atoivan

    Learning Programmer

  • Members
  • PipPipPip
  • 61 posts
A PROGRAM THAT WILL BE ABLE TO CALCULATE THE GRADIENT/SLOPE OF A LINE GIVEN TWO POINTS:

Now i am trying to put the coordinates of Y into an array but it is not working can any one correct this for me i do not want to create classes for this program

import javax.swing.JOptionPane;


/**

 *

 * @author irvan

 */

public class Main {

   // private static Object Cord_y;


    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

         double  Codr_x[];

         double Codr_y[];

           Codr_y = new double[2];

	    	      Codr_x = new double [2];

                      int z =1;

	    	         for (int i = 0; i < Cord_y.length(); i++) {

	    	          String in =  JOptionPane.showInputDialog("Enter the coordinates for Y"+z);

	    	         Cord_y =Double.parseDouble(in);

                          ++z;


#2
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
You need to store the variable in the array starting at 0 or i:
Codr_y[i] = Double.parseDouble(in);

#3
atoivan

atoivan

    Learning Programmer

  • Members
  • PipPipPip
  • 61 posts
Mr.mike thank u for the help but it is still not working
after the correction

import javax.swing.JOptionPane;

public class lindemontry1 {


	/**

	 * @param args

	 */

	public static void main(String[] args) {

		// TODO Auto-generated method stub


		  double  Codr_x[];

	         double Codr_y[];

	           Codr_y = new double[2];

		    	      Codr_x = new double [2];

	                      int z =1;

		    	         for (int i = 0; i < Cord_y.length(); i++) {

		    	          String in =  JOptionPane.showInputDialog("Enter the coordinates for Y"+z);

		    	         Cord_y[i] =Double.parseDouble(in);

	                          ++z;

	}
u can try to ran it and see for ur self

#4
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
I changed your variable names to java naming conventions and formatted the code for you. It works


import javax.swing.*;

public class Lindemontry1 {


   private static String in;

   private static double[] cordX;	

   private static double[] cordY;

	

   public static void main(String[] args){


      int z = 1;

      cordX = new double[2];

      cordY = new double[2];


      for (int i = 0; i < cordY.length; i++) {        	

         in =  JOptionPane.showInputDialog("Enter the coordinates for Y" + z);

         cordY[i] =Double.parseDouble(in);

         ++z;

      }// end of for loop


      // this is just a test

      System.out.println(cordY[1]);

      System.out.println(cordY[0]);

		

   }// end of main

}// end of class



#5
atoivan

atoivan

    Learning Programmer

  • Members
  • PipPipPip
  • 61 posts
thanks very much u gave me the idea with u first post so i have corrected it
this is ma new code

package lines;


import java.util.Arrays;

import javax.swing.JOptionPane;


/**

 *

 * @author irvan

 */

public class Main {

   // private static Object Cord_y;


    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

    double  Codr_x[];

    double Codr_y[];

    Codr_y = new double[2];

    Codr_x = new double [2];

    

    int z =1;

    for (int i = 0; i < Codr_x.length; i++) {

String in =  JOptionPane.showInputDialog("Enter the coordinates for X"+z);

Codr_x[i] =Double.parseDouble(in);

++z;

  }


  int t =1;

  for (int i = 0; i < Codr_y.length; i++) {

String in =  JOptionPane.showInputDialog("Enter the coordinates for Y"+t);

Codr_y[i] =Double.parseDouble(in);

++t;

	}

 for (int d=0;d<Codr_x.length;++d)

    { String toShowInPane = Arrays.toString(Codr_x);

      JOptionPane.showMessageDialog(null, toShowInPane);

        }


 for (int a=0;a<Codr_y.length;++a)

    {

String toShowInPane = Arrays.toString(Codr_y);

JOptionPane.showMessageDialog(null, toShowInPane);

 }

  double slope = (Codr_y[1] - Codr_y[0]) / (Codr_x[1] - Codr_x[0]);

  JOptionPane.showMessageDialog(null, slope);

}


}


#6
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
I was wondering why you dont have the user enter the coordinates as x then y so it is more natural
for the user. This will allow you to eliminate some of the extra for loops.

(userInputX, userNextInputY)

#7
atoivan

atoivan

    Learning Programmer

  • Members
  • PipPipPip
  • 61 posts
i get u but how will i do that
and i think there is a problem with dis loop
it loops more
for (int d=0;d<Codr_x.length;++d)

    { String toShowInPane = Arrays.toString(Codr_x);

      JOptionPane.showMessageDialog(null, toShowInPane);

        }


#8
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts

for (int i = 0; i < cordY.length; i++) { 

         in =  JOptionPane.showInputDialog("Enter the coordinates for X" + z);

         cordX[i] =Double.parseDouble(in);       	

         in =  JOptionPane.showInputDialog("Enter the coordinates for Y" + z);

         cordY[i] =Double.parseDouble(in);

         ++z;

}// end of for loop

Quote

there is a problem with dis loop
it loops more
Code:
for (int d=0;d<Codr_x.length;++d)
{ String toShowInPane = Arrays.toString(Codr_x);
JOptionPane.showMessageDialog(null, toShowInPane);
}
You dont need to use the abstract Arrays.methods(). You want to convert double to a string with the loop you wrote and dont forget
Codr_x[d]

#9
atoivan

atoivan

    Learning Programmer

  • Members
  • PipPipPip
  • 61 posts
so do i have to add the [d] to the loop .
i tried but it did not work it gave me an error saying double cannot be dereferenced

for (int d=0;d<Codr_x[d].length;++d)

    { String toShowInPane = Arrays.toString(Codr_x);

      JOptionPane.showMessageDialog(null, toShowInPane);

        }


#10
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
I was saying something along the lines of:


String toShowInPane = "";// initialize

for(int i = 0; i < codr_x.length; i++){

   toShowInPane = toShowInPane + codr_x[d] + " ";

}

JOptionPane.showMessageDialog(null, toShowInPane);



#11
atoivan

atoivan

    Learning Programmer

  • Members
  • PipPipPip
  • 61 posts
Mr.mike thank u very much it all working now do u want to see the code?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users