How do I make a connection for x1 and y1?

367
April 19, 2017, at 9:32 PM

This is the question I was given:

Create class (or classes) contain method to calculate the distance between two points (x1, y1) and (x2, y2) which are the data fields of the class. All numbers and return values should be of type double. Incorporate in class the methods that enable the user to enter the coordinates of the points. Test this class with client program DistanceTest.

But I am testing with using the formula of x1 + y1 instead of using the formula of finding (distance).

I want to use get and set method to do addition maths.

This is my code for distance1 :

import java.util.Scanner;
public class distance1 {
    private double result;
    public void credit(double x1,double y1)
    {
        result = x1 + y1;
    }
    public double getResult(){
        return result;
    }
}

And this is the code for distance1Test

import java.util.Scanner;
public class distancetest1 {
    public static void main(String[]args){
        distance1 x1 = new distance1();
        distance1 y1 = new distance1();
        distance1 r  = new distance1();
        Scanner input = new Scanner(System.in);
        double number;
        System.out.print("Enter a number: ");
        number = input.nextDouble();
        System.out.print("Enter a number: ");
        number = input.nextInt();
        System.out.printf("Result is: ", r.getResult());
    }
}
Answer 1

Firstly you forgot to call the credit function to do the addition, and secondly you need to change the System.out.printf("Result is: ", r.getResult()); to

System.out.println("Result is: "+ r.getResult()); 

see how to use printf in java: How to use System.out.printf

public static void main(String[] args){
    double x1,y1;
    distance1 r  = new distance1();
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a number: ");
    x1 = input.nextDouble();
    System.out.print("Enter a number: ");
    y1 = input.nextInt();
    r.credit(x1, y1);
    System.out.println("Result is: "+ r.getResult());
}
Answer 2

Jaca classes must have an uppercase first letter

public class Dimension{
  private int x1;
  ...
  // Use a constructor no initialize
  Dimension(int x1, int x2, int y1, int y2){
     this.x1 = x1;
     ...
  }
  public double getDistance(){
     return Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
  }
}

Test using:

public static void main(String args[]){
    Dimension d = new Dimension(1,10,40,30);
    System.out.print(d.getDistance());
}
Answer 3

The problem is in the method main of your class distancetest1, you should :

  1. retrieve the two number inputs in two different variables "x" and "y" of type Double
  2. instantiate only one distance1 object "r" (x1 and y1 are useless in your try)
  3. call the method r.credit(...) by passing it your two variables as arguments
  4. display the result with the method r.getResult()
READ ALSO
Vehicle Tracking [on hold]

Vehicle Tracking [on hold]

I have a file that contains traces of cars, each line contains the id of the car, temp, attitude and longitudeExample:

269
Why is volatile needed in java when there are harware stuff solving it for you?

Why is volatile needed in java when there are harware stuff solving it for you?

From what I've read, the "volatile" keyword in java ensures that a thread always fetches the most up-to-date value of a particular pointer, usually by reading/writing directly from/to memory to avoid cache inconsistencies

236
How to download an image in socket connection and remove the unwanted bytes

How to download an image in socket connection and remove the unwanted bytes

I need to download an image throught socket connectionCurrently I have a byte array which contains a

366
Espresso Unit tests against an Android emulator running my application

Espresso Unit tests against an Android emulator running my application

First of all, I am an absolute beginner at Android Studios, Gradle and Espresso

339