1 Replies - 402 Views - Last Post: 05 August 2012 - 01:31 PM Rate Topic: -----

#1 ANSAYLOR   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-August 12

Error messages in blue I believe are due to conversion problem?

Posted 05 August 2012 - 01:25 PM

My parray variable is already a Point type array; can you tell me why it is giving me an error? I tried a couple different conversions and java didn't like them.
Thank you!

import java.util.*;


public class FinalExam {

    
    public static void main(String[] args) {
        FinalExam fe = new FinalExam();
        Point[] parray = new Point[8];// there are only 8 Points
        fe.inputPoints(parray);
        System.out.println("All the Points in this plain");
        for (Point p : parray) {
            System.out.println(p);
        }
        Point[] pair = fe.shortestDistance(parray);
        System.out.println("Points with shortest distance between them");
        System.out.println(pair[0] + " and " + pair[1]);
        System.out.println("Point Closest to zero is : "
                + fe.closestToZero(parray));
    }

    
    public void inputPoints(Point[] parray) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter 8 points " + "\n");
        int points = input.nextInt();
        parray = new Point[points];

    }

   
    public Point[] shortestDistance(Point[] parray) {
        Point[] row = new Point[2];
        [color="#00BFFF"]double shortestD = distance(parray[0][0], parray[0][1], parray[1][0],
                parray[1][1]);[/color]
        for (int i = 0; i < parray.length; i++) {
            for (int j = i + 1; j < parray.length; j++) {  
                [color="#00BFFF"]double distance = distance(parray[i][0], parray[i][1],
                        parray[j][0], parray[j][1]);     [/color]  
                if (distance < shortestD) {
                    row[0] = parray[i];
                    row[1] = parray[j];
                    shortestD = distance;

                }
            }
        }
        return row;
    }

    
    public Point closestToZero(Point[] parray) {
        Point ctz = new Point();
        for (int i = 0; i < parray.length; i++) {
       [color="#00BFFF"] ctz = Math.sqrt(Math.pow(parray[i].getX(), 2) + Math.pow(parray[i].getY(), 2));[/color]
            
        }
        return ctz;
    } 


    class Point {

        double x = 0;
        double y = 0;
       
        public Point() {
        }

        
        public Point(double x, double y) {
        }

        
        public double distance(Point p) {
            Point[] pair = new Point[2];
            double shortestD = 0;
            for (int i = 0; i < pair.length; i++) {
                for (int j = i + 1; j < pair.length; j++) {
                    double distance = pair[i].distance(pair[j]);
                    shortestD = Math.sqrt(Math.pow((this.x - p.x), 2) + Math.pow((this.y - p.y), 2));
                    if (distance < shortestD) {
                        pair[0] = pair[i];
                        pair[1] = pair[j];
                        shortestD = distance;
                        return shortestD;
                    }
                }
            }
            return shortestD;
        }


        
        public double getX() {
            return x;
        }

       
        public double getY() {
            return y;
        }

        
        @Override
        public String toString() {
            System.out.println();
            return super.toString();
        }
    }
}

This post has been edited by smohd: 05 August 2012 - 01:40 PM
Reason for edit:: Code tags added. Please use [code] tags when posting codes


Is This A Good Question/Topic? 0
  • +

Replies To: Error messages in blue I believe are due to conversion problem?

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Error messages in blue I believe are due to conversion problem?

Posted 05 August 2012 - 01:31 PM

Quote

parray[0][0]


You're trying to treat a one-dimensional array as a two-dimensional one. You can't

btw please use code tags as you were asked. If you need to highlight lines, do it with obvious comments
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1