13 Replies - 854 Views - Last Post: 05 May 2014 - 12:38 PM Rate Topic: -----

#1 albmblue85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 15-April 14

java: arrays from methods coordinates... line distances HELP

Posted 22 April 2014 - 05:02 AM

Hi everyone.. i finally got to finish this program but now i am required to do the follwing... as i post here i am compiling other testers to see if i can get it to work.. a little bit of help or any suggestion would be appreciated!! thanks!
* what i know so far is that i must use arrays to get rid of xyz coordinates and also must be able to accept user input as they type...


1 - Write a java class called Point to represent a N-Dimensional point (With coordinates)
- The constructor should take any number of coordinates, and should keep track of the dimension
- The class should have accessor method for any coordinates
- Keep track of the number of points and every created point
2 - Write a java class called Line to represent a line (with a starting point and an ending point)
- The constructor arguments are the start and end points
- The constructor MUST handle points with dissimilar dimensions:
* For the point with smallest dimension create a new point with the largest dimension
* All unspecified coordinates can be set to zero
- Provide a getLineLength() method - Look up "Euclidian Distance" on wiki
- Keep track of the number of lines and every created line
3 - Write a tester class with main() to prompts and reads coordinates for 2 points from the user (CLI)
* Allow as many dimensions as the user wants for each point
* Allows abreviations for the random keyword and the exit keyword
* Allow user to request help
- Allow the user to request a random value (See Math library, random() method)
- Created the 2 points
- Create a line with those two points
- Display the starting / ending points and line length
- Loop and repeat step 3 until user request to exit
4 - When the user exits display final statistics:
* Number of points created
* List all the points created
* Number of lines created
* List all the lines created
import java.util.Scanner;
import java.lang.Math;
public class Test3
{
  public static void main ( String[] args )
  {
    String userInput;
    double x,y,z;
    Point firstPoint, secondPoint;
    Line myLine;
    do
    {
      userInput = getUserInput ( "Enter X for 1st point or \"Exit\":" );
      if ( matchString( "Exit", userInput ) ) { break; }
      x = Double.parseDouble( userInput );
      userInput = getUserInput ( "Enter Y for 1st point or \"Exit\":" );
      if ( matchString( "Exit", userInput ) ) { break; }
      y = Double.parseDouble( userInput );
      userInput = getUserInput ( "Enter Z for 1st point or \"Exit\":" );
      if ( matchString( "Exit", userInput ) ) { break; }
      z = Double.parseDouble( userInput );
      firstPoint = new Point( x, y, z );
      userInput = getUserInput ( "Enter X for 2nd point or \"Exit\":" );
      if ( matchString( "Exit", userInput ) ) { break; }
      x = Double.parseDouble( userInput );
      userInput = getUserInput ( "Enter Y for 2nd point or \"Exit\":" );
      if ( matchString( "Exit", userInput ) ) { break; }
      y = Double.parseDouble( userInput );
      userInput = getUserInput ( "Enter Z for 2nd point or \"Exit\":" );
      if ( matchString( "Exit", userInput ) ) { break; }
      z = Double.parseDouble( userInput );
      secondPoint = new Point( x, y, z );
      myLine = new Line( firstPoint, secondPoint );
      System.out.println( "(" + firstPoint.getX() + "," +
                                firstPoint.getY() + "," +
                                firstPoint.getZ() + ") to " +
                          "(" + secondPoint.getX() + "," +
                                secondPoint.getY() + "," +
                                secondPoint.getZ() + ") has length " +
                          myLine.getLineLength() );
    } while ( true );
    System.out.println( "You created " + Line.getLineCounter() + " lines and " +
                                         Point.getPointCounter() + " points... Bye!" );
  }
  public static String getUserInput( String prompt )
  {
    Scanner console = new Scanner ( System.in );
    System.out.print( prompt );
    return console.nextLine().trim();
  }
  public static boolean matchString( String what, String whatElse )
  {
    return what.substring(0,whatElse.length()).equalsIgnoreCase( whatElse );
  }
}
class Point
{
  private static int pointCounter=0;
  private double myX, myY, myZ;
  public Point( double x, double y, double z )
  {
    myX = x; myY = y; myZ = z;
    pointCounter++;
  }
  public static int getPointCounter()
  {
    return pointCounter;
  }
  public double getX()
  {
    return myX;
  }
  public double getY()
  {
    return myY;
  }
  public double getZ()
  {
    return myZ;
  }
}
class Line
{
  private static int lineCounter=0;
  Point point1, point2;
  public Line( Point start, Point end )
  {
    point1 = start; point2 = end;
    lineCounter++;
  }
  public static int getLineCounter()
  {
    return lineCounter;
  }
  public double getLineLength()
  {
    return Math.sqrt( Math.pow( point2.getX() - point1.getX(), 2.0 ) + 
                      Math.pow( point2.getY() - point1.getY(), 2.0 ) +
                      Math.pow( point2.getZ() - point1.getZ(), 2.0 ) );
  }
}



Is This A Good Question/Topic? 0
  • +

Replies To: java: arrays from methods coordinates... line distances HELP

#2 mike73  Icon User is offline

  • D.I.C Addict

Reputation: 164
  • View blog
  • Posts: 645
  • Joined: 24-April 10

Re: java: arrays from methods coordinates... line distances HELP

Posted 22 April 2014 - 05:18 AM

Which part are you having trouble with? What is not working, what is it doing vs what it should do?
Was This Post Helpful? 0
  • +
  • -

#3 albmblue85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 15-April 14

Re: java: arrays from methods coordinates... line distances HELP

Posted 22 April 2014 - 05:36 AM

@mike73 hi again, this one is actually working, we got to finish it at class yesterday, compiles and gets 3 coordinates xyz from user, what we need to do now is to have an array, to get a undetermined number of coordinates from user then from that point go the line class and obtain a distance what i am having doubt is on my point class.. this is what i have in this version i posted:
class Point
{
  private static int pointCounter=0;
  private double myX, myY, myZ;
  public Point( double x, double y, double z )
  {
    myX = x; myY = y; myZ = z;
    pointCounter++;
  }
  public static int getPointCounter()
  {
    return pointCounter;
  }
  public double getX()
  {
    return myX;
  }
  public double getY()
  {
    return myY;
  }
  public double getZ()
  {
    return myZ;
  }

and this is what i think i should do in my new point class since is not a "specified" number of coordinates... just calling an array... im not sure...
class Point
{
  private static int pointCounter=0;
  private double anyCoord;
  public Point(double coord)
  {
    anyCoord = coord;
    pointCounter++; //create an accesor for point counter to see count
  }
  public static int getPointCounter()
  {
    return pointCounter;
  }
  public double getCoord()
  {
    return anyCoord;
  }


View Postmike73, on 22 April 2014 - 05:18 AM, said:

Which part are you having trouble with? What is not working, what is it doing vs what it should do?

Was This Post Helpful? 0
  • +
  • -

#4 mike73  Icon User is offline

  • D.I.C Addict

Reputation: 164
  • View blog
  • Posts: 645
  • Joined: 24-April 10

Re: java: arrays from methods coordinates... line distances HELP

Posted 22 April 2014 - 05:47 AM

Java has a handy feature for constructors and methods that can take any number of arguments. it uses three dots: ...
Let me show you an example:
public class Example {
	
	private String[] words;
	
	public Example(String ... words) {
		this.words = words;
	}
}

public class ExampleTest {

	public static void main(String[] args) {
		Example ex1 = new Example("Hello");
		Example ex2 = new Example("Hello", "World");
		Example ex3 = new Example();
	}
}

As you can see, you can pass 0 - however many arguments you want to the constructor. They get passed in as an array.

This post has been edited by mike73: 22 April 2014 - 05:49 AM

Was This Post Helpful? 1
  • +
  • -

#5 albmblue85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 15-April 14

Re: java: arrays from methods coordinates... line distances HELP

Posted 22 April 2014 - 07:40 AM

yes i think i could use something like that, in fact for the exit, random, i was going to keep using matchstring to determine what the user enters, my question must be more like, how to ask the user to enter coordinates to an array then from the point class obtain that array...
Was This Post Helpful? 0
  • +
  • -

#6 albmblue85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 15-April 14

Re: java: arrays from methods coordinates... line distances HELP

Posted 22 April 2014 - 03:38 PM

here is the last code ive added and compiles, my problem now is how to read the points been added to my array from the method and also being able to create the two points that my line will need
import java.util.Scanner;
import java.lang.Math;
import java.util.ArrayList;
import java.util.Arrays;

public class Lab7AlmaBonilla
{
  private static String answer = null;
  //public static double number, coord;
  private static int counter =0;
  
  private static ArrayList <Double> coordList = new ArrayList<Double>();
  
  
  public static void main(String[] args)
  {
    String userInput = null;
    double coordinate;
    Point firstPoint, secondPoint;
    double minN = -1000.00;
    double maxN = +100.00;
    Line myLine;
    
    while(true)
    {
      answer = getUserInput();
      if ((answer == null) || (answer.length() == 0) || (answer.trim().equals("")))
      {
        System.out.println("BYE");
        break;
      }
      else if (answer.trim().toUpperCase().charAt(0) == 'R')
      {
        coordinate = Math.random()* (maxN - minN) + minN;; 
        coordList.add(coordinate);
        System.out.println("Random generated " + coordinate);
      }
      else if (answer.trim().toUpperCase().charAt(0) == 'E')
      {
        //System.out.println("You have created " + coordList(coordinate));
        break;
      }
      else if (answer.trim().toUpperCase().charAt(0) == 'H')
      {
        helpMenu();
      }
      else
      {
        coordinate = Double.parseDouble(answer);
        coordList.add(coordinate);
      }
      
      
      
      
      
    }
  }
  public static void helpMenu()
  {
    System.out.println("****************************** LAB 7 HELP MENU ****************************************************");
    System.out.println("**This program allows you specify points in N-dimensional space:\n" + 
                       "- Each point can have different number of non-zero coordinate \n" + 
                       "-You may request a random number for any coordinate by typing RANDOM\n" + 
                       "- When you are finished entering the cordinate just press the <Enter> key");
    System.out.println("---------------------------------------------------------------------------------------------------");
    System.out.println("**Pairs of point are used to create a lines\n" + 
                       "- If the 2 points have mismatched dimensions, the point from the lower-dimension space is\n" + 
                       "  converted to a higher dimension point with added coordinate of 0\n" + 
                       "- When a line is created, the line distance is provided");
    System.out.println("---------------------------------------------------------------------------------------------------");
    System.out.println("* When you are done specifying points and lines type EXIT to display final operation statistics\n" + 
                       "* All key words are case insensitive and can be abreviated\n" + 
                       "* Random number will be scaled between -1,000.00 and +1,000.00");
    System.out.println("****************************** LAB 7 HELP MENU ****************************************************");
    System.out.println();
  } 
  
  public static String getUserInput()
  {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter a coordinate: [R]andom: [H]elp: [E]xit to exit program: Enter key  for next point:");
    return keyboard.nextLine();
  }
  
  
}



class Point
{
  private static int pointCounter=0;
  private double[] anyCoord;
  public Point(double[] coord)
  {
    anyCoord = coord;
    pointCounter++; //create an accesor for point counter to see count
  }
  
  public static int getPointCounter()
  {
    return pointCounter;
  }
  
}
class Line
{
  private static int lineCounter=0;
  Point point1, point2;
  public Line(Point start, Point end)
  {
    point1 = start; point2 = end;
    lineCounter++; //create an accesor for point counter to see count
  }
  public static int getLineCounter()
  {
    return lineCounter;
  }
  public double getLineLength()
  {
    return 0.0;
    //Math.sqrt(math.pow(point2.getX()-point1.getX(),2.0)+
    //               (math.pow(point2.getX()-point1.getX(),2.0)+
    //              (math.pow(point2.getX()-point1.getX(),2.0))));
  }
}


Was This Post Helpful? 0
  • +
  • -

#7 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: java: arrays from methods coordinates... line distances HELP

Posted 22 April 2014 - 03:47 PM

Quote

how to read the points been added to the array

I'm not sure I follow… are these the coordinates that you pass into the Point class? Since you've declared it as private you'll need to implement a getter and setter in the Point class to be able to retrieve those values:
public class Point{
/*
Rest of code here
*/
public double[] getAnyCoord(){
return anyCoord;
}

public void setAnyCoord(double[] a){
anyCoord = a;
}
}


Was This Post Helpful? 0
  • +
  • -

#8 albmblue85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 15-April 14

Re: java: arrays from methods coordinates... line distances HELP

Posted 22 April 2014 - 03:59 PM

thanks DimitriV
yes that sounds right.. but why the a? just to call the first value??? and since its an array would it get all the numbers the user enters for that first coordinate point and after a having a pair bein able to create a line...

im confused in that part...
public double[] getAnyCoord(){
06
return anyCoord;
07
}
08
 
09
public void setAnyCoord(double[] a){
10
anyCoord = a;// call it a? could be any name? and then from my main class do 
11            //i call it with the point one, once the user hits enter,,, how do i do that..?
}
12
}



View PostDimitriV, on 22 April 2014 - 03:47 PM, said:

Quote

how to read the points been added to the array

I'm not sure I follow… are these the coordinates that you pass into the Point class? Since you've declared it as private you'll need to implement a getter and setter in the Point class to be able to retrieve those values:
public class Point{
/*
Rest of code here
*/
public double[] getAnyCoord(){
return anyCoord;
}

public void setAnyCoord(double[] a){
anyCoord = a;
}
}


Was This Post Helpful? 0
  • +
  • -

#9 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: java: arrays from methods coordinates... line distances HELP

Posted 22 April 2014 - 05:55 PM

The actual variable name a has nothing to do with its value. Basically, you could pass in an array of double with any name you want and to the setter method it is visible as an array named a.
Was This Post Helpful? 0
  • +
  • -

#10 albmblue85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 15-April 14

Re: java: arrays from methods coordinates... line distances HELP

Posted 22 April 2014 - 08:09 PM

i ve been working on the lab, and amazingly i have come out with some work lol... i got the understanding of constructors for methods, but is not working at full, i am having issues, when creating first set of xs and ys it wont count as 1 point, also, when the two points are created i am trying to call the line from those two points... nothing yet... can someone advise again PLEASE!!! i know, i am a pain in the @$$!
/*
 COSC 1436 - Lab 7
 Programmer: Alma Bonilla
 Date: 04/21/2014
 Version 3.0
 */
import java.util.Scanner;
import java.lang.Math;
import java.util.ArrayList;
import java.util.Arrays;

public class Lab7AlmaBonilla
{
  private static String answer = null;
  private static int counter =0;
  private static ArrayList <Double> coordList = new ArrayList<Double>();
  
  
  public static void main(String[] args)
  {
    String userInput = null;
    double coordinate;
    Point point1, point2;
    Line myLine;
    double minN = -1000.00;
    double maxN = +100.00;
    
    
    while(true)
    {
      answer = getUserInput1();
      if ((answer == null) || (answer.length() == 0) || (answer.trim().equals("")))
      {
        while(true)
        {
          answer = getUserInput2();
          if ((answer == null) || (answer.length() == 0) || (answer.trim().equals("")))
          {
            System.out.println("BYE");
            break;
          }
          else if (answer.trim().toUpperCase().charAt(0) == 'R')
          {
            coordinate = Math.random()* (maxN - minN) + minN;; 
            coordList.add(coordinate);
            System.out.println("Random generated " + coordinate);
          }
          else if (answer.trim().toUpperCase().charAt(0) == 'E')
          {
            System.out.println("You have created " + Point.getPointCounter() + " points and " + Line.getLineCounter() + " lines");//coordList(coordinate));
            
            break;
          }
          else if (answer.trim().toUpperCase().charAt(0) == 'H')
          {
            helpMenu();
          }
          else
          {
            coordinate = Double.parseDouble(answer);
            coordList.add(coordinate);
          }
        }
        
        //add another point
        double coord[] = new double[ coordList.size() ];
        
        point2 = new Point(coord);
        
        //myLine = new Line( point1, point2 ); //HAVE AN ERROR THERE... 
      }
      else if (answer.trim().toUpperCase().charAt(0) == 'R')
      {
        coordinate = Math.random()* (maxN - minN) + minN;; 
        coordList.add(coordinate);
        System.out.println("Random generated " + coordinate);
      }
      else if (answer.trim().toUpperCase().charAt(0) == 'E')
      {
        
        System.out.println("You have created " + Point.getPointCounter() + " points and " + Line.getLineCounter() + " lines");
        break;
      }
      else if (answer.trim().toUpperCase().charAt(0) == 'H')
      {
        helpMenu();
      }
      else
      {
        coordinate = Double.parseDouble(answer);
        coordList.add(coordinate);
      }  
    }
    double coord[] = new double[ coordList.size() ];
    point1 = new Point(coord);
  //  myLine = new Line( point1, point2 )
  }
  
  public static void helpMenu()
  {
    System.out.println("****************************** LAB 7 HELP MENU ****************************************************");
    System.out.println("**This program allows you specify points in N-dimensional space:\n" + 
                       "- Each point can have different number of non-zero coordinate \n" + 
                       "-You may request a random number for any coordinate by typing RANDOM\n" + 
                       "- When you are finished entering the cordinate just press the <Enter> key");
    System.out.println("---------------------------------------------------------------------------------------------------");
    System.out.println("**Pairs of point are used to create a lines\n" + 
                       "- If the 2 points have mismatched dimensions, the point from the lower-dimension space is\n" + 
                       "  converted to a higher dimension point with added coordinate of 0\n" + 
                       "- When a line is created, the line distance is provided");
    System.out.println("---------------------------------------------------------------------------------------------------");
    System.out.println("* When you are done specifying points and lines type EXIT to display final operation statistics\n" + 
                       "* All key words are case insensitive and can be abreviated\n" + 
                       "* Random number will be scaled between -1,000.00 and +1,000.00");
    System.out.println("****************************** LAB 7 HELP MENU ****************************************************");
    System.out.println();
  } 
  
  public static String getUserInput1()
  {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter  coordinate 1: [R]andom: [H]elp: [E]xit to exit program: Enter key  for next point:");
    return keyboard.nextLine();
  }
  public static String getUserInput2()
  {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter  coordinate 2: [R]andom: [H]elp: [E]xit to exit program: Enter key  for next point:");
    return keyboard.nextLine();
  }  
  
}



class Point
{
  private static int pointCounter=0;
  private double[] anyCoord;
  public Point(double[] coord)
  {
    anyCoord = coord;
    pointCounter++; //create an accesor for point counter to see count
  }
  public double[] getAnyCoord()
  {
    return anyCoord;
    
  }
  public void setAnyCoord(double[] a)
  {
    anyCoord = a;    
  }
  
  public static int getPointCounter()
  {
    return pointCounter;
  }
  
}
class Line
{
  private static int lineCounter=0;
  Point point1, point2;
  public Line(Point start, Point end)
  {
    point1 = start; point2 = end;
    lineCounter++; //create an accesor for point counter to see count
  }
  public static int getLineCounter()
  {
    return lineCounter;
  }
  public double getLineLength()
  {
    return 0.0;
    // Math.sqrt(math.pow(point2.getX()-point1.getX(),2.0)+
    //   (math.pow(point2.getX()-point1.getX(),2.0)));
  }
}





Was This Post Helpful? 0
  • +
  • -

#11 albmblue85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 15-April 14

Re: java: arrays from methods coordinates... line distances HELP

Posted 24 April 2014 - 09:24 AM

here is the latest additions i ve been working on.. i sent my lab to instructor and this is the feedback i got from him

Good start.
- There is not much differences between getUserInput1() and getUserInput2(), could they be merge into 1 method that takes the point # as an argument?
- You do not handle dissimilar dimensions for input Point in the Line constructor



View Postalbmblue85, on 22 April 2014 - 08:09 PM, said:

i ve been working on the lab, and amazingly i have come out with some work lol... i got the understanding of constructors for methods, but is not working at full, i am having issues, when creating first set of xs and ys it wont count as 1 point, also, when the two points are created i am trying to call the line from those two points... nothing yet... can someone advise again PLEASE!!! i know, i am a pain in the @$$!

import java.util.Scanner;
import java.lang.Math;
import java.util.ArrayList;
import java.util.Arrays;

public class Lab7AlmaBonilla
{
  private static String answer = null;
  private static int counter =0;
  private static ArrayList <Double> coordList = new ArrayList<Double>();
  
  
  public static void main(String[] args)
  {
    String userInput = null;
    double coordinate;
    Point point1, point2;
    Line myLine;
    double minN = -1000.00;
    double maxN = +100.00;
    
    
    while(true)
    {
      answer = getUserInput1();
      if ((answer == null) || (answer.length() == 0) || (answer.trim().equals("")))
      {
        while(true)
        {
          answer = getUserInput2();
          if ((answer == null) || (answer.length() == 0) || (answer.trim().equals("")))
          {
            System.out.println("BYE");
            break;
          }
          else if (answer.trim().toUpperCase().charAt(0) == 'R')
          {
            coordinate = Math.random()* (maxN - minN) + minN;; 
            coordList.add(coordinate);
            System.out.println("Random generated " + coordinate);
          }
          else if (answer.trim().toUpperCase().charAt(0) == 'E')
          {
            System.out.println("You have created " + Point.getPointCounter() + " points and " + Line.getLineCounter() + " lines");//coordList(coordinate));
            
            break;
          }
          else if (answer.trim().toUpperCase().charAt(0) == 'H')
          {
            helpMenu();
          }
          else
          {
            coordinate = Double.parseDouble(answer);
            coordList.add(coordinate);
          }
        }
        
        //add another point
        double coord[] = new double[ coordList.size() ];
        
        point2 = new Point(coord);
        
        //myLine = new Line( point1, point2 ); //HAVE AN ERROR THERE... 
      }
      else if (answer.trim().toUpperCase().charAt(0) == 'R')
      {
        coordinate = Math.random()* (maxN - minN) + minN;; 
        coordList.add(coordinate);
        System.out.println("Random generated " + coordinate);
      }
      else if (answer.trim().toUpperCase().charAt(0) == 'E')
      {
        
        System.out.println("You have created " + Point.getPointCounter() + " points and " + Line.getLineCounter() + " lines");
        break;
      }
      else if (answer.trim().toUpperCase().charAt(0) == 'H')
      {
        helpMenu();
      }
      else
      {
        coordinate = Double.parseDouble(answer);
        coordList.add(coordinate);
      }  
    }
    double coord[] = new double[ coordList.size() ];
    point1 = new Point(coord);
  //  myLine = new Line( point1, point2 )
  }
  
  public static void helpMenu()
  {
    System.out.println("****************************** LAB 7 HELP MENU ****************************************************");
    System.out.println("**This program allows you specify points in N-dimensional space:\n" + 
                       "- Each point can have different number of non-zero coordinate \n" + 
                       "-You may request a random number for any coordinate by typing RANDOM\n" + 
                       "- When you are finished entering the cordinate just press the <Enter> key");
    System.out.println("---------------------------------------------------------------------------------------------------");
    System.out.println("**Pairs of point are used to create a lines\n" + 
                       "- If the 2 points have mismatched dimensions, the point from the lower-dimension space is\n" + 
                       "  converted to a higher dimension point with added coordinate of 0\n" + 
                       "- When a line is created, the line distance is provided");
    System.out.println("---------------------------------------------------------------------------------------------------");
    System.out.println("* When you are done specifying points and lines type EXIT to display final operation statistics\n" + 
                       "* All key words are case insensitive and can be abreviated\n" + 
                       "* Random number will be scaled between -1,000.00 and +1,000.00");
    System.out.println("****************************** LAB 7 HELP MENU ****************************************************");
    System.out.println();
  } 
  
  public static String getUserInput1()
  {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter  coordinate 1: [R]andom: [H]elp: [E]xit to exit program: Enter key  for next point:");
    return keyboard.nextLine();
  }
  public static String getUserInput2()
  {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter  coordinate 2: [R]andom: [H]elp: [E]xit to exit program: Enter key  for next point:");
    return keyboard.nextLine();
  }  
  
}



class Point
{
  private static int pointCounter=0;
  private double[] anyCoord;
  public Point(double[] coord)
  {
    anyCoord = coord;
    pointCounter++; //create an accesor for point counter to see count
  }
  public double[] getAnyCoord()
  {
    return anyCoord;
    
  }
  public void setAnyCoord(double[] a)
  {
    anyCoord = a;    
  }
  
  public static int getPointCounter()
  {
    return pointCounter;
  }
  
}
class Line
{
  private static int lineCounter=0;
  Point point1, point2;
  public Line(Point start, Point end)
  {
    point1 = start; point2 = end;
    lineCounter++; //create an accesor for point counter to see count
  }
  public static int getLineCounter()
  {
    return lineCounter;
  }
  public double getLineLength()
  {
    return 0.0;
    // Math.sqrt(math.pow(point2.getX()-point1.getX(),2.0)+
    //   (math.pow(point2.getX()-point1.getX(),2.0)));
  }
}





Was This Post Helpful? 0
  • +
  • -

#12 mike73  Icon User is offline

  • D.I.C Addict

Reputation: 164
  • View blog
  • Posts: 645
  • Joined: 24-April 10

Re: java: arrays from methods coordinates... line distances HELP

Posted 25 April 2014 - 12:03 AM

Well the first point is pretty straight forward. Your two input methods are identical except for the prompt, so they could just be one method.
The second point is a bit more challenging. If you enter two points like so:
(1,45,6) and (7,2,5,6,8,10) The secon second set of points has more dimensions, so you would have to change the 1st on to:(1,45,6,0,0,0).
Was This Post Helpful? 0
  • +
  • -

#13 albmblue85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 15-April 14

Re: java: arrays from methods coordinates... line distances HELP

Posted 28 April 2014 - 06:43 AM

yeah thats what i figured, but i am confused on how to do that, add the zeros to either the first or second array if user enters more dimensions to them... ill work on it this morning since i must send it this afternoon when its due.. also, my line is not being created anywhere in the code... when trying to print out the list of points, instead i need to print the set added to the array at that instance of the loop for that first line, instead i am just printint point 1, point two.

oh man! im stucked in java

View Postmike73, on 25 April 2014 - 12:03 AM, said:

Well the first point is pretty straight forward. Your two input methods are identical except for the prompt, so they could just be one method.
The second point is a bit more challenging. If you enter two points like so:
(1,45,6) and (7,2,5,6,8,10) The secon second set of points has more dimensions, so you would have to change the 1st on to:(1,45,6,0,0,0).

Was This Post Helpful? 0
  • +
  • -

#14 albmblue85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 15-April 14

Re: java: arrays from methods coordinates... line distances HELP

Posted 05 May 2014 - 12:38 PM

STILL IN NEED OF HELP. I WAS ABLE TO GET MY POINT ARRAY BUT NOW I AM HAVING TROUBLE GETTING THE ARRAY TO CHECK FOR LENGHT AND COPYING ARRAY TO ANOTHER ARRAY SO I CAN HAVE EQUALLY POINTS FOR MY LINE METHOD...
ANYONE PLEASE!! REALLY APPRECIATED

/*
 COSC 1436 - Lab 7
 Programmer: Alma Bonilla
 Date: 04/21/2014
 Version 3.0
 */
import java.util.Scanner;
import java.lang.Math;
import java.util.ArrayList;
import java.util.Arrays;

public class Lab7AlmaBonilla
{
  private static String answer = null;
  private static ArrayList <Double> coordList = new ArrayList<Double>();
  
  //set static declare variables to use
  public static void main(String[] args)
  {
    String userInput = null;
    double coordinate;
    Point point1, point2;
    Line myLine;
    double minN = -1000.00; //use for minimum and maximum
    double maxN = +100.00;
    
    
    //run loop depending on user entry
    while(true)
    {
      answer = getUserInput1();
      //if user dont use enter, stay in same coordinate and keep asking for entries
      if ((answer == null) || (answer.length() == 0) || (answer.trim().equals("")))
      {
        //inside loop after the user has selected to go to next set of coordinates
        while(true)
        {
          answer = getUserInput1();
          //if user dont use enter, stay in same coordinate and keep asking for entries
          if ((answer == null) || (answer.length() == 0) || (answer.trim().equals("")))
          {
            //display what user entered and return points and line distance
            System.out.println("The distance between:  " );//+ Point.printPoints());// +  " is: " + Line.getLineLength());
            break;
          }
          //if user selects to use random numbers
          else if (answer.trim().toUpperCase().charAt(0) == 'R')
          {
            //create random number between -1000.00 and 1000.00 and create coordinate
            coordinate = Math.random()* (maxN - minN) + minN;; 
            coordList.add(coordinate);
            System.out.println("Random generated " + coordinate);
          }
          //if user types exit to leave the program, display information
          else if (answer.trim().toUpperCase().charAt(0) == 'E')
          {
            //display information
            System.out.println("You have created " + Point.getPointCounter() + " points and " + Line.getLineCounter() + " lines");//coordList(coordinate));
            break;
          }
          //display help if user requests it
          else if (answer.trim().toUpperCase().charAt(0) == 'H')
          {
            helpMenu();
          }
          else
          {
            //any other value, assume is a double and create coordinate, keep on loop until user desires
            coordinate = Double.parseDouble(answer);
            coordList.add(coordinate);
          }
          
        }  
        //create coordinate
        double coord[] = new double[ coordList.size() ];
        //create the point and line
        point2 = new Point(coord);
        //create line  
        // myLine = new Line( point1, point2 ); 
      }
      //if user selects to use random numbers
      else if (answer.trim().toUpperCase().charAt(0) == 'R')
      {
        //create random number between -1000.00 and 1000.00 and create coordinate
        coordinate = Math.random()* (maxN - minN) + minN;; 
        coordList.add(coordinate);
        System.out.println("Random generated " + coordinate);
      }
      //if user types exit to leave the program, display information
      else if (answer.trim().toUpperCase().charAt(0) == 'E')
      {
        //display information
        System.out.println("You have created " + Point.getPointCounter() + " points and " + Line.getLineCounter() + " lines");
        break;
      }
      //display help if user requests it
      else if (answer.trim().toUpperCase().charAt(0) == 'H')
      {
        helpMenu();
      }
      else
      {
        //any other value, assume is a double and create coordinate, keep on loop until user desires
        coordinate = Double.parseDouble(answer);
        coordList.add(coordinate);
        
      }  
    }
    //create coordinate
    double coord[] = new double[ coordList.size() ];
    //cretea point
    point1 = new Point(coord);
    
  }
  
  //help 
  public static void helpMenu()
  {
    System.out.println("****************************** LAB 7 HELP MENU ****************************************************");
    System.out.println("**This program allows you specify points in N-dimensional space:\n" + 
                       "- Each point can have different number of non-zero coordinate \n" + 
                       "-You may request a random number for any coordinate by typing RANDOM\n" + 
                       "- When you are finished entering the cordinate just press the <Enter> key");
    System.out.println("---------------------------------------------------------------------------------------------------");
    System.out.println("**Pairs of point are used to create a lines\n" + 
                       "- If the 2 points have mismatched dimensions, the point from the lower-dimension space is\n" + 
                       "  converted to a higher dimension point with added coordinate of 0\n" + 
                       "- When a line is created, the line distance is provided");
    System.out.println("---------------------------------------------------------------------------------------------------");
    System.out.println("* When you are done specifying points and lines type EXIT to display final operation statistics\n" + 
                       "* All key words are case insensitive and can be abreviated\n" + 
                       "* Random number will be scaled between -1,000.00 and +1,000.00");
    System.out.println("****************************** LAB 7 HELP MENU ****************************************************");
    System.out.println();
  } 
  
  public static String getUserInput1()
  {
    //user interaction
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter  coordinate : [R]andom: [H]elp: [E]xit to exit program: Enter key  for next point:");
    return keyboard.nextLine();
  }
}
//class point, accept any coordinates keeping track of the dimension
class Point
{
  private static int pointCounter=0;
  private double[] anyCoord;
  public Point(double[] coord)
  {
    this.anyCoord = coord;
    pointCounter++; //create an accesor for point counter to see count
  }
  public double[] getAnyCoord()
  {
    return anyCoord; 
  }
  public void setAnyCoord(double[] a)
  {
    anyCoord = a;    
  }
  public int getPointDimension()
  {
    return anyCoord.length;
  }
  public static int getPointCounter()
  {
    return pointCounter;
  }
  public void printPoints()
  {
    for ( int i = 0; i < anyCoord.length; i++ )
    {
      System.out.println("Coordinate # " + i + " : " +  anyCoord[ i ]);
    }
    System.out.println(" ");
  }
  
}
class Line
{
  private static int lineCounter=0;
  Point point1, point2;
  public Line(Point start, Point end)
  {
    point1 = start; 
    point2 = end;
    lineCounter++; //create an accesor for point counter to see count
    if (start.getPointDimension()  == end.getPointDimension())
    {
      return; 
    }
    else 
    {
      if (start.getPointDimension() > end.getPointDimension())
      {
        double coord[] = new double[ start.getPointDimension()];
        double coord[] = Arrays.copyOf(start.getPointDimension(), start.lenght);
        //int[] b = Arrays.copyOf(a, a.length);
      }
      else
      {
        double coord[] = new double[ end.getPointDimension()];
      }
      
    }
   
  }
  
  
  public static int getLineCounter()
  {
    return lineCounter;
  }
  public double getLineLength()
  {
    return 0.0;    
  // Math.sqrt((Math.pow(end.getPointDimension()-  start.getPointDimension(),2.0))+ ((math.pow(end.getPointDimension()-  start.getPointDimension(),2.0))));
  }
}




Was This Post Helpful? 0
  • +
  • -

Page 1 of 1