zoukankan      html  css  js  c++  java
  • Java入门——day44

    1.点类

    定义一个Point类,有两个数据成员:xy, 分别代表x坐标和y坐标,并有若干成员函数。 定义一个函数Distance(), 用于求两点之间的距离

     1 //点类
     2 import java.util.Scanner;
     3 public class Point {
     4     private double x; // 横坐标
     5     private double y; // 纵坐标
     6     //输入点的坐标
     7     public void input() {
     8         Scanner in=new Scanner(System.in);
     9         this.x=in.nextDouble();
    10         this.y=in.nextDouble();
    11     }
    12     public double getX() {
    13         return this.x;
    14     }
    15     public double getY() {
    16         return this.y;
    17     }
    18     //计算两点间的距离
    19     public void distance(Point p) {
    20         double d;
    21         d=Math.sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
    22         System.out.printf("%.2f",d);
    23     }
    24     public static void main(String[] args) {
    25         Point p1=new Point();
    26         Point p2=new Point();
    27         System.out.print("请输入点1:");
    28         p1.input();
    29         System.out.print("请输入点2:");
    30         p2.input();
    31         p1.distance(p2);
    32     }
    33 }


     2.复数类

    • 声明一个复数类Complex(类私有数据成员为double型的realimage

    • 定义构造函数,用于指定复数的实部与虚部。

    • 定义取反成员函数,调用时能返回该复数的相反数(实部、虚部分别是原数的相反数)。

    • 定义成员函数Print(),调用该函数时,以格式(real, image)输出当前对象。

    • 编写加法函数,以复数对象c1c2为参数,求两个复数对象相加之和。

    • 主程序实现:

    1)读入两个实数,用于初始化对象c1

    2)读入两个实数,用于初始化对象c2

    3)计算c1c2相加结果,并输出。

    4)计算c2的相反数与c1相加结果,并输出。

     1 //复数类
     2 import java.util.Scanner;
     3 public class Complex {
     4     private double real; // 实部
     5     private double image; // 虚部
     6     //无参构造函数
     7     public Complex() {
     8     }
     9     //有参构造函数
    10     public Complex(double real, double image) {
    11         this.real = real;
    12         this.image = image;
    13     }
    14     //取反成员函数
    15     public Complex oppo() {
    16         double a=-this.real;
    17         double b=-this.image;
    18         Complex result=new Complex(a,b);
    19         return result;
    20     }
    21     //输出复数
    22     public void print() {
    23         System.out.println("("+real+","+image+")");
    24     }
    25     // 复数的加法
    26     public static Complex add(Complex c1,Complex c2) {
    27         double real1 = c1.real;
    28         double image1 = c1.image;
    29         double real2=c2.real;
    30         double image2=c2.image;
    31         double a = real1 + real2;
    32         double b = image1 + image2;
    33         Complex c = new Complex(a, b);
    34         return c;
    35     }
    36     public static void main(String[] args) {
    37         Scanner in=new Scanner(System.in);
    38         System.out.print("请输入复数1的实部和虚部:");
    39         double a1=in.nextDouble();
    40         double a2=in.nextDouble();
    41         Complex c1=new Complex(a1,a2);
    42         System.out.print("请输入复数2的实部和虚部:");
    43         double b1=in.nextDouble();
    44         double b2=in.nextDouble();
    45         Complex c2=new Complex(b1,b2);
    46         Complex m=new Complex();
    47         Complex n=new Complex();
    48         m=add(c1,c2);
    49         System.out.println("复数1+复数2:");
    50         m.print();
    51         n=add(c1,c2.oppo());
    52         System.out.println("复数1+复数2的相反数:");
    53         n.print();    
    54     }
    55 }

  • 相关阅读:
    LeetCode "Palindrome Partition II"
    LeetCode "Longest Substring Without Repeating Characters"
    LeetCode "Wildcard Matching"
    LeetCode "Best Time to Buy and Sell Stock II"
    LeetCodeEPI "Best Time to Buy and Sell Stock"
    LeetCode "Substring with Concatenation of All Words"
    LeetCode "Word Break II"
    LeetCode "Word Break"
    Some thoughts..
    LeetCode "Longest Valid Parentheses"
  • 原文地址:https://www.cnblogs.com/znjy/p/13526314.html
Copyright © 2011-2022 走看看