Java :: Aufgabe #35

6 Lösungen Lösungen öffentlich

Abstand zweier Punkte

Anfänger - Java von Dome - 03.01.2013 um 01:09 Uhr
Schreiben Sie ein Programm, welches den Abstand zweier Punkte berechnet. Zuvor müssen die Koordinaten beider Punkte abgefragt werden.

Konsolenausgabe:

x1:1
y1:1
x2:2
y2:2
1.4142135623730951

Lösungen:

vote_ok
von 0 (0 Punkte) - 21.11.2013 um 21:11 Uhr
Quellcode ausblenden Java-Code
/*
 * Code written by Oppa Hansi, oppahansi on Pastebin.
 */

import java.util.Scanner;

public class GetDistance {
	
	// Methode für die Berechnung der Distanz zwischen zwei Punkten.
	public static double getDistance(double xP1, double yP1, double xP2, double yP2){
		return Math.sqrt(Math.pow((xP2 - xP1), 2) + Math.pow((yP2-yP1), 2));
	}
	
	public static void main(String[] args) {
		// Variablen um die x- und y-Werte zu speichern.
		double xP1, yP1, xP2, yP2;
		
		// Scanner zum Einlesen der Zahlen des Benutzers
		Scanner s = new Scanner(System.in);
		
		// Ausgabe und Eingabe von Werten
		// Anschließend Aufruf der getDistance()-Methode zur
		// Berechnung des Abstands zwischen P1 und P2
		System.out.println("Die Distanz zwischen 2 Punkten in einem" + 
						   "\nkartesischem Koordiantensytem berechnen.");
		System.out.println("Bitte die Koordinaten des 1. Punktes eineben:");
		System.out.println("x1: ");
		xP1 = s.nextDouble();
		System.out.println("y1: ");
		yP1 = s.nextDouble();
		System.out.println("Biite die Koordinaten des 2. Punktes eingeben:");
		System.out.println("x2: ");
		xP2 = s.nextDouble();
		System.out.println("y2: ");
		yP2 = s.nextDouble();
		
		System.out.println("Die Distanz zwischen P1(" + xP1 + "|" +
							yP1 + ") und P2(" + xP2 + "|" + yP2 +
							") beträgt: " + getDistance(xP1, yP1, xP2, yP2));
	}

}
vote_ok
von ElPapito (2690 Punkte) - 05.05.2015 um 01:41 Uhr
Quellcode ausblenden Java-Code

import java.util.Scanner;

/**
 * @author ElPapito
 * @date 05.05.2015
 */

public class AbstandZweierPunkte {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		System.out.print("x1: ");
		double x1 = scanner.nextDouble();
		System.out.print("y1: ");
		double y1 = scanner.nextDouble();
		System.out.print("x2: ");
		double x2 = scanner.nextDouble();
		System.out.print("y2: ");
		double y2 = scanner.nextDouble();
		scanner.close();

		double a = x1 - x2;
		double b = y1 - y2;
		double distance = Math.sqrt(a * a + b * b);
		System.out.println(distance);
	}

}

vote_ok
von OlleKarre (170 Punkte) - 30.08.2015 um 12:38 Uhr
Quellcode ausblenden Java-Code
import java.util.Scanner;

class Abstand {

	public static void main (String [] args) {	
		
		berechneAbstand();	
	
	}
	
		static Scanner eingabe () {
			return new Scanner (System.in);
		}
	
		static void berechneAbstand () {
		
			int a , b ,c ,d;
			double e;
		
			System.out.println("Ich bin ein Simples Java-Programm welches den Abstand zweier Punkte misst.");
			System.out.println("Doch dafür brauche ich die Koordinaten x1 , y1 , x2 und y2.");
			System.out.println("Seien Sie doch so freundlich und tippen Sie diese Koordinaten für mich ein!");
			System.out.println("(Erst x1 , dann y1 , folglich x2 und zuletzt y2.)");
		
			a = eingabe().nextInt();
			b = eingabe().nextInt();
			c = eingabe().nextInt();
			d = eingabe().nextInt();
			e = Math.sqrt( (a - c) * (a - c) + (b - d) * (b - d) );
		
			System.out.println("x1:" + a);
			System.out.println("y1:" + b);
			System.out.println("x2:" + c);
			System.out.println("y2:" + d);
			System.out.println(e);
		
	}
	
}
vote_ok
von Bufkin (1410 Punkte) - 13.09.2017 um 14:44 Uhr
Quellcode ausblenden Java-Code
class abstand
{
    public static void main (String[] args) throws java.lang.Exception
    {
        double x1 = 1.0;
        double y1 = 1.0;
        double x2 = 2.0;
        double y2 = 2.0;
        
        System.out.println("Abstand: " + Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2)));
    }
}
vote_ok
von nOrdan (1160 Punkte) - 05.06.2019 um 14:30 Uhr
Anmerkung: Ich arbeite mit dem Programm BlueJ

Quellcode ausblenden Java-Code

import Methodensammlung.Methoden;

/**
 * Der Abstand zweier Punkte kann berechnet werden
 * 
 * @author (nOrdan) 
 * @version (05.06.2019)
 */
public class Rechner
{

    Methoden m = new Methoden();
    
    public static void main(String [] args)
    {
        Rechner r = new Rechner();
        r.inputs();
    }

    private void inputs()
    {
        boolean valid1 = false;
        double x1 = 0;
        double x2 = 0;
        double y1 = 0;
        double y2 = 0;
        String input1 = null;
        while (valid1 == false)
        {
            input1 = m.userInput("Geben sie x1 ein");
            m.errorUserInput(input1);
            try
            {
                x1 = m.parseDouble(input1);
                valid1 = true;
            }
            catch(Exception e)
            {
                m.errorMessage("Invalid user input","Invalid input");
            }
        }
        valid1 = false;
        while (valid1 == false)
        {
            input1 = m.userInput("Geben sie y1 ein");
            m.errorUserInput(input1);
            try
            {
                y1 = m.parseDouble(input1);
                valid1 = true;
            }
            catch(Exception e)
            {
                m.errorMessage("Invalid user input","Invalid input");
            }       
        }
        valid1 = false;
        while (valid1 == false)
        {
            input1 = m.userInput("Geben sie x2 ein");
            m.errorUserInput(input1);
            try
            {
                x2 = m.parseDouble(input1);
                valid1 = true;
            }
            catch(Exception e)
            {
                m.errorMessage("Invalid user input","Invalid input");
            }       
        }
        valid1 = false;
        while (valid1 == false)
        {
            input1 = m.userInput("Geben sie y2 ein");
            m.errorUserInput(input1);
            try
            {
                y2 = m.parseDouble(input1);
                valid1 = true;
            }
            catch(Exception e)
            {
                m.errorMessage("Invalid user input","Invalid input");
            }       
        }
        berechnung(x1,x2,y1,y2);
    }
    
    private void berechnung(double x1, double x2, double y1, double y2)
    {
        double abstand = Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2-y1), 2));
        m.informationMessage("Der Abstand zwischen P1 (" + x1 + "/" + y1 + ") und P2 (" + x2 + "/" + y2 + ") beträgt " + abstand,"Ergebnis");
    }
}



Methoden die ich aus meinem eigenem Methoden package benutzt habe

Quellcode ausblenden Java-Code

public double parseDouble(String input)
    {
        return Double.parseDouble(input);
    }

public String userInput(String message)
    {
        return JOptionPane.showInputDialog(message);
    }

public void errorUserInput(String input)
    {
        if (input == null) 
        {
            System.exit(0); //Drückt der User auf abbrechen wird null zurück gegeben und das Programm wird beendet
        }
        else if (input.length() == 0)
        {
            //continue; wenn nötig
        }
    }

    public void errorMessage(String message,String errorName)
    {
        JOptionPane.showMessageDialog(null,message,errorName,JOptionPane.ERROR_MESSAGE); 
    }
    
    public void informationMessage(String message,String informationName)
    {
        JOptionPane.showMessageDialog(null,message,informationName,JOptionPane.INFORMATION_MESSAGE);
    }

vote_ok
von paddlboot (3970 Punkte) - 09.07.2019 um 09:19 Uhr
Quellcode ausblenden Java-Code
import java.util.*;

public class AbstandPunkte {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		int x1, x2, y1, y2;
		
		System.out.print("X1:\t");
		x1 = scanner.nextInt();
		
		System.out.print("Y1:\t");
		y1 = scanner.nextInt();
		
		System.out.print("X2:\t");
		x2 = scanner.nextInt();
		
		System.out.print("Y2:\t");
		y2 = scanner.nextInt();
		
		System.out.print(abstand(x1, x2, y1, y2));
		
		scanner.close();
	}
	
	public static double abstand(int x1, int x2, int y1, int y2) {	
		double sum = Math.pow((x1-x2), 2) + Math.pow((y1-y2), 2);
		return Math.sqrt(sum);
	}
	
}
2115523

Du scheinst einen AdBlocker zu nutzen. Ich würde mich freuen, wenn du ihn auf dieser Seite deaktivierst und dich davon überzeugst, dass die Werbung hier nicht störend ist.