1 Replies - 203 Views - Last Post: 31 January 2018 - 11:41 PM Rate Topic: -----

#1 camerontoma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-November 17

Java Error for class

Posted 31 January 2018 - 08:40 PM

The error comes in the keyboardInput class and says actual and formal argument lists differ in length. I don't know that that means or how to fix it so can you please help! The program is supposed to be able to find the midpoint and distance of two points when you enter X1, Y1, X2, and Y2.

    public class CordInfo {
    public int x1 = 0;
    public int x2 = 0;
    public int y1 = 0;
    public int y2 = 0;

import java.util.Scanner; 

public class keyboardInput extends Point1 {
    public CordInfo points() {
        Scanner scan = new Scanner(System.in);
        CordInfo info = new CordInfo();
        System.out.println("X1:  ");
        info.x1 = scan.nextInt();
        System.out.println("X2:  ");
        info.x2 = scan.nextInt();
        System.out.println("Y1:  ");
        info.y1 = scan.nextInt();
        System.out.println("Y2:  ");
        info.y2 = scan.nextInt();

        return info;

/*This class takes two member fields x and y coordinates and gives back their 
  mid point and the distance between them. */  
class Point1  
    keyboardInput keyboard = new keyboardInput();
    CordInfo info = keyboard.points();
    double x1 = info.x1;
    double x2 = info.x2;
    double y1 = info.y1;
    double y2 = info.y2;
    /*constructor, this is must to create objects of Point class. 
      This constructor initiates the variables. 
    public Point1(double x1, double y1)  
        this.x1 = x1;  
        this.y1 = y1;
        this.x2 = x2;  
        this.y2 = y2;
    /*Below method returns the midPoint, please pay attention to the return type. 
      Point is a class, so this method returns object data such as Point. 
      This method receives another point as argument.*/  
    public Point1 getMidpoint(Point1 p)  
        double midx;  
        double midy;  
        /*here x means this object's x-coordinate and p.x  
          means x coordinate of the Point p. */  
        midx = (x1 + p.x2)/2;  
        midy = (y1 + p.y2)/2;  
        //please pay attention here, I am creating another Point object which mid values.  
        Point1 midPoint = new Point1(midx, midy);  
        return midPoint;  
    /*Below method returns the distance, please pay attention  
      to the return type double, 
      so this method returns a double value. 
      This method receives another point as argument.*/  
    public double getDistance(Point1 p)  
        double distance = Math.sqrt ((x1 - p.x2)*(x1 - p.x2) + (y1 - p.y2)*(y1 - p.y2));      
        return distance;  

This post has been edited by ndc85430: 31 January 2018 - 11:31 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Java Error for class

#2 ndc85430  Icon User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 798
  • View blog
  • Posts: 3,262
  • Joined: 13-June 14

Re: Java Error for class

Posted 31 January 2018 - 11:41 PM

Can you post the full error message please? It gives you important information like on which line the error occurs.

Your classes seem a bit confused. You have both CoordInfo and Point1 that both store essentially two points. The important concept in your application is a point, so model it correctly: it should have a single pair of x and y coordinates as its fields. Of course, it seems natural to put the method for calculating the midpoint in there because that's something you might ask of a point (i.e. "what is the midpoint of the line between this point and another?"). This class should not be reading input, since that's not what a point "does".

Also, when posting code, please put it within code tags, as you're asked in the box where you type your post. You can do this using the "[CODE]" button and I've done it for you this time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1